From fe71b900759284d0e684f511177008f0a031cc5e Mon Sep 17 00:00:00 2001 From: Vadim Kravcenko Date: Fri, 19 Jun 2015 23:23:38 +0200 Subject: [PATCH] Version 0.2.8 Add --force option Clean up --- .gitignore | 1 + HISTORY.rst | 5 +++++ README.rst | 2 +- pipreqs/pipreqs.py | 45 ++++++++++++++++++++++--------------------- setup.py | 2 +- tests/test_pipreqs.py | 14 +++++++++++--- 6 files changed, 42 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 74ffd4e..fb26db5 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ output/*/index.html # Sphinx docs/_build +.idea/ diff --git a/HISTORY.rst b/HISTORY.rst index b721e42..0590fdb 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,11 @@ History ------- +0.2.8 (2015-05-11) +--------------------- + +* Add --force option as a protection for overwrites + 0.2.6 (2015-05-11) --------------------- diff --git a/README.rst b/README.rst index 953b471..a3a84ee 100644 --- a/README.rst +++ b/README.rst @@ -40,7 +40,7 @@ Usage --use-local Use ONLY local package information instead of querying PyPI --debug Print debug information --savepath Save the list of requirements in the given file - + --force Overwrite existing requirements.txt Example ------- diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index 34ad65f..a209923 100755 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -9,11 +9,11 @@ Options: --use-local Use ONLY local package information instead of querying PyPI --debug Print debug information --savepath Save the list of requirements in the given file + --force Overwrite existing requirements.txt """ from __future__ import print_function import os import sys -from distutils.sysconfig import get_python_lib import re import logging @@ -28,8 +28,6 @@ REGEXP = [ ] - - def get_all_imports(path): imports = [] candidates = [] @@ -92,21 +90,21 @@ def get_locally_installed_packages(): packages = {} ignore = ["tests", "_tests", "egg", "EGG", "info"] for path in sys.path: - 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("-") - try: - package_import = f.read().strip().split("\n") - except: - continue - for item in package_import: - if item not in ignore and package[0] not in ignore: - packages[item] = { - 'version': package[1].replace(".dist", ""), - 'name': package[0] - } + 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("-") + try: + package_import = f.read().strip().split("\n") + except: + continue + for i_item in package_import: + if i_item not in ignore and package[0] not in ignore: + packages[i_item] = { + 'version': package[1].replace(".dist", ""), + 'name': package[0] + } return packages @@ -147,7 +145,7 @@ def join(f): def init(args): candidates = get_all_imports(args['']) - candidates = get_pkg_names(get_all_imports(args[''])) + candidates = get_pkg_names(candidates) logging.debug("Found imports: " + ", ".join(candidates)) if args['--use-local']: @@ -162,10 +160,13 @@ def init(args): for z in local]] imports = local + get_imports_info(difference) - path = args[ - "--savepath"] if args["--savepath"] else os.path.join(args[''], "requirements.txt") + path = args["--savepath"] if args["--savepath"] else os.path.join(args[''], "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) - print("Successfully saved requirements file in " + path) + logging.info("Successfully saved requirements file in " + path) def main(): # pragma: no cover diff --git a/setup.py b/setup.py index efaad25..f1b54df 100755 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ requirements = [ setup( name='pipreqs', - version='0.2.7', + version='0.2.8', description="Pip requirements.txt generator based on imports in project", long_description=readme + '\n\n' + history, author="Vadim Kravcenko", diff --git a/tests/test_pipreqs.py b/tests/test_pipreqs.py index 426ad25..b60380d 100755 --- a/tests/test_pipreqs.py +++ b/tests/test_pipreqs.py @@ -51,13 +51,12 @@ class TestPipreqs(unittest.TestCase): def test_get_use_local_only(self): # should find only docopt and requests imports_with_info = pipreqs.get_import_local(self.modules) - print(imports_with_info) for item in imports_with_info: self.assertTrue(item['name'].lower() in self.local) def test_init(self): pipreqs.init( - {'': self.project, '--savepath': None, '--use-local': None}) + {'': self.project, '--savepath': None, '--use-local': None, '--force': True}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.read().lower() @@ -66,7 +65,7 @@ class TestPipreqs(unittest.TestCase): def test_init_local_only(self): pipreqs.init( - {'': self.project, '--savepath': None, '--use-local': True}) + {'': self.project, '--savepath': None, '--use-local': True, '--force': True}) assert os.path.exists(self.requirements_path) == 1 with open(self.requirements_path, "r") as f: data = f.readlines() @@ -85,6 +84,15 @@ class TestPipreqs(unittest.TestCase): for item in self.modules2: 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({'': 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): import_name_with_alias = "requests as R" expected_import_name_without_alias = "requests"