Merge bb256b091fbf166e4480d1eb45b9f3ebab9c1619 into b3d0b4443baf5da952f3135bd017ff1e60e654bb

This commit is contained in:
Tdarnell 2025-04-14 01:08:59 +00:00 committed by GitHub
commit 413125bc94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 6 deletions

View File

@ -67,6 +67,7 @@ Usage
<gt> | e.g. Flask>=1.1.2
<no-pin> | e.g. Flask
--scan-notebooks Look for imports in jupyter notebook files.
--venv <dirs>... Look for imports in the specified virtualenv
Example
-------

View File

@ -37,6 +37,7 @@ Options:
<gt> | e.g. Flask>=1.1.2
<no-pin> | e.g. Flask
--scan-notebooks Look for imports in jupyter notebook files.
--venv <dirs>... Look for imports in the specified virtualenv
"""
from contextlib import contextmanager
import os
@ -109,6 +110,7 @@ def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links
".tox",
"__pycache__",
"env",
".env",
"venv",
".venv",
".ipynb_checkpoints",
@ -260,10 +262,10 @@ def get_imports_info(imports, pypi_server="https://pypi.python.org/pypi/", proxy
return result
def get_locally_installed_packages(encoding="utf-8"):
def get_locally_installed_packages(encoding="utf-8", paths: list = sys.path):
packages = []
ignore = ["tests", "_tests", "egg", "EGG", "info"]
for path in sys.path:
for path in paths:
for root, dirs, files in os.walk(path):
for item in files:
if "top_level" in item:
@ -301,8 +303,8 @@ def get_locally_installed_packages(encoding="utf-8"):
return packages
def get_import_local(imports, encoding="utf-8"):
local = get_locally_installed_packages()
def get_import_local(imports, encoding="utf-8", paths: list = sys.path):
local = get_locally_installed_packages(encoding=encoding, paths=paths)
result = []
for item in imports:
# search through local packages
@ -504,6 +506,7 @@ def init(args):
global scan_noteboooks
encoding = args.get("--encoding")
extra_ignore_dirs = args.get("--ignore")
venv_dirs = args.get("--venv")
follow_links = not args.get("--no-follow-links")
ignore_errors = args.get("--ignore-errors")
@ -520,6 +523,9 @@ def init(args):
if extra_ignore_dirs:
extra_ignore_dirs = extra_ignore_dirs.split(",")
if venv_dirs:
venv_dirs = venv_dirs.split(",")
path = (
args["--savepath"] if args["--savepath"] else os.path.join(input_path, "requirements.txt")
)
@ -554,7 +560,10 @@ def init(args):
imports = get_import_local(candidates, encoding=encoding)
else:
logging.debug("Getting packages information from Local/PyPI")
local = get_import_local(candidates, encoding=encoding)
if venv_dirs:
local = get_import_local(candidates, encoding=encoding, paths=venv_dirs)
else:
local = get_import_local(candidates, encoding=encoding)
# check if candidate name is found in
# the list of exported modules, installed locally