Skip to content
This repository was archived by the owner on Jan 31, 2019. It is now read-only.

Commit be552de

Browse files
authored
Merge pull request #35 from purduesigbots/BetterProjects
Closes #19 : Better Project Creation
2 parents ac8d018 + 275e24b commit be552de

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

proscli/conductor.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,11 @@ def download(cfg, name, version, depot, no_check):
326326
@click.argument('location')
327327
@click.argument('kernel', default='latest')
328328
@click.argument('depot', default='auto')
329-
@click.option('--verify-only', is_flag=True, default=False)
329+
@click.option('--force', 'mode', flag_value='force')
330+
@click.option('--safe', 'mode', flag_value='safe')
331+
@click.option('--default', 'mode', flag_value='default', default=True)
330332
@default_cfg
331-
def new(cfg, kernel, location, depot, verify_only):
333+
def new(cfg, kernel, location, depot, mode):
332334
first_run(cfg)
333335
templates = local.get_local_templates(pros_cfg=cfg.pros_cfg,
334336
template_types=[TemplateTypes.kernel]) # type: Set[Identifier]
@@ -362,7 +364,8 @@ def new(cfg, kernel, location, depot, verify_only):
362364
if not os.path.isabs(location):
363365
location = os.path.abspath(location)
364366
click.echo('Creating new project from {} on {} at {}'.format(template.version, template.depot, location))
365-
local.create_project(identifier=template, dest=location, pros_cli=cfg.pros_cfg)
367+
local.create_project(identifier=template, dest=location, pros_cli=cfg.pros_cfg,
368+
require_empty=(mode == 'safe'), overwrite=(mode == 'force'))
366369

367370

368371
@conduct.command('upgrade', aliases=['update'], help='Upgrades a PROS project')

prosconductor/providers/local.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import sys
1212
# from typing import Set, List
1313

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,
1515
ignore_dangling_symlinks=False):
1616
"""Modified shutil.copytree, but with exist_ok=True
1717
"""
@@ -43,15 +43,30 @@ def copytree(src, dst, symlinks=False, ignore=None, copy_function=shutil.copy2,
4343
continue
4444
# otherwise let the copy occurs. copy2 will raise an error
4545
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)
4848
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)
5057
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)
5260
else:
5361
# 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)
5570
# catch the Error from the recursive copytree so that we can
5671
# continue with other files
5772
except shutil.Error as err:
@@ -99,7 +114,7 @@ def create_template(identifier, location=None, pros_cli=None):
99114
return config
100115

101116

102-
def create_project(identifier, dest, pros_cli=None):
117+
def create_project(identifier, dest, pros_cli=None, require_empty=False, overwrite=False):
103118
if pros_cli is None or not pros_cli:
104119
pros_cli = CliConfig()
105120
filename = os.path.join(pros_cli.directory, identifier.depot,
@@ -109,12 +124,13 @@ def create_project(identifier, dest, pros_cli=None):
109124
click.echo('Error: template.pros not found for {}-{}'.format(identifier.name, identifier.version))
110125
click.get_current_context().abort()
111126
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()
116132
config = TemplateConfig(file=filename)
117-
copytree(config.directory, dest)
133+
copytree(config.directory, dest, overwrite=overwrite)
118134
for root, dirs, files in os.walk(dest):
119135
for d in dirs:
120136
d = os.path.relpath(os.path.join(root, d), dest)
@@ -171,7 +187,7 @@ def upgrade_project(identifier, dest, pros_cli=None):
171187
proj_config.save()
172188

173189

174-
def install_lib(identifier, dest, pros_cli):
190+
def install_lib(identifier, dest, pros_cli, overwrite=False):
175191
if pros_cli is None or not pros_cli:
176192
pros_cli = CliConfig()
177193
filename = os.path.join(pros_cli.directory, identifier.depot,
@@ -183,7 +199,7 @@ def install_lib(identifier, dest, pros_cli):
183199
sys.exit()
184200
proj_config = prosconfig.ProjectConfig(dest)
185201
config = TemplateConfig(file=filename)
186-
copytree(config.directory, dest)
202+
copytree(config.directory, dest, overwrite=overwrite)
187203
for root, dirs, files in os.walk(dest):
188204
for d in dirs:
189205
if any([fnmatch.fnmatch(d, p) for p in config.template_ignore]):

0 commit comments

Comments
 (0)