|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Single source of truth for addon *system* dependencies in CI. |
| 3 | +
|
| 4 | +Addons declare three dependency kinds in their ``.gpr.py``: |
| 5 | +
|
| 6 | +* ``requires_mod`` — importable Python modules. pip-installable; ci.yml already |
| 7 | + auto-derives these from the ``.gpr.py`` files. Nothing to do here. |
| 8 | +* ``requires_gi`` — GObject-introspection typelibs (e.g. ``GooCanvas``). |
| 9 | +* ``requires_exe`` — system executables (e.g. ``dot`` from graphviz). |
| 10 | +
|
| 11 | +The latter two are *system* packages: not pip-installable, named differently per |
| 12 | +platform, and Gramps' own ``Requirements`` only *checks* them (never installs). |
| 13 | +This module maps each declared ``requires_gi`` namespace / ``requires_exe`` name |
| 14 | +to its package on each CI platform and scans the addons for what they declare, |
| 15 | +so ci.yml derives the install list from one place instead of a hand-kept list. |
| 16 | +
|
| 17 | +Platform availability is asymmetric and encoded here: the GTK 3 addon libs |
| 18 | +(goocanvas, osm-gps-map, gexiv2) exist on Debian/apt but **not on conda-forge**, |
| 19 | +so the conda (Windows) lane cannot install them — addons needing them skip there |
| 20 | +by necessity. A ``conda`` value of ``None`` records that. |
| 21 | +
|
| 22 | +Pure stdlib so it runs anywhere in CI without bootstrapping. |
| 23 | +
|
| 24 | +CLI:: |
| 25 | +
|
| 26 | + addon_system_deps.py --platform apt # space-separated install list |
| 27 | + addon_system_deps.py --platform conda # (only packages available there) |
| 28 | + addon_system_deps.py --unmapped . # declared deps with no map entry; exit 1 if any |
| 29 | +""" |
| 30 | + |
| 31 | +# ------------------------ |
| 32 | +# Python modules |
| 33 | +# ------------------------ |
| 34 | +from __future__ import annotations |
| 35 | + |
| 36 | +import argparse |
| 37 | +import ast |
| 38 | +import glob |
| 39 | +import os |
| 40 | +import re |
| 41 | +import sys |
| 42 | + |
| 43 | +# --------------------------------------------------------------------------- |
| 44 | +# The map. Keys are what addons declare; values give the package per platform. |
| 45 | +# A None value means "no package provides this on that platform" (so it is not |
| 46 | +# installed there and an addon needing it is expected to skip). |
| 47 | +# --------------------------------------------------------------------------- |
| 48 | + |
| 49 | +# requires_gi namespace -> package providing the typelib, per platform. |
| 50 | +GI_PACKAGES: dict[str, dict[str, str | None]] = { |
| 51 | + "GExiv2": {"apt": "gir1.2-gexiv2-0.10", "conda": None}, |
| 52 | + "GooCanvas": {"apt": "gir1.2-goocanvas-2.0", "conda": None}, |
| 53 | + "OsmGpsMap": {"apt": "gir1.2-osmgpsmap-1.0", "conda": None}, |
| 54 | + # PlaceCoordinateGramplet declares GeocodeGlib 1.0, but modern distros ship |
| 55 | + # only the 2.0 typelib and conda-forge ships none; the addon has no tests. |
| 56 | + # Recorded so the drift-guard recognises the namespace; not installed. |
| 57 | + "GeocodeGlib": {"apt": None, "conda": None}, |
| 58 | +} |
| 59 | + |
| 60 | +# requires_exe executable -> package providing it, per platform. |
| 61 | +EXE_PACKAGES: dict[str, dict[str, str | None]] = { |
| 62 | + "dot": {"apt": "graphviz", "conda": "graphviz"}, |
| 63 | +} |
| 64 | + |
| 65 | +PLATFORMS = ("apt", "conda") |
| 66 | + |
| 67 | + |
| 68 | +# ------------------------------------------------------------ |
| 69 | +# |
| 70 | +# scanning |
| 71 | +# |
| 72 | +# ------------------------------------------------------------ |
| 73 | +_GI_RE = re.compile(r"requires_gi\s*=\s*(\[[^\]]*\])") |
| 74 | +_EXE_RE = re.compile(r"requires_exe\s*=\s*(\[[^\]]*\])") |
| 75 | + |
| 76 | + |
| 77 | +def _gpr_files(root: str) -> list[str]: |
| 78 | + return sorted(glob.glob(os.path.join(root, "*", "*.gpr.py"))) |
| 79 | + |
| 80 | + |
| 81 | +def _literal(src: str): |
| 82 | + try: |
| 83 | + return ast.literal_eval(src) |
| 84 | + except (ValueError, SyntaxError): |
| 85 | + return [] |
| 86 | + |
| 87 | + |
| 88 | +def _scan(root: str, pattern: re.Pattern, first_of_tuple: bool) -> set[str]: |
| 89 | + found: set[str] = set() |
| 90 | + for path in _gpr_files(root): |
| 91 | + try: |
| 92 | + text = open(path, encoding="utf-8").read() |
| 93 | + except OSError: |
| 94 | + continue |
| 95 | + for match in pattern.finditer(text): |
| 96 | + for entry in _literal(match.group(1)): |
| 97 | + if first_of_tuple and isinstance(entry, (tuple, list)): |
| 98 | + entry = entry[0] if entry else None |
| 99 | + if entry: |
| 100 | + found.add(entry) |
| 101 | + return found |
| 102 | + |
| 103 | + |
| 104 | +def scan_gi_namespaces(root: str) -> set[str]: |
| 105 | + return _scan(root, _GI_RE, first_of_tuple=True) |
| 106 | + |
| 107 | + |
| 108 | +def scan_executables(root: str) -> set[str]: |
| 109 | + return _scan(root, _EXE_RE, first_of_tuple=False) |
| 110 | + |
| 111 | + |
| 112 | +def addon_requirements(addon_dir: str) -> tuple[set[str], set[str]]: |
| 113 | + """Return (gi_namespaces, executables) declared by a single addon dir.""" |
| 114 | + gi: set[str] = set() |
| 115 | + exe: set[str] = set() |
| 116 | + for path in sorted(glob.glob(os.path.join(addon_dir, "*.gpr.py"))): |
| 117 | + try: |
| 118 | + text = open(path, encoding="utf-8").read() |
| 119 | + except OSError: |
| 120 | + continue |
| 121 | + for match in _GI_RE.finditer(text): |
| 122 | + for entry in _literal(match.group(1)): |
| 123 | + ns = entry[0] if isinstance(entry, (tuple, list)) else entry |
| 124 | + if ns: |
| 125 | + gi.add(ns) |
| 126 | + for match in _EXE_RE.finditer(text): |
| 127 | + for entry in _literal(match.group(1)): |
| 128 | + if entry: |
| 129 | + exe.add(entry) |
| 130 | + return gi, exe |
| 131 | + |
| 132 | + |
| 133 | +# ------------------------------------------------------------ |
| 134 | +# |
| 135 | +# derivation |
| 136 | +# |
| 137 | +# ------------------------------------------------------------ |
| 138 | +def packages(platform: str) -> list[str]: |
| 139 | + """All install-by-name packages available for a platform (full mapped set).""" |
| 140 | + pkgs: list[str] = [] |
| 141 | + for table in (GI_PACKAGES, EXE_PACKAGES): |
| 142 | + for entry in table.values(): |
| 143 | + pkg = entry.get(platform) |
| 144 | + if pkg: |
| 145 | + pkgs.append(pkg) |
| 146 | + return sorted(set(pkgs)) |
| 147 | + |
| 148 | + |
| 149 | +def unmapped(root: str) -> tuple[set[str], set[str]]: |
| 150 | + """Declared deps with no entry in the maps at all (drift).""" |
| 151 | + return ( |
| 152 | + scan_gi_namespaces(root) - set(GI_PACKAGES), |
| 153 | + scan_executables(root) - set(EXE_PACKAGES), |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +def addon_satisfiable_on(addon_dir: str, platform: str) -> bool: |
| 158 | + """ |
| 159 | + True if every system dep the addon declares has a package on this platform. |
| 160 | +
|
| 161 | + Used by the test runner to tell an *expected* platform skip (a declared dep |
| 162 | + that simply is not packaged here, e.g. goocanvas on conda) from a suspicious |
| 163 | + all-skip that should fail. |
| 164 | + """ |
| 165 | + gi, exe = addon_requirements(addon_dir) |
| 166 | + for ns in gi: |
| 167 | + entry = GI_PACKAGES.get(ns) |
| 168 | + if entry is None or entry.get(platform) is None: |
| 169 | + return False |
| 170 | + for name in exe: |
| 171 | + entry = EXE_PACKAGES.get(name) |
| 172 | + if entry is None or entry.get(platform) is None: |
| 173 | + return False |
| 174 | + return True |
| 175 | + |
| 176 | + |
| 177 | +# ------------------------------------------------------------ |
| 178 | +# |
| 179 | +# CLI |
| 180 | +# |
| 181 | +# ------------------------------------------------------------ |
| 182 | +def main(argv: list[str] | None = None) -> int: |
| 183 | + parser = argparse.ArgumentParser(description=__doc__) |
| 184 | + parser.add_argument("--platform", choices=PLATFORMS) |
| 185 | + parser.add_argument( |
| 186 | + "--unmapped", |
| 187 | + metavar="ROOT", |
| 188 | + help="print declared GI/exe deps with no map entry; exit 1 if any", |
| 189 | + ) |
| 190 | + args = parser.parse_args(argv) |
| 191 | + |
| 192 | + if args.unmapped is not None: |
| 193 | + gi, exe = unmapped(args.unmapped) |
| 194 | + for ns in sorted(gi): |
| 195 | + print(f"gi:{ns}") |
| 196 | + for name in sorted(exe): |
| 197 | + print(f"exe:{name}") |
| 198 | + return 1 if (gi or exe) else 0 |
| 199 | + |
| 200 | + if args.platform: |
| 201 | + print(" ".join(packages(args.platform))) |
| 202 | + return 0 |
| 203 | + |
| 204 | + parser.error("nothing to do: pass --platform or --unmapped") |
| 205 | + return 2 |
| 206 | + |
| 207 | + |
| 208 | +if __name__ == "__main__": |
| 209 | + sys.exit(main()) |
0 commit comments