def add_command(project_dir, name, command_type, command, env_spec_name, supports_http_options): """Add command to anaconda-project.yml. Returns: int exit code """ project = load_project(project_dir) command_as_filename = os.path.join(project.directory_path, command) if command_type is None and command.endswith(".ipynb") and os.path.isfile( command_as_filename): command_type = 'notebook' if command_type is None or command_type == 'ask': command_type = _ask_command(name) if command_type is None: # EOF, probably not an interactive console print("Specify the --type option to add this command.", file=sys.stderr) return 1 status = project_ops.add_command(project, name, command_type, command, env_spec_name, supports_http_options) if not status: console_utils.print_status_errors(status) return 1 else: print( "Added a command '%s' to the project. Run it with `anaconda-project run %s`." % (name, name)) return 0
def download_command( project, unpack, parent_dir, site, username, token, ): """Download project from Anaconda Cloud. Returns: exit code """ status = project_ops.download(project, unpack=unpack, parent_dir=parent_dir, site=site, username=username, token=token) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def _handle_status(status, success_message=None): if status: print(status.status_description) if success_message is not None: print(success_message) return 0 else: console_utils.print_status_errors(status) return 1
def remove_download(project_dir, filename_variable): """Remove a download requirement from project and from file system.""" project = load_project(project_dir) result = prepare_without_interaction(project, mode=PROVIDE_MODE_CHECK) status = project_ops.remove_download(project, result, env_var=filename_variable) if status: print(status.status_description) print("Removed {} from the project file.".format(filename_variable)) return 0 else: console_utils.print_status_errors(status) return 1
def remove_service(project_dir, variable_name): """Remove an item from the services section.""" project = load_project(project_dir) result = prepare_without_interaction(project, mode=PROVIDE_MODE_CHECK) status = project_ops.remove_service(project, result, variable_name=variable_name) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def remove_variables(project_dir, vars_to_remove): """Remove env variable requirements from the project file. Returns: Returns exit code """ project = load_project(project_dir) status = project_ops.remove_variables(project, vars_to_remove) if status: return 0 else: console_utils.print_status_errors(status) return 1
def archive_command(project_dir, archive_filename): """Make an archive of the project. Returns: exit code """ project = load_project(project_dir) status = project_ops.archive(project, archive_filename) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def unset_variables(project_dir, env_spec_name, vars_to_unset): """Unset the variables for local project. Returns: Returns exit code """ project = load_project(project_dir) status = project_ops.unset_variables(project, env_spec_name, vars_to_unset) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def add_service(project_dir, env_spec_name, service_type, variable_name): """Add an item to the services section.""" project = load_project(project_dir) status = project_ops.add_service(project, env_spec_name=env_spec_name, service_type=service_type, variable_name=variable_name) if status: print(status.status_description) print("Added service %s to the project file, its address will be in %s." % (status.requirement.service_type, status.requirement.env_var)) return 0 else: console_utils.print_status_errors(status) return 1
def remove_command(project_dir, name): """Remove a command from the project. Returns: int exit code """ project = load_project(project_dir) status = project_ops.remove_command(project, name) if not status: console_utils.print_status_errors(status) return 1 else: print("Removed the command '{}' from the project.".format(name)) return 0
def unarchive_command(archive_filename, project_dir): """Unpack an archive of the project. Returns: exit code """ status = project_ops.unarchive(archive_filename, project_dir, frontend=CliFrontend()) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def clean_command(project_dir): """Clean up generated state. Returns: exit code """ project = load_project(project_dir) result = prepare_without_interaction(project, mode=PROVIDE_MODE_CHECK) status = project_ops.clean(project, result) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def remove_service(project_dir, env_spec_name, variable_name): """Remove an item from the services section.""" project = load_project(project_dir) # we don't want to print errors during this prepare, remove # service can proceed even though the prepare fails. with project.null_frontend(): result = prepare_without_interaction(project, env_spec_name=env_spec_name, mode=PROVIDE_MODE_CHECK) status = project_ops.remove_service(project, env_spec_name=env_spec_name, variable_name=variable_name, prepare_result=result) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def upload_command(project_dir, site, username, token): """Upload project to Anaconda. Returns: exit code """ project = load_project(project_dir) status = project_ops.upload(project, site=site, username=username, token=token) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def clean_command(project_dir): """Clean up generated state. Returns: exit code """ project = load_project(project_dir) # we don't want to print errors during this prepare, clean # can proceed even though the prepare fails. with project.null_frontend(): result = prepare_without_interaction(project, mode=PROVIDE_MODE_CHECK) status = project_ops.clean(project, result) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def dockerize_command(project_dir, tag, command, builder_image, build_args): """Build docker image. Returns: exit code """ project = load_project(project_dir) status = project_ops.dockerize(project, tag=tag, command=command, builder_image=builder_image, build_args=build_args) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def remove_download(project_dir, env_spec_name, filename_variable): """Remove a download requirement from project and from file system.""" project = load_project(project_dir) # we can remove a download even if prepare fails, so disable # printing errors in the frontend. with project.null_frontend(): result = prepare_without_interaction(project, env_spec_name=env_spec_name, mode=PROVIDE_MODE_CHECK) status = project_ops.remove_download(project, env_spec_name=env_spec_name, env_var=filename_variable, prepare_result=result) if status: print(status.status_description) print("Removed {} from the project file.".format(filename_variable)) return 0 else: console_utils.print_status_errors(status) return 1
def add_variables(project_dir, env_spec_name, vars_to_add, default): """Add env variables to project file. Returns: Returns exit code """ if len(vars_to_add) > 1 and default is not None: print("It isn't clear which variable your --default option goes with; " "add one variable at a time if using --default.", file=sys.stderr) return 1 project = load_project(project_dir) status = project_ops.add_variables(project, env_spec_name, vars_to_add, {vars_to_add[0]: default}) if status: return 0 else: console_utils.print_status_errors(status) return 1
def _interactively_fix_missing_variables(project, result): """Return True if we need to re-prepare.""" if project.problems: return False if not console_utils.stdin_is_interactive(): return False # We don't ask the user to manually enter CONDA_PREFIX # (CondaEnvRequirement) because it's a bizarre/confusing # thing to ask. can_ask_about = [ status for status in result.statuses if (not status and isinstance(status.requirement, EnvVarRequirement) and not isinstance(status.requirement, CondaEnvRequirement)) ] if can_ask_about: print("(Use Ctrl+C to quit.)") start_over = False values = dict() for status in can_ask_about: reply = console_utils.console_input( "Value for " + status.requirement.env_var + ": ", encrypted=status.requirement.encrypted) if reply is None: return False # EOF reply = reply.strip() if reply == '': start_over = True break values[status.requirement.env_var] = reply if len(values) > 0: status = project_ops.set_variables(project, result.env_spec_name, values.items(), result) if status: return True else: console_utils.print_status_errors(status) return False else: return start_over
def add_download(project_dir, filename_variable, download_url, filename, hash_algorithm, hash_value): """Add an item to the downloads section.""" project = load_project(project_dir) if (hash_algorithm or hash_value) and not bool(hash_algorithm and hash_value): print("Error: mutually dependant parameters: --hash-algorithm and --hash-value.", file=sys.stderr) return 1 status = project_ops.add_download(project, env_var=filename_variable, url=download_url, filename=filename, hash_algorithm=hash_algorithm, hash_value=hash_value) if status: print(status.status_description) print("Added %s to the project file." % download_url) return 0 else: console_utils.print_status_errors(status) return 1
def set_variables(project_dir, env_spec_name, vars_and_values): """Set the given variables to the given values. Returns: Returns exit code """ fixed_vars = [] for var in vars_and_values: if '=' not in var: print("Error: argument '{}' should be in NAME=value format".format(var)) return 1 # maxsplit=1 -- no maxsplit keywork in py27 fixed_vars.append(tuple(var.split('=', 1))) project = load_project(project_dir) status = project_ops.set_variables(project, env_spec_name, fixed_vars) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1
def archive_command(project_dir, archive_filename, pack_envs): """Make an archive of the project. Returns: exit code """ if pack_envs: prepare_status = prepare_command(project_dir, ui_mode='production_defaults', conda_environment=None, command_name=None, all=True) if not prepare_status: return 1 project = load_project(project_dir) status = project_ops.archive(project, archive_filename, pack_envs) if status: print(status.status_description) return 0 else: console_utils.print_status_errors(status) return 1