Skip to content

Commit 9a04bc0

Browse files
lambertacopybara-github
authored andcommitted
Remove nbfmt.py --ignore_warn option.
Used for license check and this functionality scoped to the nblint.py command. PiperOrigin-RevId: 313474501
1 parent 3d50067 commit 9a04bc0

File tree

1 file changed

+8
-61
lines changed

1 file changed

+8
-61
lines changed

tools/nbfmt.py

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
Usage:
2222
$ nbfmt.py [options] notebook.ipynb [...]
23-
$ find . -name "*\.ipynb" | xargs ./tools/nbfmt.py [--ignore_warn]
23+
$ find . -name "*\.ipynb" | xargs ./tools/nbfmt.py
2424
2525
See the TensorFlow notebook template:
2626
https://github.com/tensorflow/docs/blob/master/tools/templates/notebook.ipynb
@@ -52,18 +52,13 @@
5252
"If a notebook is configured to clear outputs, this script will clear them "
5353
"when run.\n"
5454
"Colab respects this setting. Juptyer does not.")
55-
flags.DEFINE_bool("ignore_warn", False, "Overwrite notebook despite warnings.")
5655
flags.DEFINE_integer(
5756
"indent", 2, "Indention level for pretty-printed JSON.", lower_bound=0)
5857
flags.DEFINE_bool("test", False,
5958
"Test if the notebook is formatted (useful for CI).")
6059

6160
FLAGS = flags.FLAGS
6261

63-
_REQUIRED_REGEXPS = {
64-
"copyright": r"Copyright 20[1-9][0-9] The TensorFlow\s.*?\s?Authors",
65-
}
66-
6762

6863
def warn(msg: str) -> None:
6964
"""Print highlighted warning message to stderr.
@@ -166,62 +161,24 @@ def update_metadata(data: Dict[str, Any],
166161
data["metadata"] = metadata
167162

168163

169-
def has_license_and_update(data: Dict[str, Any]) -> bool:
170-
"""Check if license header exists anywhere in notebook and format.
164+
def update_license_cell(data: Dict[str, Any]) -> None:
165+
"""Format license cell to hide code pane from the Colab form.
171166
172167
Args:
173168
data: object representing a parsed JSON notebook.
174-
175-
Returns:
176-
Boolean: True if notebook contains the license header, False if it doesn't.
177169
"""
178-
has_license = False
179-
license_header = "#@title Licensed under the Apache License"
170+
# This pattern in Apache and MIT license boilerplate.
171+
license_re = re.compile(r"#@title.*License")
180172

181173
for idx, cell in enumerate(data["cells"]):
182174
src_text = "".join(cell["source"])
183175

184-
if license_header in src_text:
185-
has_license = True
176+
if license_re.search(src_text):
186177
# Hide code pane from license form
187178
metadata = cell.get("metadata", {})
188179
metadata["cellView"] = "form"
189180
data["cells"][idx]["metadata"] = metadata
190181

191-
if not has_license:
192-
warn(f"Missing license: {license_header}")
193-
194-
return has_license
195-
196-
197-
def has_required_regexps(data: Dict[str, Any]) -> bool:
198-
"""Check if all regexp patterns are found in a notebook.
199-
200-
Args:
201-
data: object representing a parsed JSON notebook.
202-
203-
Returns:
204-
Boolean: True if notebook contains all the patterns, False if it doesn't.
205-
"""
206-
has_all_patterns = True
207-
208-
for desc, pattern in _REQUIRED_REGEXPS.items():
209-
regexp = re.compile(pattern)
210-
has_pattern = False
211-
212-
for cell in data["cells"]:
213-
src_text = "".join(cell["source"])
214-
if regexp.search(src_text):
215-
has_pattern = True
216-
break # Found this match so skip the rest of the notebook.
217-
218-
if not has_pattern:
219-
warn(f"Missing {desc}: {pattern}")
220-
has_all_patterns = False
221-
return False
222-
223-
return has_all_patterns
224-
225182

226183
def main(argv):
227184
if len(argv) <= 1:
@@ -272,17 +229,7 @@ def main(argv):
272229

273230
clean_cells(data)
274231
update_metadata(data, filepath=fp)
275-
has_license = has_license_and_update(data)
276-
has_patterns = has_required_regexps(data)
277-
278-
if not FLAGS.ignore_warn:
279-
if not has_license or not has_patterns:
280-
print(
281-
" Found warnings. Notebook not written, skipping.",
282-
file=sys.stderr)
283-
found_error = True
284-
test_fail_notebooks.append(fp)
285-
continue
232+
update_license_cell(data)
286233

287234
nbjson = json.dumps(
288235
data, sort_keys=True, ensure_ascii=False, indent=FLAGS.indent)
@@ -303,7 +250,7 @@ def main(argv):
303250
error_template = textwrap.dedent("""
304251
[test] The following notebooks are not formatted:
305252
{notebooks}
306-
Format with: nbfmt.py --ignore_warn notebook.ipynb [...]
253+
Format with: nbfmt.py notebook.ipynb [...]
307254
""")
308255
notebooks = "\n".join([f"- {str(fp)}" for fp in test_fail_notebooks])
309256
print(error_template.format(notebooks=notebooks), file=sys.stderr)

0 commit comments

Comments
 (0)