Example #1
0
def _get_default_option(option_name: str) -> Any:
    """
    Get default value of the pip's option (including option from pip.conf)
    by a given option name.
    """
    install_command = create_command("install")
    default_values = install_command.parser.get_default_values()
    return getattr(default_values, option_name)
Example #2
0
def check_commands(pred: Callable[[Command], bool],
                   expected: List[str]) -> None:
    """
    Check the commands satisfying a predicate.
    """
    commands = [create_command(name) for name in sorted(commands_dict)]
    actual = [command.name for command in commands if pred(command)]
    assert actual == expected, f"actual: {actual}"
Example #3
0
def install_packages(requirements_path, directory, pip_args):
    pip_args = [
        "--isolated", "--disable-pip-version-check", "--target", directory,
        "--no-deps", "--ignore-requires-python", "--require-hashes", "-r",
        requirements_path
    ] + pip_args
    cmd = create_command("install")
    assert not cmd.main(pip_args)
Example #4
0
def test_run_method_should_return_success_when_command_name_not_specified():
    """
    Test HelpCommand.run when there are no args
    """
    options_mock = Mock()
    args = ()
    help_cmd = create_command('help')
    status = help_cmd.run(options_mock, args)
    assert status == SUCCESS
Example #5
0
def test_run_method_should_return_success_when_finds_command_name():
    """
    Test HelpCommand.run for existing command
    """
    options_mock = Mock()
    args = ('freeze', )
    help_cmd = create_command('help')
    status = help_cmd.run(options_mock, args)
    assert status == SUCCESS
Example #6
0
def test_run_method_should_return_no_matches_found_when_does_not_find_pkgs():
    """
    Test SearchCommand.run for no matches
    """
    command = create_command('search')
    cmdline = "--index=https://pypi.org/pypi nonexistentpackage"
    options, args = command.parse_args(cmdline.split())
    status = command.run(options, args)
    assert status == NO_MATCHES_FOUND
Example #7
0
def test_run_method_should_return_success_when_find_packages():
    """
    Test SearchCommand.run for found package
    """
    command = create_command('search')
    cmdline = "--index=https://pypi.org/pypi pip"
    options, args = command.parse_args(cmdline.split())
    status = command.run(options, args)
    assert status == SUCCESS
Example #8
0
def create_install_command():
    """
    Return an instance of InstallCommand.
    """
    if PIP_VERSION < (19, 3):
        return InstallCommand()

    from pip._internal.commands import create_command

    return create_command("install")
Example #9
0
 def run(self):
     node = nodes.paragraph()
     node.document = self.state.document
     desc = ViewList()
     cmd = create_command(self.arguments[0])
     description = dedent(cmd.__doc__)
     for line in description.split('\n'):
         desc.append(line, "")
     self.state.nested_parse(desc, 0, node)
     return [node]
Example #10
0
def test_list_all_versions_returns_no_matches_found_when_name_not_exact():
    """
    Test that non exact name do not match
    """
    command = create_command('index')
    cmdline = "versions pand"
    with command.main_context():
        options, args = command.parse_args(cmdline.split())
        status = command.run(options, args)
    assert status == ERROR
Example #11
0
def test_list_all_versions_returns_matches_found_when_name_is_exact():
    """
    Test that exact name matches
    """
    command = create_command('index')
    cmdline = "versions pandas"
    with command.main_context():
        options, args = command.parse_args(cmdline.split())
        status = command.run(options, args)
    assert status == SUCCESS
Example #12
0
def test_run_method_should_raise_command_error_when_command_does_not_exist():
    """
    Test HelpCommand.run for non-existing command
    """
    options_mock = Mock()
    args = ('mycommand', )
    help_cmd = create_command('help')

    with pytest.raises(CommandError):
        help_cmd.run(options_mock, args)
Example #13
0
 def run(self) -> List[nodes.Node]:
     cmd = create_command(self.arguments[0])
     cmd_prefix = "python -m pip"
     if len(self.arguments) > 1:
         cmd_prefix = " ".join(self.arguments[1:])
         cmd_prefix = cmd_prefix.strip('"')
         cmd_prefix = cmd_prefix.strip("'")
     usage = dedent(cmd.usage.replace("%prog",
                                      f"{cmd_prefix} {cmd.name}")).strip()
     node = nodes.literal_block(usage, usage)
     return [node]
