|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | +print("Beginning Platform Metapackage Test", file=sys.stderr) |
| 9 | + |
| 10 | + |
| 11 | +def _run(*cmd): |
| 12 | + """Run a command and return its stdout as a string (stderr suppressed).""" |
| 13 | + return subprocess.run( |
| 14 | + cmd, |
| 15 | + stdout=subprocess.PIPE, |
| 16 | + stderr=subprocess.DEVNULL, |
| 17 | + text=True, |
| 18 | + ).stdout |
| 19 | + |
| 20 | + |
| 21 | +def _dpkg_query_status(package): |
| 22 | + """Return the dpkg Status string for *package* (empty string on error).""" |
| 23 | + return subprocess.run( |
| 24 | + ["dpkg-query", "-W", "-f=${Status}\n", package], |
| 25 | + stdout=subprocess.PIPE, |
| 26 | + stderr=subprocess.PIPE, |
| 27 | + text=True, |
| 28 | + ).stdout |
| 29 | + |
| 30 | + |
| 31 | +def _apt_cache_modaliases(package): |
| 32 | + """Return the Modaliases line from apt-cache show output, or ''.""" |
| 33 | + output = _run("apt-cache", "show", package) |
| 34 | + for line in output.splitlines(): |
| 35 | + if line.startswith("Modaliases"): |
| 36 | + return line |
| 37 | + return "" |
| 38 | + |
| 39 | + |
| 40 | +def _is_installed(package): |
| 41 | + return "install ok installed" in _dpkg_query_status(package) |
| 42 | + |
| 43 | + |
| 44 | +# --------------------------------------------------------------------------- |
| 45 | +# DMI / OS helpers |
| 46 | +# --------------------------------------------------------------------------- |
| 47 | + |
| 48 | + |
| 49 | +def _read_dmi(field): |
| 50 | + with open(f"/sys/devices/virtual/dmi/id/{field}") as f: |
| 51 | + return f.read().strip() |
| 52 | + |
| 53 | + |
| 54 | +def _ubuntu_codename(): |
| 55 | + return subprocess.run( |
| 56 | + ["lsb_release", "-cs"], |
| 57 | + stdout=subprocess.PIPE, |
| 58 | + stderr=subprocess.DEVNULL, |
| 59 | + text=True, |
| 60 | + ).stdout.strip() |
| 61 | + |
| 62 | + |
| 63 | +def _ubuntu_drivers_list(): |
| 64 | + """Return the list of OEM meta packages from ubuntu-drivers.""" |
| 65 | + output = _run("ubuntu-drivers", "list") |
| 66 | + return [ |
| 67 | + pkg |
| 68 | + for pkg in output.splitlines() |
| 69 | + if pkg.startswith("oem") and pkg.endswith("meta") |
| 70 | + ] |
| 71 | + |
| 72 | + |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | +# OEM meta check |
| 75 | +# --------------------------------------------------------------------------- |
| 76 | + |
| 77 | +_OEM_CONFIGS = { |
| 78 | + "somerville": { |
| 79 | + "dmi_field": "product_sku", |
| 80 | + "biosid_fn": lambda raw: raw, |
| 81 | + "pattern_fn": lambda biosid: rf"sv00001028sd0000{re.escape(biosid)}", |
| 82 | + "factory_prefix": "oem-somerville", |
| 83 | + }, |
| 84 | + "stella": { |
| 85 | + "dmi_field": "board_name", |
| 86 | + "biosid_fn": lambda raw: raw, |
| 87 | + "pattern_fn": lambda biosid: rf"sv0000103csd0000{re.escape(biosid)}", |
| 88 | + "factory_prefix": "oem-stella", |
| 89 | + }, |
| 90 | + "sutton": { |
| 91 | + "dmi_field": "bios_version", |
| 92 | + "biosid_fn": lambda raw: raw[:3], |
| 93 | + "pattern_fn": lambda biosid: rf"bvr{re.escape(biosid)}", |
| 94 | + "factory_prefix": "oem-sutton", |
| 95 | + }, |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +def check_oem_meta(oem): |
| 100 | + cfg = _OEM_CONFIGS[oem] |
| 101 | + raw = _read_dmi(cfg["dmi_field"]) |
| 102 | + biosid = cfg["biosid_fn"](raw) |
| 103 | + codename = _ubuntu_codename() |
| 104 | + |
| 105 | + if codename in ("jammy", "noble"): |
| 106 | + for meta in _ubuntu_drivers_list(): |
| 107 | + modaliases = _apt_cache_modaliases(meta) |
| 108 | + if not re.search( |
| 109 | + cfg["pattern_fn"](biosid), modaliases, re.IGNORECASE |
| 110 | + ): |
| 111 | + continue |
| 112 | + if not _is_installed(meta): |
| 113 | + continue |
| 114 | + factory = meta.replace( |
| 115 | + cfg["factory_prefix"], cfg["factory_prefix"] + "-factory", 1 |
| 116 | + ) |
| 117 | + if not _is_installed(factory): |
| 118 | + print( |
| 119 | + f"Factory meta package '{factory}' is not installed!!!", |
| 120 | + file=sys.stderr, |
| 121 | + ) |
| 122 | + continue |
| 123 | + print( |
| 124 | + f"Found the platform meta package '{meta}' containing " |
| 125 | + f"BIOS ID '{biosid}' and the platform factory meta " |
| 126 | + f"package '{factory}'" |
| 127 | + ) |
| 128 | + sys.exit(0) |
| 129 | + print(f"BIOS ID: {biosid}", file=sys.stderr) |
| 130 | + print("Meta package not installed", file=sys.stderr) |
| 131 | + sys.exit(1) |
| 132 | + |
| 133 | + print(f"{codename} is not supported yet.", file=sys.stderr) |
| 134 | + print(f"{sys.argv[0]} failed!", file=sys.stderr) |
| 135 | + sys.exit(1) |
| 136 | + |
| 137 | + |
| 138 | +# --------------------------------------------------------------------------- |
| 139 | +# Entry point |
| 140 | +# --------------------------------------------------------------------------- |
| 141 | + |
| 142 | + |
| 143 | +def main(): |
| 144 | + parser = argparse.ArgumentParser( |
| 145 | + prog=sys.argv[0], |
| 146 | + description="Platform metapackage sanity test", |
| 147 | + add_help=True, |
| 148 | + ) |
| 149 | + parser.add_argument( |
| 150 | + "--oem-codename", |
| 151 | + choices=_OEM_CONFIGS.keys(), |
| 152 | + required=True, |
| 153 | + metavar="CODENAME", |
| 154 | + help="OEM codename: %(choices)s", |
| 155 | + ) |
| 156 | + args = parser.parse_args() |
| 157 | + |
| 158 | + check_oem_meta(args.oem_codename) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + main() |
0 commit comments