Exemplo n.º 1
0
    def test_make_candidate_evaluator(
        self,
        allow_all_prereleases,
        prefer_binary,
    ):
        target_python = TargetPython()
        target_python._valid_tags = [('py36', 'none', 'any')]
        candidate_prefs = CandidatePreferences(
            prefer_binary=prefer_binary,
            allow_all_prereleases=allow_all_prereleases,
        )
        finder = PackageFinder(
            search_scope=SearchScope([], []),
            session=PipSession(),
            target_python=target_python,
            allow_yanked=True,
            candidate_prefs=candidate_prefs,
        )

        specifier = SpecifierSet()
        # Pass hashes to check that _hashes is set.
        hashes = Hashes({'sha256': [64 * 'a']})
        evaluator = finder.make_candidate_evaluator(
            'my-project',
            specifier=specifier,
            hashes=hashes,
        )
        assert evaluator._allow_all_prereleases == allow_all_prereleases
        assert evaluator._hashes == hashes
        assert evaluator._prefer_binary == prefer_binary
        assert evaluator._project_name == 'my-project'
        assert evaluator._specifier is specifier
        assert evaluator._supported_tags == [('py36', 'none', 'any')]
Exemplo n.º 2
0
    def test_make_link_evaluator(
        self,
        allow_yanked,
        ignore_requires_python,
        only_binary,
        expected_formats,
    ):
        # Create a test TargetPython that we can check for.
        target_python = TargetPython(py_version_info=(3, 7))
        format_control = FormatControl(set(), only_binary)
        finder = PackageFinder(
            search_scope=SearchScope([], []),
            session=PipSession(),
            target_python=target_python,
            allow_yanked=allow_yanked,
            format_control=format_control,
            ignore_requires_python=ignore_requires_python,
        )

        # Pass a project_name that will be different from canonical_name.
        link_evaluator = finder.make_link_evaluator('Twine')

        assert link_evaluator.project_name == 'Twine'
        assert link_evaluator._canonical_name == 'twine'
        assert link_evaluator._allow_yanked == allow_yanked
        assert link_evaluator._ignore_requires_python == ignore_requires_python
        assert link_evaluator._formats == expected_formats

        # Test the _target_python attribute.
        actual_target_python = link_evaluator._target_python
        # The target_python attribute should be set as is.
        assert actual_target_python is target_python
        # For good measure, check that the attributes weren't reset.
        assert actual_target_python._given_py_version_info == (3, 7)
        assert actual_target_python.py_version_info == (3, 7, 0)
Exemplo n.º 3
0
    def create(
            cls,
            project_name,  # type: str
            target_python=None,  # type: Optional[TargetPython]
            prefer_binary=False,  # type: bool
            allow_all_prereleases=False,  # type: bool
            specifier=None,  # type: Optional[specifiers.BaseSpecifier]
            hashes=None,  # type: Optional[Hashes]
    ):
        # type: (...) -> CandidateEvaluator
        """Create a CandidateEvaluator object.

        :param target_python: The target Python interpreter to use when
            checking compatibility. If None (the default), a TargetPython
            object will be constructed from the running Python.
        :param specifier: An optional object implementing `filter`
            (e.g. `packaging.specifiers.SpecifierSet`) to filter applicable
            versions.
        :param hashes: An optional collection of allowed hashes.
        """
        if target_python is None:
            target_python = TargetPython()
        if specifier is None:
            specifier = specifiers.SpecifierSet()

        supported_tags = target_python.get_tags()

        return cls(
            project_name=project_name,
            supported_tags=supported_tags,
            specifier=specifier,
            prefer_binary=prefer_binary,
            allow_all_prereleases=allow_all_prereleases,
            hashes=hashes,
        )
Exemplo n.º 4
0
    def _get_finder():
        try:
            return PackageFinder(find_links=[],
                                 index_urls=[],
                                 session=PipSession())
        except TypeError:
            pass

        # pip 19.3
        from pip._internal.models.search_scope import SearchScope
        from pip._internal.models.selection_prefs import SelectionPreferences
        try:
            return PackageFinder.create(
                search_scope=SearchScope(find_links=[], index_urls=[]),
                selection_prefs=SelectionPreferences(allow_yanked=False),
                session=PipSession(),
            )
        except TypeError:
            pass

        from pip._internal.models.target_python import TargetPython
        try:
            # pip 19.3.1
            from pip._internal.collector import LinkCollector
        except ImportError:
            from pip._internal.index.collector import LinkCollector
        return PackageFinder.create(
            link_collector=LinkCollector(
                search_scope=SearchScope(find_links=[], index_urls=[]),
                session=PipSession(),
            ),
            selection_prefs=SelectionPreferences(allow_yanked=False),
            target_python=TargetPython(),
        )
Exemplo n.º 5
0
    def create(
            cls,
            link_collector,  # type: LinkCollector
            selection_prefs,  # type: SelectionPreferences
            target_python=None,  # type: Optional[TargetPython]
    ):
        # type: (...) -> PackageFinder
        """Create a PackageFinder.

        :param selection_prefs: The candidate selection preferences, as a
            SelectionPreferences object.
        :param target_python: The target Python interpreter to use when
            checking compatibility. If None (the default), a TargetPython
            object will be constructed from the running Python.
        """
        if target_python is None:
            target_python = TargetPython()

        candidate_prefs = CandidatePreferences(
            prefer_binary=selection_prefs.prefer_binary,
            allow_all_prereleases=selection_prefs.allow_all_prereleases,
        )

        return cls(
            candidate_prefs=candidate_prefs,
            link_collector=link_collector,
            target_python=target_python,
            allow_yanked=selection_prefs.allow_yanked,
            format_control=selection_prefs.format_control,
            ignore_requires_python=selection_prefs.ignore_requires_python,
        )
    def run(self, options, args):
        options.timeout = TIMEOUT
        options.retries = RETRIES
        options.ignore_installed = True
        options.editables = []

        with self._build_session(options) as session:
            finder = self._build_package_finder(
                options=options,
                session=session,
                target_python=TargetPython(
                    platform=options.platform,
                    py_version_info=options.python_version,
                    abi=options.abi,
                    implementation=options.implementation,
                ),
            )

            requirement_set = self.get_requirements(
                args,
                options,
                finder,
                session,
            )

            candidates = []
            for req in requirement_set:
                # extract from finder.find_requirement
                all_candidates = finder.find_all_candidates(req.name)
                candidates.extend(all_candidates)

        self.candidates = candidates
        return SUCCESS
Exemplo n.º 7
0
    def __init__(
            self,
            target_python=None,  # type: Optional[TargetPython]
            prefer_binary=False,  # type: bool
            allow_all_prereleases=False,  # type: bool
            ignore_requires_python=None,  # type: Optional[bool]
    ):
        # type: (...) -> None
        """
        :param target_python: The target Python interpreter to use to check
            both the Python version embedded in the filename and the package's
            "Requires-Python" metadata. If None (the default), then a
            TargetPython object will be constructed from the running Python.
        :param allow_all_prereleases: Whether to allow all pre-releases.
        :param ignore_requires_python: Whether to ignore incompatible
            "Requires-Python" values in links. Defaults to False.
        """
        if target_python is None:
            target_python = TargetPython()
        if ignore_requires_python is None:
            ignore_requires_python = False

        self._ignore_requires_python = ignore_requires_python
        self._prefer_binary = prefer_binary
        self._target_python = target_python

        # We compile the regex here instead of as a class attribute so as
        # not to impact pip start-up time.  This is also okay because
        # CandidateEvaluator is generally instantiated only once per pip
        # invocation (when PackageFinder is instantiated).
        self._py_version_re = re.compile(r'-py([123]\.?[0-9]?)$')

        self.allow_all_prereleases = allow_all_prereleases
Exemplo n.º 8
0
def make_target_python(options):
    # type: (Values) -> TargetPython
    target_python = TargetPython(
        platform=options.platform,
        py_version_info=options.python_version,
        abi=options.abi,
        implementation=options.implementation,
    )
Exemplo n.º 9
0
 def test_get_tags__uses_cached_value(self):
     """
     Test that get_tags() uses the cached value.
     """
     target_python = TargetPython(py_version_info=None)
     target_python._valid_tags = ['tag-1', 'tag-2']
     actual = target_python.get_tags()
     assert actual == ['tag-1', 'tag-2']
Exemplo n.º 10
0
 def test_init__target_python(self):
     """
     Test the target_python argument.
     """
     target_python = TargetPython(py_version_info=(3, 7, 3))
     evaluator = CandidateEvaluator(target_python=target_python)
     # The target_python attribute should be set as is.
     assert evaluator._target_python is target_python
Exemplo n.º 11
0
def make_target_python(options: Values) -> TargetPython:
    target_python = TargetPython(
        platforms=options.platforms,
        py_version_info=options.python_version,
        abis=options.abis,
        implementation=options.implementation,
    )

    return target_python
Exemplo n.º 12
0
 def make_test_link_evaluator(self, formats: Iterable[str]) -> LinkEvaluator:
     target_python = TargetPython()
     return LinkEvaluator(
         project_name="pytest",
         canonical_name="pytest",
         formats=frozenset(formats),
         target_python=target_python,
         allow_yanked=True,
     )
Exemplo n.º 13
0
 def make_test_link_evaluator(self, formats):
     target_python = TargetPython()
     return LinkEvaluator(
         project_name='pytest',
         canonical_name='pytest',
         formats=formats,
         target_python=target_python,
         allow_yanked=True,
     )
Exemplo n.º 14
0
    def test_init__py_version_info_none(self):
        """
        Test passing py_version_info=None.
        """
        target_python = TargetPython(py_version_info=None)

        assert target_python._given_py_version_info is None

        assert target_python.py_version_info == CURRENT_PY_VERSION_INFO
        assert target_python.py_version == pyversion
Exemplo n.º 15
0
 def test_create(self, allow_all_prereleases, prefer_binary):
     target_python = TargetPython()
     target_python._valid_tags = [('py36', 'none', 'any')]
     evaluator = CandidateEvaluator.create(
         target_python=target_python,
         allow_all_prereleases=allow_all_prereleases,
         prefer_binary=prefer_binary,
     )
     assert evaluator._allow_all_prereleases == allow_all_prereleases
     assert evaluator._prefer_binary == prefer_binary
     assert evaluator._supported_tags == [('py36', 'none', 'any')]
Exemplo n.º 16
0
 def test_get_tags__uses_cached_value(self) -> None:
     """
     Test that get_tags() uses the cached value.
     """
     target_python = TargetPython(py_version_info=None)
     target_python._valid_tags = [
         Tag("py2", "none", "any"),
         Tag("py3", "none", "any"),
     ]
     actual = target_python.get_tags()
     assert actual == [Tag("py2", "none", "any"), Tag("py3", "none", "any")]
Exemplo n.º 17
0
    def test_get_tags(self, mock_get_supported, py_version_info):
        mock_get_supported.return_value = ['tag-1', 'tag-2']

        target_python = TargetPython(py_version_info=py_version_info)
        actual = target_python.get_tags()
        assert actual == ['tag-1', 'tag-2']

        actual = mock_get_supported.call_args[1]['version_info']
        assert actual == py_version_info

        # Check that the value was cached.
        assert target_python._valid_tags == ['tag-1', 'tag-2']
Exemplo n.º 18
0
    def test_init__py_version_info(self, py_version_info, expected):
        """
        Test passing the py_version_info argument.
        """
        expected_py_version_info, expected_py_version = expected

        target_python = TargetPython(py_version_info=py_version_info)

        # The _given_py_version_info attribute should be set as is.
        assert target_python._given_py_version_info == py_version_info

        assert target_python.py_version_info == expected_py_version_info
        assert target_python.py_version == expected_py_version
