Merge pull request #8 from Ohcanep/output-tuning

Output tuning
This commit is contained in:
Vadim Kravcenko 2015-04-29 09:21:02 +02:00
commit 68187a6fa5
2 changed files with 17 additions and 15 deletions

View File

@ -34,11 +34,11 @@ Usage
:: ::
Usage: Usage:
pipreqs <path> [options] pipreqs [options] <path>
Options: Options:
--savepath Supply custom path for requirements.txt --debug Print debug information
--debug See debug output --savepath <file> Save the list of requirements in the given file
Example Example
------- -------
@ -47,9 +47,9 @@ Example
$ pipreqs /home/project/location $ pipreqs /home/project/location
Looking for imports Looking for imports
Getting latest version of packages information from PyPi Getting latest information about packages from PyPI
Found third-party imports: flask, requests, sqlalchemy, docopt Found third-party imports: flask, requests, sqlalchemy, docopt
Successfuly saved requirements file in: /home/project/location/requirements.txt Successfully saved requirements file in /home/project/location/requirements.txt
Why not pip freeze? Why not pip freeze?
------------------- -------------------

View File

@ -3,12 +3,11 @@
"""pipreqs - Generate pip requirements.txt file based on imports """pipreqs - Generate pip requirements.txt file based on imports
Usage: Usage:
pipreqs <path> pipreqs [options] <path>
pipreqs <path>[options]
Options: Options:
--debug prints debug information. --debug Print debug information
--savepath path to requirements.txt (Optional) --savepath <file> Save the list of requirements in the given file
""" """
from __future__ import print_function from __future__ import print_function
import os import os
@ -30,7 +29,7 @@ REGEXP = [
def get_all_imports(start_path): def get_all_imports(start_path):
imports = [] imports = []
packages = [] packages = []
logging.debug('Traversing tree, start: %s', start_path) logging.debug('Traversing tree, start: {0}'.format(start_path))
for root, dirs, files in os.walk(start_path): for root, dirs, files in os.walk(start_path):
packages.append(os.path.basename(root)) packages.append(os.path.basename(root))
files = [fn for fn in files if os.path.splitext(fn)[1] == ".py"] files = [fn for fn in files if os.path.splitext(fn)[1] == ".py"]
@ -55,15 +54,18 @@ def get_all_imports(start_path):
to_append = item.partition(' as ')[0].partition('.')[0] to_append = item.partition(' as ')[0].partition('.')[0]
imports.append(to_append.strip()) imports.append(to_append.strip())
third_party_packages = set(imports) - set(set(packages) & set(imports)) third_party_packages = set(imports) - set(set(packages) & set(imports))
logging.debug('Found third-party packages: %s', third_party_packages) logging.debug('Found third-party packages: {0}'.format(third_party_packages))
with open(os.path.join(os.path.dirname(__file__), "stdlib"), "r") as f: with open(os.path.join(os.path.dirname(__file__), "stdlib"), "r") as f:
data = [x.strip() for x in f.readlines()] data = [x.strip() for x in f.readlines()]
return list(set(third_party_packages) - set(data)) return sorted(list(set(third_party_packages) - set(data)))
def generate_requirements_file(path, imports): def generate_requirements_file(path, imports):
with open(path, "w") as out_file: with open(path, "w") as out_file:
logging.debug('Writing %d requirements to file %s', (len(imports), path)) logging.debug('Writing {num} requirements to {file}'.format(
num=len(imports),
file=path
))
fmt = '{name} == {version}' fmt = '{name} == {version}'
out_file.write('\n'.join(fmt.format(**item) for item in imports) + '\n') out_file.write('\n'.join(fmt.format(**item) for item in imports) + '\n')
@ -86,12 +88,12 @@ def get_imports_info(imports):
def init(args): def init(args):
print("Looking for imports") print("Looking for imports")
imports = get_all_imports(args['<path>']) imports = get_all_imports(args['<path>'])
print("Getting latest version of packages information from PyPi") print("Getting latest information about packages from PyPI")
imports_with_info = get_imports_info(imports) imports_with_info = get_imports_info(imports)
print("Found third-party imports: " + ", ".join(imports)) print("Found third-party imports: " + ", ".join(imports))
path = args["--savepath"] if args["--savepath"] else os.path.join(args['<path>'], "requirements.txt") path = args["--savepath"] if args["--savepath"] else os.path.join(args['<path>'], "requirements.txt")
generate_requirements_file(path, imports_with_info) generate_requirements_file(path, imports_with_info)
print("Successfuly saved requirements file in: " + path) print("Successfully saved requirements file in " + path)
def main(): # pragma: no cover def main(): # pragma: no cover