Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/tools/notebook-status.sh
21430 views
1
#!/usr/bin/env bash
2
## Generate reports for failed notebooks.
3
## Present each languages failed notebooks in `/site/<lang>/FAILURES.md`.
4
## Collected from: https://storage.googleapis.com/tensorflow_docs/failed_notebooks/<lang>/failed_notebooks.txt
5
## Usage:
6
## $ ./tools/notebook-status.sh [-h]
7
8
usage() {
9
echo "Usage: $(basename $0) [options]"
10
echo " Generate reports for failed notebooks."
11
echo "Options:"
12
echo " -c Auto-create the git commit w/ status files"
13
echo " -h Print this help and exit"
14
}
15
16
while getopts "ch" opt; do
17
case $opt in
18
c) COMMIT_FLAG=1;;
19
h | *)
20
usage
21
exit 0
22
;;
23
esac
24
done
25
26
LOG_NAME="[$(basename $0)]"
27
TIMESTAMP=$(date '+%s')
28
REPO_ROOT="$(cd $(dirname ${BASH_SOURCE[0]}) >/dev/null 2>&1 && cd .. && pwd)"
29
TEMP_DIR=$(mktemp -d -t "$(basename $0 '.sh').XXXXX")
30
31
##
32
## CHECK SETUP
33
##
34
35
# Git status
36
if [[ -n "$COMMIT_FLAG" ]]; then
37
# Want a clean branch if making a commit.
38
if [[ -n $(git status -s) ]]; then
39
echo "${LOG_NAME} git status not clear, exiting." >&2
40
echo "Can run without committing, see help: $(basename $0) -h" >&2
41
exit 1
42
fi
43
44
# Create new branch if on master
45
if [[ $(git branch --show-current) == "master" ]]; then
46
branch_name="notebook-status-${TIMESTAMP}"
47
echo "${LOG_NAME} Create new branch for notebook status: ${branch_name}"
48
git checkout -b "${branch_name}"
49
fi
50
fi
51
52
##
53
## DOWNLOAD LOGS
54
##
55
56
readarray -t langs < <(find "$REPO_ROOT"/site -mindepth 1 -maxdepth 1 -type d \
57
| xargs -n1 basename \
58
| grep -v "en-snapshot" \
59
| sort)
60
61
# Entry values contain a list of failed notebook GitHub paths, separated by a newline.
62
declare -A LANG_FILES
63
declare -A LANG_MODIFIED
64
65
for lang in "${langs[@]}"; do
66
# Get file list. Save headers to parse for last-modified time.
67
files=$(curl --silent --fail --dump-header "$TEMP_DIR/headers-${lang}" \
68
"https://storage.googleapis.com/tensorflow_docs/failed_notebooks/${lang}/failed_notebooks.txt")
69
70
if [[ $? != 0 ]]; then
71
echo "${LOG_NAME} Unable to download log for '${lang}', skipping." >&2
72
continue
73
fi
74
75
# Parse header for time of last file update. (xargs to trim whitespace)
76
last_modified=$(cat "$TEMP_DIR/headers-${lang}" \
77
| grep -i "last-modified" \
78
| cut -d':' -f2- \
79
| xargs)
80
LANG_MODIFIED["$lang"]="$last_modified"
81
LANG_FILES["$lang"]="$files"
82
done
83
84
##
85
## STATUS REPORT
86
##
87
88
print_timestamp() {
89
local timestamp="$1"
90
local timestamp_str=""
91
local fmt_rfc7231="%a, %d %b %Y %H:%M:%S %Z"
92
if [[ $(uname) == "Darwin" ]]; then
93
timestamp_str=$(TZ=GMT date -r "$timestamp" +"$fmt_rfc7231") #BSD
94
else
95
timestamp_str=$(TZ=GMT date -d "@$timestamp" +"$fmt_rfc7231") # Linux
96
fi
97
echo "$timestamp_str"
98
}
99
100
TIMESTAMP_STR="$(print_timestamp $TIMESTAMP)"
101
102
COMMIT_MSG="Notebook status: ${TIMESTAMP_STR}\n\n
103
Update notebook failures.\n"
104
105
README_STR="__DO NOT EDIT__
106
107
## Notebook failures
108
109
The notebooks listed below did *not* run to completion. This usually indicates
110
that a translated file is out-of-date and not synced to its
111
[source counterpart](../en-snapshot/). Notebooks can be run in Colab and synced
112
using the [GitLocalize project](https://gitlocalize.com/tensorflow/docs-l10n).
113
114
*Notebooks are tested on a periodic basis (usually weekly or bi-weekly) so the
115
following list may not reflect recent updates.*
116
117
Updated: ${TIMESTAMP_STR}<br/>"
118
119
# GitLocalize may use a different path name.
120
declare -A LANG_GITLOCALIZE
121
LANG_GITLOCALIZE["es-419"]="es"
122
123
echo -n "${LOG_NAME} Create status report files ... "
124
125
for lang in "${!LANG_FILES[@]}"; do
126
STATUS_FILE="${REPO_ROOT}/site/${lang}/FAILURES.md"
127
echo -e "$README_STR" > "$STATUS_FILE"
128
echo -e "Last run: ${LANG_MODIFIED[$lang]}" >> "$STATUS_FILE"
129
130
# Get file list and remove empty lines.
131
readarray -t failed_notebooks < <(echo "${LANG_FILES[$lang]}" \
132
| grep -v '^$' \
133
| sort)
134
135
if [[ ${#failed_notebooks[@]} == 0 ]]; then
136
# No failures.
137
echo -e "\nAll <code>site/${lang}/</code> notebooks pass :)\n" >> "$STATUS_FILE"
138
continue
139
else
140
echo -e "\nFailures in <code>site/${lang}/</code>:\n" >> "$STATUS_FILE"
141
fi
142
143
if [[ -v LANG_GITLOCALIZE["$lang"] ]]; then
144
gitlocalize_lang=${LANG_GITLOCALIZE["$lang"]}
145
else
146
gitlocalize_lang="$lang"
147
fi
148
149
# Full path like: https://github.com/tensorflow/docs-l10n/blob/master/site/<lang>/guide/autodiff.ipynb
150
for github_path in "${failed_notebooks[@]}"; do
151
rel_path="${github_path#*site/${lang}/}"
152
gitlocalize_path="https://gitlocalize.com/repo/4592/${gitlocalize_lang}/site/en-snapshot/${rel_path}"
153
colab_path="https://colab.research.google.com/github/tensorflow/docs-l10n/blob/master/site/${lang}/${rel_path}"
154
155
# Create list entry.
156
li_entry="* [$rel_path]($github_path) · [[Sync in GitLocalize]($gitlocalize_path)] · [[Test in Colab]($colab_path)]"
157
echo "$li_entry" >> "$STATUS_FILE"
158
done
159
echo -n -e "\n" >> "$STATUS_FILE" # Add final newline.
160
done
161
162
echo "done."
163
164
##
165
## FINISH OPTIONS
166
##
167
168
# Commit change
169
if [[ -n "$COMMIT_FLAG" ]]; then
170
cd "$REPO_ROOT"
171
echo "${LOG_NAME} Create status commit ..."
172
git add "$REPO_ROOT/site/"
173
COMMIT_MSG=$(echo -e "$COMMIT_MSG")
174
git commit --message "$COMMIT_MSG"
175
fi
176
177
# Cleanup
178
rm -rf "$TEMP_DIR"
179
180