mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-06 03:25:21 +00:00
style(pipreqs): pep8
This commit is contained in:
parent
c4d0fb8cf0
commit
099af5594c
@ -38,7 +38,8 @@ def get_all_imports(start_path):
|
||||
packages += [os.path.splitext(fn)[0] for fn in files]
|
||||
for file_name in files:
|
||||
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:
|
||||
if line[0] == "#":
|
||||
continue
|
||||
@ -53,10 +54,12 @@ def get_all_imports(start_path):
|
||||
for match in item.split(","):
|
||||
imports.append(match.strip())
|
||||
else:
|
||||
to_append = item.partition(' as ')[0].partition('.')[0]
|
||||
to_append = item.partition(
|
||||
' as ')[0].partition('.')[0]
|
||||
imports.append(to_append.strip())
|
||||
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:
|
||||
data = [x.strip() for x in f.readlines()]
|
||||
return sorted(list(set(third_party_packages) - set(data)))
|
||||
@ -69,7 +72,8 @@ def generate_requirements_file(path, imports):
|
||||
file=path
|
||||
))
|
||||
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):
|
||||
@ -86,36 +90,39 @@ 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
|
||||
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
|
||||
local = get_locally_installed_packages()
|
||||
result = []
|
||||
for item in imports:
|
||||
if item in local:
|
||||
result.append(local[item])
|
||||
return result
|
||||
|
||||
|
||||
def init(args):
|
||||
@ -123,16 +130,21 @@ def init(args):
|
||||
imports = get_all_imports(args['<path>'])
|
||||
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)
|
||||
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")
|
||||
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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user