Esempio n. 1
0
def init_command(project_dir, assume_yes, with_anaconda_package):
    """Initialize a new project.

    Returns:
        Exit code (0 on success)
    """
    # we don't want False right now because either you specify
    # --yes or we go with the default in project_ops.create
    # (depends on whether project file already exists).
    assert assume_yes is None or assume_yes is True
    assert with_anaconda_package is None or with_anaconda_package is True

    if not os.path.exists(project_dir):
        if assume_yes:
            make_directory = True
        else:
            make_directory = console_ask_yes_or_no("Create directory '%s'?" % project_dir, default=False)
    else:
        make_directory = False

    project = project_ops.create(project_dir,
                                 make_directory=make_directory,
                                 fix_problems=assume_yes,
                                 with_anaconda_package=with_anaconda_package)
    if print_project_problems(project):
        return 1
    else:
        print("Project configuration is in %s" % project.project_file.filename)
        return 0
Esempio n. 2
0
def load_project(dirname):
    """Load a Project, fixing it if needed and possible."""
    project = Project(dirname, frontend=CliFrontend(), must_exist=True)

    # No sense in engaging the user if we cannot achieve a fixed state.
    if project.unfixable_problems:
        return project

    if console_utils.stdin_is_interactive():
        regressions = 0
        problems = project.fixable_problems
        while problems and regressions < 3:
            # Instead of looping through the problems in the list, we
            # fix only the first one and refresh the list. This allows
            # us to detect when fixing one problem impacts another,
            # positively or negatively.
            problem = problems[0]
            print(problem.text)
            should_fix = console_utils.console_ask_yes_or_no(problem.fix_prompt, default=False)
            if not should_fix:
                break
            problem.fix(project)
            project.use_changes_without_saving()
            o_problems, problems = problems, project.fixable_problems
            # If the number of problems doesn't decrease as a result of
            # fixing a problem, it suggests some sort of negative cycle.
            # We can't reliably detect a cycle, so instead we simply let
            # this happen 3 times before we give up.
            regressions += (len(problems) >= len(o_problems))
        if not problems:
            project.save()

    return project
Esempio n. 3
0
def test_console_yes_or_no(monkeypatch, capsys):
    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.internal.cli.console_utils.stdin_is_interactive',
        mock_isatty_true)

    for answer in ("y", "Y", "yes", "Yes", "YES", "yoyo"):
        _monkeypatch_input(monkeypatch, answer)
        assert console_utils.console_ask_yes_or_no("foo?", False)
        out, err = capsys.readouterr()
        assert out == "foo? "
        assert err == ""

    for answer in ("n", "N", "no", "No", "NO", "nay"):
        _monkeypatch_input(monkeypatch, answer)
        assert not console_utils.console_ask_yes_or_no("foo?", True)
        out, err = capsys.readouterr()
        assert out == "foo? "
        assert err == ""

    _monkeypatch_input(monkeypatch, ("", "yes"))
    assert console_utils.console_ask_yes_or_no("foo?", False)
    out, err = capsys.readouterr()
    assert out == "foo? foo? (enter y or n): "
    assert err == ""

    _monkeypatch_input(monkeypatch, None)
    assert console_utils.console_ask_yes_or_no("foo?", True)
    out, err = capsys.readouterr()
    assert out == "foo? "
    assert err == ""

    _monkeypatch_input(monkeypatch, None)
    assert not console_utils.console_ask_yes_or_no("foo?", False)
    out, err = capsys.readouterr()
    assert out == "foo? "
    assert err == ""
def load_project(dirname):
    """Load a Project, fixing it if needed and possible."""
    project = Project(dirname, frontend=CliFrontend())

    if console_utils.stdin_is_interactive():
        had_fixable = len(project.fixable_problems) > 0
        for problem in project.fixable_problems:
            print(problem.text)
            should_fix = console_utils.console_ask_yes_or_no(problem.fix_prompt, default=False)
            if should_fix:
                problem.fix(project)
            else:
                problem.no_fix(project)

        # both fix() and no_fix() can modify project_file, if no changes
        # were made this is a no-op.
        if had_fixable:
            project.save()

    return project