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

Make editable requirements use relative paths where appropriate. #507

Closed
wants to merge 4 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
5 changes: 5 additions & 0 deletions 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)

# 1.9.0

Features:
Expand Down
20 changes: 19 additions & 1 deletion piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import os
import sys
from itertools import chain, groupby
from collections import OrderedDict
Expand Down Expand Up @@ -61,13 +62,30 @@ def make_install_requirement(name, version, extras, constraint=False):
return InstallRequirement.from_line('{}{}=={}'.format(name, extras_string, str(version)), constraint=constraint)


def is_subdirectory(base, directory):
"""
Return True if directory is a child directory of base
"""
base = os.path.join(os.path.realpath(base), '')
directory = os.path.join(os.path.realpath(directory), '')

return os.path.commonprefix([base, directory]) == base


def format_requirement(ireq, marker=None):
"""
Generic formatter for pretty printing InstallRequirements to the terminal
in a less verbose way than using its `__str__` method.
"""
if ireq.editable:
line = '-e {}'.format(ireq.link)
path = ireq.link.path
if ireq.link.scheme == 'file' and is_subdirectory(os.getcwd(), path):
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not certain that cwd is a correct base here. I could call pip-compile outside of the directory which contains the requirement file, and that would leave us with a non installable compiled requirements.txt. Am I mistaken in thinking that the base should always be relative to the directory of the input file ?

# If the ireq.link is relative to the current directory then output a relative path
path = 'file:' + os.path.join('.', os.path.relpath(path))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that there are Windows idiosyncrasies that would be taken care of if you used pathname2url (like in the original pip code you pointed at:

urllib_parse.urljoin('file:', urllib_request.pathname2url(path)) 

this should also help the failing Windows build.

else:
path = ireq.link

line = '-e {}'.format(path)
else:
line = str(ireq.req).lower()

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 functools import partial

from pip._vendor.packaging.version import Version
Expand Down Expand Up @@ -106,3 +107,18 @@ def from_line():
@fixture
def from_editable():
return InstallRequirement.from_editable


@fixture
def fake_package_dir():
return os.path.join(os.path.split(__file__)[0], 'fixtures', 'fake_package')


@fixture
def small_fake_package_dir():
return os.path.join(os.path.split(__file__)[0], 'fixtures', 'small_fake_package')


@fixture
def minimal_wheels_dir():
return os.path.join(os.path.split(__file__)[0], 'fixtures', 'minimal_wheels')
Copy link
Contributor

Choose a reason for hiding this comment

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

perfect

42 changes: 30 additions & 12 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from six.moves.urllib.request import pathname2url
import subprocess
import sys
import shutil

from click.testing import CliRunner

Expand Down Expand Up @@ -65,8 +66,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 @@ -177,7 +177,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 @@ -205,24 +205,43 @@ def test_run_as_module_sync():
assert status == 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], 'fixtures', 'small_fake_package')
fake_package_dir = 'file:' + pathname2url(fake_package_dir)
small_fake_package_dir = 'file:' + pathname2url(small_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

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

print(out.output)
assert out.exit_code == 0
assert fake_package_dir in out.output
assert small_fake_package_dir in out.output
assert 'six==1.10.0' in out.output


def test_input_file_without_extension(tmpdir):
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 file:' + os.path.join('.', 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'])

print(out.output)
assert out.exit_code == 0
assert relative_package_req in out.output


def test_input_file_without_extension():
"""
piptools can compile a file without an extension,
and add .txt as the defaut output file extension.
Expand All @@ -240,11 +259,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], 'fixtures', 'minimal_wheels')
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
Expand All @@ -254,7 +272,7 @@ def test_upgrade_packages_option(tmpdir):

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

print(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 mock
Expand Down Expand Up @@ -161,14 +160,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__), 'fixtures', 'small_fake_package')
reqs = [
from_editable(path_to_package),
from_editable(small_fake_package_dir),
]
to_install, to_uninstall = diff(reqs, installed)

Expand All @@ -179,13 +177,12 @@ 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)


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__), 'fixtures', '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)])
27 changes: 26 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import os
import shutil

from pytest import raises

from piptools.utils import (
as_tuple, format_requirement, format_specifier, flat_map, dedup)
as_tuple, format_requirement, format_specifier, flat_map, dedup, is_subdirectory)


def test_is_subdirectory():
cwd = os.getcwd()
test_dir = os.path.join(cwd, 'test')
assert is_subdirectory(cwd, test_dir)
assert is_subdirectory(os.path.join(test_dir, '..'), test_dir)
assert is_subdirectory(cwd, cwd)

assert not is_subdirectory(test_dir, cwd)


def test_format_requirement(from_line):
Expand All @@ -14,6 +27,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) == '-e file:./tests/fixtures/small_fake_package'


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


def test_format_requirements_relative_path(from_editable, writer):
ireq = from_editable('file:fixtures/fake_package#egg=fake_package')
assert writer._format_requirement(ireq, {}, primary_packages=['fake_package']) == '-e file:./fixtures/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