示例#1
0
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 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
示例#3
0
def list_variables(project_dir):
    """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, name_attr='env_var')
    return 0
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 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))
示例#6
0
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
示例#7
0
def list_services(project_dir):
    """List the services listed on the project."""
    project = load_project(project_dir)
    if console_utils.print_project_problems(project):
        return 1

    if project.services:
        print("Services for project: {}\n".format(project_dir))
        console_utils.print_names_and_descriptions(
            project.service_requirements, name_attr='title')
    else:
        print("No services found for project: {}".format(project_dir))
    return 0
示例#8
0
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
示例#9
0
def list_downloads(project_dir):
    """List the downloads present in project."""
    project = load_project(project_dir)
    if console_utils.print_project_problems(project):
        return 1

    if project.downloads:
        print("Downloads for project: {}\n".format(project_dir))
        console_utils.print_names_and_descriptions(
            project.download_requirements, name_attr='title')
    else:
        print("No downloads found in project.")
    return 0
示例#10
0
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 remove_packages(project, environment, packages):
    """Remove packages from the project."""
    project = load_project(project)
    status = project_ops.remove_packages(project,
                                         env_spec_name=environment,
                                         packages=packages)
    package_list = ", ".join(packages)
    if environment is None:
        success_message = "Removed packages from project file: %s." % (
            package_list)
    else:
        success_message = "Removed packages from environment %s in project file: %s." % (
            environment, package_list)
    return _handle_status(status, success_message)
示例#12
0
def unset_variables(project_dir, vars_to_unset):
    """Unset the variables for local project.

    Returns:
        Returns exit code
    """
    project = load_project(project_dir)
    status = project_ops.unset_variables(project, vars_to_unset)
    if status:
        print(status.status_description)
        return 0
    else:
        console_utils.print_status_errors(status)
        return 1
示例#13
0
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
示例#14
0
def add_service(project_dir, service_type, variable_name):
    """Add an item to the services section."""
    project = load_project(project_dir)
    status = project_ops.add_service(project,
                                     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 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)
示例#17
0
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 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
示例#19
0
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:
        for line in status.logs:
            print(line)
        print(status.status_description)
        return 0
    else:
        console_utils.print_status_errors(status)
        return 1
def list_packages(project_dir, environment):
    """List the packages 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("Packages for environment '{}':\n".format(env.name))
    print("\n".join(sorted(env.conda_packages)), end='\n\n')
    return 0
示例#21
0
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:
        for line in status.logs:
            print(line)
        print(status.status_description)
        return 0
    else:
        console_utils.print_status_errors(status)
        return 1
示例#22
0
def add_variables(project_dir, 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, vars_to_add, {vars_to_add[0]: default})
    if status:
        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

        def mock_isatty_true():
            return True

        # python 2 can throw a "readonly" error if you try to patch sys.stdin.isatty itself
        monkeypatch.setattr('anaconda_project.commands.console_utils.stdin_is_interactive', mock_isatty_true)
        _monkeypatch_input(monkeypatch, ["n"])

        project = load_project(dirname)
        assert project.problems == ["%s: The env_specs section is empty." % DEFAULT_PROJECT_FILENAME]

        out, err = capsys.readouterr()
        assert out == ("%s: The env_specs section is empty.\nAdd an environment spec to anaconda-project.yml? " %
                       DEFAULT_PROJECT_FILENAME)
        assert err == ""
示例#24
0
def set_variables(project_dir, 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, fixed_vars)
    if status:
        print(status.status_description)
        return 0
    else:
        console_utils.print_status_errors(status)
        return 1
示例#25
0
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 remove_env_spec(project_dir, name):
    """Remove an environment with packages from the project."""
    project = load_project(project_dir)
    status = project_ops.remove_env_spec(project, name=name)
    return _handle_status(
        status, "Removed environment {} from the project file.".format(name))
def export_env_spec(project_dir, name, filename):
    """Save an environment.yml file."""
    project = load_project(project_dir)
    status = project_ops.export_env_spec(project, name=name, filename=filename)
    return _handle_status(status)