Skip to content

Commit 1d9f6ba

Browse files
committed
Added download-ok switch
Will elaborate in PR
1 parent b122cce commit 1d9f6ba

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

pros/cli/conductor.py

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ def uninstall_template(project: c.Project, query: c.BaseTemplate, remove_user: b
178178
@click.argument('path', type=click.Path())
179179
@click.argument('target', default=c.Conductor().default_target, type=click.Choice(['v5', 'cortex']))
180180
@click.argument('version', default='latest')
181+
@click.option('--download/--no-download', 'download_ok', default=True, show_default=True,
182+
help='(Dis)allow download and use of remote templates in new projects')
181183
@click.option('--force-user', 'force_user', default=False, is_flag=True,
182184
help='Replace all user files in a template')
183185
@click.option('--force-system', '-f', 'force_system', default=False, is_flag=True,
@@ -207,23 +209,45 @@ def new_project(ctx: click.Context, path: str, target: str, version: str,
207209
'! Delete it first. Are you creating a project in an existing one?', extra={'sentry': False})
208210
ctx.exit(-1)
209211
try:
210-
_conductor = c.Conductor()
211-
if target is None:
212+
project = _create_project(ctx=ctx, path=path,target=target, version=version,
213+
force_user=force_user, force_system=force_system,
214+
compile_after=compile_after, build_cache=build_cache, **kwargs)
215+
ctx.exit(project.compile([], scan_build=build_cache))
216+
except Exception as e:
217+
try:
218+
kwargs['allow_online'] = False
219+
project = _create_project(ctx=ctx, path=path,target=target, version=version,
220+
force_user=force_user, force_system=force_system,
221+
compile_after=compile_after, build_cache=build_cache, **kwargs)
222+
logger(__name__).error('Could not connect to GitHub. Check your internet connection or consult a network administrator.',
223+
extra={'sentry': False})
224+
ctx.exit(project.compile([], scan_build=build_cache))
225+
except Exception as _e:
226+
pros.common.logger(__name__).exception(e)
227+
ctx.exit(-1)
228+
229+
230+
def _create_project(ctx: click.Context, path: str, target: str, version: str,
231+
force_user: bool = False, force_system: bool = False,
232+
no_default_libs: bool = False, compile_after: bool = True, build_cache: bool = None, **kwargs):
233+
"""
234+
Helper function for new_project
235+
236+
Visit https://pros.cs.purdue.edu/v5/cli/conductor.html to learn more
237+
"""
238+
_conductor = c.Conductor()
239+
if target is None:
212240
target = _conductor.default_target
213-
project = _conductor.new_project(path, target=target, version=version,
241+
project = _conductor.new_project(path, target=target, version=version,
214242
force_user=force_user, force_system=force_system,
215243
no_default_libs=no_default_libs, **kwargs)
216-
ui.echo('New PROS Project was created:', output_machine=False)
217-
ctx.invoke(info_project, project=project)
218-
219-
if compile_after or build_cache:
220-
with ui.Notification():
221-
ui.echo('Building project...')
222-
ctx.exit(project.compile([], scan_build=build_cache))
223-
224-
except Exception as e:
225-
pros.common.logger(__name__).exception(e)
226-
ctx.exit(-1)
244+
ui.echo('New PROS Project was created:', output_machine=False)
245+
ctx.invoke(info_project, project=project)
246+
if compile_after or build_cache:
247+
with ui.Notification():
248+
ui.echo('Building project...')
249+
return project
250+
227251

228252

229253
@conductor.command('query-templates',

pros/conductor/conductor.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ def purge_template(self, template: LocalTemplate):
9797
shutil.rmtree(template.location)
9898
self.save()
9999

100-
def resolve_templates(self, identifier: Union[str, BaseTemplate], allow_online: bool = True,
100+
def resolve_templates(self, identifier: Union[str, BaseTemplate],
101101
allow_offline: bool = True, force_refresh: bool = False,
102102
unique: bool = True, **kwargs) -> List[BaseTemplate]:
103103
results = list() if not unique else set()
104104
kernel_version = kwargs.get('kernel_version', None)
105+
allow_online = kwargs.get('allow_online', True)
106+
download_ok = kwargs.get('download_ok', True)
105107
if isinstance(identifier, str):
106108
query = BaseTemplate.create_query(name=identifier, **kwargs)
107109
else:
@@ -112,7 +114,7 @@ def resolve_templates(self, identifier: Union[str, BaseTemplate], allow_online:
112114
results.update(offline_results)
113115
else:
114116
results.extend(offline_results)
115-
if allow_online:
117+
if allow_online and download_ok:
116118
for depot in self.depots.values():
117119
online_results = filter(lambda t: t.satisfies(query, kernel_version=kernel_version),
118120
depot.get_remote_templates(force_check=force_refresh, **kwargs))
@@ -173,7 +175,7 @@ def apply_template(self, project: Project, identifier: Union[str, BaseTemplate],
173175
if 'kernel' in project.templates:
174176
# support_kernels for backwards compatibility, but kernel_version should be getting most of the exposure
175177
kwargs['kernel_version'] = kwargs['supported_kernels'] = project.templates['kernel'].version
176-
template = self.resolve_template(identifier=identifier, allow_online=download_ok, **kwargs)
178+
template = self.resolve_template(identifier=identifier, **kwargs)
177179
if template is None:
178180
raise dont_send(
179181
InvalidTemplateException(f'Could not find a template satisfying {identifier} for {project.target}'))

0 commit comments

Comments
 (0)