Add and implement function diff, improve inline comments in function parse_requirements

This commit is contained in:
kxrd 2017-06-10 21:24:39 +02:00
parent a78203dc2b
commit 7549b1c416

View File

@ -18,6 +18,7 @@ Options:
--savepath <file> Save the list of requirements in the given file --savepath <file> Save the list of requirements in the given file
--print Output the list of requirements in the standard output --print Output the list of requirements in the standard output
--force Overwrite existing requirements.txt --force Overwrite existing requirements.txt
--diff <file> Compare modules in requirements.txt to project imports.
""" """
from __future__ import print_function, absolute_import from __future__ import print_function, absolute_import
import os import os
@ -239,7 +240,7 @@ def parse_requirements(file_):
tuple: The contents of the file, excluding comments. tuple: The contents of the file, excluding comments.
""" """
modules = [] modules = []
delim = ["<", ">", "=", "!", "~"] # https://www.python.org/dev/peps/pep-0508/#complete-grammar delim = ["<", ">", "=", "!", "~"] # https://www.python.org/dev/peps/pep-0508/#complete-grammar
try: try:
with open(file_, "r") as f: with open(file_, "r") as f:
@ -252,7 +253,7 @@ def parse_requirements(file_):
data = [x for x in data if x[0].isalpha()] data = [x for x in data if x[0].isalpha()]
for x in data: for x in data:
if not any([y in x for y in delim]): # Check for modules w/o a specifier. if not any([y in x for y in delim]): # Check for modules w/o a specifier.
modules.append({"name": x, "version": None}) modules.append({"name": x, "version": None})
for y in x: for y in x:
if y in delim: if y in delim:
@ -289,6 +290,14 @@ def compare_modules(file_, imports):
return modules_not_imported return modules_not_imported
def diff(file_, imports):
"""Display the difference between modules in file a and imported modules."""
modules_not_imported = compare_modules(file_, imports)
logging.info("The following modules are in {} but do not seem to be imported: "
"{}".format(file_, ", ".join(x for x in modules_not_imported)))
def init(args): def init(args):
encoding = args.get('--encoding') encoding = args.get('--encoding')
extra_ignore_dirs = args.get('--ignore') extra_ignore_dirs = args.get('--ignore')
@ -326,6 +335,10 @@ def init(args):
path = (args["--savepath"] if args["--savepath"] else path = (args["--savepath"] if args["--savepath"] else
os.path.join(args['<path>'], "requirements.txt")) os.path.join(args['<path>'], "requirements.txt"))
if args["--diff"]:
diff(args["--diff"], imports)
return
if not args["--print"] and not args["--savepath"] and not args["--force"] and os.path.exists(path): if not args["--print"] and not args["--savepath"] and not args["--force"] and os.path.exists(path):
logging.warning("Requirements.txt already exists, " logging.warning("Requirements.txt already exists, "
"use --force to overwrite it") "use --force to overwrite it")