Example #14
0
    def test_config_file_options(self, monkeypatch, args, expect):
        cmd = create_command('config')
        # Replace a handler with a no-op to avoid side effects
        monkeypatch.setattr(cmd, "get_name", lambda *a: None)

        options, args = cmd.parser.parse_args(args + ["get", "name"])
        if expect is PipError:
            with pytest.raises(PipError):
                cmd._determine_file(options, need_value=False)
        else:
            assert expect == cmd._determine_file(options, need_value=False)
Example #15
0
 def test_missing_hash_with_require_hashes_in_reqs_file(self, data, tmpdir):
     """--require-hashes in a requirements file should make its way to the
     RequirementSet.
     """
     finder = make_test_finder(find_links=[data.find_links])
     session = finder._link_collector.session
     command = create_command('install')
     with requirements_file('--require-hashes', tmpdir) as reqs_file:
         options, args = command.parse_args(['-r', reqs_file])
         command.get_requirements(args, options, finder, session)
     assert options.require_hashes
Example #16
0
 def run(self):
     cmd = create_command(self.arguments[0])
     cmd_prefix = 'python -m pip'
     if len(self.arguments) > 1:
         cmd_prefix = " ".join(self.arguments[1:])
         cmd_prefix = cmd_prefix.strip('"')
         cmd_prefix = cmd_prefix.strip("'")
     usage = dedent(cmd.usage.replace('%prog',
                                      f'{cmd_prefix} {cmd.name}')).strip()
     node = nodes.literal_block(usage, usage)
     return [node]
Example #17
0
 def test_missing_hash_with_require_hashes_in_reqs_file(
         self, data: TestData, tmpdir: Path) -> None:
     """--require-hashes in a requirements file should make its way to the
     RequirementSet.
     """
     finder = make_test_finder(find_links=[data.find_links])
     session = finder._link_collector.session
     command = cast(InstallCommand, create_command("install"))
     with requirements_file("--require-hashes", tmpdir) as reqs_file:
         options, args = command.parse_args(["-r", reqs_file])
         command.get_requirements(args, options, finder, session)
     assert options.require_hashes
Example #18
0
def install_package(pkg, directory, pip_args):
    """Downloads wheel for a package. Assumes python binary provided has
    pip and wheel package installed.

    Args:
        pkg: package name
        directory: destination directory to download the wheel file in
        python: python binary path used to run pip command
        pip_args: extra pip args sent to pip
    Returns:
        str: path to the wheel file
    """
    pip_args = [
        "--isolated",
        "--disable-pip-version-check",
        "--target",
        directory,
        "--no-deps",
        "--ignore-requires-python",
        "--use-deprecated=legacy-resolver",
        pkg,
    ] + pip_args
    cmd = create_command("install")
    cmd.main(pip_args)

    # need dist-info directory for pkg_resources to be able to find the packages
    dist_info = glob.glob(os.path.join(directory, "*.dist-info"))[0]
    # fix namespace packages by adding proper __init__.py files
    namespace_packages = os.path.join(dist_info, "namespace_packages.txt")
    if os.path.exists(namespace_packages):
        with open(namespace_packages) as nspkg:
            for line in nspkg.readlines():
                namespace = line.strip().replace(".", os.sep)
                if namespace:
                    _create_nspkg_init(os.path.join(directory, namespace))

    # PEP 420 -- Implicit Namespace Packages
    if (sys.version_info[0], sys.version_info[1]) >= (3, 3):
        for dirpath, dirnames, filenames in os.walk(directory):
            # we are only interested in dirs with no init file
            if "__init__.py" in filenames:
                dirnames[:] = []
                continue
            # remove bin and dist-info dirs
            for ignored in ("bin", os.path.basename(dist_info)):
                if ignored in dirnames:
                    dirnames.remove(ignored)
            _create_nspkg_init(dirpath)

    return pkginfo.Wheel(dist_info)
Example #19
0
 def test_missing_hash_with_require_hashes_in_reqs_file(self, data, tmpdir):
     """--require-hashes in a requirements file should make its way to the
     RequirementSet.
     """
     req_set = RequirementSet(require_hashes=False)
     finder = make_test_finder(find_links=[data.find_links])
     session = finder._link_collector.session
     command = create_command('install')
     with requirements_file('--require-hashes', tmpdir) as reqs_file:
         options, args = command.parse_args(['-r', reqs_file])
         command.populate_requirement_set(
             req_set, args, options, finder, session, wheel_cache=None,
         )
     assert req_set.require_hashes
