Skip to content

Commit 9e0a0a7

Browse files
committed
Add a script to check various links
1 parent ae6b8d2 commit 9e0a0a7

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

tests/verify_paths.py

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#! /usr/bin/env python3
2+
3+
import argparse
4+
import os
5+
import re
6+
import subprocess
7+
import urllib.error
8+
import urllib.request
9+
10+
11+
SUCCESS_STRING = "\u001b[32m✓\u001b[0m"
12+
FAIL_STRING = "\u001b[31m✗\u001b[0m"
13+
14+
15+
def MoveToRoot():
16+
while not os.path.exists('.git'):
17+
os.chdir(os.pardir)
18+
print("Found root at", os.getcwd())
19+
20+
21+
def _VerifyHelper(f, verbose, prefix):
22+
if f.startswith(prefix):
23+
f = f[len(prefix):]
24+
if os.path.exists(f):
25+
if verbose:
26+
print(SUCCESS_STRING, f)
27+
else:
28+
print(FAIL_STRING, f)
29+
return 1
30+
else:
31+
print("Unexpected path:", f)
32+
return 0
33+
34+
35+
def VerifyFilePathMatch(f, verbose):
36+
return _VerifyHelper(f, verbose, "step/")
37+
38+
39+
def VerifyTeachMeMatch(f, verbose):
40+
return _VerifyHelper(f.rstrip('`'), verbose, "~/step/")
41+
42+
43+
def VerifyLinkMatch(url, verbose):
44+
if not url.startswith("http"):
45+
print("Ignoring path:", url)
46+
return 0
47+
48+
if url == "https://translate.google.com/":
49+
# Can't curl translate.google.com. Believe me, it's there.
50+
return 0
51+
52+
url = url.replace("\\(", "(").replace("\\)", ")")
53+
54+
process = subprocess.run('curl -I "' + url + '"', shell=True, check=True,
55+
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
56+
universal_newlines=True)
57+
code = int(process.stdout.split()[1])
58+
59+
#try:
60+
# code = urllib.request.urlopen(url).getcode()
61+
#except urllib.error.HTTPError as exception:
62+
# code = exception.getcode()
63+
64+
if code in [200, 301, 302]:
65+
if verbose:
66+
print(SUCCESS_STRING, url)
67+
else:
68+
print(FAIL_STRING, code, url)
69+
return 1
70+
return 0
71+
72+
73+
def ProcessMdFile(filepath, verbose):
74+
errors = 0
75+
print("Checking:", filepath)
76+
with open(filepath) as f:
77+
content = f.read()
78+
m = re.findall('filePath=\"([^\"]+)\"', content)
79+
for f in m:
80+
errors += VerifyFilePathMatch(f, verbose)
81+
m = re.findall('teachme\s+(.+)', content)
82+
for f in m:
83+
errors += VerifyTeachMeMatch(f, verbose)
84+
m = re.findall(r'\[.*\]\(((?:\\\)|[^\)\s])+)\)', content)
85+
for f in m:
86+
errors += VerifyLinkMatch(f, verbose)
87+
return errors
88+
89+
90+
91+
def ProcessAllMdFiles(verbose):
92+
errors = 0
93+
for (root,dirs,files) in os.walk('.', topdown=True):
94+
for f in files:
95+
if f.endswith(".md"):
96+
errors += ProcessMdFile(os.path.join(root, f), verbose)
97+
if errors:
98+
print("Found", errors, "problematic link(s).")
99+
100+
101+
def VerifyAllPaths(args):
102+
MoveToRoot()
103+
ProcessAllMdFiles(args.v)
104+
105+
106+
if __name__ == "__main__":
107+
parser = argparse.ArgumentParser(description='Check the paths in this repo.')
108+
parser.add_argument('-v', action='store_true',
109+
help='print successful matches')
110+
args = parser.parse_args()
111+
VerifyAllPaths(args)

walkthroughs/week-3-libraries/sentiment-analysis/sentiment-analysis-walkthrough.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ tools designed for parsing and understanding **unstructured text**, also called
4444
The Cloud Natural Language APIs use machine learning models that have already
4545
been trained, so you can skip straight to the fun stuff. It's also possible to
4646
train your own models (you can learn more about that
47-
[here](https://cloud.google.com/automl/)), but this guide is going to stick with
47+
[here](https://cloud.google.com/automl/), but this guide is going to stick with
4848
the pre-trained models.
4949

5050
## Enable Cloud Natural Language API

walkthroughs/week-3-libraries/translation/translation-walkthrough.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ allows us to programmatically translate text.
3636
The Cloud Translation APIs use machine learning models that have already been
3737
trained, so you can skip straight to the fun stuff. It's also possible to train
3838
your own models (you can learn more about that
39-
[here](https://cloud.google.com/automl/)), but this guide is going to stick with
39+
[here](https://cloud.google.com/automl/), but this guide is going to stick with
4040
the pre-trained models.
4141

4242
## Enable Cloud Translation API
@@ -88,7 +88,7 @@ file.
8888
```
8989

9090
You can read the documentation for the Java library
91-
[here](http://googleapis.github.io/google-cloud-java/google-cloud-clients/apidocs/com/google/cloud/translate/package-summary.html),
91+
[here](https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/translate/package-summary.html),
9292
and the next few steps work through an example.
9393

9494
## TranslationServlet

0 commit comments

Comments
 (0)