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

add --exclude option #388

Closed
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.7.2

- Add `--exclude` option (#333)

# 1.7.1

- Add `--allow-unsafe` option (#377)
Expand Down
12 changes: 11 additions & 1 deletion piptools/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def __str__(self):


class Resolver(object):
def __init__(self, constraints, repository, cache=None, prereleases=False, clear_caches=False):
def __init__(self, constraints, repository, cache=None,
exclude=None, prereleases=False, clear_caches=False):
"""
This class resolves a given set of constraints (a collection of
InstallRequirement objects) by consulting the given Repository and the
Expand All @@ -67,6 +68,7 @@ def __init__(self, constraints, repository, cache=None, prereleases=False, clear
self.dependency_cache = cache
self.prereleases = prereleases
self.clear_caches = clear_caches
self.exclude = exclude

@property
def constraints(self):
Expand Down Expand Up @@ -259,6 +261,14 @@ def _iter_dependencies(self, ireq):
dependency_strings = self.dependency_cache[ireq]
log.debug(' {:25} requires {}'.format(format_requirement(ireq),
', '.join(sorted(dependency_strings, key=lambda s: s.lower())) or '-'))

if self.exclude:
dep_set, exclude_set = set(dependency_strings), set(self.exclude)
log.debug('Excluding following dependencies:')
for exclude in (dep_set & exclude_set):
log.debug(' {}'.format(exclude))
dependency_strings = dep_set - exclude_set

for dependency_string in dependency_strings:
yield InstallRequirement.from_line(dependency_string)

Expand Down
5 changes: 3 additions & 2 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ class PipCommand(pip.basecommand.Command):
'Will be derived from input file otherwise.'))
@click.option('--allow-unsafe', is_flag=True, default=False,
help="Pin packages considered unsafe: pip, setuptools & distribute")
@click.option('--exclude', multiple=True, help="Exclude a package", type=str)
@click.argument('src_files', nargs=-1, type=click.Path(exists=True, allow_dash=True))
def cli(verbose, dry_run, pre, rebuild, find_links, index_url, extra_index_url,
client_cert, trusted_host, header, index, annotate, upgrade,
output_file, allow_unsafe, src_files):
output_file, allow_unsafe, src_files, exclude):
"""Compiles requirements.txt from requirements.in specs."""
log.verbose = verbose

Expand Down Expand Up @@ -159,7 +160,7 @@ def cli(verbose, dry_run, pre, rebuild, find_links, index_url, extra_index_url,

try:
resolver = Resolver(constraints, repository, prereleases=pre,
clear_caches=rebuild)
clear_caches=rebuild, exclude=exclude)
results = resolver.resolve()
except PipToolsError as e:
log.error(str(e))
Expand Down
7 changes: 7 additions & 0 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ def test_resolver(resolver, from_line, input, expected, prereleases):
output = resolver(input, prereleases=prereleases).resolve()
output = {str(line) for line in output}
assert output == {str(line) for line in expected}


def test_resolver_with_exclude(resolver, from_line):
input, expected = [from_line('ipython')], ['ipython==2.1.0']
output = resolver(input, exclude=['gnureadline']).resolve()
output = {str(line) for line in output}
assert output == {str(line) for line in expected}