From 6a1a469eb542fcc368ac8788a857b128134f1ee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B8=E8=8A=B3=E6=BA=90?= Date: Fri, 19 Nov 2021 11:55:48 +0800 Subject: [PATCH] Add --ignore-errors option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: 司芳源 --- README.rst | 1 + pipreqs/pipreqs.py | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 16c477a..76a3c4a 100644 --- a/README.rst +++ b/README.rst @@ -46,6 +46,7 @@ Usage $ export HTTPS_PROXY="https://10.10.1.10:1080" --debug Print debug information --ignore ... 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 Use encoding parameter for file open --savepath Save the list of requirements in the given file diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index 24eeeb7..6d319d2 100644 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -20,6 +20,7 @@ Options: $ export HTTPS_PROXY="https://10.10.1.10:1080" --debug Print debug information --ignore ... 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 Use encoding parameter for file open --savepath 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/"