Merge eeccc1cf1fdfe547424dfd457bc91781e804fff9 into b3d0b4443baf5da952f3135bd017ff1e60e654bb

This commit is contained in:
José Alexandre 2025-04-10 21:13:07 -03:00 committed by GitHub
commit ab86079aea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 66 additions and 5 deletions

2
.gitignore vendored
View File

@ -53,3 +53,5 @@ Session.vim
*~
/pipreqs/*.bak
.venv

View File

@ -47,6 +47,7 @@ Usage
Options:
--use-local Use ONLY local package info instead of querying PyPI
--only-venv Use ONLY venv packages instead local system
--pypi-server <url> Use custom PyPi server
--proxy <url> Use Proxy, parameter will be passed to requests library. You can also just set the
environments parameter in your terminal:

4
pipreqs/__main__.py Normal file
View File

@ -0,0 +1,4 @@
from pipreqs import pipreqs
if __name__ == "__main__":
pipreqs.main()

View File

@ -12,6 +12,7 @@ Arguments:
Options:
--use-local Use ONLY local package info instead of querying PyPI.
--only-venv Use ONLY venv packages instead local system.
--pypi-server <url> Use custom PyPi server.
--proxy <url> Use Proxy, parameter will be passed to requests
library. You can also just set the environments
@ -49,6 +50,7 @@ from docopt import docopt
import requests
from yarg import json2package
from yarg.exceptions import HTTPError
from typing import Optional
from pipreqs import __version__
@ -260,10 +262,29 @@ 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(use_venv_packages: bool, encoding="utf-8"):
packages = []
ignore = ["tests", "_tests", "egg", "EGG", "info"]
for path in sys.path:
venv_path = os.environ.get("VIRTUAL_ENV")
paths_to_search = []
if use_venv_packages and venv_path:
lib_path = os.path.join(venv_path, "lib")
if os.path.isdir(lib_path):
for entry in os.listdir(lib_path):
site_packages = os.path.join(lib_path, entry, "site-packages")
if os.path.isdir(site_packages):
paths_to_search.append(site_packages)
break
else:
if use_venv_packages and not venv_path:
logging.warning(
"You specified to use only the virtual environment packages, "
"but no virtual environment is currently active."
)
paths_to_search = sys.path
for path in paths_to_search:
for root, dirs, files in os.walk(path):
for item in files:
if "top_level" in item:
@ -301,8 +322,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, use_venv_packages: Optional[bool] = False, encoding="utf-8"):
local = get_locally_installed_packages(use_venv_packages=use_venv_packages)
result = []
for item in imports:
# search through local packages
@ -551,7 +572,9 @@ def init(args):
if args["--use-local"]:
logging.debug("Getting package information ONLY from local installation.")
imports = get_import_local(candidates, encoding=encoding)
print(candidates)
imports = get_import_local(candidates, args["--only-venv"], encoding=encoding)
print(imports)
else:
logging.debug("Getting packages information from Local/PyPI")
local = get_import_local(candidates, encoding=encoding)

View File

@ -0,0 +1,13 @@
asposestorage==1.0.2
beautifulsoup4==4.13.3
boto==2.49.0
docopt==0.6.2
Flask==3.1.0
ipython==8.12.3
nose==1.3.7
peewee==3.17.9
pyflakes==3.1.0
pyflakes==3.3.2
Requests==2.32.3
SQLAlchemy==2.0.40
ujson==5.10.0

View File

@ -0,0 +1 @@

View File

@ -156,6 +156,22 @@ class TestPipreqs(unittest.TestCase):
for item in imports_with_info:
self.assertTrue(item["name"].lower() in self.local)
@patch("pipreqs.pipreqs.get_locally_installed_packages")
def test_get_import_local_only_from_venv(self, mock_get_local_packages):
mock_get_local_packages.return_value = [
{"name": "requests", "version": "2.31.0", "exports": ["requests"]},
{"name": "docopt", "version": "0.6.2", "exports": ["docopt"]},
]
imports = {"requests": "2.31.0", "docopt": "0.6.2", "flask": "3.0.2"}
result = pipreqs.get_import_local(imports, use_venv_packages=True)
self.assertEqual(result, [
{"name": "requests", "version": "2.31.0", "exports": ["requests"]},
{"name": "docopt", "version": "0.6.2", "exports": ["docopt"]},
])
def test_init(self):
"""
Test that all modules we will test upon are in requirements file
@ -194,6 +210,7 @@ class TestPipreqs(unittest.TestCase):
"--savepath": None,
"--print": False,
"--use-local": True,
"--only-venv": False,
"--force": True,
"--proxy": None,
"--pypi-server": None,