Skip to content

Commit

Permalink
Moved allow_unsafe to instance attribute of OutputWriter
Browse files Browse the repository at this point in the history
now that it's used by more of its methods
  • Loading branch information
richafrank committed Jan 15, 2019
1 parent 4e31e57 commit 897dda6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
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
18 changes: 10 additions & 8 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,11 +25,12 @@ 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())

def write_header(self, allow_unsafe):
def write_header(self):
if self.emit_header:
yield comment('#')
yield comment('# This file is autogenerated by pip-compile')
Expand All @@ -47,7 +49,7 @@ def write_header(self, allow_unsafe):
params += ['--no-annotate']
if self.generate_hashes:
params += ["--generate-hashes"]
if allow_unsafe:
if self.allow_unsafe:
params += ["--allow-unsafe"]
params += ['--output-file', self.dst_file]
params += self.src_files
Expand Down Expand Up @@ -84,8 +86,8 @@ def write_flags(self):
yield ''

def _iter_lines(self, results, unsafe_requirements, reverse_dependencies,
primary_packages, markers, hashes, allow_unsafe=False):
for line in self.write_header(allow_unsafe):
primary_packages, markers, hashes):
for line in self.write_header():
yield line
for line in self.write_flags():
yield line
Expand All @@ -112,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
34 changes: 24 additions & 10 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -83,19 +84,32 @@ def test_format_requirement_environment_marker(from_line, writer):


@mark.parametrize(('allow_unsafe',), [(True,), (False,)])
def test_iter_lines__unsafe_dependencies(from_line, writer, allow_unsafe):
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,
allow_unsafe=allow_unsafe)
str_lines = list(lines)
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
if allow_unsafe:
assert comment('# pip-compile --allow-unsafe --output-file dst_file src_file src_file2') in str_lines
Expand Down

0 comments on commit 897dda6

Please sign in to comment.