From 77c865253c4d0249582749a37fbda76b1ec8a8d7 Mon Sep 17 00:00:00 2001 From: Robert Spencer Date: Thu, 31 Aug 2017 23:17:31 +0200 Subject: [PATCH] Follow symbolic linked directories This makes pipreqs dive into directories that are symlinks as Python recognises these. If symlinks are not followed, then the requirements will be incorrect. We also add a command line option to disable following of symlinks if need be. --- pipreqs/pipreqs.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index 791168a..2fa71ac 100755 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -14,6 +14,7 @@ Options: $ export HTTPS_PROXY="https://10.10.1.10:1080" --debug Print debug information --ignore ... Ignore extra directories, each separated by a comma + --no-follow-links Do not follow symbolic links in the project --encoding Use encoding parameter for file open --savepath Save the list of requirements in the given file --print Output the list of requirements in the standard output @@ -50,7 +51,7 @@ else: py2_exclude = ["concurrent", "concurrent.futures"] -def get_all_imports(path, encoding=None, extra_ignore_dirs=None): +def get_all_imports(path, encoding=None, extra_ignore_dirs=None, follow_links=True): imports = set() raw_imports = set() candidates = [] @@ -63,7 +64,8 @@ def get_all_imports(path, encoding=None, extra_ignore_dirs=None): ignore_dirs_parsed.append(os.path.basename(os.path.realpath(e))) ignore_dirs.extend(ignore_dirs_parsed) - for root, dirs, files in os.walk(path): + walk = os.walk(path, followlinks=follow_links) + for root, dirs, files in walk: dirs[:] = [d for d in dirs if d not in ignore_dirs] candidates.append(os.path.basename(root)) @@ -332,13 +334,15 @@ def clean(file_, imports): def init(args): encoding = args.get('--encoding') extra_ignore_dirs = args.get('--ignore') + follow_links = not args.get('--no-follow-links') if extra_ignore_dirs: extra_ignore_dirs = extra_ignore_dirs.split(',') candidates = get_all_imports(args[''], encoding=encoding, - extra_ignore_dirs=extra_ignore_dirs) + extra_ignore_dirs=extra_ignore_dirs, + follow_links=follow_links) candidates = get_pkg_names(candidates) logging.debug("Found imports: " + ", ".join(candidates)) pypi_server = "https://pypi.python.org/pypi/"