diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index 431764a..fb93beb 100755 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -18,6 +18,7 @@ Options: --savepath Save the list of requirements in the given file --print Output the list of requirements in the standard output --force Overwrite existing requirements.txt + --diff Compare modules in requirements.txt to project imports. """ from __future__ import print_function, absolute_import import os @@ -239,7 +240,7 @@ def parse_requirements(file_): tuple: The contents of the file, excluding comments. """ modules = [] - delim = ["<", ">", "=", "!", "~"] # https://www.python.org/dev/peps/pep-0508/#complete-grammar + delim = ["<", ">", "=", "!", "~"] # https://www.python.org/dev/peps/pep-0508/#complete-grammar try: with open(file_, "r") as f: @@ -252,7 +253,7 @@ def parse_requirements(file_): data = [x for x in data if x[0].isalpha()] 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}) for y in x: if y in delim: @@ -289,6 +290,14 @@ def compare_modules(file_, imports): 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): encoding = args.get('--encoding') extra_ignore_dirs = args.get('--ignore') @@ -326,6 +335,10 @@ def init(args): path = (args["--savepath"] if args["--savepath"] else os.path.join(args[''], "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): logging.warning("Requirements.txt already exists, " "use --force to overwrite it")