Skip to content

Commit 08eead3

Browse files
committed
Add --ignore-errors to skip files with syntax errors and attempt to find requirements on a best-effort basis
1 parent cd3f437 commit 08eead3

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Usage
5555
--debug Print debug information
5656
--ignore <dirs>... Ignore extra directories, each separated by a comma
5757
--no-follow-links Do not follow symbolic links in the project
58+
--ignore-errors Ignore errors while scanning files
5859
--encoding <charset> Use encoding parameter for file open
5960
--savepath <file> Save the list of requirements in the given file
6061
--print Output the list of requirements in the standard output

pipreqs/pipreqs.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
$ export HTTPS_PROXY="https://10.10.1.10:1080"
2121
--debug Print debug information
2222
--ignore <dirs>... Ignore extra directories, each separated by a comma
23+
--ignore-errors Ignore errors while scanning files
2324
--no-follow-links Do not follow symbolic links in the project
2425
--encoding <charset> Use encoding parameter for file open
2526
--savepath <file> Save the list of requirements in the given file
@@ -97,11 +98,10 @@ def _open(filename=None, mode="r"):
9798
file.close()
9899

99100

100-
def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links=True):
101+
def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links=True, ignore_errors=False):
101102
imports = set()
102103
raw_imports = set()
103104
candidates = []
104-
ignore_errors = False
105105
ignore_dirs = [
106106
".hg",
107107
".svn",
@@ -133,9 +133,9 @@ def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links
133133

134134
for file_name in files:
135135
file_name = os.path.join(root, file_name)
136-
contents = read_file_content(file_name, encoding)
137136

138137
try:
138+
contents = read_file_content(file_name, encoding)
139139
tree = ast.parse(contents)
140140
for node in ast.walk(tree):
141141
if isinstance(node, ast.Import):
@@ -145,7 +145,7 @@ def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links
145145
raw_imports.add(node.module)
146146
except Exception as exc:
147147
if ignore_errors:
148-
traceback.print_exc(exc)
148+
traceback.print_exc()
149149
logging.warn("Failed on file: %s" % file_name)
150150
continue
151151
else:
@@ -504,6 +504,7 @@ def init(args):
504504
encoding = args.get("--encoding")
505505
extra_ignore_dirs = args.get("--ignore")
506506
follow_links = not args.get("--no-follow-links")
507+
ignore_errors = args.get("--ignore-errors")
507508

508509
scan_noteboooks = args.get("--scan-notebooks", False)
509510
handle_scan_noteboooks()
@@ -535,6 +536,7 @@ def init(args):
535536
encoding=encoding,
536537
extra_ignore_dirs=extra_ignore_dirs,
537538
follow_links=follow_links,
539+
ignore_errors=ignore_errors,
538540
)
539541
candidates = get_pkg_names(candidates)
540542
logging.debug("Found imports: " + ", ".join(candidates))

tests/test_pipreqs.py

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ def test_invalid_python(self):
114114
"""
115115
self.assertRaises(SyntaxError, pipreqs.get_all_imports, self.project_invalid)
116116

117+
def test_ignore_errors(self):
118+
"""
119+
Test that invalid python files do not raise an exception when ignore_errors is True.
120+
"""
121+
imports = pipreqs.get_all_imports(self.project_invalid, ignore_errors=True)
122+
self.assertEqual(len(imports), 0)
123+
117124
def test_get_imports_info(self):
118125
"""
119126
Test to see that the right number of packages were found on PyPI

0 commit comments

Comments
 (0)