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 --allow-unsafe to update instructions #708

Merged
merged 3 commits into from
Jan 15, 2019
Merged
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: 3 additions & 3 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ def cli(verbose, dry_run, pre, rebuild, find_links, index_url, extra_index_url,
default_index_url=repository.DEFAULT_INDEX_URL,
index_urls=repository.finder.index_urls,
trusted_hosts=pip_options.trusted_hosts,
format_control=repository.finder.format_control)
format_control=repository.finder.format_control,
allow_unsafe=allow_unsafe)
writer.write(results=results,
unsafe_requirements=resolver.unsafe_constraints,
reverse_dependencies=reverse_dependencies,
primary_packages={key_from_req(ireq.req) for ireq in constraints if not ireq.constraint},
markers={key_from_req(ireq.req): ireq.markers
for ireq in constraints if ireq.markers},
hashes=hashes,
allow_unsafe=allow_unsafe)
hashes=hashes)

if dry_run:
log.warning('Dry-run, so nothing updated.')
Expand Down
14 changes: 9 additions & 5 deletions piptools/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
class OutputWriter(object):
def __init__(self, src_files, dst_file, dry_run, emit_header, emit_index,
emit_trusted_host, annotate, generate_hashes,
default_index_url, index_urls, trusted_hosts, format_control):
default_index_url, index_urls, trusted_hosts, format_control,
allow_unsafe):
self.src_files = src_files
self.dst_file = dst_file
self.dry_run = dry_run
Expand All @@ -24,6 +25,7 @@ def __init__(self, src_files, dst_file, dry_run, emit_header, emit_index,
self.index_urls = index_urls
self.trusted_hosts = trusted_hosts
self.format_control = format_control
self.allow_unsafe = allow_unsafe

def _sort_key(self, ireq):
return (not ireq.editable, str(ireq.req).lower())
Expand All @@ -47,6 +49,8 @@ def write_header(self):
params += ['--no-annotate']
if self.generate_hashes:
params += ["--generate-hashes"]
if self.allow_unsafe:
params += ["--allow-unsafe"]
params += ['--output-file', self.dst_file]
params += self.src_files
yield comment('# pip-compile {}'.format(' '.join(params)))
Expand Down Expand Up @@ -82,7 +86,7 @@ def write_flags(self):
yield ''

def _iter_lines(self, results, unsafe_requirements, reverse_dependencies,
primary_packages, markers, hashes, allow_unsafe=False):
primary_packages, markers, hashes):
for line in self.write_header():
yield line
for line in self.write_flags():
Expand Down Expand Up @@ -110,20 +114,20 @@ def _iter_lines(self, results, unsafe_requirements, reverse_dependencies,
primary_packages,
marker=markers.get(key_from_req(ireq.req)),
hashes=hashes)
if not allow_unsafe:
if not self.allow_unsafe:
yield comment('# {}'.format(req))
else:
yield req

def write(self, results, unsafe_requirements, reverse_dependencies,
primary_packages, markers, hashes, allow_unsafe=False):
primary_packages, markers, hashes):
with ExitStack() as stack:
f = None
if not self.dry_run:
f = stack.enter_context(AtomicSaver(self.dst_file))

for line in self._iter_lines(results, unsafe_requirements, reverse_dependencies,
primary_packages, markers, hashes, allow_unsafe=allow_unsafe):
primary_packages, markers, hashes):
log.info(line)
if f:
f.write(unstyle(line).encode('utf-8'))
Expand Down
45 changes: 32 additions & 13 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pytest import fixture
from pytest import fixture, mark

from piptools._compat import FormatControl
from piptools.utils import comment
Expand All @@ -14,7 +14,8 @@ def writer():
generate_hashes=False,
default_index_url=None, index_urls=[],
trusted_hosts=[],
format_control=FormatControl(set(), set()))
format_control=FormatControl(set(), set()),
allow_unsafe=False)


def test_format_requirement_annotation_editable(from_editable, writer):
Expand Down Expand Up @@ -82,20 +83,38 @@ def test_format_requirement_environment_marker(from_line, writer):
'test ; python_version == "2.7" and platform_python_implementation == "CPython"')


def test_iter_lines__unsafe_dependencies(from_line, writer):
@mark.parametrize(('allow_unsafe',), [(True,), (False,)])
def test_iter_lines__unsafe_dependencies(from_line, allow_unsafe):

writer = OutputWriter(
src_files=["src_file", "src_file2"], dst_file="dst_file",
dry_run=True,
emit_header=True, emit_index=True, emit_trusted_host=True,
annotate=True,
generate_hashes=False,
default_index_url=None, index_urls=[],
trusted_hosts=[],
format_control=FormatControl(set(), set()),
allow_unsafe=allow_unsafe,
)

ireq = [from_line('test==1.2')]
unsafe_req = [from_line('setuptools')]
reverse_dependencies = {'test': ['xyz']}

lines = writer._iter_lines(ireq,
unsafe_req,
reverse_dependencies,
['test'],
{},
None)
str_lines = []
for line in lines:
str_lines.append(line)
str_lines = list(writer._iter_lines(
ireq,
unsafe_req,
reverse_dependencies,
['test'],
{},
None,
))
assert comment('# The following packages are considered to be unsafe in a requirements file:') in str_lines
assert comment('# setuptools') in str_lines
if allow_unsafe:
assert comment('# pip-compile --allow-unsafe --output-file dst_file src_file src_file2') in str_lines
assert 'setuptools' in str_lines
else:
assert comment('# pip-compile --output-file dst_file src_file src_file2') in str_lines
assert comment('# setuptools') in str_lines
assert 'test==1.2' in str_lines