11#! /usr/bin/env python
2- import io
32import json
43import logging
5- import os
64import sys
75import typing as t
8- from contextlib import redirect_stdout
9- from multiprocessing import Process
106from pathlib import Path
117
12- from poetry . console import Application
8+ import sh
139
1410logging .basicConfig (stream = sys .stdout , level = logging .INFO )
1511
1612
1713def run_poetry_command (* args_list : t .Iterable [str ]):
18- app = Application ()
19- app .config .set_terminate_after_run (False )
20- output_buffer = io .StringIO ()
21-
22- with redirect_stdout (output_buffer ):
23- sys .argv [1 :] = args_list
24- logging .info (f"Poetry command: { ' ' . join (args_list )} " )
25- app .run ()
26-
27- stdout = output_buffer .getvalue ()
28-
29- # remove exit code
30- return stdout .split ()
14+ return str (sh .poetry (* args_list ))
3115
3216
3317def try_remove (path : t .Union [str , Path ]):
3418 # Coerce into path obj
35- path : Path = Path (path )
19+ path = Path (path )
3620
3721 # Delete either file or dir (true if exists)
3822 if path .is_file ():
@@ -58,7 +42,7 @@ def update_json(
5842 Returns:
5943 settings_json (t.Dict): updated settings dictionary
6044 """
61- path : Path = Path (path )
45+ path = Path (path )
6246 if not path .is_file ():
6347 raise ValueError (f"Invalid path to json file { path } " )
6448
@@ -74,49 +58,25 @@ def update_json(
7458
7559
7660def main ():
77- os . system ( "git init" )
78- os . system ( " git checkout -b main" )
61+ sh . git ( " init" )
62+ sh . git . checkout ( "-b" , " main" )
7963
8064 # Commands to run
8165 for command in (
82- # ("config", "virtualenvs.in-project", "true", "--local"),
66+ (
67+ "config" ,
68+ "virtualenvs.in-project" ,
69+ "true" ,
70+ ),
71+ ("add" , "--dev" , "pytest" , "black" , "isort" , "ipdb" ),
8372 ("update" ,),
8473 ("install" ,),
8574 ):
8675 run_poetry_command (* command )
8776
88- # TODO: Handle vscode / dotenv options
89- if "{{ cookiecutter.use_vscode }}" == "y" :
90- logging .info ("Setting up vscode python interpreter path" )
91-
92- # Fetch path to python interpreter for this poetry env
93- path = run_poetry_command ("env" , "info" , "--path" )[0 ]
94-
95- logging .info (f"Setting python interpreter path to { path } " )
96-
97- # Add environment python interpreter to vscode settings.json
98- updated_settings = update_json (
99- Path (".vscode" , "settings.json" ), {"python.defaultInterpreterPath" : path }
100- )
101- logging .info (f"vscode settings successfully updated { updated_settings } " )
102- else :
103- logging .info ("No vscode: deleting .vscode settings directory" )
104- # Remove .vscode/
105- try_remove (".vscode" )
106-
107- if "{{ cookiecutter.use_dotenv }}" != "y" :
108- # Remove .env & config.py
109- logging .info ("No dotenv: deleting boilerplate .env and config.py" )
110- try_remove ("{{ cookiecutter.project_slug }}" , "config.py" )
111-
112- with open (".gitignore" , "a" ) as f :
113- f .write (".vscode/\n " )
114-
115- p = Process (target = run_poetry_command , args = ("run" , "pre-commit" , "install" , "-f" ))
116- p .start ()
117- p .join ()
118-
119- os .system ("git add -A; git commit -m 'Cut a cookie from a cookiecutter'" )
77+ getattr (sh , "pre-commit" ).install ("-f" )
78+ sh .git .add ("-A" )
79+ sh .git .commit ("-m" , "'Cut a cookie from a cookiecutter'" )
12080
12181
12282if __name__ == "__main__" :
0 commit comments