forked from NVIDIA-Merlin/HugeCTR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_for_broken_links.sh
executable file
·50 lines (46 loc) · 1.47 KB
/
check_for_broken_links.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
DOCS_DIR=$(dirname "${BASH_SOURCE[0]}")
FALSE_POSITIVES_JSON="${DOCS_DIR}/false_positives.json"
LINKCHECK_JSON="${DOCS_DIR}/build/linkcheck/output.json"
function check_environment {
local err=0
if ! [ -x "$(command -v jq)" ]; then
>&2 echo "jq is required but is not found."
((err++))
fi
if [ ! -f "${FALSE_POSITIVES_JSON}" ]; then
>&2 echo "A JSON file with false positives is required: ${FALSE_POSITIVES_JSON}"
((err++))
fi
if [ ! -f "${LINKCHECK_JSON}" ]; then
>&2 echo "Did not find linkcheck output JSON file: ${LINKCHECK_JSON}."
>&2 echo "Run Sphinx with the linkcheck arg: make -C docs clean linkcheck"
((err++))
fi
if [ "${err}" -gt 0 ]; then
exit 2
fi
}
function check_links {
local err=0
# If you know how to prevent the hack with using jq twice, lmk.
broken=$(jq 'select(.status == "broken")' "${LINKCHECK_JSON}" | jq -s)
count=$(echo "${broken}" | jq 'length')
for i in $(seq 0 $(($count - 1)))
do
entry=$(echo "${broken}" | jq ".[${i}]")
link=$(echo "${entry}" | jq -r '.uri')
[ -n "${DEBUG}" ] && {
echo >&2 "Checking for false positive: ${link}"
}
local resp; resp=$(jq --arg check "${link}" -s 'any(.uri == $check)' < "${FALSE_POSITIVES_JSON}")
# "false" indicates that the URL did not match any of the URIs in the false positive file.
if [ "false" = "${resp}" ]; then
((err++))
echo "${entry}"
fi
done
exit "${err}"
}
check_environment
check_links