mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-06 03:25:21 +00:00
adding only venv use feature
This commit is contained in:
parent
081a024d63
commit
7c946be04a
2
.gitignore
vendored
2
.gitignore
vendored
@ -53,3 +53,5 @@ Session.vim
|
|||||||
*~
|
*~
|
||||||
|
|
||||||
/pipreqs/*.bak
|
/pipreqs/*.bak
|
||||||
|
|
||||||
|
.venv
|
4
pipreqs/__main__.py
Normal file
4
pipreqs/__main__.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from pipreqs import pipreqs
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
pipreqs.main()
|
@ -12,6 +12,7 @@ Arguments:
|
|||||||
|
|
||||||
Options:
|
Options:
|
||||||
--use-local Use ONLY local package info instead of querying PyPI.
|
--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.
|
--pypi-server <url> Use custom PyPi server.
|
||||||
--proxy <url> Use Proxy, parameter will be passed to requests
|
--proxy <url> Use Proxy, parameter will be passed to requests
|
||||||
library. You can also just set the environments
|
library. You can also just set the environments
|
||||||
@ -260,10 +261,26 @@ def get_imports_info(imports, pypi_server="https://pypi.python.org/pypi/", proxy
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def get_locally_installed_packages(encoding="utf-8"):
|
def get_locally_installed_packages(use_venv_packages: bool, encoding="utf-8"):
|
||||||
packages = []
|
packages = []
|
||||||
ignore = ["tests", "_tests", "egg", "EGG", "info"]
|
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 root, dirs, files in os.walk(path):
|
||||||
for item in files:
|
for item in files:
|
||||||
if "top_level" in item:
|
if "top_level" in item:
|
||||||
@ -301,8 +318,9 @@ def get_locally_installed_packages(encoding="utf-8"):
|
|||||||
return packages
|
return packages
|
||||||
|
|
||||||
|
|
||||||
def get_import_local(imports, encoding="utf-8"):
|
def get_import_local(imports, use_venv_packages: bool, encoding="utf-8"):
|
||||||
local = get_locally_installed_packages()
|
local = get_locally_installed_packages(use_venv_packages=use_venv_packages)
|
||||||
|
print('LOCAL: ', local)
|
||||||
result = []
|
result = []
|
||||||
for item in imports:
|
for item in imports:
|
||||||
# search through local packages
|
# search through local packages
|
||||||
@ -313,6 +331,7 @@ def get_import_local(imports, encoding="utf-8"):
|
|||||||
if item in package["exports"] or item == package["name"]:
|
if item in package["exports"] or item == package["name"]:
|
||||||
result.append(package)
|
result.append(package)
|
||||||
|
|
||||||
|
print(result)
|
||||||
# removing duplicates of package/version
|
# removing duplicates of package/version
|
||||||
# had to use second method instead of the previous one,
|
# had to use second method instead of the previous one,
|
||||||
# because we have a list in the 'exports' field
|
# because we have a list in the 'exports' field
|
||||||
@ -551,7 +570,9 @@ def init(args):
|
|||||||
|
|
||||||
if args["--use-local"]:
|
if args["--use-local"]:
|
||||||
logging.debug("Getting package information ONLY from local installation.")
|
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:
|
else:
|
||||||
logging.debug("Getting packages information from Local/PyPI")
|
logging.debug("Getting packages information from Local/PyPI")
|
||||||
local = get_import_local(candidates, encoding=encoding)
|
local = get_import_local(candidates, encoding=encoding)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user