Compare commits

...
2 Commits
Author SHA1 Message Date
gericdongandGitHub d1a72f9d61 Merge branch 'main' into autoreview_6 2022-09-30 14:09:35 -04:00
Andrew Ferlitsch c3a85795e2 fix: error handling improvements 2022-09-30 17:47:02 +00:00
+26 -12
View File
@@ -37,6 +37,20 @@ if args.errors_codes:
if args.errors_csv:
args.errors = True
# Copyright cell
ERROR_COPYRIGHT = 0
# Links cell
ERROR_TITLE_HEADING = 1
ERROR_HEADING_CASE = 2
ERROR_HEADING_CAP = 3
ERROR_LINK_GIT_MISSING = 4
ERROR_LINK_COLAB_MISSING = 5
ERROR_LINK_WORKBENCH_MISSING = 6
ERROR_LINK_GIT_BAD = 7
ERROR_LINK_COLAB_BAD = 8
ERROR_LINK_WORKBENCH_BAD = 9
def parse_dir(directory):
entries = os.scandir(directory)
for entry in entries:
@@ -64,7 +78,7 @@ def parse_notebook(path):
nth = 0
cell, nth = get_cell(path, cells, nth)
if not 'Copyright' in cell['source'][0]:
report_error(path, 0, "missing copyright cell")
report_error(path, ERROR_COPYRIGHT, "missing copyright cell")
# check for notices
cell, nth = get_cell(path, cells, nth)
@@ -73,7 +87,7 @@ def parse_notebook(path):
# cell 2 is title and links
if not cell['source'][0].startswith('# '):
report_error(path, 1, "title cell must start with H1 heading")
report_error(path, ERROR_TITLE_HEADING, "title cell must start with H1 heading")
title = ''
else:
title = cell['source'][0][2:].strip()
@@ -95,30 +109,28 @@ def parse_notebook(path):
try:
code = urllib.request.urlopen(git_link).getcode()
except Exception as e:
report_error(path, 7, f"bad GitHub link: {link}")
report_error(path, ERROR_LINK_GIT_BAD, f"bad GitHub link: {git_link}")
if '<a href="https://colab.research.google.com/' in line:
colab_link = 'https://github.com/' + line.strip()[50:-2].replace('" target="_blank', '')
try:
code = urllib.request.urlopen(colab_link).getcode()
except Exception as e:
report_error(path, 8, f"bad Colab link: {link}")
report_error(path, ERROR_LINK_COLAB_BAD, f"bad Colab link: {colab_link}")
if '<a href="https://console.cloud.google.com/vertex-ai/workbench/' in line:
workbench_link = line.strip()[91:-2].replace('" target="_blank', '')
try:
code = urllib.request.urlopen(workbench_link).getcode()
except Exception as e:
report_error(path, 9, f"bad Workbench link: {link}")
report_error(path, ERROR_LINK_WORKBENCH_BAD, f"bad Workbench link: {workbench_link}")
if 'View on GitHub' not in source or not git_link:
report_error(path, 4, 'Missing link for GitHub')
report_error(path, ERROR_LINK_GIT_MISSING, 'Missing link for GitHub')
if 'Run in Colab' not in source or not colab_link:
report_error(path, 4, 'Missing link for Colab') # needs new error number
report_error(path, ERROR_LINK_COLAB_MISSING, 'Missing link for Colab')
if 'Open in Vertex AI Workbench' not in source or not workbench_link:
report_error(path, 5, 'Missing link for Workbench')
if 'master' in source:
report_error(path, 6, 'Outdated branch (master) used in link')
report_error(path, ERROR_LINK_WORKBENCH_MISSING, 'Missing link for Workbench')
# Overview
cell, nth = get_cell(path, cells, nth)
@@ -328,7 +340,7 @@ def check_text_cell(path, cell):
def check_sentence_case(path, heading):
words = heading.split(' ')
if not words[0][0].isupper():
report_error(path, 2, f"heading must start with capitalized word: {words[0]}")
report_error(path, ERROR_HEADING_CAP, f"heading must start with capitalized word: {words[0]}")
for word in words[1:]:
word = word.replace(':', '').replace('(', '').replace(')', '')
@@ -336,7 +348,7 @@ def check_sentence_case(path, heading):
'VM', 'CPR', 'NVIDIA', 'ID', 'DASK', 'ARIMA_PLUS', 'KFP', 'I/O']:
continue
if word.isupper():
report_error(path, 3, f"heading is not sentence case: {word}")
report_error(path, ERROR_HEADING_CASE, f"heading is not sentence case: {word}")
def report_error(notebook, code, msg):
@@ -460,11 +472,13 @@ if args.notebook_dir:
if not os.path.isdir(args.notebook_dir):
print("Error: not a directory:", args.notebook_dir)
exit(1)
tag = ''
parse_dir(args.notebook_dir)
elif args.notebook:
if not os.path.isfile(args.notebook):
print("Error: not a notebook:", args.notebook)
exit(1)
tag = ''
parse_notebook(args.notebook)
elif args.notebook_file:
if not os.path.isfile(args.notebook_file):