Example #20
0
    def test_config_file_options(
        self,
        monkeypatch: pytest.MonkeyPatch,
        args: List[str],
        expect: Union[None, str, PipError],
    ) -> None:
        cmd = cast(ConfigurationCommand, create_command("config"))
        # Replace a handler with a no-op to avoid side effects
        monkeypatch.setattr(cmd, "get_name", lambda *a: None)

        options, args = cmd.parser.parse_args(args + ["get", "name"])
        if expect is PipError:
            with pytest.raises(PipError):
                cmd._determine_file(options, need_value=False)
        else:
            assert expect == cmd._determine_file(options, need_value=False)
Example #21
0
def main(requirements):
    """ Get installed pip packages, compare them to the passed packages `requirements` file,
        install missing packages, uninstall packages not needed anymore
    """
    install_command = create_command("install")
    options, _ = install_command.parse_args([])
    session = install_command._build_session(options)
    finder = install_command._build_package_finder(options=options,
                                                   session=session)

    requirements = parse_requirements(requirements,
                                      finder=finder,
                                      session=session)

    installed_dists = get_installed_distributions()
    to_install, to_uninstall = sync.diff(requirements, installed_dists)
    sync.sync(to_install, to_uninstall, verbose=True)
Example #22
0
    def updateScipion(cls, outdatedPackages):
        """
        Update a module from which there is released a higher version
        """
        kwargs = {'isolated': False}

        for packageName in outdatedPackages:
            cmd_args = [packageName[0], '--upgrade',
                        '-vvv']  # highest level of verbosity

            command = create_command('install', **kwargs)
            status = command.main(cmd_args)
            if status == 0:
                print('%s was correctly updated.' % packageName[0])
            else:
                print('Something went wrong during the update of %s.' %
                      packageName[0])
Example #23
0
def test_index_group_handle_pip_version_check(
    mock_version_check, command_name, disable_pip_version_check, no_index,
    expected_called,
):
    """
    Test whether pip_self_version_check() is called when
    handle_pip_version_check() is called, for each of the
    IndexGroupCommand classes.
    """
    command = create_command(command_name)
    options = command.parser.get_default_values()
    options.disable_pip_version_check = disable_pip_version_check
    options.no_index = no_index

    command.handle_pip_version_check(options)
    if expected_called:
        mock_version_check.assert_called_once()
    else:
        mock_version_check.assert_not_called()
Example #24
0
def main(args=None):

    # type: (Optional[List[str]]) -> int

    if args is None:

        args = sys.argv[1:]

    # Configure our deprecation warnings to be sent through loggers

    deprecation.install_warning_logger()

    autocomplete()

    try:

        cmd_name, cmd_args = parse_command(args)

    except PipError as exc:

        sys.stderr.write(f"ERROR: {exc}")

        sys.stderr.write(os.linesep)

        sys.exit(1)

    # Needed for locale.getpreferredencoding(False) to work

    # in pip._internal.utils.encoding.auto_decode

    try:

        locale.setlocale(locale.LC_ALL, '')

    except locale.Error as e:

        # setlocale can apparently crash if locale are uninitialized

        logger.debug("Ignoring error %s when setting locale", e)

    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))

    return command.main(cmd_args)
Example #25
0
    def __init__(self, pip_args, cache_dir):
        # Use pip's parser for pip.conf management and defaults.
        # General options (find_links, index_url, extra_index_url, trusted_host,
        # and pre) are deferred to pip.
        self.command = create_command("install")
        extra_pip_args = (
            []
            if PIP_VERSION[:2] <= (20, 2)
            else ["--use-deprecated", "legacy-resolver"]
        )
        self.options, _ = self.command.parse_args(pip_args + extra_pip_args)
        if self.options.cache_dir:
            self.options.cache_dir = normalize_path(self.options.cache_dir)

        self.options.require_hashes = False
        self.options.ignore_dependencies = False

        self.session = self.command._build_session(self.options)
        self.finder = self.command._build_package_finder(
            options=self.options, session=self.session
        )

        # Caches
        # stores project_name => InstallationCandidate mappings for all
        # versions reported by PyPI, so we only have to ask once for each
        # project
        self._available_candidates_cache = {}

        # stores InstallRequirement => list(InstallRequirement) mappings
        # of all secondary dependencies for the given requirement, so we
        # only have to go to disk once for each requirement
        self._dependencies_cache = {}

        # Setup file paths
        self._build_dir = None
        self._source_dir = None
        self._cache_dir = normalize_path(cache_dir)
        self._download_dir = fs_str(os.path.join(self._cache_dir, "pkgs"))
        if PIP_VERSION[:2] <= (20, 2):
            self._wheel_download_dir = fs_str(os.path.join(self._cache_dir, "wheels"))

        self._setup_logging()
