Skip to content

Commit

Permalink
fix(dry-run): show diff with dry-run option
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wittig committed Oct 5, 2018
1 parent 63a92a9 commit ce4ab79
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
17 changes: 9 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,22 @@ this as a fallback in the case a suitable wheel cannot be found.
Dry run mode
============

There are some use cases, when you maybe don't want to edit your requirements.txt
right away. You can use the ``-dry-run`` argument to just print the lines, as if they
would be added to your requirements.txt file.
There are some use cases, when you maybe don't want to edit your ``requirements.txt``
right away. You can use the ``--dry-run`` argument to show the diff, so you
can preview the changes to your ``requirements.txt`` file.

Example::

hashin --dry-run requests==2.19.1

Would result in a printout on the command line::

requests==2.19.1 \
--hash=sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1 \
--hash=sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6a


--- Old
+++ New
@@ -0,0 +1,3 @@
+requests==2.19.1 \
+ --hash=sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1 \
+ --hash=sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6a

PEP-0496 Environment Markers
============================
Expand Down
34 changes: 22 additions & 12 deletions hashin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import pip_api
from packaging.version import parse
import difflib

if sys.version_info >= (3,):
from urllib.request import urlopen
Expand Down Expand Up @@ -77,7 +78,7 @@
)
parser.add_argument(
'--dry-run',
help='Don\'t touch requirements.txt and just show hashes',
help="Don't touch requirements.txt and just show the diff",
action='store_true',
default=False,
)
Expand Down Expand Up @@ -181,22 +182,31 @@ def run_single_package(
new_lines += ' \\'
new_lines += '\n'

if dry_run:
if verbose:
_verbose('Dry run, not editing ', file)
print(new_lines)
return
if verbose:
_verbose('Editing', file)
with open(file) as f:
requirements = f.read()
old_requirements = f.read()
requirements = amend_requirements_content(
requirements,
old_requirements,
package,
new_lines
)
with open(file, 'w') as f:
f.write(requirements)
if dry_run:
if verbose:
_verbose('Dry run, not editing ', file)
print(
"".join(
difflib.unified_diff(
old_requirements.splitlines(True),
requirements.splitlines(True),
fromfile="Old",
tofile="New",
)
)
)
else:
with open(file, 'w') as f:
f.write(requirements)
if verbose:
_verbose('Editing', file)


def amend_requirements_content(requirements, package, new_lines):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,10 @@ def mocked_get(url, **options):
# Check dry run output
out_lines = my_stdout.getvalue().splitlines()
self.assertTrue(
'hashin==0.10' in out_lines[0]
'+hashin==0.10' in out_lines[3]
)
self.assertTrue(
'--hash=sha256:aaaaa' in out_lines[1]
'+--hash=sha256:aaaaa' in out_lines[4].replace(" ", "")
)

@cleanup_tmpdir('hashin*')
Expand Down

0 comments on commit ce4ab79

Please # to comment.