5
5
6
6
PROJECT_DIRECTORY = os .path .realpath (os .path .curdir )
7
7
8
-
9
8
def remove_file (filepath ):
10
- os .remove (os .path .join (PROJECT_DIRECTORY , filepath ))
9
+ try :
10
+ os .remove (os .path .join (PROJECT_DIRECTORY , filepath ))
11
+ except FileNotFoundError :
12
+ pass
13
+
14
+ def execute (* args , supress_exception = False , cwd = None ):
15
+ cur_dir = os .getcwd ()
11
16
12
- def execute (* args , supress_exception = False ):
13
- proc = subprocess .Popen (args , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
17
+ try :
18
+ if cwd :
19
+ os .chdir (cwd )
14
20
15
- out , err = proc .communicate ()
16
- out = out .decode ('utf-8' )
17
- err = err .decode ('utf-8' )
18
- if err and not supress_exception :
19
- raise Exception (err )
20
- else :
21
- return out
21
+ proc = subprocess .Popen (args , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
22
+
23
+ out , err = proc .communicate ()
24
+ out = out .decode ('utf-8' )
25
+ err = err .decode ('utf-8' )
26
+ if err and not supress_exception :
27
+ raise Exception (err )
28
+ else :
29
+ return out
30
+ finally :
31
+ os .chdir (cur_dir )
22
32
23
33
def init_git ():
24
34
# workaround for issue #1
25
35
if not os .path .exists (os .path .join (PROJECT_DIRECTORY , ".git" )):
26
- execute ("git" , "init" )
27
- execute ("git" , "tag " , "0.1.0" )
36
+ execute ("git" , "config" , "--global" , " init.defaultBranch" , "main" , cwd = PROJECT_DIRECTORY )
37
+ execute ("git" , "init " , cwd = PROJECT_DIRECTORY )
28
38
29
39
def install_pre_commit_hooks ():
30
40
execute (sys .executable , "-m" , "pip" , "install" , "pre-commit==2.12.0" )
@@ -43,8 +53,11 @@ def install_pre_commit_hooks():
43
53
if 'Not open source' == '{{ cookiecutter.open_source_license }}' :
44
54
remove_file ('LICENSE' )
45
55
46
- init_git ()
47
-
56
+ try :
57
+ init_git ()
58
+ except Exception as e :
59
+ print (e )
60
+
48
61
if '{{ cookiecutter.install_precommit_hooks }}' == 'y' :
49
62
try :
50
63
install_pre_commit_hooks ()
0 commit comments