feat: add dry-run option - #78
Conversation
|
I restarted the failing (3.6) tests. |
peterbe
left a comment
There was a problem hiding this comment.
This is great! I'm tempting to land it as is but there's one major thing that I don't like and that it doesn't actually test what changes would be made.
See:
dev/PYTHON/hashin (63a92a9...) ✔
▶ touch /tmp/reqs.txt
dev/PYTHON/hashin (63a92a9...) ✔
▶ python hashin.py -r /tmp/reqs.txt requests
dev/PYTHON/hashin (63a92a9...) ✔
▶ python hashin.py -r /tmp/reqs.txt --dry-run requests
requests==2.19.1 \
--hash=sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1 \
--hash=sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6aWhat it actually should notice is that the version of requests hasn't actually changed in /tmp/reqs.txt.
|
So, your patch is this: 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()
requirements = amend_requirements_content(
requirements,
package,
new_lines
)
with open(file, 'w') as f:
f.write(requirements)Perhaps what we could do is something like this: if verbose:
_verbose('Editing', file)
with open(file) as f:
requirements = f.read()
requirements = amend_requirements_content(
requirements,
package,
new_lines
)
if dry_run:
if verbose:
_verbose('Dry run, not editing ', file)
with open(file) as f:
old_requirements = f.read()
print(diff(old_requirements, requirements))
else:
with open(file, 'w') as f:
f.write(requirements)What do you think? |
|
@peterbe Thanks for the review. Will fix it probably on the weekend |
|
@peterbe Actually I'm not sure about the diff. It would remove my use-case for this function. Normally I use pipenv for handling lock files, hashes etc.
With the dry-run feature I could quickly copy&paste everything into the Maybe dry-run is simply the wrong name for this and I need to open a separate PR after this one. What do you think? |
|
@peterbe Thinking more about it, I've applied your diff approach now. Maybe I will open a separate PR with another argument later (e.g. |
|
@max-wittig I don't totally follow what you're trying to do, but have you seen the |
|
@peterbe I will look into it. I've made your requested changes for this PR. I think some people will still find it useful 😄 |
peterbe
left a comment
There was a problem hiding this comment.
Thanks for being understand and that you're not giving up on this PR even if it might not be what you're after.
|
|
||
| @@ -3,3 +3,6 @@ | ||
|
|
||
| --hash=sha256:ad2edb7da2fc50a1d1a117cc9cfd8fc1f8b30c8b04932d5efef4fb09cbb2e984 |
There was a problem hiding this comment.
There's too many empty newlines in this diff output. Also, see my comment below about unified_diff.
|
|
||
| # Check dry run output | ||
| out_lines = my_stdout.getvalue().splitlines() | ||
| print(out_lines) |
| verbose=False, | ||
| version=False, | ||
| include_prereleases=False, | ||
| dry_run=False, |
There was a problem hiding this comment.
Tests fail otherwise here
There was a problem hiding this comment.
Should I leave it like this or what changes should be made?
| if verbose: | ||
| _verbose('Editing', file) | ||
| with open(file) as f: | ||
| requirements = f.read() |
There was a problem hiding this comment.
Change this to old_requirements = f.read() and 2 lines down pass in old_requirements (instead of requirements) to the amend_requirements_content function.
Then you don't need lines 195-196 since you'll already have a copy of the whole old file content.
| with open(file) as f: | ||
| old_requirements = f.read() | ||
| for line in difflib.unified_diff(old_requirements.splitlines(), requirements.splitlines()): | ||
| print(line) |
There was a problem hiding this comment.
The correct way to print a nice diff is to do it like this:
old = """
One
Two
Three
""".strip()
new = """
One
One and a half
Twoish
Three
""".strip()
print(
"".join(
difflib.unified_diff(
old.splitlines(True), new.splitlines(True), fromfile="Old", tofile="New"
)
)
)Then you get an output which resembles what you get with git diff kinda:
--- Old
+++ New
@@ -1,3 +1,4 @@
One
-Two
+One and a half
+Twoish
Three
This snippet works in Python 2 and Python 3.
There was a problem hiding this comment.
Thanks for all this help in fixing the MR!
e94d3c9 to
9028fe5
Compare
mythmon
left a comment
There was a problem hiding this comment.
I have a couple nits, but overall this looks good to me.
|
|
||
| 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 was a problem hiding this comment.
I think that this still should have backticks aroud requirements.txt. Also, the argument is listed as -dry-run with only one -, when it should two.
More holistically, didn't this change so that it shows a diff, isntead of "just print[ing] the lines"?
| ) | ||
| parser.add_argument( | ||
| '--dry-run', | ||
| help='Don\'t touch requirements.txt and just show hashes', |
There was a problem hiding this comment.
Nit: Most style guides allows for switching quotes to avoid escaping quotes in a string.
There was a problem hiding this comment.
I wouldn't worry about this one. Once this PR is squared away there are no open PRs and I can take a massive knife of black and therapist to this code base and then these kinds of nits become unimportant.
9028fe5 to
ce4ab79
Compare
ce4ab79 to
db4da3c
Compare
|
hashin 0.13.4 just released. |
No description provided.