mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-07 12:05:33 +00:00
Add --ignore-errors option
This commit adds a new option --ignore-errors. Originally it's a variable defined in get_all_imports and is always False. The purpose is to ignore errors when parsing single .py file. Also the opening/reading file part is moved to the try block, as those operations may also fail(encoding problem). Signed-off-by: 司芳源 <sify1221@gmail.com>
This commit is contained in:
parent
a593d27e3d
commit
6a1a469eb5
@ -46,6 +46,7 @@ Usage
|
|||||||
$ export HTTPS_PROXY="https://10.10.1.10:1080"
|
$ export HTTPS_PROXY="https://10.10.1.10:1080"
|
||||||
--debug Print debug information
|
--debug Print debug information
|
||||||
--ignore <dirs>... Ignore extra directories, each separated by a comma
|
--ignore <dirs>... Ignore extra directories, each separated by a comma
|
||||||
|
--ignore-errors Ignore errors when parsing single .py file
|
||||||
--no-follow-links Do not follow symbolic links in the project
|
--no-follow-links Do not follow symbolic links in the project
|
||||||
--encoding <charset> Use encoding parameter for file open
|
--encoding <charset> Use encoding parameter for file open
|
||||||
--savepath <file> Save the list of requirements in the given file
|
--savepath <file> Save the list of requirements in the given file
|
||||||
|
@ -20,6 +20,7 @@ Options:
|
|||||||
$ export HTTPS_PROXY="https://10.10.1.10:1080"
|
$ export HTTPS_PROXY="https://10.10.1.10:1080"
|
||||||
--debug Print debug information
|
--debug Print debug information
|
||||||
--ignore <dirs>... Ignore extra directories, each separated by a comma
|
--ignore <dirs>... Ignore extra directories, each separated by a comma
|
||||||
|
--ignore-errors Ignore errors when parsing single .py file
|
||||||
--no-follow-links Do not follow symbolic links in the project
|
--no-follow-links Do not follow symbolic links in the project
|
||||||
--encoding <charset> Use encoding parameter for file open
|
--encoding <charset> Use encoding parameter for file open
|
||||||
--savepath <file> Save the list of requirements in the given file
|
--savepath <file> Save the list of requirements in the given file
|
||||||
@ -88,11 +89,10 @@ def _open(filename=None, mode='r'):
|
|||||||
|
|
||||||
|
|
||||||
def get_all_imports(
|
def get_all_imports(
|
||||||
path, encoding=None, extra_ignore_dirs=None, follow_links=True):
|
path, encoding=None, extra_ignore_dirs=None, follow_links=True, ignore_errors=False):
|
||||||
imports = set()
|
imports = set()
|
||||||
raw_imports = set()
|
raw_imports = set()
|
||||||
candidates = []
|
candidates = []
|
||||||
ignore_errors = False
|
|
||||||
ignore_dirs = [".hg", ".svn", ".git", ".tox", "__pycache__", "env", "venv"]
|
ignore_dirs = [".hg", ".svn", ".git", ".tox", "__pycache__", "env", "venv"]
|
||||||
|
|
||||||
if extra_ignore_dirs:
|
if extra_ignore_dirs:
|
||||||
@ -111,9 +111,9 @@ def get_all_imports(
|
|||||||
candidates += [os.path.splitext(fn)[0] for fn in files]
|
candidates += [os.path.splitext(fn)[0] for fn in files]
|
||||||
for file_name in files:
|
for file_name in files:
|
||||||
file_name = os.path.join(root, file_name)
|
file_name = os.path.join(root, file_name)
|
||||||
with open(file_name, "r", encoding=encoding) as f:
|
|
||||||
contents = f.read()
|
|
||||||
try:
|
try:
|
||||||
|
with open(file_name, "r", encoding=encoding) as f:
|
||||||
|
contents = f.read()
|
||||||
tree = ast.parse(contents)
|
tree = ast.parse(contents)
|
||||||
for node in ast.walk(tree):
|
for node in ast.walk(tree):
|
||||||
if isinstance(node, ast.Import):
|
if isinstance(node, ast.Import):
|
||||||
@ -415,7 +415,8 @@ def init(args):
|
|||||||
candidates = get_all_imports(input_path,
|
candidates = get_all_imports(input_path,
|
||||||
encoding=encoding,
|
encoding=encoding,
|
||||||
extra_ignore_dirs=extra_ignore_dirs,
|
extra_ignore_dirs=extra_ignore_dirs,
|
||||||
follow_links=follow_links)
|
follow_links=follow_links,
|
||||||
|
ignore_errors=args["--ignore-errors"])
|
||||||
candidates = get_pkg_names(candidates)
|
candidates = get_pkg_names(candidates)
|
||||||
logging.debug("Found imports: " + ", ".join(candidates))
|
logging.debug("Found imports: " + ", ".join(candidates))
|
||||||
pypi_server = "https://pypi.python.org/pypi/"
|
pypi_server = "https://pypi.python.org/pypi/"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user