mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-06 03:25:21 +00:00
Version 0.2.8
Add --force option Clean up
This commit is contained in:
parent
91db3afac7
commit
fe71b90075
1
.gitignore
vendored
1
.gitignore
vendored
@ -41,3 +41,4 @@ output/*/index.html
|
|||||||
|
|
||||||
# Sphinx
|
# Sphinx
|
||||||
docs/_build
|
docs/_build
|
||||||
|
.idea/
|
||||||
|
@ -3,6 +3,11 @@
|
|||||||
History
|
History
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
0.2.8 (2015-05-11)
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
* Add --force option as a protection for overwrites
|
||||||
|
|
||||||
0.2.6 (2015-05-11)
|
0.2.6 (2015-05-11)
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ Usage
|
|||||||
--use-local Use ONLY local package information instead of querying PyPI
|
--use-local Use ONLY local package information instead of querying PyPI
|
||||||
--debug Print debug information
|
--debug Print debug information
|
||||||
--savepath <file> Save the list of requirements in the given file
|
--savepath <file> Save the list of requirements in the given file
|
||||||
|
--force Overwrite existing requirements.txt
|
||||||
Example
|
Example
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
@ -9,11 +9,11 @@ Options:
|
|||||||
--use-local Use ONLY local package information instead of querying PyPI
|
--use-local Use ONLY local package information instead of querying PyPI
|
||||||
--debug Print debug information
|
--debug Print debug information
|
||||||
--savepath <file> Save the list of requirements in the given file
|
--savepath <file> Save the list of requirements in the given file
|
||||||
|
--force Overwrite existing requirements.txt
|
||||||
"""
|
"""
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from distutils.sysconfig import get_python_lib
|
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -28,8 +28,6 @@ REGEXP = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_all_imports(path):
|
def get_all_imports(path):
|
||||||
imports = []
|
imports = []
|
||||||
candidates = []
|
candidates = []
|
||||||
@ -92,21 +90,21 @@ def get_locally_installed_packages():
|
|||||||
packages = {}
|
packages = {}
|
||||||
ignore = ["tests", "_tests", "egg", "EGG", "info"]
|
ignore = ["tests", "_tests", "egg", "EGG", "info"]
|
||||||
for path in sys.path:
|
for path in sys.path:
|
||||||
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:
|
||||||
with open(os.path.join(root, item), "r") as f:
|
with open(os.path.join(root, item), "r") as f:
|
||||||
package = root.split("/")[-1].split("-")
|
package = root.split("/")[-1].split("-")
|
||||||
try:
|
try:
|
||||||
package_import = f.read().strip().split("\n")
|
package_import = f.read().strip().split("\n")
|
||||||
except:
|
except:
|
||||||
continue
|
continue
|
||||||
for item in package_import:
|
for i_item in package_import:
|
||||||
if item not in ignore and package[0] not in ignore:
|
if i_item not in ignore and package[0] not in ignore:
|
||||||
packages[item] = {
|
packages[i_item] = {
|
||||||
'version': package[1].replace(".dist", ""),
|
'version': package[1].replace(".dist", ""),
|
||||||
'name': package[0]
|
'name': package[0]
|
||||||
}
|
}
|
||||||
return packages
|
return packages
|
||||||
|
|
||||||
|
|
||||||
@ -147,7 +145,7 @@ def join(f):
|
|||||||
|
|
||||||
def init(args):
|
def init(args):
|
||||||
candidates = get_all_imports(args['<path>'])
|
candidates = get_all_imports(args['<path>'])
|
||||||
candidates = get_pkg_names(get_all_imports(args['<path>']))
|
candidates = get_pkg_names(candidates)
|
||||||
logging.debug("Found imports: " + ", ".join(candidates))
|
logging.debug("Found imports: " + ", ".join(candidates))
|
||||||
|
|
||||||
if args['--use-local']:
|
if args['--use-local']:
|
||||||
@ -162,10 +160,13 @@ def init(args):
|
|||||||
for z in local]]
|
for z in local]]
|
||||||
imports = local + get_imports_info(difference)
|
imports = local + get_imports_info(difference)
|
||||||
|
|
||||||
path = args[
|
path = args["--savepath"] if args["--savepath"] else os.path.join(args['<path>'], "requirements.txt")
|
||||||
"--savepath"] if args["--savepath"] else os.path.join(args['<path>'], "requirements.txt")
|
|
||||||
|
if not args["--savepath"] and not args["--force"] and os.path.exists(path):
|
||||||
|
logging.info("Requirements.txt already exists, use --force to overwrite it")
|
||||||
|
return
|
||||||
generate_requirements_file(path, imports)
|
generate_requirements_file(path, imports)
|
||||||
print("Successfully saved requirements file in " + path)
|
logging.info("Successfully saved requirements file in " + path)
|
||||||
|
|
||||||
|
|
||||||
def main(): # pragma: no cover
|
def main(): # pragma: no cover
|
||||||
|
2
setup.py
2
setup.py
@ -20,7 +20,7 @@ requirements = [
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='pipreqs',
|
name='pipreqs',
|
||||||
version='0.2.7',
|
version='0.2.8',
|
||||||
description="Pip requirements.txt generator based on imports in project",
|
description="Pip requirements.txt generator based on imports in project",
|
||||||
long_description=readme + '\n\n' + history,
|
long_description=readme + '\n\n' + history,
|
||||||
author="Vadim Kravcenko",
|
author="Vadim Kravcenko",
|
||||||
|
@ -51,13 +51,12 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
def test_get_use_local_only(self):
|
def test_get_use_local_only(self):
|
||||||
# should find only docopt and requests
|
# should find only docopt and requests
|
||||||
imports_with_info = pipreqs.get_import_local(self.modules)
|
imports_with_info = pipreqs.get_import_local(self.modules)
|
||||||
print(imports_with_info)
|
|
||||||
for item in imports_with_info:
|
for item in imports_with_info:
|
||||||
self.assertTrue(item['name'].lower() in self.local)
|
self.assertTrue(item['name'].lower() in self.local)
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
pipreqs.init(
|
pipreqs.init(
|
||||||
{'<path>': self.project, '--savepath': None, '--use-local': None})
|
{'<path>': self.project, '--savepath': None, '--use-local': None, '--force': True})
|
||||||
assert os.path.exists(self.requirements_path) == 1
|
assert os.path.exists(self.requirements_path) == 1
|
||||||
with open(self.requirements_path, "r") as f:
|
with open(self.requirements_path, "r") as f:
|
||||||
data = f.read().lower()
|
data = f.read().lower()
|
||||||
@ -66,7 +65,7 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
|
|
||||||
def test_init_local_only(self):
|
def test_init_local_only(self):
|
||||||
pipreqs.init(
|
pipreqs.init(
|
||||||
{'<path>': self.project, '--savepath': None, '--use-local': True})
|
{'<path>': self.project, '--savepath': None, '--use-local': True, '--force': True})
|
||||||
assert os.path.exists(self.requirements_path) == 1
|
assert os.path.exists(self.requirements_path) == 1
|
||||||
with open(self.requirements_path, "r") as f:
|
with open(self.requirements_path, "r") as f:
|
||||||
data = f.readlines()
|
data = f.readlines()
|
||||||
@ -85,6 +84,15 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
for item in self.modules2:
|
for item in self.modules2:
|
||||||
self.assertTrue(item.lower() in data)
|
self.assertTrue(item.lower() in data)
|
||||||
|
|
||||||
|
def test_init_overwrite(self):
|
||||||
|
with open(self.requirements_path, "w") as f:
|
||||||
|
f.write("should_not_be_overwritten")
|
||||||
|
pipreqs.init({'<path>': self.project, '--savepath': None, '--use-local': None, '--force': None})
|
||||||
|
assert os.path.exists(self.requirements_path) == 1
|
||||||
|
with open(self.requirements_path, "r") as f:
|
||||||
|
data = f.read().lower()
|
||||||
|
self.assertEqual(data, "should_not_be_overwritten")
|
||||||
|
|
||||||
def test_get_import_name_without_alias(self):
|
def test_get_import_name_without_alias(self):
|
||||||
import_name_with_alias = "requests as R"
|
import_name_with_alias = "requests as R"
|
||||||
expected_import_name_without_alias = "requests"
|
expected_import_name_without_alias = "requests"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user