From e0137f2245cc9181097667240cf12b69429fd402 Mon Sep 17 00:00:00 2001 From: EJ Lee Date: Tue, 20 Oct 2015 19:39:48 +0900 Subject: [PATCH 1/4] fix(pipreqs): support windows directory separator --- pipreqs/pipreqs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index e2855dc..e14b4d3 100755 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -111,7 +111,7 @@ def get_locally_installed_packages(): 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 = root.split(os.sep)[-1].split("-") try: package_import = f.read().strip().split("\n") except: From 195565b50c9a4ee7858562b0c17c4a0c5382fefe Mon Sep 17 00:00:00 2001 From: EJ Lee Date: Tue, 20 Oct 2015 19:45:20 +0900 Subject: [PATCH 2/4] feat(cli): add --encoding parameter for open() --- pipreqs/pipreqs.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index e14b4d3..e6d28ac 100755 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -6,12 +6,13 @@ Usage: pipreqs [options] Options: - --use-local Use ONLY local package info instead of querying PyPI - --pypi-server Use custom PyPi server - --proxy Use Proxy, parameter will be passed to requests library - --debug Print debug information - --savepath Save the list of requirements in the given file - --force Overwrite existing requirements.txt + --use-local Use ONLY local package info instead of querying PyPI + --pypi-server Use custom PyPi server + --proxy Use Proxy, parameter will be passed to requests library + --debug Print debug information + --encoding Use encoding parameter for file open + --savepath Save the list of requirements in the given file + --force Overwrite existing requirements.txt """ from __future__ import print_function, absolute_import import os @@ -32,7 +33,7 @@ REGEXP = [ ] -def get_all_imports(path): +def get_all_imports(path, encoding=None): imports = [] candidates = [] ignore_dirs = [".hg", ".svn", ".git", "__pycache__", "env"] @@ -45,7 +46,7 @@ def get_all_imports(path): candidates += [os.path.splitext(fn)[0] for fn in files] for file_name in files: - with open(os.path.join(root, file_name), "r") as f: + with open(os.path.join(root, file_name), "r", encoding=encoding) as f: lines = filter( filter_line, map(lambda l: l.partition("#")[0].strip(), f)) for line in lines: @@ -162,7 +163,8 @@ def join(f): def init(args): - candidates = get_all_imports(args['']) + encoding = args.get('encoding') + candidates = get_all_imports(args[''], encoding=encoding) candidates = get_pkg_names(candidates) logging.debug("Found imports: " + ", ".join(candidates)) pypi_server = "https://pypi.python.org/pypi/" From a4e7cbc7723d31e56141b7c046297c22bd2ae15c Mon Sep 17 00:00:00 2001 From: EJ Lee Date: Tue, 20 Oct 2015 19:47:22 +0900 Subject: [PATCH 3/4] update usage --- README.rst | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index a3a84ee..2abab3c 100644 --- a/README.rst +++ b/README.rst @@ -37,10 +37,14 @@ Usage pipreqs [options] 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 + --use-local Use ONLY local package information instead of querying PyPI + --pypi-server Use custom PyPi server + --proxy Use Proxy, parameter will be passed to requests library + --debug Print debug information + --encoding Use encoding parameter for file open + --savepath Save the list of requirements in the given file + --force Overwrite existing requirements.txt + Example ------- From 061a9b5f64b53ff1befdfd8175b8110aa20645da Mon Sep 17 00:00:00 2001 From: EJ Lee Date: Tue, 20 Oct 2015 19:51:39 +0900 Subject: [PATCH 4/4] fixed lint warnings --- pipreqs/pipreqs.py | 5 ++--- tests/test_pipreqs.py | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index e6d28ac..2100a4e 100755 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -95,7 +95,7 @@ def get_imports_info(imports, pypi_server="https://pypi.python.org/pypi/", proxy data = json2package(response.content) elif response.status_code >= 300: raise HTTPError(status_code=response.status_code, - reason=response.reason) + reason=response.reason) except HTTPError: logging.debug( 'Package %s does not exist or network problems', item) @@ -173,7 +173,7 @@ def init(args): pypi_server = args["--pypi-server"] if args["--proxy"]: - proxy = {'http':args["--proxy"], 'https':args["--proxy"]} + proxy = {'http': args["--proxy"], 'https': args["--proxy"]} if args["--use-local"]: logging.debug( @@ -192,7 +192,6 @@ def init(args): 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.warning("Requirements.txt already exists, " "use --force to overwrite it") diff --git a/tests/test_pipreqs.py b/tests/test_pipreqs.py index b24a8b8..acf26d8 100755 --- a/tests/test_pipreqs.py +++ b/tests/test_pipreqs.py @@ -105,11 +105,9 @@ class TestPipreqs(unittest.TestCase): self.assertEqual( import_name_without_aliases, expected_import_name_without_alias) - def test_custom_pypi_server(self): self.assertRaises(requests.exceptions.MissingSchema, pipreqs.init, {'': self.project, '--savepath': None, - '--use-local': None, '--force': True, '--proxy':None, '--pypi-server':'nonexistent'}) - + '--use-local': None, '--force': True, '--proxy': None, '--pypi-server': 'nonexistent'}) def tearDown(self): try: