Skip to content

Commit

Permalink
Merge pull request #5996 from cjerdonek/simplify-get-src-requirement
Browse files Browse the repository at this point in the history
Change VersionControl.get_src_requirement() to accept the project name
  • Loading branch information
cjerdonek committed Nov 9, 2018
2 parents 5f54157 + 07900ed commit 8a4e285
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/pip/_internal/operations/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def get_requirement_info(dist):
return (location, True, comments)

try:
req = vc_type().get_src_requirement(dist, location)
req = vc_type().get_src_requirement(location, dist.project_name)
except BadCommand:
logger.warning(
'cannot determine version of editable source in %s '
Expand Down
4 changes: 3 additions & 1 deletion src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,15 @@ def enum(*sequential, **named):
return type('Enum', (), enums)


def make_vcs_requirement_url(repo_url, rev, egg_project_name, subdir=None):
def make_vcs_requirement_url(repo_url, rev, project_name, subdir=None):
"""
Return the URL for a VCS requirement.
Args:
repo_url: the remote VCS url, with any needed VCS prefix (e.g. "git+").
project_name: the (unescaped) project name.
"""
egg_project_name = pkg_resources.to_filename(project_name)
req = '{}@{}#egg={}'.format(repo_url, rev, egg_project_name)
if subdir:
req += '&subdirectory={}'.format(subdir)
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/vcs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def unpack(self, location):
rmtree(location)
self.obtain(location)

def get_src_requirement(self, dist, location):
def get_src_requirement(self, location, project_name):
"""
Return a string representing the requirement needed to
redownload the files currently present in location, something
Expand Down
5 changes: 2 additions & 3 deletions src/pip/_internal/vcs/bazaar.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,14 @@ def get_revision(self, location):
)
return revision.splitlines()[-1]

def get_src_requirement(self, dist, location):
def get_src_requirement(self, location, project_name):
repo = self.get_url(location)
if not repo:
return None
if not repo.lower().startswith('bzr:'):
repo = 'bzr+' + repo
current_rev = self.get_revision(location)
egg_project_name = dist.egg_name().split('-', 1)[0]
return make_vcs_requirement_url(repo, current_rev, egg_project_name)
return make_vcs_requirement_url(repo, current_rev, project_name)

def is_commit_id_equal(self, dest, name):
"""Always assume the versions don't match"""
Expand Down
5 changes: 2 additions & 3 deletions src/pip/_internal/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,13 @@ def _get_subdirectory(self, location):
return None
return os.path.relpath(location, root_dir)

def get_src_requirement(self, dist, location):
def get_src_requirement(self, location, project_name):
repo = self.get_url(location)
if not repo.lower().startswith('git:'):
repo = 'git+' + repo
current_rev = self.get_revision(location)
egg_project_name = dist.egg_name().split('-', 1)[0]
subdir = self._get_subdirectory(location)
req = make_vcs_requirement_url(repo, current_rev, egg_project_name,
req = make_vcs_requirement_url(repo, current_rev, project_name,
subdir=subdir)

return req
Expand Down
6 changes: 2 additions & 4 deletions src/pip/_internal/vcs/mercurial.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ def get_revision_hash(self, location):
show_stdout=False, cwd=location).strip()
return current_rev_hash

def get_src_requirement(self, dist, location):
def get_src_requirement(self, location, project_name):
repo = self.get_url(location)
if not repo.lower().startswith('hg:'):
repo = 'hg+' + repo
current_rev_hash = self.get_revision_hash(location)
egg_project_name = dist.egg_name().split('-', 1)[0]
return make_vcs_requirement_url(repo, current_rev_hash,
egg_project_name)
return make_vcs_requirement_url(repo, current_rev_hash, project_name)

def is_commit_id_equal(self, dest, name):
"""Always assume the versions don't match"""
Expand Down
6 changes: 2 additions & 4 deletions src/pip/_internal/vcs/subversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,13 @@ def _get_svn_url_rev(self, location):

return url, rev

def get_src_requirement(self, dist, location):
def get_src_requirement(self, location, project_name):
repo = self.get_url(location)
if repo is None:
return None
repo = 'svn+' + repo
rev = self.get_revision(location)
# FIXME: why not project name?
egg_project_name = dist.egg_name().split('-', 1)[0]
return make_vcs_requirement_url(repo, rev, egg_project_name)
return make_vcs_requirement_url(repo, rev, project_name)

def is_commit_id_equal(self, dest, name):
"""Always assume the versions don't match"""
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,9 @@ def test_call_subprocess_closes_stdin():
# Test with None subdir.
(('git+https://example.com/pkg', 'dev', 'myproj', None),
'git+https://example.com/pkg@dev#egg=myproj'),
# Test an unescaped project name.
(('git+https://example.com/pkg', 'dev', 'zope-interface'),
'git+https://example.com/pkg@dev#egg=zope_interface'),
])
def test_make_vcs_requirement_url(args, expected):
actual = make_vcs_requirement_url(*args)
Expand Down
22 changes: 7 additions & 15 deletions tests/unit/test_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ def git():
return git


@pytest.fixture
def dist():
dist = Mock()
dist.egg_name = Mock(return_value='pip_test_package')
return dist


def test_looks_like_hash():
assert looks_like_hash(40 * 'a')
assert looks_like_hash(40 * 'A')
Expand All @@ -97,14 +90,13 @@ def test_looks_like_hash():


@pytest.mark.network
def test_git_get_src_requirements(git, dist):
ret = git.get_src_requirement(dist, location='.')

assert ret == ''.join([
'git+https://github.com/pypa/pip-test-package',
'@5547fa909e83df8bd743d3978d6667497983a4b7',
'#egg=pip_test_package'
])
def test_git_get_src_requirements(git):
ret = git.get_src_requirement('.', 'pip-test-package')

assert ret == (
'git+https://github.com/pypa/pip-test-package'
'@5547fa909e83df8bd743d3978d6667497983a4b7#egg=pip_test_package'
)


@patch('pip._internal.vcs.git.Git.get_revision_sha')
Expand Down

0 comments on commit 8a4e285

Please sign in to comment.