mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-01 18:56:52 +00:00
Merge pull request #206 from dlgallagher/requirements_txt_fixer_followup
Some style tweaks (requirements_txt_fixer)
This commit is contained in:
commit
50871f83cc
2 changed files with 37 additions and 26 deletions
|
|
@ -3,6 +3,10 @@ from __future__ import print_function
|
|||
import argparse
|
||||
|
||||
|
||||
PASS = 0
|
||||
FAIL = 1
|
||||
|
||||
|
||||
class Requirement(object):
|
||||
|
||||
def __init__(self):
|
||||
|
|
@ -30,14 +34,14 @@ class Requirement(object):
|
|||
|
||||
def fix_requirements(f):
|
||||
requirements = []
|
||||
before = list(f)
|
||||
before = tuple(f)
|
||||
after = []
|
||||
|
||||
before_string = b''.join(before)
|
||||
|
||||
# If the file is empty (i.e. only whitespace/newlines) exit early
|
||||
if before_string.strip() == b'':
|
||||
return 0
|
||||
return PASS
|
||||
|
||||
for line in before:
|
||||
# If the most recent requirement object has a value, then it's
|
||||
|
|
@ -60,19 +64,18 @@ def fix_requirements(f):
|
|||
requirement.value = line
|
||||
|
||||
for requirement in sorted(requirements):
|
||||
for comment in requirement.comments:
|
||||
after.append(comment)
|
||||
after.extend(requirement.comments)
|
||||
after.append(requirement.value)
|
||||
|
||||
after_string = b''.join(after)
|
||||
|
||||
if before_string == after_string:
|
||||
return 0
|
||||
return PASS
|
||||
else:
|
||||
f.seek(0)
|
||||
f.write(after_string)
|
||||
f.truncate()
|
||||
return 1
|
||||
return FAIL
|
||||
|
||||
|
||||
def fix_requirements_txt(argv=None):
|
||||
|
|
@ -80,7 +83,7 @@ def fix_requirements_txt(argv=None):
|
|||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retv = 0
|
||||
retv = PASS
|
||||
|
||||
for arg in args.filenames:
|
||||
with open(arg, 'rb+') as file_obj:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue