Skip to content

Commit

Permalink
Add a script to check various links
Browse files Browse the repository at this point in the history
  • Loading branch information
apicardgoog committed May 15, 2020
1 parent ae6b8d2 commit 9e0a0a7
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
111 changes: 111 additions & 0 deletions tests/verify_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#! /usr/bin/env python3

import argparse
import os
import re
import subprocess
import urllib.error
import urllib.request


SUCCESS_STRING = "\u001b[32m✓\u001b[0m"
FAIL_STRING = "\u001b[31m✗\u001b[0m"


def MoveToRoot():
while not os.path.exists('.git'):
os.chdir(os.pardir)
print("Found root at", os.getcwd())


def _VerifyHelper(f, verbose, prefix):
if f.startswith(prefix):
f = f[len(prefix):]
if os.path.exists(f):
if verbose:
print(SUCCESS_STRING, f)
else:
print(FAIL_STRING, f)
return 1
else:
print("Unexpected path:", f)
return 0


def VerifyFilePathMatch(f, verbose):
return _VerifyHelper(f, verbose, "step/")


def VerifyTeachMeMatch(f, verbose):
return _VerifyHelper(f.rstrip('`'), verbose, "~/step/")


def VerifyLinkMatch(url, verbose):
if not url.startswith("http"):
print("Ignoring path:", url)
return 0

if url == "https://translate.google.com/":
# Can't curl translate.google.com. Believe me, it's there.
return 0

url = url.replace("\\(", "(").replace("\\)", ")")

process = subprocess.run('curl -I "' + url + '"', shell=True, check=True,
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
universal_newlines=True)
code = int(process.stdout.split()[1])

#try:
# code = urllib.request.urlopen(url).getcode()
#except urllib.error.HTTPError as exception:
# code = exception.getcode()

if code in [200, 301, 302]:
if verbose:
print(SUCCESS_STRING, url)
else:
print(FAIL_STRING, code, url)
return 1
return 0


def ProcessMdFile(filepath, verbose):
errors = 0
print("Checking:", filepath)
with open(filepath) as f:
content = f.read()
m = re.findall('filePath=\"([^\"]+)\"', content)
for f in m:
errors += VerifyFilePathMatch(f, verbose)
m = re.findall('teachme\s+(.+)', content)
for f in m:
errors += VerifyTeachMeMatch(f, verbose)
m = re.findall(r'\[.*\]\(((?:\\\)|[^\)\s])+)\)', content)
for f in m:
errors += VerifyLinkMatch(f, verbose)
return errors



def ProcessAllMdFiles(verbose):
errors = 0
for (root,dirs,files) in os.walk('.', topdown=True):
for f in files:
if f.endswith(".md"):
errors += ProcessMdFile(os.path.join(root, f), verbose)
if errors:
print("Found", errors, "problematic link(s).")


def VerifyAllPaths(args):
MoveToRoot()
ProcessAllMdFiles(args.v)


if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Check the paths in this repo.')
parser.add_argument('-v', action='store_true',
help='print successful matches')
args = parser.parse_args()
VerifyAllPaths(args)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ tools designed for parsing and understanding **unstructured text**, also called
The Cloud Natural Language APIs use machine learning models that have already
been trained, so you can skip straight to the fun stuff. It's also possible to
train your own models (you can learn more about that
[here](https://cloud.google.com/automl/)), but this guide is going to stick with
[here](https://cloud.google.com/automl/), but this guide is going to stick with
the pre-trained models.

## Enable Cloud Natural Language API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ allows us to programmatically translate text.
The Cloud Translation APIs use machine learning models that have already been
trained, so you can skip straight to the fun stuff. It's also possible to train
your own models (you can learn more about that
[here](https://cloud.google.com/automl/)), but this guide is going to stick with
[here](https://cloud.google.com/automl/), but this guide is going to stick with
the pre-trained models.

## Enable Cloud Translation API
Expand Down Expand Up @@ -88,7 +88,7 @@ file.
```

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

## TranslationServlet
Expand Down

0 comments on commit 9e0a0a7

Please sign in to comment.