Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Create new project without internet #175

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
52 changes: 38 additions & 14 deletions pros/cli/conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def uninstall_template(project: c.Project, query: c.BaseTemplate, remove_user: b
@click.argument('path', type=click.Path())
@click.argument('target', default=c.Conductor().default_target, type=click.Choice(['v5', 'cortex']))
@click.argument('version', default='latest')
@click.option('--download/--no-download', 'download_ok', default=True, show_default=True,
help='(Dis)allow download and use of remote templates in new projects')
@click.option('--force-user', 'force_user', default=False, is_flag=True,
help='Replace all user files in a template')
@click.option('--force-system', '-f', 'force_system', default=False, is_flag=True,
Expand Down Expand Up @@ -207,23 +209,45 @@ def new_project(ctx: click.Context, path: str, target: str, version: str,
'! Delete it first. Are you creating a project in an existing one?', extra={'sentry': False})
ctx.exit(-1)
try:
_conductor = c.Conductor()
if target is None:
project = _create_project(ctx=ctx, path=path,target=target, version=version,
force_user=force_user, force_system=force_system,
compile_after=compile_after, build_cache=build_cache, **kwargs)
ctx.exit(project.compile([], scan_build=build_cache))
except Exception as e:
try:
kwargs['allow_online'] = False
project = _create_project(ctx=ctx, path=path,target=target, version=version,
force_user=force_user, force_system=force_system,
compile_after=compile_after, build_cache=build_cache, **kwargs)
logger(__name__).error('Could not connect to GitHub. Check your internet connection or consult a network administrator.',
extra={'sentry': False})
ctx.exit(project.compile([], scan_build=build_cache))
except Exception as _e:
pros.common.logger(__name__).exception(e)
ctx.exit(-1)


def _create_project(ctx: click.Context, path: str, target: str, version: str,
force_user: bool = False, force_system: bool = False,
no_default_libs: bool = False, compile_after: bool = True, build_cache: bool = None, **kwargs):
"""
Helper function for new_project

Visit https://pros.cs.purdue.edu/v5/cli/conductor.html to learn more
"""
_conductor = c.Conductor()
if target is None:
target = _conductor.default_target
project = _conductor.new_project(path, target=target, version=version,
project = _conductor.new_project(path, target=target, version=version,
force_user=force_user, force_system=force_system,
no_default_libs=no_default_libs, **kwargs)
ui.echo('New PROS Project was created:', output_machine=False)
ctx.invoke(info_project, project=project)

if compile_after or build_cache:
with ui.Notification():
ui.echo('Building project...')
ctx.exit(project.compile([], scan_build=build_cache))

except Exception as e:
pros.common.logger(__name__).exception(e)
ctx.exit(-1)
ui.echo('New PROS Project was created:', output_machine=False)
ctx.invoke(info_project, project=project)
if compile_after or build_cache:
with ui.Notification():
ui.echo('Building project...')
return project



@conductor.command('query-templates',
Expand Down
8 changes: 5 additions & 3 deletions pros/conductor/conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ def purge_template(self, template: LocalTemplate):
shutil.rmtree(template.location)
self.save()

def resolve_templates(self, identifier: Union[str, BaseTemplate], allow_online: bool = True,
def resolve_templates(self, identifier: Union[str, BaseTemplate],
allow_offline: bool = True, force_refresh: bool = False,
unique: bool = True, **kwargs) -> List[BaseTemplate]:
results = list() if not unique else set()
kernel_version = kwargs.get('kernel_version', None)
allow_online = kwargs.get('allow_online', True)
download_ok = kwargs.get('download_ok', True)
if isinstance(identifier, str):
query = BaseTemplate.create_query(name=identifier, **kwargs)
else:
Expand All @@ -112,7 +114,7 @@ def resolve_templates(self, identifier: Union[str, BaseTemplate], allow_online:
results.update(offline_results)
else:
results.extend(offline_results)
if allow_online:
if allow_online and download_ok:
for depot in self.depots.values():
online_results = filter(lambda t: t.satisfies(query, kernel_version=kernel_version),
depot.get_remote_templates(force_check=force_refresh, **kwargs))
Expand Down Expand Up @@ -173,7 +175,7 @@ def apply_template(self, project: Project, identifier: Union[str, BaseTemplate],
if 'kernel' in project.templates:
# support_kernels for backwards compatibility, but kernel_version should be getting most of the exposure
kwargs['kernel_version'] = kwargs['supported_kernels'] = project.templates['kernel'].version
template = self.resolve_template(identifier=identifier, allow_online=download_ok, **kwargs)
template = self.resolve_template(identifier=identifier, **kwargs)
if template is None:
raise dont_send(
InvalidTemplateException(f'Could not find a template satisfying {identifier} for {project.target}'))
Expand Down