|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to generate auto publication workflows for given specs |
| 4 | +""" |
| 5 | + |
| 6 | +import re |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +# ----------------------------------------------------------------------------- |
| 10 | +# List of properties that can appear to describe a spec. |
| 11 | +# ----------------------------------------------------------------------------- |
| 12 | + |
| 13 | +PROPERTIES = [ |
| 14 | + "shortname", |
| 15 | + "publicationStatus", |
| 16 | + "tokenName", |
| 17 | +] |
| 18 | + |
| 19 | +# ----------------------------------------------------------------------------- |
| 20 | +# List of specs for which an auto-publish script needs to be created. |
| 21 | +# ----------------------------------------------------------------------------- |
| 22 | + |
| 23 | +SPECS = [ |
| 24 | + { |
| 25 | + "shortname": "css-color-4", |
| 26 | + "publicationStatus": "CRD", |
| 27 | + }, |
| 28 | + { |
| 29 | + "shortname": "css-color-5", |
| 30 | + "publicationStatus": "WD", |
| 31 | + }, |
| 32 | + { |
| 33 | + "shortname": "css-fonts-4", |
| 34 | + "publicationStatus": "WD", |
| 35 | + }, |
| 36 | +] |
| 37 | + |
| 38 | +# ----------------------------------------------------------------------------- |
| 39 | +# Main |
| 40 | +# ----------------------------------------------------------------------------- |
| 41 | + |
| 42 | +def main(): |
| 43 | + script_path = Path(__file__).parent |
| 44 | + template_path = script_path / "auto-publish-template.yml" |
| 45 | + workflows_dir = script_path / "workflows" |
| 46 | + |
| 47 | + workflows_dir.mkdir(parents=True, exist_ok=True) |
| 48 | + |
| 49 | + template = template_path.read_text(encoding="utf-8") |
| 50 | + |
| 51 | + # Replace the large comment header block (#####...#####) |
| 52 | + template = re.sub( |
| 53 | + r"#{5,}.*?#{5,}", |
| 54 | + """###################################################################### |
| 55 | +# IMPORTANT: Do not edit this file directly! |
| 56 | +# |
| 57 | +# This workflow was automatically generated through the |
| 58 | +# generate-auto-publish-workflows.py script. To update the workflow, |
| 59 | +# make changes to the template file and run the script again. |
| 60 | +######################################################################""", |
| 61 | + template, |
| 62 | + flags=re.S, |
| 63 | + ) |
| 64 | + |
| 65 | + for spec in SPECS: |
| 66 | + spec = spec.copy() |
| 67 | + |
| 68 | + # Derive shortname from source if not provided |
| 69 | + if "shortname" not in spec or not spec["shortname"]: |
| 70 | + spec["shortname"] = spec["source"].split(".")[0].replace("_", "-") |
| 71 | + |
| 72 | + # Derive tokenName if not provided |
| 73 | + if "tokenName" not in spec or not spec["tokenName"]: |
| 74 | + token = ( |
| 75 | + spec["shortname"] |
| 76 | + .upper() |
| 77 | + .replace("-", "_") |
| 78 | + ) |
| 79 | + spec["tokenName"] = f"TR_TOKEN_{token}" |
| 80 | + |
| 81 | + # Apply template substitutions |
| 82 | + content = template |
| 83 | + for prop in PROPERTIES: |
| 84 | + value = spec.get(prop, "") |
| 85 | + content = content.replace(f"{{{{{prop}}}}}", value) |
| 86 | + |
| 87 | + # Write workflow file |
| 88 | + filename = workflows_dir / f"{spec['shortname']}.yml" |
| 89 | + filename.write_text(content, encoding="utf-8") |
| 90 | + print(f"Generated {filename}") |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments