|
| 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) |
0 commit comments