|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate META.json against the PGXN Meta Specification v1. |
| 3 | +
|
| 4 | +PGXN rejects a distribution whose META.json does not satisfy the spec, and it |
| 5 | +does so at upload time, which is after a release has been tagged and published. |
| 6 | +That is an expensive place to discover a typo, so the constraints are checked |
| 7 | +here instead. |
| 8 | +
|
| 9 | +The rules mirror the v1 schemas at https://github.com/pgxn/meta. Note that a |
| 10 | +Tag and a Term are not the same: both exclude slash, backslash and control |
| 11 | +characters, but only a Term also excludes spaces, so "sql server" is a valid |
| 12 | +tag and an invalid term. |
| 13 | +
|
| 14 | +Run with `make metacheck`. |
| 15 | +""" |
| 16 | +import json |
| 17 | +import os |
| 18 | +import re |
| 19 | +import sys |
| 20 | +import unicodedata |
| 21 | + |
| 22 | +HERE = os.path.dirname(os.path.abspath(__file__)) |
| 23 | +META = os.environ.get("PLX_META", os.path.join(HERE, os.pardir, "META.json")) |
| 24 | + |
| 25 | +REQUIRED = ["name", "version", "abstract", "maintainer", "license", "provides", |
| 26 | + "meta-spec"] |
| 27 | +SEMVER = re.compile(r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)" |
| 28 | + r"(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?" |
| 29 | + r"(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$") |
| 30 | +RELEASE_STATUS = {"stable", "testing", "unstable"} |
| 31 | + |
| 32 | + |
| 33 | +def has_control(s): |
| 34 | + return any(unicodedata.category(c) == "Cc" for c in s) |
| 35 | + |
| 36 | + |
| 37 | +def check_term(value, label, errs): |
| 38 | + """A Term: 2 or more characters, no slash, backslash, space or control.""" |
| 39 | + if not isinstance(value, str) or len(value) < 2: |
| 40 | + errs.append("%s must be a string of at least two characters" % label) |
| 41 | + return |
| 42 | + for ch, name in (("/", "slash"), ("\\", "backslash")): |
| 43 | + if ch in value: |
| 44 | + errs.append("%s %r must not contain a %s" % (label, value, name)) |
| 45 | + if any(c.isspace() for c in value): |
| 46 | + errs.append("%s %r must not contain whitespace" % (label, value)) |
| 47 | + if has_control(value): |
| 48 | + errs.append("%s %r must not contain control characters" % (label, value)) |
| 49 | + |
| 50 | + |
| 51 | +def check_tag(value, label, errs): |
| 52 | + """A Tag: 2 to 255 characters, no slash, backslash or control. Spaces are |
| 53 | + allowed, which is the one way a Tag is looser than a Term.""" |
| 54 | + if not isinstance(value, str): |
| 55 | + errs.append("%s must be a string" % label) |
| 56 | + return |
| 57 | + if not 2 <= len(value) <= 255: |
| 58 | + errs.append("%s %r must be between 2 and 255 characters" % (label, value)) |
| 59 | + for ch, name in (("/", "slash"), ("\\", "backslash")): |
| 60 | + if ch in value: |
| 61 | + errs.append("%s %r must not contain a %s" % (label, value, name)) |
| 62 | + if has_control(value): |
| 63 | + errs.append("%s %r must not contain control characters" % (label, value)) |
| 64 | + |
| 65 | + |
| 66 | +def main(): |
| 67 | + try: |
| 68 | + with open(META, encoding="utf-8") as fh: |
| 69 | + m = json.load(fh) |
| 70 | + except (OSError, ValueError) as exc: |
| 71 | + sys.stderr.write("META.json could not be read as JSON: %s\n" % exc) |
| 72 | + return 2 |
| 73 | + |
| 74 | + errs = [] |
| 75 | + for field in REQUIRED: |
| 76 | + if field not in m: |
| 77 | + errs.append("required field %s is missing" % field) |
| 78 | + |
| 79 | + if "name" in m: |
| 80 | + check_term(m["name"], "name", errs) |
| 81 | + |
| 82 | + if "version" in m and not SEMVER.match(str(m["version"])): |
| 83 | + errs.append("version %r is not a semantic version" % m["version"]) |
| 84 | + |
| 85 | + if "maintainer" in m: |
| 86 | + mt = m["maintainer"] |
| 87 | + mt = [mt] if isinstance(mt, str) else mt |
| 88 | + if not isinstance(mt, list) or not mt: |
| 89 | + errs.append("maintainer must be a string or a non-empty list") |
| 90 | + elif not all(isinstance(x, str) and x.strip() for x in mt): |
| 91 | + errs.append("every maintainer must be a non-empty string") |
| 92 | + |
| 93 | + if "meta-spec" in m and "version" not in (m["meta-spec"] or {}): |
| 94 | + errs.append("meta-spec must carry a version") |
| 95 | + |
| 96 | + prov = m.get("provides") |
| 97 | + if not isinstance(prov, dict) or not prov: |
| 98 | + errs.append("provides must be a non-empty object") |
| 99 | + else: |
| 100 | + for name, ext in prov.items(): |
| 101 | + check_term(name, "provides key", errs) |
| 102 | + if not isinstance(ext, dict): |
| 103 | + errs.append("provides/%s must be an object" % name) |
| 104 | + continue |
| 105 | + if "file" not in ext: |
| 106 | + errs.append("provides/%s is missing file" % name) |
| 107 | + elif not os.path.exists(os.path.join(os.path.dirname(META), |
| 108 | + ext["file"])): |
| 109 | + errs.append("provides/%s names %s, which is not in the " |
| 110 | + "distribution" % (name, ext["file"])) |
| 111 | + if "version" not in ext: |
| 112 | + errs.append("provides/%s is missing version" % name) |
| 113 | + elif not SEMVER.match(str(ext["version"])): |
| 114 | + errs.append("provides/%s version %r is not a semantic version" |
| 115 | + % (name, ext["version"])) |
| 116 | + |
| 117 | + if "tags" in m: |
| 118 | + tags = m["tags"] |
| 119 | + if not isinstance(tags, list) or not tags: |
| 120 | + errs.append("tags must be a non-empty list") |
| 121 | + else: |
| 122 | + if len(set(tags)) != len(tags): |
| 123 | + errs.append("tags must be unique") |
| 124 | + for i, t in enumerate(tags): |
| 125 | + check_tag(t, "tags[%d]" % i, errs) |
| 126 | + |
| 127 | + if "release_status" in m and m["release_status"] not in RELEASE_STATUS: |
| 128 | + errs.append("release_status %r must be one of %s" |
| 129 | + % (m["release_status"], ", ".join(sorted(RELEASE_STATUS)))) |
| 130 | + |
| 131 | + if errs: |
| 132 | + print("META.json does not satisfy the PGXN Meta Spec v1:") |
| 133 | + for e in errs: |
| 134 | + print(" %s" % e) |
| 135 | + return 1 |
| 136 | + |
| 137 | + print("META.json satisfies the PGXN Meta Spec v1") |
| 138 | + print(" name %s" % m["name"]) |
| 139 | + print(" version %s (distribution)" % m["version"]) |
| 140 | + for name, ext in m["provides"].items(): |
| 141 | + print(" provides %s %s -> %s" |
| 142 | + % (name, ext["version"], ext["file"])) |
| 143 | + print(" tags %d, all valid" % len(m.get("tags", []))) |
| 144 | + return 0 |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + sys.exit(main()) |
0 commit comments