Exemplo n.º 19
0
    def test_init__py_version_info_none(self):
        """
        Test passing py_version_info=None.
        """
        # Get the index of the second dot.
        index = sys.version.find('.', 2)
        current_major_minor = sys.version[:index]  # e.g. "3.6"

        target_python = TargetPython(py_version_info=None)

        assert target_python._given_py_version_info is None

        assert target_python.py_version_info == CURRENT_PY_VERSION_INFO
        assert target_python.py_version == current_major_minor
Exemplo n.º 20
0
 def test_create(self, allow_all_prereleases: bool, prefer_binary: bool) -> None:
     target_python = TargetPython()
     target_python._valid_tags = [Tag("py36", "none", "any")]
     specifier = SpecifierSet()
     evaluator = CandidateEvaluator.create(
         project_name="my-project",
         target_python=target_python,
         allow_all_prereleases=allow_all_prereleases,
         prefer_binary=prefer_binary,
         specifier=specifier,
     )
     assert evaluator._allow_all_prereleases == allow_all_prereleases
     assert evaluator._prefer_binary == prefer_binary
     assert evaluator._specifier is specifier
     assert evaluator._supported_tags == [Tag("py36", "none", "any")]
Exemplo n.º 21
0
 def test_create__target_python(self):
     """
     Test that target_python is passed to CandidateEvaluator as is.
     """
     target_python = TargetPython(py_version_info=(3, 7, 3))
     finder = PackageFinder.create(
         [],
         [],
         session=object(),
         target_python=target_python,
     )
     evaluator = finder.candidate_evaluator
     actual_target_python = evaluator._target_python
     assert actual_target_python is target_python
     assert actual_target_python.py_version_info == (3, 7, 3)
Exemplo n.º 22
0
 def test_create(self, allow_all_prereleases, prefer_binary):
     target_python = TargetPython()
     target_python._valid_tags = [('py36', 'none', 'any')]
     specifier = SpecifierSet()
     evaluator = CandidateEvaluator.create(
         project_name='my-project',
         target_python=target_python,
         allow_all_prereleases=allow_all_prereleases,
         prefer_binary=prefer_binary,
         specifier=specifier,
     )
     assert evaluator._allow_all_prereleases == allow_all_prereleases
     assert evaluator._prefer_binary == prefer_binary
     assert evaluator._specifier is specifier
     assert evaluator._supported_tags == [('py36', 'none', 'any')]
Exemplo n.º 23
0
    def test_not_find_wheel_not_supported(self, data, monkeypatch):
        """
        Test not finding an unsupported wheel.
        """
        req = install_req_from_line("simple.dist")
        target_python = TargetPython()
        # Make sure no tags will match.
        target_python._valid_tags = []
        finder = make_test_finder(
            find_links=[data.find_links],
            target_python=target_python,
        )

        with pytest.raises(DistributionNotFound):
            finder.find_requirement(req, True)
Exemplo n.º 24
0
 def test_create__target_python(self):
     """
     Test that target_python is passed to CandidateEvaluator as is.
     """
     search_scope = SearchScope([], [])
     target_python = TargetPython(py_version_info=(3, 7, 3))
     finder = PackageFinder.create(
         search_scope=search_scope,
         allow_yanked=True,
         session=object(),
         target_python=target_python,
     )
     evaluator = finder.candidate_evaluator
     actual_target_python = evaluator._target_python
     assert actual_target_python is target_python
     assert actual_target_python.py_version_info == (3, 7, 3)
Exemplo n.º 25
0
 def test_create__target_python(self):
     """
     Test that the _target_python attribute is set correctly.
     """
     target_python = TargetPython(py_version_info=(3, 7, 3))
     finder = PackageFinder.create(
         search_scope=SearchScope([], []),
         selection_prefs=SelectionPreferences(allow_yanked=True),
         session=PipSession(),
         target_python=target_python,
     )
     actual_target_python = finder._target_python
     # The target_python attribute should be set as is.
     assert actual_target_python is target_python
     # Check that the attributes weren't reset.
     assert actual_target_python.py_version_info == (3, 7, 3)
Exemplo n.º 26
0
    def test_init__py_version_info(
        self,
        py_version_info: Tuple[int, ...],
        expected: Tuple[Tuple[int, int, int], str],
    ) -> None:
        """
        Test passing the py_version_info argument.
        """
        expected_py_version_info, expected_py_version = expected

        target_python = TargetPython(py_version_info=py_version_info)

        # The _given_py_version_info attribute should be set as is.
        assert target_python._given_py_version_info == py_version_info

        assert target_python.py_version_info == expected_py_version_info
        assert target_python.py_version == expected_py_version
Exemplo n.º 27
0
    def test_get_tags(
        self,
        mock_get_supported: mock.Mock,
        py_version_info: Optional[Tuple[int, ...]],
        expected_version: Optional[str],
    ) -> None:
        mock_get_supported.return_value = ["tag-1", "tag-2"]

        target_python = TargetPython(py_version_info=py_version_info)
        actual = target_python.get_tags()
        assert actual == ["tag-1", "tag-2"]

        actual = mock_get_supported.call_args[1]["version"]
        assert actual == expected_version

        # Check that the value was cached.
        assert target_python._valid_tags == ["tag-1", "tag-2"]
Exemplo n.º 28
0
 def test_evaluate_link__allow_yanked(
     self, yanked_reason, allow_yanked, expected,
 ):
     target_python = TargetPython(py_version_info=(3, 6, 4))
     evaluator = LinkEvaluator(
         project_name='twine',
         canonical_name='twine',
         formats={'source'},
         target_python=target_python,
         allow_yanked=allow_yanked,
     )
     link = Link(
         'https://example.com/#egg=twine-1.12',
         yanked_reason=yanked_reason,
     )
     actual = evaluator.evaluate_link(link)
     assert actual == expected
Exemplo n.º 29
0
 def test_evaluate_link(
     self, py_version_info, ignore_requires_python, expected,
 ):
     target_python = TargetPython(py_version_info=py_version_info)
     evaluator = LinkEvaluator(
         project_name='twine',
         canonical_name='twine',
         formats={'source'},
         target_python=target_python,
         allow_yanked=True,
         ignore_requires_python=ignore_requires_python,
     )
     link = Link(
         'https://example.com/#egg=twine-1.12',
         requires_python='== 3.6.5',
     )
     actual = evaluator.evaluate_link(link)
     assert actual == expected
Exemplo n.º 30
0
    def create(
            cls,
            search_scope,  # type: SearchScope
            selection_prefs,  # type: SelectionPreferences
            trusted_hosts=None,  # type: Optional[List[str]]
            session=None,  # type: Optional[PipSession]
            target_python=None,  # type: Optional[TargetPython]
    ):
        # type: (...) -> PackageFinder
        """Create a PackageFinder.

        :param selection_prefs: The candidate selection preferences, as a
            SelectionPreferences object.
        :param trusted_hosts: Domains not to emit warnings for when not using
            HTTPS.
        :param session: The Session to use to make requests.
        :param target_python: The target Python interpreter to use when
            checking compatibility. If None (the default), a TargetPython
            object will be constructed from the running Python.
        """
        if session is None:
            raise TypeError(
                "PackageFinder.create() missing 1 required keyword argument: "
                "'session'")
        if target_python is None:
            target_python = TargetPython()

        supported_tags = target_python.get_tags()
        candidate_evaluator = CandidateEvaluator(
            supported_tags=supported_tags,
            prefer_binary=selection_prefs.prefer_binary,
            allow_all_prereleases=selection_prefs.allow_all_prereleases,
        )

        return cls(
            candidate_evaluator=candidate_evaluator,
            search_scope=search_scope,
            session=session,
            target_python=target_python,
            allow_yanked=selection_prefs.allow_yanked,
            format_control=selection_prefs.format_control,
            trusted_hosts=trusted_hosts,
            ignore_requires_python=selection_prefs.ignore_requires_python,
        )