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.
This commit is contained in:
Robert Spencer 2017-08-31 23:17:31 +02:00
parent 22e80c27c2
commit 77c865253c

View File

@ -14,6 +14,7 @@ Options:
$ export HTTPS_PROXY="https://10.10.1.10:1080" $ export HTTPS_PROXY="https://10.10.1.10:1080"
--debug Print debug information --debug Print debug information
--ignore <dirs>... Ignore extra directories, each separated by a comma --ignore <dirs>... Ignore extra directories, each separated by a comma
--no-follow-links Do not follow symbolic links in the project
--encoding <charset> Use encoding parameter for file open --encoding <charset> Use encoding parameter for file open
--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
@ -50,7 +51,7 @@ else:
py2_exclude = ["concurrent", "concurrent.futures"] 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() imports = set()
raw_imports = set() raw_imports = set()
candidates = [] 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_parsed.append(os.path.basename(os.path.realpath(e)))
ignore_dirs.extend(ignore_dirs_parsed) 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] dirs[:] = [d for d in dirs if d not in ignore_dirs]
candidates.append(os.path.basename(root)) candidates.append(os.path.basename(root))
@ -332,13 +334,15 @@ def clean(file_, imports):
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')
follow_links = not args.get('--no-follow-links')
if extra_ignore_dirs: if extra_ignore_dirs:
extra_ignore_dirs = extra_ignore_dirs.split(',') extra_ignore_dirs = extra_ignore_dirs.split(',')
candidates = get_all_imports(args['<path>'], candidates = get_all_imports(args['<path>'],
encoding=encoding, encoding=encoding,
extra_ignore_dirs=extra_ignore_dirs) extra_ignore_dirs=extra_ignore_dirs,
follow_links=follow_links)
candidates = get_pkg_names(candidates) candidates = get_pkg_names(candidates)
logging.debug("Found imports: " + ", ".join(candidates)) logging.debug("Found imports: " + ", ".join(candidates))
pypi_server = "https://pypi.python.org/pypi/" pypi_server = "https://pypi.python.org/pypi/"