mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-06 03:25:21 +00:00
feat(cli): Add --use-local option
When --use-local option is passed, only package information in local site-packages directory is considered, otherwise BOTH local and PyPI information is loaded.
This commit is contained in:
parent
22ac931a45
commit
c4d0fb8cf0
@ -37,6 +37,7 @@ Usage
|
||||
pipreqs [options] <path>
|
||||
|
||||
Options:
|
||||
--use-local Use ONLY local package information instead of querying PyPI
|
||||
--debug Print debug information
|
||||
--savepath <file> Save the list of requirements in the given file
|
||||
|
||||
|
@ -6,12 +6,14 @@ Usage:
|
||||
pipreqs [options] <path>
|
||||
|
||||
Options:
|
||||
--use-local Use ONLY local package information instead of querying PyPI
|
||||
--debug Print debug information
|
||||
--savepath <file> Save the list of requirements in the given file
|
||||
"""
|
||||
from __future__ import print_function
|
||||
import os
|
||||
import sys
|
||||
from distutils.sysconfig import get_python_lib
|
||||
import re
|
||||
import logging
|
||||
|
||||
@ -84,13 +86,52 @@ def get_imports_info(imports):
|
||||
result.append({'name': item, 'version': last_release})
|
||||
return result
|
||||
|
||||
def get_locally_installed_packages():
|
||||
path = get_python_lib()
|
||||
packages = {}
|
||||
for root, dirs, files in os.walk(path):
|
||||
for item in files:
|
||||
if "top_level" in item:
|
||||
with open(os.path.join(root,item), "r") as f:
|
||||
package = root.split("/")[-1].split("-")
|
||||
package_import = f.read().strip().split("\n")
|
||||
package_import_name = ""
|
||||
for item in package_import:
|
||||
if item not in ["tests","_tests"]:
|
||||
package_import_name = item
|
||||
break
|
||||
if package_import_name == "":
|
||||
logging.debug('Could not determine import name for package ' + package_import)
|
||||
else:
|
||||
packages[package_import_name] = {
|
||||
'version':package[1].replace(".dist",""),
|
||||
'name': package[0]
|
||||
}
|
||||
return packages
|
||||
|
||||
def get_import_local(imports):
|
||||
local = get_locally_installed_packages()
|
||||
result = []
|
||||
for item in imports:
|
||||
if item in local:
|
||||
result.append(local[item])
|
||||
return result
|
||||
|
||||
|
||||
def init(args):
|
||||
print("Looking for imports")
|
||||
imports = get_all_imports(args['<path>'])
|
||||
print("Getting latest information about packages from PyPI")
|
||||
imports_with_info = get_imports_info(imports)
|
||||
print("Found third-party imports: " + ", ".join(imports))
|
||||
if args['--use-local']:
|
||||
print("Getting package version information ONLY from local installation.")
|
||||
imports_with_info = get_import_local(imports)
|
||||
else:
|
||||
print("Getting latest version information about packages from Local/PyPI")
|
||||
imports_local = get_import_local(imports)
|
||||
difference = [x for x in imports if x not in [z['name'] for z in imports_local]]
|
||||
imports_pypi = get_imports_info(difference)
|
||||
imports_with_info = imports_local + imports_pypi
|
||||
print("Imports written to requirements file:", ", ".join([x['name'] for x in imports_with_info]))
|
||||
path = args["--savepath"] if args["--savepath"] else os.path.join(args['<path>'], "requirements.txt")
|
||||
generate_requirements_file(path, imports_with_info)
|
||||
print("Successfully saved requirements file in " + path)
|
||||
|
4
setup.py
4
setup.py
@ -24,7 +24,7 @@ test_requirements = [
|
||||
|
||||
setup(
|
||||
name='pipreqs',
|
||||
version='0.1.9',
|
||||
version='0.2.0',
|
||||
description="Pip requirements.txt generator based on imports in project",
|
||||
long_description=readme + '\n\n' + history,
|
||||
author="Vadim Kravcenko",
|
||||
@ -42,7 +42,7 @@ setup(
|
||||
zip_safe=False,
|
||||
keywords='pip requirements imports',
|
||||
classifiers=[
|
||||
'Development Status :: 2 - Pre-Alpha',
|
||||
'Development Status :: 4 - Beta',
|
||||
'Intended Audience :: Developers',
|
||||
'License :: OSI Approved :: Apache Software License',
|
||||
'Natural Language :: English',
|
||||
|
@ -39,8 +39,13 @@ class TestPipreqs(unittest.TestCase):
|
||||
for item in with_info:
|
||||
self.assertTrue(item['name'] in self.modules, "Import item appears to be missing")
|
||||
|
||||
def test_get_use_local_only(self):
|
||||
# should find only docopt and requests
|
||||
imports_with_info = pipreqs.get_import_local(self.modules)
|
||||
self.assertTrue(len(imports_with_info) == 2)
|
||||
|
||||
def test_init(self):
|
||||
pipreqs.init({'<path>': self.project, '--savepath': None})
|
||||
pipreqs.init({'<path>': self.project, '--savepath': None,'--use-local':None})
|
||||
assert os.path.exists(self.requirements_path) == 1
|
||||
with open(self.requirements_path, "r") as f:
|
||||
data = f.read()
|
||||
|
Loading…
x
Reference in New Issue
Block a user