Skip to content

Continuing Feature/record download info #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ notifications:
branches:
only:
- develop
- feature/record-download-info
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this addition should not be part of the pull request

env:
- PIP_USE_MIRRORS=true
55 changes: 51 additions & 4 deletions pip/req.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
class InstallRequirement(object):

def __init__(self, req, comes_from, source_dir=None, editable=False,
url=None, update=True):
url=None, update=True, requirement_line=None):
self.extras = ()
if isinstance(req, string_types):
req = pkg_resources.Requirement.parse(req)
Expand All @@ -45,6 +45,7 @@ def __init__(self, req, comes_from, source_dir=None, editable=False,
self.source_dir = source_dir
self.editable = editable
self.url = url
self.requirement_line = requirement_line
self._egg_info_path = None
# This holds the pkg_resources.Distribution object if this requirement
# is already available:
Expand All @@ -63,12 +64,13 @@ def __init__(self, req, comes_from, source_dir=None, editable=False,

@classmethod
def from_editable(cls, editable_req, comes_from=None, default_vcs=None):
requirement_line = "--editable=%s" % editable_req
name, url = parse_editable(editable_req, default_vcs)
if url.startswith('file:'):
source_dir = url_to_path(url)
else:
source_dir = None
return cls(name, comes_from, source_dir=source_dir, editable=True, url=url)
return cls(name, comes_from, source_dir=source_dir, editable=True, url=url, requirement_line=requirement_line)

@classmethod
def from_line(cls, name, comes_from=None):
Expand Down Expand Up @@ -105,7 +107,7 @@ def from_line(cls, name, comes_from=None):
else:
req = name

return cls(req, comes_from, url=url)
return cls(req, comes_from, url=url, requirement_line=name)

def __str__(self):
if self.req:
Expand Down Expand Up @@ -549,6 +551,19 @@ def _clean_zip_name(self, name, prefix):
name = name.replace(os.path.sep, '/')
return name

def _write_info_ini(self, egg_info_dir, options):
info = ConfigParser.RawConfigParser()

for section, values in options.items():
info.add_section(section)

for key, value in values.items():
info.set(section, key, value)

f = open(os.path.join(egg_info_dir, 'pip.ini'), 'w')
info.write(f)
f.close()

def install(self, install_options, global_options=()):
if self.editable:
self.install_editable(install_options, global_options)
Expand Down Expand Up @@ -602,8 +617,24 @@ def install(self, install_options, global_options=()):
filename += os.path.sep
new_lines.append(make_path_relative(filename, egg_info_dir))
f.close()

info_data = {
'download': {
'url': self.url.url,
}
}

if self.requirement_line:
info_data['download']['requirement'] = self.requirement_line
else:
logger.warn("No requirement line recorded for %s" % self.name)
self._write_info_ini(egg_info_dir, info_data)

# Add the pip.ini to the installed-files.txt
new_lines.append('pip.ini')

f = open(os.path.join(egg_info_dir, 'installed-files.txt'), 'w')
f.write('\n'.join(new_lines)+'\n')
f.write('\n'.join(new_lines) + '\n')
f.close()
finally:
if os.path.exists(record_filename):
Expand Down Expand Up @@ -638,6 +669,21 @@ def install_editable(self, install_options, global_options=()):
logger.indent -= 2
self.install_succeeded = True

egg_info_dir = os.path.dirname(self.egg_info_path("pip.ini"))

if egg_info_dir:
info_data = {
'download': {
'url': self.url,
}
}

if self.requirement_line:
info_data['download']['requirement'] = self.requirement_line
else:
logger.warn("No requirement line recorded for %s" % self.name)
self._write_info_ini(egg_info_dir, info_data)

def _filter_install(self, line):
level = logger.NOTIFY
for regex in [r'^running .*', r'^writing .*', '^creating .*', '^[Cc]opying .*',
Expand Down Expand Up @@ -987,6 +1033,7 @@ def prepare_files(self, finder, force_root_egg_info=False, bundle=False):
url = Link(req_to_install.url)
assert url
if url:
req_to_install.url = url
try:
self.unpack_url(url, location, self.is_download)
except HTTPError:
Expand Down
61 changes: 61 additions & 0 deletions tests/test_info_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os

from pip.backwardcompat import ConfigParser
from pip.download import path_to_url2
from tests.test_pip import here, reset_env, run_pip, pyversion

from tests.path import Path


def get_pip_ini(egg_info_dir):
infofp = open(os.path.join(egg_info_dir, "pip.ini"))
info = ConfigParser.RawConfigParser()
info.readfp(infofp)
infofp.close()
return info


def test_index():
"""
Test that the pip.ini is written and works from an index (PyPI).
"""
env = reset_env()
run_pip('install', '-i http://pypi.python.org/simple/', 'INITools==0.3.1')

egg_info_dir = env.base_path / env.site_packages / 'INITools-0.3.1-py%s.egg-info' % pyversion
info = get_pip_ini(egg_info_dir)

assert info.has_section("download")
assert info.get("download", "url").startswith("http://pypi.python.org/packages/source/I/INITools/INITools")
assert info.get("download", "requirement") == "INITools==0.3.1"


def test_tarball():
"""
Test that the pip.ini is written and works from an tarball.
"""
env = reset_env()
run_pip('install', 'http://pypi.python.org/packages/source/I/INITools/INITools-0.3.1.tar.gz')

egg_info_dir = env.base_path / env.site_packages / 'INITools-0.3.1-py%s.egg-info' % pyversion
info = get_pip_ini(egg_info_dir)

assert info.has_section("download")
assert info.get("download", "url") == "http://pypi.python.org/packages/source/I/INITools/INITools-0.3.1.tar.gz"
assert info.get("download", "requirement") == "http://pypi.python.org/packages/source/I/INITools/INITools-0.3.1.tar.gz"


def test_editable():
"""
Test that the pip.ini is written and works from an editable.
"""
env = reset_env()
fspkg = path_to_url2(Path(here) / 'packages' / 'FSPkg')
run_pip('install', '-e', fspkg)

egg_info_dir = Path(here) / 'packages' / 'FSPkg' / 'FSPkg.egg-info'
info = get_pip_ini(egg_info_dir)

assert info.has_section("download")
assert info.get("download", "url") == fspkg
assert info.get("download", "requirement") == "--editable=" + fspkg