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

View File

@ -81,9 +81,9 @@ class TestPipreqs(unittest.TestCase):
"original": os.path.join(os.path.dirname(__file__), "_data/test.py"), "original": os.path.join(os.path.dirname(__file__), "_data/test.py"),
"notebook": os.path.join(os.path.dirname(__file__), "_data_notebook/test.ipynb"), "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" self.non_existing_filepath = "xpto"
def test_get_all_imports(self): def test_get_all_imports(self):
imports = pipreqs.get_all_imports(self.project) imports = pipreqs.get_all_imports(self.project)
self.assertEqual(len(imports), 15) 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.") 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): def tearDown(self):
""" """
Remove requiremnts.txt files that were written Remove requiremnts.txt files that were written