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:
司芳源 2021-11-19 11:55:48 +08:00
parent a593d27e3d
commit 6a1a469eb5
2 changed files with 7 additions and 5 deletions

View File

@ -46,6 +46,7 @@ Usage
$ export HTTPS_PROXY="https://10.10.1.10:1080"
--debug Print debug information
--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
--encoding <charset> Use encoding parameter for file open
--savepath <file> Save the list of requirements in the given file

View File

@ -20,6 +20,7 @@ Options:
$ export HTTPS_PROXY="https://10.10.1.10:1080"
--debug Print debug information
--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
--encoding <charset> Use encoding parameter for file open
--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(
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()
raw_imports = set()
candidates = []
ignore_errors = False
ignore_dirs = [".hg", ".svn", ".git", ".tox", "__pycache__", "env", "venv"]
if extra_ignore_dirs:
@ -111,9 +111,9 @@ def get_all_imports(
candidates += [os.path.splitext(fn)[0] for fn in files]
for file_name in files:
file_name = os.path.join(root, file_name)
with open(file_name, "r", encoding=encoding) as f:
contents = f.read()
try:
with open(file_name, "r", encoding=encoding) as f:
contents = f.read()
tree = ast.parse(contents)
for node in ast.walk(tree):
if isinstance(node, ast.Import):
@ -415,7 +415,8 @@ def init(args):
candidates = get_all_imports(input_path,
encoding=encoding,
extra_ignore_dirs=extra_ignore_dirs,
follow_links=follow_links)
follow_links=follow_links,
ignore_errors=args["--ignore-errors"])
candidates = get_pkg_names(candidates)
logging.debug("Found imports: " + ", ".join(candidates))
pypi_server = "https://pypi.python.org/pypi/"