Skip to content
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

Rewrite absolute path as relative to the destination requirements.txt file #673

Closed
wants to merge 8 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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Master

Features:
- Made local, editable, and relative requirements resolve to relative paths instead of absolute ones ([#507](https://github.com/jazzband/pip-tools/pull/507). Thanks to @orf, @maciej-gol, @AmiroNasr)

# 2.0.2 (2018-04-28)

Bug Fixes:
Expand All @@ -10,7 +15,6 @@ Bug Fixes:
- Added missing package data from vendored pip, such as missing cacert.pem file. Thanks @vphilippon

# 2.0.0 (2018-04-15)

Major changes:
- Vendored `pip` 9.0.3 to keep compatibility for users with `pip` 10.0.0
([#644](https://github.com/jazzband/pip-tools/pull/644)). Thanks @vphilippon
Expand Down
17 changes: 15 additions & 2 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,26 @@ def make_install_requirement(name, version, extras, constraint=False):
constraint=constraint)


def format_requirement(ireq, marker=None):
def _link_as_relative_to(ireq_link, relative_to):
if not relative_to:
return ireq_link

if ireq_link.scheme != 'file':
return ireq_link

return os.path.relpath(ireq_link.path, start=relative_to)


def format_requirement(ireq, marker=None, relative_to=None):
"""
Generic formatter for pretty printing InstallRequirements to the terminal
in a less verbose way than using its `__str__` method.

If `relative_to` is a path to a directory, all file path requirements will
be relative to that directory.
"""
if ireq.editable:
line = '-e {}'.format(ireq.link)
line = '-e {}'.format(_link_as_relative_to(ireq.link, relative_to=relative_to))
else:
line = str(ireq.req).lower()

Expand Down
6 changes: 5 additions & 1 deletion piptools/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ def write(self, results, unsafe_requirements, reverse_dependencies,
f.write(os.linesep.encode('utf-8'))

def _format_requirement(self, ireq, reverse_dependencies, primary_packages, marker=None, hashes=None):
line = format_requirement(ireq, marker=marker)
line = format_requirement(
ireq,
marker=marker,
relative_to=os.path.dirname(os.path.abspath(self.dst_file)),
)

ireq_hashes = (hashes if hashes is not None else {}).get(ireq)
if ireq_hashes:
Expand Down
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
from contextlib import contextmanager
from functools import partial

Expand Down Expand Up @@ -112,3 +113,18 @@ def from_line():
@fixture
def from_editable():
return InstallRequirement.from_editable


@fixture
def fake_package_dir():
return os.path.join(os.path.dirname(__file__), 'test_data', 'fake_package')


@fixture
def small_fake_package_dir():
return os.path.join(os.path.dirname(__file__), 'test_data', 'small_fake_package')


@fixture
def minimal_wheels_dir():
return os.path.join(os.path.dirname(__file__), 'test_data', 'minimal_wheels')
61 changes: 37 additions & 24 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from textwrap import dedent
from six.moves.urllib.request import pathname2url
import subprocess
import sys
import shutil
import mock

from click.testing import CliRunner
Expand Down Expand Up @@ -79,8 +79,7 @@ def test_command_line_overrides_pip_conf(pip_conf):
assert 'Using indexes:\n http://override.com' in out.output


def test_command_line_setuptools_read(pip_conf):

def test_command_line_setuptools_read():
runner = CliRunner()
with runner.isolated_filesystem():
package = open('setup.py', 'w')
Expand Down Expand Up @@ -192,7 +191,7 @@ def _invoke(command):
return status, output


def test_run_as_module_compile(tmpdir):
def test_run_as_module_compile():
"""piptools can be run as ``python -m piptools ...``."""

status, output = _invoke([
Expand Down Expand Up @@ -237,22 +236,40 @@ def test_sync_quiet(tmpdir):
assert '-q' in call[0][0]


def test_editable_package(tmpdir):
def test_editable_package(small_fake_package_dir):
""" piptools can compile an editable """
fake_package_dir = os.path.join(os.path.split(__file__)[0], 'test_data', 'small_fake_package')
fake_package_dir = 'file:' + pathname2url(fake_package_dir)
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
req_in.write('-e ' + fake_package_dir) # require editable fake package
req_in.write('-e ' + small_fake_package_dir) # require editable fake package

relative_small_fake_package_dir = os.path.relpath(small_fake_package_dir)
out = runner.invoke(cli, ['-n'])

assert out.exit_code == 0
assert fake_package_dir in out.output
assert relative_small_fake_package_dir in out.output
assert 'six==1.10.0' in out.output


def test_relative_editable_package(small_fake_package_dir):
# fake_package_dir = 'file:' + pathname2url(fake_package_dir)
runner = CliRunner()
with runner.isolated_filesystem() as loc:
new_package_dir = os.path.join(loc, 'small_fake_package')
# Move the small_fake_package inside the temp directory
shutil.copytree(small_fake_package_dir, new_package_dir)
relative_package_dir = os.path.relpath(new_package_dir)
relative_package_req = '-e {}'.format(relative_package_dir)

with open('requirements.in', 'w') as req_in:
req_in.write('-e small_fake_package') # require editable fake package

out = runner.invoke(cli, ['-n'])

assert out.exit_code == 0
assert relative_package_req in out.output


def test_editable_package_vcs(tmpdir):
vcs_package = (
'git+git://github.com/pytest-dev/pytest-django'
Expand All @@ -271,23 +288,21 @@ def test_editable_package_vcs(tmpdir):
assert 'pytest' in out.output # dependency of pytest-django


def test_locally_available_editable_package_is_not_archived_in_cache_dir(tmpdir):
def test_locally_available_editable_package_is_not_archived_in_cache_dir(tmpdir, small_fake_package_dir):
""" piptools will not create an archive for a locally available editable requirement """
cache_dir = tmpdir.mkdir('cache_dir')

fake_package_dir = os.path.join(os.path.split(__file__)[0], 'test_data', 'small_fake_package')
fake_package_dir = 'file:' + pathname2url(fake_package_dir)

with mock.patch('piptools.repositories.pypi.CACHE_DIR', new=str(cache_dir)):
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
req_in.write('-e ' + fake_package_dir) # require editable fake package
req_in.write('-e ' + small_fake_package_dir) # require editable fake package

relative_package_path = os.path.relpath(small_fake_package_dir)
out = runner.invoke(cli, ['-n'])

assert out.exit_code == 0
assert fake_package_dir in out.output
assert relative_package_path in out.output
assert 'six==1.10.0' in out.output

# we should not find any archived file in {cache_dir}/pkgs
Expand All @@ -311,11 +326,10 @@ def test_input_file_without_extension(tmpdir):
assert 'six==1.10.0' in out.output


def test_upgrade_packages_option(tmpdir):
def test_upgrade_packages_option(minimal_wheels_dir):
"""
piptools respects --upgrade-package/-P inline list.
"""
fake_package_dir = os.path.join(os.path.split(__file__)[0], 'test_data', 'minimal_wheels')
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
Expand All @@ -325,24 +339,23 @@ def test_upgrade_packages_option(tmpdir):

out = runner.invoke(cli, [
'-P', 'small_fake_b',
'-f', fake_package_dir,
'-f', minimal_wheels_dir,
])

assert out.exit_code == 0
assert 'small-fake-a==0.1' in out.output
assert 'small-fake-b==0.2' in out.output


def test_generate_hashes_with_editable():
small_fake_package_dir = os.path.join(
os.path.split(__file__)[0], 'test_data', 'small_fake_package')
small_fake_package_url = 'file:' + pathname2url(small_fake_package_dir)
def test_generate_hashes_with_editable(small_fake_package_dir):
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as fp:
fp.write('-e {}\n'.format(small_fake_package_url))
fp.write('-e {}\n'.format(small_fake_package_dir))
fp.write('pytz==2017.2\n')
out = runner.invoke(cli, ['--generate-hashes'])
relative_package_dir = os.path.relpath(small_fake_package_dir)

expected = (
'#\n'
'# This file is autogenerated by pip-compile\n'
Expand All @@ -354,7 +367,7 @@ def test_generate_hashes_with_editable():
'pytz==2017.2 \\\n'
' --hash=sha256:d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67 \\\n'
' --hash=sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589\n'
).format(small_fake_package_url)
).format(relative_package_dir)
assert out.exit_code == 0
assert expected in out.output

Expand Down
15 changes: 6 additions & 9 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from collections import Counter
import os
import platform
import sys

Expand Down Expand Up @@ -190,14 +189,13 @@ def _get_file_url(local_path):
return 'file://%s' % local_path


def test_diff_with_editable(fake_dist, from_editable):
def test_diff_with_editable(fake_dist, from_editable, small_fake_package_dir):
installed = [
fake_dist('small-fake-with-deps==0.0.1'),
fake_dist('six==1.10.0'),
]
path_to_package = os.path.join(os.path.dirname(__file__), 'test_data', 'small_fake_package')
reqs = [
from_editable(path_to_package),
from_editable(small_fake_package_dir),
]
to_install, to_uninstall = diff(reqs, installed)

Expand All @@ -208,7 +206,7 @@ def test_diff_with_editable(fake_dist, from_editable):
assert len(to_install) == 1
package = list(to_install)[0]
assert package.editable
assert str(package.link) == _get_file_url(path_to_package)
assert str(package.link) == _get_file_url(small_fake_package_dir)


@pytest.mark.parametrize(
Expand All @@ -226,10 +224,9 @@ def test_sync_install(from_line, lines):
check_call.assert_called_once_with(['pip', 'install', '-q'] + sorted(lines))


def test_sync_with_editable(from_editable):
def test_sync_with_editable(from_editable, small_fake_package_dir):
with mock.patch('piptools.sync.check_call') as check_call:
path_to_package = os.path.join(os.path.dirname(__file__), 'test_data', 'small_fake_package')
to_install = {from_editable(path_to_package)}
to_install = {from_editable(small_fake_package_dir)}

sync(to_install, set())
check_call.assert_called_once_with(['pip', 'install', '-q', '-e', _get_file_url(path_to_package)])
check_call.assert_called_once_with(['pip', 'install', '-q', '-e', _get_file_url(small_fake_package_dir)])
15 changes: 15 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
import shutil

from pytest import raises

from piptools.utils import (
Expand All @@ -14,6 +17,18 @@ def test_format_requirement_editable(from_editable):
assert format_requirement(ireq) == '-e git+git://fake.org/x/y.git#egg=y'


def test_format_requirement_non_relative_editable(from_editable, small_fake_package_dir, tmpdir):
tmp_package_dir = os.path.join(str(tmpdir), 'small_fake_package')
shutil.copytree(small_fake_package_dir, tmp_package_dir)
ireq = from_editable(tmp_package_dir)
assert format_requirement(ireq) == '-e file://' + tmp_package_dir


def test_format_requirement_relative_editable(from_editable, small_fake_package_dir):
ireq = from_editable(small_fake_package_dir)
assert format_requirement(ireq, relative_to='.') == '-e tests/test_data/small_fake_package'


def test_format_specifier(from_line):
ireq = from_line('foo')
assert format_specifier(ireq) == '<any>'
Expand Down
8 changes: 8 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def writer():
format_control=FormatControl(set(), set()))


def test_format_requirements_relative_path(from_editable, writer, fake_package_dir):
# The os.getcwd() is set to the piptools directory. Thus the expected path
# starts with tests directory.
ireq = from_editable('file://{}#egg=fake_package'.format(fake_package_dir))
formatted_requirement = writer._format_requirement(ireq, {}, primary_packages=['fake_package'])
assert formatted_requirement == '-e tests/test_data/fake_package'


def test_format_requirement_annotation_editable(from_editable, writer):
# Annotations are printed as comments at a fixed column
ireq = from_editable('git+git://fake.org/x/y.git#egg=y')
Expand Down