diff --git a/README.rst b/README.rst index 35e7c55..9c29ac9 100644 --- a/README.rst +++ b/README.rst @@ -124,9 +124,9 @@ 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:: @@ -134,11 +134,12 @@ Example:: 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 ============================ diff --git a/hashin.py b/hashin.py index 754a885..28b8083 100755 --- a/hashin.py +++ b/hashin.py @@ -15,6 +15,7 @@ import pip_api from packaging.version import parse +import difflib if sys.version_info >= (3,): from urllib.request import urlopen @@ -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, ) @@ -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): diff --git a/tests/test_cli.py b/tests/test_cli.py index e7cf547..a536d6b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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*')