File tree Expand file tree Collapse file tree 3 files changed +33
-22
lines changed
Expand file tree Collapse file tree 3 files changed +33
-22
lines changed Original file line number Diff line number Diff line change @@ -124,21 +124,22 @@ this as a fallback in the case a suitable wheel cannot be found.
124124Dry run mode
125125============
126126
127- There are some use cases, when you maybe don't want to edit your requirements.txt
128- right away. You can use the ``-dry-run `` argument to just print the lines, as if they
129- would be added to your requirements.txt file.
127+ There are some use cases, when you maybe don't want to edit your `` requirements.txt ``
128+ right away. You can use the ``-- dry-run `` argument to show the diff, so you
129+ can preview the changes to your `` requirements.txt `` file.
130130
131131Example::
132132
133133 hashin --dry-run requests==2.19.1
134134
135135Would result in a printout on the command line::
136136
137- requests==2.19.1 \
138- --hash=sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1 \
139- --hash=sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6a
140-
141-
137+ --- Old
138+ +++ New
139+ @@ -0,0 +1,3 @@
140+ +requests==2.19.1 \
141+ + --hash=sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1 \
142+ + --hash=sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6a
142143
143144PEP-0496 Environment Markers
144145============================
Original file line number Diff line number Diff line change 1515
1616import pip_api
1717from packaging .version import parse
18+ import difflib
1819
1920if sys .version_info >= (3 ,):
2021 from urllib .request import urlopen
7778)
7879parser .add_argument (
7980 '--dry-run' ,
80- help = ' Don\ ' t touch requirements.txt and just show hashes' ,
81+ help = " Don't touch requirements.txt and just show the diff" ,
8182 action = 'store_true' ,
8283 default = False ,
8384)
@@ -181,22 +182,31 @@ def run_single_package(
181182 new_lines += ' \\ '
182183 new_lines += '\n '
183184
184- if dry_run :
185- if verbose :
186- _verbose ('Dry run, not editing ' , file )
187- print (new_lines )
188- return
189- if verbose :
190- _verbose ('Editing' , file )
191185 with open (file ) as f :
192- requirements = f .read ()
186+ old_requirements = f .read ()
193187 requirements = amend_requirements_content (
194- requirements ,
188+ old_requirements ,
195189 package ,
196190 new_lines
197191 )
198- with open (file , 'w' ) as f :
199- f .write (requirements )
192+ if dry_run :
193+ if verbose :
194+ _verbose ('Dry run, not editing ' , file )
195+ print (
196+ "" .join (
197+ difflib .unified_diff (
198+ old_requirements .splitlines (True ),
199+ requirements .splitlines (True ),
200+ fromfile = "Old" ,
201+ tofile = "New" ,
202+ )
203+ )
204+ )
205+ else :
206+ with open (file , 'w' ) as f :
207+ f .write (requirements )
208+ if verbose :
209+ _verbose ('Editing' , file )
200210
201211
202212def amend_requirements_content (requirements , package , new_lines ):
Original file line number Diff line number Diff line change @@ -745,10 +745,10 @@ def mocked_get(url, **options):
745745 # Check dry run output
746746 out_lines = my_stdout .getvalue ().splitlines ()
747747 self .assertTrue (
748- 'hashin==0.10' in out_lines [0 ]
748+ '+ hashin==0.10' in out_lines [3 ]
749749 )
750750 self .assertTrue (
751- '--hash=sha256:aaaaa' in out_lines [1 ]
751+ '+ --hash=sha256:aaaaa' in out_lines [4 ]. replace ( " " , "" )
752752 )
753753
754754 @cleanup_tmpdir ('hashin*' )
You can’t perform that action at this time.
0 commit comments