11
11
import sys
12
12
# from typing import Set, List
13
13
14
- def copytree (src , dst , symlinks = False , ignore = None , copy_function = shutil .copy2 ,
14
+ def copytree (src , dst , symlinks = False , ignore = None , overwrite = False , copy_function = shutil .copy2 ,
15
15
ignore_dangling_symlinks = False ):
16
16
"""Modified shutil.copytree, but with exist_ok=True
17
17
"""
@@ -43,15 +43,30 @@ def copytree(src, dst, symlinks=False, ignore=None, copy_function=shutil.copy2,
43
43
continue
44
44
# otherwise let the copy occurs. copy2 will raise an error
45
45
if os .path .isdir (srcname ):
46
- copytree (srcname , dstname , symlinks , ignore ,
47
- copy_function )
46
+ copytree (srcname , dstname , symlinks , ignore , overwrite = overwrite ,
47
+ copy_function = copy_function )
48
48
else :
49
- copy_function (srcname , dstname )
49
+ if os .path .exists (dstname ):
50
+ if overwrite :
51
+ click .echo ('Overwriting {}' .format (dstname ))
52
+ copy_function (srcname , dstname )
53
+ else :
54
+ click .echo ('Skipping {} because it already exists' .format (dstname ))
55
+ else :
56
+ copy_function (srcname , dstname )
50
57
elif os .path .isdir (srcname ):
51
- copytree (srcname , dstname , symlinks , ignore , copy_function )
58
+ copytree (srcname , dstname , symlinks , ignore , overwrite = overwrite ,
59
+ copy_function = copy_function )
52
60
else :
53
61
# Will raise a SpecialFileError for unsupported file types
54
- copy_function (srcname , dstname )
62
+ if os .path .exists (dstname ):
63
+ if overwrite :
64
+ click .echo ('Overwriting {}' .format (dstname ))
65
+ copy_function (srcname , dstname )
66
+ else :
67
+ click .echo ('Skipping {} because it already exists' .format (dstname ))
68
+ else :
69
+ copy_function (srcname , dstname )
55
70
# catch the Error from the recursive copytree so that we can
56
71
# continue with other files
57
72
except shutil .Error as err :
@@ -99,7 +114,7 @@ def create_template(identifier, location=None, pros_cli=None):
99
114
return config
100
115
101
116
102
- def create_project (identifier , dest , pros_cli = None ):
117
+ def create_project (identifier , dest , pros_cli = None , require_empty = False , overwrite = False ):
103
118
if pros_cli is None or not pros_cli :
104
119
pros_cli = CliConfig ()
105
120
filename = os .path .join (pros_cli .directory , identifier .depot ,
@@ -109,12 +124,13 @@ def create_project(identifier, dest, pros_cli=None):
109
124
click .echo ('Error: template.pros not found for {}-{}' .format (identifier .name , identifier .version ))
110
125
click .get_current_context ().abort ()
111
126
sys .exit ()
112
- if os .path .isfile (dest ) or (os .path .isdir (dest ) and len (os .listdir (dest )) > 0 ):
113
- click .echo ('Error! Destination is a file or a nonempty directory! Delete the file(s) and try again.' )
114
- click .get_current_context ().abort ()
115
- sys .exit ()
127
+ if require_empty :
128
+ if os .path .isfile (dest ) or (os .path .isdir (dest ) and len (os .listdir (dest )) > 0 ):
129
+ click .echo ('Error! Destination is a file or a nonempty directory! Delete the file(s) and try again.' )
130
+ click .get_current_context ().abort ()
131
+ sys .exit ()
116
132
config = TemplateConfig (file = filename )
117
- copytree (config .directory , dest )
133
+ copytree (config .directory , dest , overwrite = overwrite )
118
134
for root , dirs , files in os .walk (dest ):
119
135
for d in dirs :
120
136
d = os .path .relpath (os .path .join (root , d ), dest )
@@ -171,7 +187,7 @@ def upgrade_project(identifier, dest, pros_cli=None):
171
187
proj_config .save ()
172
188
173
189
174
- def install_lib (identifier , dest , pros_cli ):
190
+ def install_lib (identifier , dest , pros_cli , overwrite = False ):
175
191
if pros_cli is None or not pros_cli :
176
192
pros_cli = CliConfig ()
177
193
filename = os .path .join (pros_cli .directory , identifier .depot ,
@@ -183,7 +199,7 @@ def install_lib(identifier, dest, pros_cli):
183
199
sys .exit ()
184
200
proj_config = prosconfig .ProjectConfig (dest )
185
201
config = TemplateConfig (file = filename )
186
- copytree (config .directory , dest )
202
+ copytree (config .directory , dest , overwrite = overwrite )
187
203
for root , dirs , files in os .walk (dest ):
188
204
for d in dirs :
189
205
if any ([fnmatch .fnmatch (d , p ) for p in config .template_ignore ]):
0 commit comments