style(pipreqs): pep8

This commit is contained in:
Vadim Kravcenko 2015-05-06 15:21:19 +02:00
parent c4d0fb8cf0
commit 099af5594c

View File

@ -38,7 +38,8 @@ def get_all_imports(start_path):
packages += [os.path.splitext(fn)[0] for fn in files] packages += [os.path.splitext(fn)[0] for fn in files]
for file_name in files: for file_name in files:
with open(os.path.join(root, file_name), "r") as file_object: with open(os.path.join(root, file_name), "r") as file_object:
lines = filter(lambda l:len(l) > 0, map(lambda l:l.strip(), file_object)) lines = filter(
lambda l: len(l) > 0, map(lambda l: l.strip(), file_object))
for line in lines: for line in lines:
if line[0] == "#": if line[0] == "#":
continue continue
@ -53,10 +54,12 @@ def get_all_imports(start_path):
for match in item.split(","): for match in item.split(","):
imports.append(match.strip()) imports.append(match.strip())
else: else:
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: {0}'.format(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 sorted(list(set(third_party_packages) - set(data))) return sorted(list(set(third_party_packages) - set(data)))
@ -69,7 +72,8 @@ def generate_requirements_file(path, imports):
file=path 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')
def get_imports_info(imports): def get_imports_info(imports):
@ -86,29 +90,32 @@ def get_imports_info(imports):
result.append({'name': item, 'version': last_release}) result.append({'name': item, 'version': last_release})
return result return result
def get_locally_installed_packages(): def get_locally_installed_packages():
path = get_python_lib() path = get_python_lib()
packages = {} packages = {}
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("-")
package_import = f.read().strip().split("\n") package_import = f.read().strip().split("\n")
package_import_name = "" package_import_name = ""
for item in package_import: for item in package_import:
if item not in ["tests","_tests"]: if item not in ["tests", "_tests"]:
package_import_name = item package_import_name = item
break break
if package_import_name == "": if package_import_name == "":
logging.debug('Could not determine import name for package ' + package_import) logging.debug(
'Could not determine import name for package ' + package_import)
else: else:
packages[package_import_name] = { packages[package_import_name] = {
'version':package[1].replace(".dist",""), 'version': package[1].replace(".dist", ""),
'name': package[0] 'name': package[0]
} }
return packages return packages
def get_import_local(imports): def get_import_local(imports):
local = get_locally_installed_packages() local = get_locally_installed_packages()
result = [] result = []
@ -123,16 +130,21 @@ def init(args):
imports = get_all_imports(args['<path>']) imports = get_all_imports(args['<path>'])
print("Found third-party imports: " + ", ".join(imports)) print("Found third-party imports: " + ", ".join(imports))
if args['--use-local']: if args['--use-local']:
print("Getting package version information ONLY from local installation.") print(
"Getting package version information ONLY from local installation.")
imports_with_info = get_import_local(imports) imports_with_info = get_import_local(imports)
else: else:
print("Getting latest version information about packages from Local/PyPI") print(
"Getting latest version information about packages from Local/PyPI")
imports_local = get_import_local(imports) imports_local = get_import_local(imports)
difference = [x for x in imports if x not in [z['name'] for z in imports_local]] difference = [x for x in imports if x not in [z['name']
for z in imports_local]]
imports_pypi = get_imports_info(difference) imports_pypi = get_imports_info(difference)
imports_with_info = imports_local + imports_pypi imports_with_info = imports_local + imports_pypi
print("Imports written to requirements file:", ", ".join([x['name'] for x in imports_with_info])) print("Imports written to requirements file:", ", ".join(
path = args["--savepath"] if args["--savepath"] else os.path.join(args['<path>'], "requirements.txt") [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) generate_requirements_file(path, imports_with_info)
print("Successfully saved requirements file in " + path) print("Successfully saved requirements file in " + path)