def _get_target_project(conf, target): """Ask for project ID (or use given target if numerical) and confirm that user is logged in and has project access""" # Will raise MissingAuthException if user is not logged in endpoint, apikey = conf.get_endpoint(0), conf.get_apikey(0) if target.isdigit(): project = int(target) target = 'default' else: project = click.prompt("Target project ID", type=int) if not has_project_access(project, endpoint, apikey): raise InvalidAuthException( "The account you logged in to has no access to project {}. " "Please double-check the project ID and make sure you logged " "in to the correct acount.".format(project), ) return target, project
def _deploy_wizard(conf, target='default'): """ Ask user for project ID, ensure they have access to that project, and save it to given ``target`` in local ``scrapinghub.yml`` if desired. """ closest_scrapycfg = closest_file('scrapy.cfg') # Double-checking to make deploy_wizard() independent of cli() if not closest_scrapycfg: raise NotFoundException("No Scrapy project found in this location.") closest_sh_yml = os.path.join(os.path.dirname(closest_scrapycfg), 'scrapinghub.yml') # Get default endpoint and API key (meanwhile making sure the user is # logged in) endpoint, apikey = conf.get_endpoint(0), conf.get_apikey(0) project = click.prompt("Target project ID", type=int) if not _has_project_access(project, endpoint, apikey): raise InvalidAuthException( "The account you logged in to has no access to project {}. Please " "double-check the project ID and make sure you logged in to the " "correct acount.".format(project), ) conf.projects[target] = project if click.confirm("Save as default", default=True): try: with update_yaml_dict(closest_sh_yml) as conf_yml: default_entry = {'default': project} if 'projects' in conf_yml: conf_yml['projects'].update(default_entry) else: conf_yml['projects'] = default_entry except Exception: click.echo( "There was an error while trying to write to scrapinghub.yml. " "Could not save project {} as default.".format(project), ) else: click.echo( "Project {} was set as default in scrapinghub.yml. You can " "deploy to it via 'shub deploy' from now on.".format(project), )