20
20
21
21
Usage:
22
22
$ nbfmt.py [options] notebook.ipynb [...]
23
- $ find . -name "*\.ipynb" | xargs ./tools/nbfmt.py [--ignore_warn]
23
+ $ find . -name "*\.ipynb" | xargs ./tools/nbfmt.py
24
24
25
25
See the TensorFlow notebook template:
26
26
https://github.com/tensorflow/docs/blob/master/tools/templates/notebook.ipynb
52
52
"If a notebook is configured to clear outputs, this script will clear them "
53
53
"when run.\n "
54
54
"Colab respects this setting. Juptyer does not." )
55
- flags .DEFINE_bool ("ignore_warn" , False , "Overwrite notebook despite warnings." )
56
55
flags .DEFINE_integer (
57
56
"indent" , 2 , "Indention level for pretty-printed JSON." , lower_bound = 0 )
58
57
flags .DEFINE_bool ("test" , False ,
59
58
"Test if the notebook is formatted (useful for CI)." )
60
59
61
60
FLAGS = flags .FLAGS
62
61
63
- _REQUIRED_REGEXPS = {
64
- "copyright" : r"Copyright 20[1-9][0-9] The TensorFlow\s.*?\s?Authors" ,
65
- }
66
-
67
62
68
63
def warn (msg : str ) -> None :
69
64
"""Print highlighted warning message to stderr.
@@ -166,62 +161,24 @@ def update_metadata(data: Dict[str, Any],
166
161
data ["metadata" ] = metadata
167
162
168
163
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 .
171
166
172
167
Args:
173
168
data: object representing a parsed JSON notebook.
174
-
175
- Returns:
176
- Boolean: True if notebook contains the license header, False if it doesn't.
177
169
"""
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")
180
172
181
173
for idx , cell in enumerate (data ["cells" ]):
182
174
src_text = "" .join (cell ["source" ])
183
175
184
- if license_header in src_text :
185
- has_license = True
176
+ if license_re .search (src_text ):
186
177
# Hide code pane from license form
187
178
metadata = cell .get ("metadata" , {})
188
179
metadata ["cellView" ] = "form"
189
180
data ["cells" ][idx ]["metadata" ] = metadata
190
181
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
-
225
182
226
183
def main (argv ):
227
184
if len (argv ) <= 1 :
@@ -272,17 +229,7 @@ def main(argv):
272
229
273
230
clean_cells (data )
274
231
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 )
286
233
287
234
nbjson = json .dumps (
288
235
data , sort_keys = True , ensure_ascii = False , indent = FLAGS .indent )
@@ -303,7 +250,7 @@ def main(argv):
303
250
error_template = textwrap .dedent ("""
304
251
[test] The following notebooks are not formatted:
305
252
{notebooks}
306
- Format with: nbfmt.py --ignore_warn notebook.ipynb [...]
253
+ Format with: nbfmt.py notebook.ipynb [...]
307
254
""" )
308
255
notebooks = "\n " .join ([f"- { str (fp )} " for fp in test_fail_notebooks ])
309
256
print (error_template .format (notebooks = notebooks ), file = sys .stderr )
0 commit comments