mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-07 12:05:33 +00:00
fix(pipreqs): Fix regular expressions matching
This commit is contained in:
parent
45047652ff
commit
4ea48f9c7c
@ -13,8 +13,12 @@ Options:
|
|||||||
import os, re, logging
|
import os, re, logging
|
||||||
from docopt import docopt
|
from docopt import docopt
|
||||||
import yarg
|
import yarg
|
||||||
|
from yarg.exceptions import HTTPError
|
||||||
|
|
||||||
REGEXP = [re.compile(r'import ([a-zA-Z123456789]+)'),re.compile(r'from (.*?) import (?:.*)')]
|
REGEXP = [
|
||||||
|
re.compile(r'^import (.+)$'),
|
||||||
|
re.compile(r'from (.*?) import (?:.*)')
|
||||||
|
]
|
||||||
|
|
||||||
def get_all_imports(start_path):
|
def get_all_imports(start_path):
|
||||||
imports = []
|
imports = []
|
||||||
@ -26,32 +30,41 @@ def get_all_imports(start_path):
|
|||||||
if file[-3:] != ".py":
|
if file[-3:] != ".py":
|
||||||
continue
|
continue
|
||||||
for rex in REGEXP:
|
for rex in REGEXP:
|
||||||
with open(root + "/" + file, "r") as f:
|
with open(os.path.join(root,file), "r") as f:
|
||||||
s = rex.match(f.read())
|
lines = f.readlines()
|
||||||
if s:
|
for line in lines:
|
||||||
|
if line[0] == "#":
|
||||||
|
continue
|
||||||
|
if "(" in line:
|
||||||
|
break
|
||||||
|
s = rex.match(line)
|
||||||
|
if not s:
|
||||||
|
continue
|
||||||
for item in s.groups():
|
for item in s.groups():
|
||||||
if "." in item:
|
if "," in item:
|
||||||
imports.append(item.split(".")[0])
|
for match in item.split(","):
|
||||||
|
imports.append(match.strip())
|
||||||
else:
|
else:
|
||||||
imports.append(item)
|
to_append = item if "." not in item else item.split(".")[0]
|
||||||
local_packages = list(set(packages) & set(imports))
|
imports.append(to_append.strip())
|
||||||
third_party_packages = list(set(imports) - set(local_packages))
|
third_party_packages = set(imports) - set(set(packages) & set(imports))
|
||||||
with open(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 list(set(third_party_packages) - set(data))
|
||||||
|
|
||||||
def generate_requirements_file(path, imports):
|
def generate_requirements_file(path, imports):
|
||||||
with open(path, "w") as ff:
|
with open(path, "w") as ff:
|
||||||
for item in imports:
|
for item in imports:
|
||||||
ff.write(item['name'])
|
ff.write(item['name'] + "==" + item['version'])
|
||||||
ff.write("==")
|
|
||||||
ff.write(item['version'])
|
|
||||||
ff.write("\n")
|
ff.write("\n")
|
||||||
|
|
||||||
def get_imports_info(imports):
|
def get_imports_info(imports):
|
||||||
result = []
|
result = []
|
||||||
for item in imports:
|
for item in imports:
|
||||||
|
try:
|
||||||
data = yarg.get(item)
|
data = yarg.get(item)
|
||||||
|
except HTTPError:
|
||||||
|
continue
|
||||||
if not data or len(data.release_ids) < 1:
|
if not data or len(data.release_ids) < 1:
|
||||||
continue
|
continue
|
||||||
last_release = data.release_ids[-1]
|
last_release = data.release_ids[-1]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user