Example #26
0
    def test_config_file_venv_option(self, monkeypatch):
        cmd = create_command('config')
        # Replace a handler with a no-op to avoid side effects
        monkeypatch.setattr(cmd, "get_name", lambda *a: None)

        collected_warnings = []

        def _warn(message, *a, **kw):
            collected_warnings.append(message)
        monkeypatch.setattr("warnings.warn", _warn)

        options, args = cmd.parser.parse_args(["--venv", "get", "name"])
        assert "site" == cmd._determine_file(options, need_value=False)
        assert collected_warnings
        assert "--site" in collected_warnings[0]

        # No warning or error if both "--venv" and "--site" are specified
        collected_warnings[:] = []
        options, args = cmd.parser.parse_args(["--venv", "--site", "get",
                                               "name"])
        assert "site" == cmd._determine_file(options, need_value=False)
        assert not collected_warnings
Example #27
0
def install_package(pkg, directory, pip_args):
    """Downloads wheel for a package. Assumes python binary provided has
    pip and wheel package installed.

    Args:
        pkg: package name
        directory: destination directory to download the wheel file in
        python: python binary path used to run pip command
        pip_args: extra pip args sent to pip
    Returns:
        str: path to the wheel file
    """
    pip_args = [
        "--isolated",
        "--disable-pip-version-check",
        "--target",
        directory,
        "--no-deps",
        "--ignore-requires-python",
        pkg,
    ] + pip_args
    cmd = create_command("install")
    cmd.main(pip_args)
Example #28
0
    def __init__(self, pip_args: List[str], cache_dir: str):
        # Use pip's parser for pip.conf management and defaults.
        # General options (find_links, index_url, extra_index_url, trusted_host,
        # and pre) are deferred to pip.
        self.command: InstallCommand = create_command("install")
        extra_pip_args = ["--use-deprecated", "legacy-resolver"]

        options, _ = self.command.parse_args(pip_args + extra_pip_args)
        if options.cache_dir:
            options.cache_dir = normalize_path(options.cache_dir)
        options.require_hashes = False
        options.ignore_dependencies = False

        self._options: optparse.Values = options
        self._session = self.command._build_session(options)
        self._finder = self.command._build_package_finder(options=options,
                                                          session=self.session)

        # Caches
        # stores project_name => InstallationCandidate mappings for all
        # versions reported by PyPI, so we only have to ask once for each
        # project
        self._available_candidates_cache: Dict[
            str, List[InstallationCandidate]] = {}

        # stores InstallRequirement => list(InstallRequirement) mappings
        # of all secondary dependencies for the given requirement, so we
        # only have to go to disk once for each requirement
        self._dependencies_cache: Dict[InstallRequirement,
                                       Set[InstallRequirement]] = {}

        # Setup file paths
        self._cache_dir = normalize_path(str(cache_dir))
        self._download_dir = os.path.join(self._cache_dir, "pkgs")

        if PIP_VERSION[0] < 22:
            self._setup_logging()
Example #29
0
File: help.py Project: quiet-it/bot
    def run(self, options, args):
        from pip._internal.commands import (
            commands_dict, create_command, get_similar_commands,
        )

        try:
            # 'pip help' with no args is handled by pip.__init__.parseopt()
            cmd_name = args[0]  # the command we need help for
        except IndexError:
            return SUCCESS

        if cmd_name not in commands_dict:
            guess = get_similar_commands(cmd_name)

            msg = ['unknown command "%s"' % cmd_name]
            if guess:
                msg.append('maybe you meant "%s"' % guess)

            raise CommandError(' - '.join(msg))

        command = create_command(cmd_name)
        command.parser.print_help()

        return SUCCESS
Example #30
0
    def run(self, options, args):
        # type: (Values, List[str]) -> int
        from pip._internal.commands import (commands_dict, create_command,
                                            get_similar_commands)

        try:
            # 'pip help' with no args is handled by pip.__init__.parseopt()
            cmd_name = args[0]  # the command we need help for
        except IndexError:
            return SUCCESS

        if cmd_name not in commands_dict:
            guess = get_similar_commands(cmd_name)

            msg = [f'unknown command "{cmd_name}"']
            if guess:
                msg.append(f'maybe you meant "{guess}"')

            raise CommandError(" - ".join(msg))

        command = create_command(cmd_name)
        command.parser.print_help()

        return SUCCESS