Skip to content

Commit

Permalink
Derive source names from URLs when not supplied
Browse files Browse the repository at this point in the history
- Fixes #3216

Signed-off-by: Dan Ryan <[email protected]>
  • Loading branch information
techalchemy committed Nov 13, 2018
1 parent c3cdbfb commit 72e2ef7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions news/3216.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When sources are missing names, names will now be derived from the supplied URL.
15 changes: 12 additions & 3 deletions pipenv/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import vistir
import pipenv

from .utils import normalize_path

BASE_WORKING_SET = pkg_resources.WorkingSet(sys.path)


Expand All @@ -27,8 +29,8 @@ def __init__(self, prefix=None, is_venv=False, base_working_set=None, pipfile=No
super(Environment, self).__init__()
self._modules = {'pkg_resources': pkg_resources, 'pipenv': pipenv}
self.base_working_set = base_working_set if base_working_set else BASE_WORKING_SET
prefix = os.path.normcase(os.path.normpath(os.path.abspath(str(prefix))))
self.is_venv = not prefix == os.path.normcase(os.path.normpath(sys.prefix))
prefix = normalize_path(prefix)
self.is_venv = not prefix == normalize_path(sys.prefix)
if not sources:
sources = []
self.project = project
Expand Down Expand Up @@ -81,7 +83,7 @@ def resolve_dist(cls, dist, working_set):
deps.add(dist)
try:
reqs = dist.requires()
except (AttributeError, OSError): # The METADATA file can't be found
except (AttributeError, OSError, IOError): # The METADATA file can't be found
return deps
for req in reqs:
dist = working_set.find(req)
Expand Down Expand Up @@ -243,6 +245,7 @@ def get_distributions(self):
return pkg_resources.find_distributions(self.paths["PYTHONPATH"])

def find_egg(self, egg_dist):
"""Find an egg by name in the given environment"""
site_packages = get_python_lib()
search_filename = "{0}.egg-link".format(egg_dist.project_name)
try:
Expand All @@ -256,11 +259,16 @@ def find_egg(self, egg_dist):
return egg

def locate_dist(self, dist):
"""Given a distribution, try to find a corresponding egg link first.
If the egg - link doesn 't exist, return the supplied distribution."""

location = self.find_egg(dist)
if not location:
return dist.location

def dist_is_in_project(self, dist):
"""Determine whether the supplied distribution is in the environment."""
from .project import _normalized
prefix = _normalized(self.base_paths["prefix"])
location = self.locate_dist(dist)
Expand All @@ -269,6 +277,7 @@ def dist_is_in_project(self, dist):
return _normalized(location).startswith(prefix)

def get_installed_packages(self):
"""Returns all of the installed packages in a given environment"""
workingset = self.get_working_set()
packages = [pkg for pkg in workingset if self.dist_is_in_project(pkg)]
return packages
Expand Down
3 changes: 2 additions & 1 deletion pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
convert_toml_outline_tables,
is_installable_file,
is_valid_url,
get_url_name,
normalize_drive,
python_version,
safe_expandvars,
Expand Down Expand Up @@ -747,7 +748,7 @@ def get_or_create_lockfile(self):
sources = [sources,]
lockfile_dict["_meta"]["sources"] = [
{
"name": s["name"],
"name": s.get("name", get_url_name(s.get("url"))),
"url": s["url"],
"verify_ssl": (
s["verify_ssl"] if isinstance(s["verify_ssl"], bool) else (
Expand Down
13 changes: 13 additions & 0 deletions pipenv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,19 @@ def path_to_url(path):
return Path(normalize_drive(os.path.abspath(path))).as_uri()


def normalize_path(path):
return os.path.expandvars(os.path.expanduser(
os.path.normcase(os.path.normpath(os.path.abspath(str(path))))
))


def get_url_name(url):
if not isinstance(url, six.string_types):
return
from urllib3.util import parse as urllib3_parse
return urllib3_parse(url).host


def get_canonical_names(packages):
"""Canonicalize a list of packages and return a set of canonical names"""
from .vendor.packaging.utils import canonicalize_name
Expand Down

0 comments on commit 72e2ef7

Please sign in to comment.