|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import json |
| 4 | +from termcolor import cprint |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +import requests |
| 8 | + |
| 9 | +REQUIRED_KEYS = {'name', 'maintainer', 'stable', 'home_url', 'repo_url', 'coordinated'} |
| 10 | + |
| 11 | +OPTIONAL_KEYS = {'provisional', 'stable', 'pypi_name', 'image', 'review', 'description'} |
| 12 | + |
| 13 | +ALL_KEYS = REQUIRED_KEYS | OPTIONAL_KEYS |
| 14 | + |
| 15 | +REVIEW_KEYS = {'functionality', 'ecointegration', 'documentation', 'testing', |
| 16 | + 'devstatus', 'python3', 'last-updated'} |
| 17 | + |
| 18 | +REVIEW_FUNCTIONALITY = {'Specialized package', 'General package'} |
| 19 | +REVIEW_DEVSTATUS = {'Unmaintained', 'Functional but low activity', |
| 20 | + 'Functional but unmaintained', 'Heavy development', 'Good'} |
| 21 | +REVIEW_PYTHON3 = {'No', 'Yes'} |
| 22 | +REVIEW_GENERIC = {'Needs work', 'Partial', 'Good'} |
| 23 | + |
| 24 | + |
| 25 | +jsonfn = os.path.join(os.path.dirname(__file__), "registry.json") |
| 26 | +try: |
| 27 | + registry = json.load(open(jsonfn)) |
| 28 | +except json.decoder.JSONDecodeError as e: |
| 29 | + cprint(jsonfn + ' : ' + e.args[0], color='red') |
| 30 | + cprint("*** JSON file appears to be malformed - see above ***", color='red') |
| 31 | + sys.exit(2) |
| 32 | + |
| 33 | +error = 0 |
| 34 | + |
| 35 | +for package in registry['packages']: |
| 36 | + |
| 37 | + if 'name' in package: |
| 38 | + name = package['name'] |
| 39 | + else: |
| 40 | + cprint("ERROR: Missing package name: {0}".format(package), file=sys.stderr, color='red') |
| 41 | + error += 1 |
| 42 | + continue |
| 43 | + |
| 44 | + cprint("Checking {0}".format(name), color='blue') |
| 45 | + |
| 46 | + print(" - verifying keys") |
| 47 | + |
| 48 | + difference = set(package.keys()) - (REQUIRED_KEYS | OPTIONAL_KEYS) |
| 49 | + if difference: |
| 50 | + cprint(f" ERROR: Unrecognized key(s) for {name}: {difference}. " |
| 51 | + f"Valid options are {', '.join(ALL_KEYS)}", file=sys.stderr, color='red') |
| 52 | + error += 1 |
| 53 | + |
| 54 | + difference = REQUIRED_KEYS - set(package.keys()) |
| 55 | + if difference: |
| 56 | + cprint(f" ERROR: Missing key(s) for {name}: {difference}", file=sys.stderr, color='red') |
| 57 | + error += 1 |
| 58 | + |
| 59 | + # Check that URLs work |
| 60 | + |
| 61 | + print(" - verifying URLs") |
| 62 | + |
| 63 | + try: |
| 64 | + r = requests.get(package['home_url']) |
| 65 | + assert r.ok |
| 66 | + except Exception: |
| 67 | + cprint(f" ERROR: Home URL for {name} - {package['home_url']} - did not work", file=sys.stderr, color='red') |
| 68 | + error += 1 |
| 69 | + |
| 70 | + try: |
| 71 | + r = requests.get(package['repo_url']) |
| 72 | + assert r.ok |
| 73 | + except Exception: |
| 74 | + cprint(f" ERROR: Repository URL for {name} - {package['repo_url']} - did not work", file=sys.stderr, color='red') |
| 75 | + error += 1 |
| 76 | + |
| 77 | + if package.get('pypi_name'): |
| 78 | + |
| 79 | + print(" - verifying PyPI name") |
| 80 | + |
| 81 | + r = requests.get(f"https://pypi.python.org/pypi/{package['pypi_name']}/json") |
| 82 | + if not r.ok: |
| 83 | + cprint(f" ERROR: PyPI package {package['pypi_name']} doesn't appear to exist", file=sys.stderr, color='red') |
| 84 | + error += 1 |
| 85 | + |
| 86 | + if package.get('review'): |
| 87 | + |
| 88 | + print(" - verifying review") |
| 89 | + |
| 90 | + review = package['review'] |
| 91 | + |
| 92 | + difference = set(review.keys()) - REVIEW_KEYS |
| 93 | + if difference: |
| 94 | + cprint(f" ERROR: Unrecognized review key(s) for {name}: {difference}. " |
| 95 | + f"Valid options are {', '.join(REVIEW_KEYS)}", file=sys.stderr, color='red') |
| 96 | + error += 1 |
| 97 | + |
| 98 | + difference = REVIEW_KEYS - set(review.keys()) |
| 99 | + if difference: |
| 100 | + cprint(f" ERROR: Missing review key(s) for {name}: {difference}", file=sys.stderr, color='red') |
| 101 | + error += 1 |
| 102 | + |
| 103 | + for key, value in review.items(): |
| 104 | + |
| 105 | + if key == 'functionality': |
| 106 | + if value not in REVIEW_FUNCTIONALITY: |
| 107 | + cprint(f" ERROR: Invalid functionality in review for {name}: '{value}'. " |
| 108 | + f"Valid options are {', '.join(REVIEW_FUNCTIONALITY)}", file=sys.stderr, color='red') |
| 109 | + error += 1 |
| 110 | + elif key == 'last-updated': |
| 111 | + try: |
| 112 | + dt = datetime.strptime(value, "%Y-%m-%d") |
| 113 | + except Exception: |
| 114 | + cprint(f" ERROR: Could not parse date: '{value}'", file=sys.stderr, color='red') |
| 115 | + error += 1 |
| 116 | + elif key == 'devstatus': |
| 117 | + if value not in REVIEW_DEVSTATUS: |
| 118 | + cprint(f" ERROR: Invalid devstatus in review for {name}: '{value}'. " |
| 119 | + f"Valid options are {', '.join(REVIEW_DEVSTATUS)}", file=sys.stderr, color='red') |
| 120 | + error += 1 |
| 121 | + elif key == 'python3': |
| 122 | + if value not in REVIEW_PYTHON3: |
| 123 | + cprint(f" ERROR: Invalid python3 in review for {name}: '{value}'. " |
| 124 | + f"Valid options are {', '.join(REVIEW_PYTHON3)}", file=sys.stderr, color='red') |
| 125 | + error += 1 |
| 126 | + else: |
| 127 | + if value not in REVIEW_GENERIC: |
| 128 | + cprint(f" ERROR: Invalid {key} in review for {name}: '{value}'. " |
| 129 | + f"Valid options are {', '.join(REVIEW_GENERIC)}", file=sys.stderr, color='red') |
| 130 | + error += 1 |
| 131 | + |
| 132 | +if error > 0: |
| 133 | + sornot = 's' if error > 1 else '' |
| 134 | + cprint(f"** {error} error{sornot} occurred - see above for details **", file=sys.stderr, color='red') |
| 135 | + sys.exit(1) |
0 commit comments