def test_local_editable_ref(monkeypatch): with monkeypatch.context() as m: m.setattr(pip_shims.shims, "unpack_url", mock_unpack) path = Path(ARTIFACTS_DIR) / "git/requests" req = Requirement.from_pipfile( "requests", {"editable": True, "git": path.as_uri(), "ref": "2.18.4"} ) assert req.as_line() == "-e git+{0}@2.18.4#egg=requests".format(path.as_uri())
def get_name(self): loc = self.path or self.uri if loc: self._uri_scheme = "path" if self.path else "uri" name = None if self.link and self.link.egg_fragment: return self.link.egg_fragment elif self.link and self.link.is_wheel: from pip_shims import Wheel return Wheel(self.link.filename).name if (self._uri_scheme != "uri" and self.path and self.setup_path and self.setup_path.exists()): from setuptools.dist import distutils old_curdir = os.path.abspath(os.getcwd()) try: os.chdir(str(self.setup_path.parent)) dist = distutils.core.run_setup(self.setup_path.as_posix()) name = dist.get_name() except (FileNotFoundError, IOError) as e: dist = None except Exception as e: from pip_shims.shims import make_abstract_dist try: if not isinstance(Path, self.path): _path = Path(self.path) else: _path = self.path if self.editable: _ireq = pip_shims.shims.install_req_from_editable( _path.as_uri()) else: _ireq = pip_shims.shims.install_req_from_line( _path.as_posix()) dist = make_abstract_dist(_ireq).get_dist() name = dist.project_name except (TypeError, ValueError, AttributeError) as e: dist = None finally: os.chdir(old_curdir) hashed_loc = hashlib.sha256(loc.encode("utf-8")).hexdigest() hashed_name = hashed_loc[-7:] if not name or name == "UNKNOWN": self._has_hashed_name = True name = hashed_name if self.link and not self._has_hashed_name: self.link = create_link("{0}#egg={1}".format(self.link.url, name)) return name
def get_link_from_line(cls, line): """Parse link information from given requirement line. Return a 6-tuple: - `vcs_type` indicates the VCS to use (e.g. "git"), or None. - `prefer` is either "file", "path" or "uri", indicating how the information should be used in later stages. - `relpath` is the relative path to use when recording the dependency, instead of the absolute path/URI used to perform installation. This can be None (to prefer the absolute path or URI). - `path` is the absolute file path to the package. This will always use forward slashes. Can be None if the line is a remote URI. - `uri` is the absolute URI to the package. Can be None if the line is not a URI. - `link` is an instance of :class:`pip._internal.index.Link`, representing a URI parse result based on the value of `uri`. This function is provided to deal with edge cases concerning URIs without a valid netloc. Those URIs are problematic to a straight ``urlsplit` call because they cannot be reliably reconstructed with ``urlunsplit`` due to a bug in the standard library: >>> from urllib.parse import urlsplit, urlunsplit >>> urlunsplit(urlsplit('git+file:///this/breaks')) 'git+file:/this/breaks' >>> urlunsplit(urlsplit('file:///this/works')) 'file:///this/works' See `https://bugs.python.org/issue23505#msg277350`. """ # Git allows `[email protected]...` lines that are not really URIs. # Add "ssh://" so we can parse correctly, and restore afterwards. fixed_line = add_ssh_scheme_to_git_uri(line) added_ssh_scheme = fixed_line != line # We can assume a lot of things if this is a local filesystem path. if "://" not in fixed_line: p = Path(fixed_line).absolute() path = p.as_posix() uri = p.as_uri() link = create_link(uri) try: relpath = get_converted_relative_path(path) except ValueError: relpath = None return LinkInfo(None, "path", relpath, path, uri, link) # This is an URI. We'll need to perform some elaborated parsing. parsed_url = urllib_parse.urlsplit(fixed_line) original_url = parsed_url._replace() if added_ssh_scheme and ':' in parsed_url.netloc: original_netloc, original_path_start = parsed_url.netloc.rsplit( ':', 1) uri_path = '/{0}{1}'.format(original_path_start, parsed_url.path) parsed_url = original_url._replace(netloc=original_netloc, path=uri_path) # Split the VCS part out if needed. original_scheme = parsed_url.scheme if "+" in original_scheme: vcs_type, scheme = original_scheme.split("+", 1) parsed_url = parsed_url._replace(scheme=scheme) prefer = "uri" else: vcs_type = None prefer = "file" if parsed_url.scheme == "file" and parsed_url.path: # This is a "file://" URI. Use url_to_path and path_to_url to # ensure the path is absolute. Also we need to build relpath. path = Path(url_to_path( urllib_parse.urlunsplit(parsed_url))).as_posix() try: relpath = get_converted_relative_path(path) except ValueError: relpath = None uri = path_to_url(path) else: # This is a remote URI. Simply use it. path = None relpath = None # Cut the fragment, but otherwise this is fixed_line. uri = urllib_parse.urlunsplit( parsed_url._replace(scheme=original_scheme, fragment="")) if added_ssh_scheme: original_uri = urllib_parse.urlunsplit( original_url._replace(scheme=original_scheme, fragment="")) uri = strip_ssh_from_git_uri(original_uri) # Re-attach VCS prefix to build a Link. link = create_link( urllib_parse.urlunsplit( parsed_url._replace(scheme=original_scheme))) return LinkInfo(vcs_type, prefer, relpath, path, uri, link)