Skip to content

Commit

Permalink
feat(cli): Add --use-local option
Browse files Browse the repository at this point in the history
When --use-local option is passed, only package information in local
site-packages directory is considered, otherwise BOTH local and PyPI
information is loaded.
  • Loading branch information
bndr committed May 6, 2015
1 parent 22ac931 commit c4d0fb8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 43 additions & 2 deletions pipreqs/pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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",
Expand All @@ -42,7 +42,7 @@
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',
Expand Down
7 changes: 6 additions & 1 deletion tests/test_pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ def test_get_imports_info(self):
self.assertEqual(len(with_info), 5, "Length of imports array with info is wrong")
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()
Expand Down

0 comments on commit c4d0fb8

Please sign in to comment.