def update(self, ref): target_ref = self.repo_instance.make_rev_options(ref) if pip_shims.parse_version(pip_shims.pip_version) > pip_shims.parse_version("18.0"): self.repo_instance.update(self.checkout_directory, self.url, target_ref) else: self.repo_instance.update(self.checkout_directory, target_ref) self.commit_sha = self.get_commit_hash()
def obtain(self, verbosity=1): # type: () -> None lt_pip_19_2 = (pip_shims.parsed_pip_version.parsed_version < pip_shims.parse_version("19.2")) gte_pip_22_0 = (pip_shims.parsed_pip_version.parsed_version >= pip_shims.parse_version("22.0")) if lt_pip_19_2: self.repo_backend = self.repo_backend(self.url) if os.path.exists(self.checkout_directory ) and not self.repo_backend.is_repository_directory( self.checkout_directory): self.repo_backend.unpack(self.checkout_directory) elif not os.path.exists(self.checkout_directory): if lt_pip_19_2: self.repo_backend.obtain(self.checkout_directory) elif gte_pip_22_0: self.repo_backend.obtain(self.checkout_directory, self.parsed_url, verbosity) else: # at least Pip 19.2 but not quite pip 22.x self.repo_backend.obtain(self.checkout_directory, self.parsed_url) else: if self.ref: self.checkout_ref(self.ref) if not self.commit_sha: self.commit_sha = self.get_commit_hash()
def update(self, ref): target_ref = self.repo_instance.make_rev_options(ref) sha = self.repo_instance.get_revision_sha(self.checkout_directory, target_ref.arg_rev) target_rev = target_ref.make_new(sha) if parse_version(pip_version) > parse_version("18.0"): self.repo_instance.update(self.checkout_directory, self.url, target_rev) else: self.repo_instance.update(self.checkout_directory, target_ref) self.commit_hash = self.get_commit_hash(ref)
def update(self, ref): # type: (str) -> None target_ref = self.repo_backend.make_rev_options(ref) if pip_shims.parse_version(pip_shims.pip_version) > pip_shims.parse_version( "18.0" ): self.repo_backend.update(self.checkout_directory, self.url, target_ref) else: self.repo_backend.update(self.checkout_directory, target_ref) self.commit_sha = self.get_commit_hash()
def obtain(self): # type: () -> None lte_pip_19 = (pip_shims.parsed_pip_version.parsed_version < pip_shims.parse_version("19.0")) if os.path.exists(self.checkout_directory ) and not self.repo_backend.is_repository_directory( self.checkout_directory): self.repo_backend.unpack(self.checkout_directory) elif not os.path.exists(self.checkout_directory): if lte_pip_19: self.repo_backend.obtain(self.checkout_directory) else: self.repo_backend.obtain(self.checkout_directory, self.parsed_url) else: if self.ref: self.checkout_ref(self.ref) if not self.commit_sha: self.commit_sha = self.get_commit_hash()
def test_wheelbuilder(tmpdir, PipCommand): output_dir = tmpdir.join("output") output_dir.mkdir() pip_command = PipCommand() pip_command.parser.add_option_group( make_option_group(index_group, pip_command.parser) ) pip_options, _ = pip_command.parser.parse_args([]) CACHE_DIR = tmpdir.mkdir("CACHE_DIR") pip_options.cache_dir = CACHE_DIR.strpath session = pip_command._build_session(pip_options) if parse_version(pip_version) > parse_version("19.1.1"): index_urls = [pip_options.index_url] + pip_options.extra_index_urls search_scope = SearchScope.create( find_links=pip_options.find_links, index_urls=index_urls ) selection_prefs = SelectionPreferences( True, allow_all_prereleases=False, format_control=None, prefer_binary=False, ignore_requires_python=False, ) target_python = TargetPython() candidate_prefs = CandidatePreferences( prefer_binary=selection_prefs.prefer_binary, allow_all_prereleases=selection_prefs.allow_all_prereleases, ) if parse_version(pip_version) > parse_version("19.2.3"): link_collector = LinkCollector(session=session, search_scope=search_scope) finder_args = {"link_collector": link_collector} else: finder_args = {"search_scope": search_scope, "session": session} finder_args.update( { "candidate_prefs": candidate_prefs, "target_python": target_python, "allow_yanked": selection_prefs.allow_yanked, "format_control": selection_prefs.format_control, "ignore_requires_python": selection_prefs.ignore_requires_python, } ) else: finder_args = { "find_links": pip_options.find_links, "index_urls": [pip_options.index_url] + pip_options.extra_index_urls, "trusted_hosts": pip_options.trusted_hosts, "session": session, "allow_all_prereleases": False, } # finder_args["allow_all_prereleases"] = False finder = PackageFinder(**finder_args) build_dir = tmpdir.mkdir("build_dir") source_dir = tmpdir.mkdir("source_dir") download_dir = tmpdir.mkdir("download_dir") wheel_download_dir = CACHE_DIR.mkdir("wheels") with wheel_cache(USER_CACHE_DIR, FormatControl(None, None)) as wheelcache: kwargs = { "build_dir": build_dir.strpath, "src_dir": source_dir.strpath, "download_dir": download_dir.strpath, "wheel_download_dir": wheel_download_dir.strpath, "finder": finder, "require_hashes": False, "use_user_site": False, "progress_bar": "off", "build_isolation": False, } ireq = InstallRequirement.from_editable( "git+https://github.com/urllib3/[email protected]#egg=urllib3" ) if parse_version(pip_version) <= parse_version("20.0.9999999"): ireq.populate_link(finder, False, False) ireq.ensure_has_source_dir(kwargs["src_dir"]) # Ensure the remote artifact is downloaded locally. For wheels, it is # enough to just download because we'll use them directly. For an sdist, # we need to unpack so we can build it. unpack_kwargs = { "session": session, "hashes": ireq.hashes(True), "link": ireq.link, "location": ireq.source_dir, "download_dir": kwargs["download_dir"], } if parse_version(pip_version) < parse_version("19.2.0"): unpack_kwargs["only_download"] = ireq.is_wheel if parse_version(pip_version) >= parse_version("10"): unpack_kwargs["progress_bar"] = "off" if not is_file_url(ireq.link): shim_unpack(**unpack_kwargs) output_file = None ireq.is_direct = True build_args = { "req": ireq, "output_dir": output_dir.strpath, "verify": False, "build_options": [], "global_options": [], "editable": False, } output_file = call_function_with_correct_args(build_one, **build_args) # XXX: skipping to here is functionally the same and should pass all tests # output_file = build_wheel(**build_wheel_kwargs) assert output_file, output_file
def test_resolution(tmpdir, PipCommand): pip_command = PipCommand() pip_command.parser.add_option_group( make_option_group(index_group, pip_command.parser) ) pip_options, _ = pip_command.parser.parse_args([]) CACHE_DIR = tmpdir.mkdir("CACHE_DIR") pip_options.cache_dir = CACHE_DIR.strpath session = pip_command._build_session(pip_options) assert session if parse_version(pip_version) > parse_version("19.1.1"): index_urls = [pip_options.index_url] + pip_options.extra_index_urls search_scope = SearchScope.create( find_links=pip_options.find_links, index_urls=index_urls ) selection_prefs = SelectionPreferences( True, allow_all_prereleases=False, format_control=None, prefer_binary=False, ignore_requires_python=False, ) target_python = TargetPython() candidate_prefs = CandidatePreferences( prefer_binary=selection_prefs.prefer_binary, allow_all_prereleases=selection_prefs.allow_all_prereleases, ) if parse_version(pip_version) > parse_version("19.2.3"): link_collector = LinkCollector(session=session, search_scope=search_scope) finder_args = {"link_collector": link_collector} else: finder_args = {"search_scope": search_scope, "session": session} finder_args.update( { "candidate_prefs": candidate_prefs, "target_python": target_python, "allow_yanked": selection_prefs.allow_yanked, "format_control": selection_prefs.format_control, "ignore_requires_python": selection_prefs.ignore_requires_python, } ) else: finder_args = { "find_links": pip_options.find_links, "index_urls": [pip_options.index_url], "trusted_hosts": pip_options.trusted_hosts, "session": session, "allow_all_prereleases": False, } # finder_args["allow_all_prereleases"] = False finder = PackageFinder(**finder_args) ireq = InstallRequirement.from_line("requests>=2.18") if install_req_from_line: ireq2 = install_req_from_line("requests>=2.18") assert str(ireq) == str(ireq2) requests_candidates = finder.find_all_candidates(ireq.name) candidates = sorted( [ c for c in requests_candidates if c.version in ireq.specifier.filter( candidate.version for candidate in requests_candidates ) ], key=lambda c: c.version, ) best_version = candidates[-1] location = getattr(best_version, "location", getattr(best_version, "link", None)) assert "pythonhosted" in location.url req_file = tmpdir.mkdir("req_dir").join("requirements.txt") req_file.write_text( textwrap.dedent( """ requests>=2.18 """ ), encoding="utf-8", ) build_dir = tmpdir.mkdir("build_dir") source_dir = tmpdir.mkdir("source_dir") download_dir = tmpdir.mkdir("download_dir") results = None with wheel_cache(USER_CACHE_DIR, FormatControl(None, None)) as wheelcache: preparer_kwargs = { "build_dir": build_dir.strpath, "src_dir": source_dir.strpath, "download_dir": download_dir.strpath, "progress_bar": "off", "build_isolation": False, "finder": finder, "session": session, "require_hashes": False, "use_user_site": False, } resolver_kwargs = { "finder": finder, "upgrade_strategy": "to-satisfy-only", "force_reinstall": False, "ignore_dependencies": False, "ignore_requires_python": False, "ignore_installed": True, "use_user_site": False, } if ( parse_version("19.3") <= parse_version(pip_version) <= parse_version("20.0.99999") ): make_install_req = partial( install_req_from_req_string, isolated=False, wheel_cache=wheelcache, # use_pep517=use_pep517, ) resolver_kwargs["make_install_req"] = make_install_req else: resolver_kwargs["wheel_cache"] = wheelcache if parse_version(pip_version) >= parse_version("20.1"): make_install_req = partial( install_req_from_req_string, isolated=False, use_pep517=True, ) resolver_kwargs["make_install_req"] = make_install_req else: resolver_kwargs["isolated"] = False resolver = None with make_preparer(**preparer_kwargs) as preparer: resolver_kwargs["preparer"] = preparer reqset = RequirementSet() ireq.is_direct = True reqset.add_requirement(ireq) resolver = get_resolver(**resolver_kwargs) resolver.require_hashes = False if parse_version(pip_version) > parse_version("20.0.9999999"): resolver._populate_link(ireq) results = resolver._resolve_one(reqset, ireq) try: reqset.cleanup_files() except AttributeError: pass results = set(results) result_names = [r.name for r in results] assert "urllib3" in result_names
def test_pip_version(): assert str(parse_version(pip_version)) == pip_version
def test_parse_version(): assert str(parse_version("0.0.1")) == "0.0.1"
def test_format_control(): from collections import namedtuple if issubclass(FormatControl, tuple): # through pip 18.0 this object was a named tuple fc = namedtuple("FormatControl", "no_binary, only_binary") else: # after pip 18.0 this has its own model clsname = "fc" fc = type(clsname, (FormatControl,), {}) assert fc(None, None) == FormatControl(None, None) @pytest.mark.skipif( parse_version(pip_version) >= parse_version("21.3"), reason="Removed in pip 21.3" ) def test_get_installed(): dists = get_installed_distributions() assert "pip-shims" in [p.project_name for p in dists] def test_link_and_ireq(): url = "git+https://github.com/requests/[email protected]#egg=requests" link = Link(url) ireq = InstallRequirement.from_editable(url) if install_req_from_editable: ireq2 = install_req_from_editable(url) assert ireq2.link == link assert ireq.link == link