def prepare_command(project_dir, ui_mode, conda_environment, command_name, all=False, refresh=False): """Configure the project to run. Returns: Prepare result (can be treated as True on success). """ project = load_project(project_dir) project_dir = project.directory_path if console_utils.print_project_problems(project): return False if all: specs = project.env_specs else: specs = {conda_environment: project.env_specs.get(conda_environment)} result = True for k, v in specs.items(): if not prepare_with_ui_mode_printing_errors(project, env_spec_name=k, ui_mode=ui_mode, command_name=command_name, refresh=refresh): result = False return result
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 unlock(project_dir, env_spec_name): """Unlock dependency versions.""" project = load_project(project_dir) if console_utils.print_project_problems(project): return 1 status = project_ops.unlock(project, env_spec_name=env_spec_name) return _handle_status(status)
def run_command(project_dir, ui_mode, conda_environment, command_name, extra_command_args): """Run the project. Returns: Does not return if successful. """ project = load_project(project_dir) environ = None command = _command_from_name(project, command_name) result = prepare_with_ui_mode_printing_errors( project, ui_mode=ui_mode, env_spec_name=conda_environment, command=command, extra_command_args=extra_command_args, environ=environ) if result.failed: # errors were printed already return elif result.command_exec_info is None: print( "No known run command for project %s; try adding a 'commands:' section to anaconda-project.yml" % project_dir, file=sys.stderr) else: try: result.command_exec_info.execvpe() except OSError as e: print("Failed to execute '%s': %s" % (" ".join(result.command_exec_info.args), e.strerror), file=sys.stderr)
def activate(dirname, ui_mode, conda_environment, command_name): """Prepare project and return lines to be sourced. Future direction: should also activate the proper conda env. Returns: None on failure or a list of lines to print. """ project = load_project(dirname) result = prepare_with_ui_mode_printing_errors( project, ui_mode=ui_mode, env_spec_name=conda_environment, command_name=command_name) if result.failed: return None exports = [] # sort so we have deterministic output order for tests sorted_keys = list(result.environ.keys()) sorted_keys.sort() for key in sorted_keys: value = result.environ[key] if key not in os.environ or os.environ[key] != value: exports.append("export {key}={value}".format(key=key, value=quote(value))) return exports
def list_env_specs(project_dir): """List environments in the project.""" project = load_project(project_dir) if console_utils.print_project_problems(project): return 1 print("Environments for project: {}\n".format(project_dir)) console_utils.print_names_and_descriptions(project.env_specs.values()) return 0
def list_variables(project_dir, env_spec_name): """List variables present in project.""" project = load_project(project_dir) if console_utils.print_project_problems(project): return 1 print("Variables for project: {}\n".format(project_dir)) console_utils.print_names_and_descriptions(project.all_variable_requirements(env_spec_name), name_attr='env_var') return 0
def add_env_spec(project_dir, name, packages, channels): """Add an environment with packages from specified channels to the project.""" project = load_project(project_dir) status = project_ops.add_env_spec(project, name=name, packages=packages, channels=channels) return _handle_status( status, "Added environment {} to the project file.".format(name))
def add_packages(project, environment, packages, channels): """Add packages to the project.""" project = load_project(project) status = project_ops.add_packages(project, env_spec_name=environment, packages=packages, channels=channels) package_list = ", ".join(packages) if environment is None: success_message = "Added packages to project file: %s." % (package_list) else: success_message = "Added packages to environment %s in project file: %s." % (environment, package_list) return _handle_status(status, success_message)
def remove_platforms(project, environment, platforms): """Remove platforms from the project.""" project = load_project(project) status = project_ops.remove_platforms(project, env_spec_name=environment, platforms=platforms) package_list = ", ".join(platforms) if environment is None: success_message = "Removed platforms from project file: %s." % (package_list) else: success_message = "Removed platforms from environment %s in project file: %s." % (environment, package_list) return _handle_status(status, success_message)
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 list_downloads(project_dir, env_spec_name): """List the downloads present in project.""" project = load_project(project_dir) if console_utils.print_project_problems(project): return 1 if project.downloads(env_spec_name): print("Downloads for project: {}\n".format(project_dir)) console_utils.print_names_and_descriptions(project.download_requirements(env_spec_name), name_attr='title') else: print("No downloads found in project.") return 0
def list_services(project_dir, env_spec_name): """List the services listed on the project.""" project = load_project(project_dir) if console_utils.print_project_problems(project): return 1 if project.services(env_spec_name): print("Services for project: {}\n".format(project_dir)) console_utils.print_names_and_descriptions(project.service_requirements(env_spec_name), name_attr='title') else: print("No services found for project: {}".format(project_dir)) return 0
def check(dirname): _monkeypatch_input(monkeypatch, ["y"]) project = load_project(dirname) assert project.problems == [] out, err = capsys.readouterr() assert out == ( "Project file '%s' does not exist.\nCreate file '%s'? " % (DEFAULT_PROJECT_FILENAME, os.path.join(dirname, DEFAULT_PROJECT_FILENAME))) assert err == ""
def check(dirname): _monkeypatch_input(monkeypatch, ["n"]) project = load_project(dirname) first_line = "Project file '%s' does not exist." % DEFAULT_PROJECT_FILENAME assert project.problems == [first_line] out, err = capsys.readouterr() assert out == ( "%s\nCreate file '%s'? " % (first_line, os.path.join(dirname, DEFAULT_PROJECT_FILENAME))) assert err == ""
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 check(dirname): broken_project = Project(dirname) assert len(broken_project.fixable_problems) == 1 _monkeypatch_input(monkeypatch, ["n"]) project = load_project(dirname) first_line = "%s: The env_specs section is missing." % DEFAULT_PROJECT_FILENAME assert project.problems == [first_line] out, err = capsys.readouterr() assert out == "%s\nAdd an environment spec to anaconda-project.yml? " % first_line assert err == ""
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 prepare_command(project_dir, ui_mode, conda_environment, command_name): """Configure the project to run. Returns: Prepare result (can be treated as True on success). """ project = load_project(project_dir) result = prepare_with_ui_mode_printing_errors(project, env_spec_name=conda_environment, ui_mode=ui_mode, command_name=command_name) return result
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 list_platforms(project_dir, environment): """List the platforms for an environment in the project.""" project = load_project(project_dir) if console_utils.print_project_problems(project): return 1 if environment is None: environment = project.default_env_spec_name env = project.env_specs.get(environment, None) if env is None: print("Project doesn't have an environment called '{}'".format(environment), file=sys.stderr) return 1 print("Platforms for environment '{}':\n".format(env.name)) print("\n".join(sorted(env.platforms)), end='\n\n') return 0
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 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_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 list_commands(project_dir): """List the commands on the project. Returns: int exit code """ project = load_project(project_dir) if console_utils.print_project_problems(project): return 1 if project.commands: print("Commands for project: {}\n".format(project_dir)) console_utils.print_names_and_descriptions(project.commands.values()) else: print("No commands found for project: {}\n".format(project_dir)) return 0
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 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 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