Add ignore-notebooks parameter

This commit is contained in:
fernandocrz 2023-10-29 16:41:09 -03:00
parent f4db52c5c0
commit 7033f3824d
2 changed files with 32 additions and 5 deletions

View File

@ -35,6 +35,7 @@ Options:
<compat> | e.g. Flask~=1.1.2
<gt> | e.g. Flask>=1.1.2
<no-pin> | e.g. Flask
--ignore-notebooks Ignore jupyter notebook files.
"""
from contextlib import contextmanager
import os
@ -50,6 +51,7 @@ from yarg.exceptions import HTTPError
try:
PythonExporter = None
ignore_notebooks = False
from nbconvert import PythonExporter
except ImportError:
pass
@ -61,6 +63,7 @@ REGEXP = [
re.compile(r"^from ((?!\.+).*?) import (?:.*)$"),
]
@contextmanager
def _open(filename=None, mode="r"):
"""Open a file or ``sys.stdout`` depending on the provided filename.
@ -119,7 +122,7 @@ def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links
dirs[:] = [d for d in dirs if d not in ignore_dirs]
candidates.append(os.path.basename(root))
if PythonExporter:
if PythonExporter and not ignore_notebooks:
files = [fn for fn in files if filter_ext(fn, [".py", ".ipynb"])]
else:
files = [fn for fn in files if filter_ext(fn, [".py"])]
@ -137,7 +140,7 @@ def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links
if filter_ext(file_name, [".py"]):
with open(file_name, "r", encoding=encoding) as f:
contents = f.read()
elif filter_ext(file_name, [".ipynb"]) and PythonExporter:
elif filter_ext(file_name, [".ipynb"]) and PythonExporter and not ignore_notebooks:
contents = ipynb_2_py(file_name, encoding=encoding)
try:
tree = ast.parse(contents)
@ -154,7 +157,7 @@ def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links
continue
else:
logging.error("Failed on file: %s" % file_name)
if filter_ext(file_name, [".ipynb"]) and PythonExporter:
if filter_ext(file_name, [".ipynb"]) and PythonExporter and not ignore_notebooks:
logging.error("Magic command without % might be failed")
raise exc
@ -484,11 +487,13 @@ def dynamic_versioning(scheme, imports):
def init(args):
global ignore_notebooks
encoding = args.get("--encoding")
extra_ignore_dirs = args.get("--ignore")
follow_links = not args.get("--no-follow-links")
ignore_notebooks = args.get("--ignore-notebooks")
input_path = args["<path>"]
if encoding is None:
encoding = "utf-8"
if input_path is None:

View File

@ -81,9 +81,9 @@ class TestPipreqs(unittest.TestCase):
"original": os.path.join(os.path.dirname(__file__), "_data/test.py"),
"notebook": os.path.join(os.path.dirname(__file__), "_data_notebook/test.ipynb"),
}
self.requirements_notebook_path = os.path.join(self.project_with_notebooks, "requirements.txt")
self.non_existing_filepath = "xpto"
def test_get_all_imports(self):
imports = pipreqs.get_all_imports(self.project)
self.assertEqual(len(imports), 15)
@ -603,6 +603,28 @@ class TestPipreqs(unittest.TestCase):
self.assertEqual(printed_text, "File xpto was not found. Please, fix it and run again.")
def test_ignore_notebooks(self):
"""
Test the --ignore-notebooks parameter
"""
pipreqs.init(
{
"<path>": self.project_with_notebooks,
"--savepath": None,
"--use-local": None,
"--force": True,
"--proxy": None,
"--pypi-server": None,
"--print": False,
"--diff": None,
"--clean": None,
"--mode": None,
"--ignore-notebooks": True,
}
)
assert os.path.exists(self.requirements_notebook_path) == 1
assert os.path.getsize(self.requirements_notebook_path) <= 1
def tearDown(self):
"""
Remove requiremnts.txt files that were written