Add --save option to normalize-requirements command

Change-Id: Id6fe76e76fa87939475cb56f376aa084e61a9db0
This commit is contained in:
Haikel Guemar 2016-06-23 10:10:52 +02:00
parent 346553c0a6
commit 4ede8784c9

View File

@ -16,21 +16,40 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import argparse import argparse
import os.path
from openstack_requirements import requirement from openstack_requirements import requirement
def write_requirements_file(filename, reqs):
with open(filename + 'tmp', 'wt') as f:
f.write(reqs)
if os.path.exists(filename):
os.remove(filename)
os.rename(filename + 'tmp', filename)
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Normalize requirements files") description="Normalize requirements files")
parser.add_argument('requirements', help='requirements file input') parser.add_argument('requirements', help='requirements file input')
parser.add_argument('-s', '--save', action='store_true', default=False,
help=('save normalized requirements '
'file instead of displaying it'))
args = parser.parse_args() args = parser.parse_args()
with open(args.requirements) as f: with open(args.requirements) as f:
requirements = [line.strip() for line in f.readlines()] requirements = [line.strip() for line in f.readlines()]
normed_reqs = ""
for line in requirements: for line in requirements:
req = requirement.parse_line(line) req = requirement.parse_line(line)
print(req.to_line(comment_prefix=' ', normed_req = req.to_line(comment_prefix=' ', sort_specifiers=True)
sort_specifiers=True), end='') normed_reqs += normed_req
if args.save:
write_requirements_file(args.requirements, normed_reqs)
else:
print(normed_reqs, end='')
if __name__ == '__main__': if __name__ == '__main__':