|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2026 Romain Beauxis <romain.beauxis@gmail.com> |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# 1. Redistributions of source code must retain the above copyright notice, |
| 8 | +# this list of conditions and the following disclaimer. |
| 9 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +# this list of conditions and the following disclaimer in the documentation |
| 11 | +# and/or other materials provided with the distribution. |
| 12 | +# |
| 13 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 17 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 23 | +# POSSIBILITY OF SUCH DAMAGE. |
| 24 | + |
| 25 | +"""Inject PR attachment samples into the fate-suite directory. |
| 26 | +
|
| 27 | +Usage: inject-pr-samples.py <pr-number> |
| 28 | +
|
| 29 | +Reads SAMPLES from the environment (defaults to fate-suite). For each path |
| 30 | +listed in a ```fate-samples``` block in the PR description, downloads the |
| 31 | +matching PR attachment into $SAMPLES/<path>. |
| 32 | +
|
| 33 | +The PR description should contain a block like: |
| 34 | +
|
| 35 | + ```fate-samples |
| 36 | + vorbis/tos.ogg |
| 37 | + mov/some-new-sample.mov |
| 38 | + ``` |
| 39 | +
|
| 40 | +Each filename must match a file attached to the PR. |
| 41 | +""" |
| 42 | + |
| 43 | +import hashlib |
| 44 | +import json |
| 45 | +import os |
| 46 | +import re |
| 47 | +import sys |
| 48 | +import tempfile |
| 49 | +import urllib.request |
| 50 | +from pathlib import Path, PurePosixPath |
| 51 | + |
| 52 | +FORGEJO_API = "https://code.ffmpeg.org/api/v1/repos/ffmpeg/ffmpeg/issues" |
| 53 | +ATTACHMENT_BASE = "https://code.ffmpeg.org/attachments/" |
| 54 | + |
| 55 | + |
| 56 | +def fetch_json(url): |
| 57 | + with urllib.request.urlopen(url) as r: |
| 58 | + return json.load(r) |
| 59 | + |
| 60 | + |
| 61 | +def parse_fate_samples(body): |
| 62 | + paths = [] |
| 63 | + in_block = False |
| 64 | + for line in body.splitlines(): |
| 65 | + if line == "```fate-samples": |
| 66 | + in_block = True |
| 67 | + elif line == "```" and in_block: |
| 68 | + break |
| 69 | + elif in_block: |
| 70 | + parts = line.split() |
| 71 | + if len(parts) == 1: |
| 72 | + paths.append(parts[0]) |
| 73 | + return paths |
| 74 | + |
| 75 | + |
| 76 | +MAX_PATH_DEPTH = 3 |
| 77 | + |
| 78 | + |
| 79 | +def validate_path(path): |
| 80 | + p = PurePosixPath(path) |
| 81 | + if p.is_absolute(): |
| 82 | + raise ValueError(f"path must be relative: {path!r}") |
| 83 | + if ".." in p.parts: |
| 84 | + raise ValueError(f"path must not contain '..': {path!r}") |
| 85 | + if not p.parts: |
| 86 | + raise ValueError(f"empty path") |
| 87 | + if len(p.parts) > MAX_PATH_DEPTH: |
| 88 | + raise ValueError(f"path too deep (max {MAX_PATH_DEPTH} components): {path!r}") |
| 89 | + |
| 90 | + |
| 91 | +def validate_url(url): |
| 92 | + if not url.startswith(ATTACHMENT_BASE): |
| 93 | + raise ValueError(f"unexpected attachment URL: {url!r}") |
| 94 | + |
| 95 | + |
| 96 | +def digest(path): |
| 97 | + h = hashlib.sha256() |
| 98 | + with open(path, "rb") as f: |
| 99 | + while chunk := f.read(1 << 16): |
| 100 | + h.update(chunk) |
| 101 | + return h.digest() |
| 102 | + |
| 103 | + |
| 104 | +def download(url, dst): |
| 105 | + dst.parent.mkdir(parents=True, exist_ok=True) |
| 106 | + with tempfile.NamedTemporaryFile(dir=dst.parent, delete=False) as tmp: |
| 107 | + tmp_path = Path(tmp.name) |
| 108 | + try: |
| 109 | + with urllib.request.urlopen(url) as r: |
| 110 | + while chunk := r.read(1 << 16): |
| 111 | + tmp.write(chunk) |
| 112 | + if dst.exists() and digest(dst) != digest(tmp_path): |
| 113 | + raise ValueError(f"already exists with different content: {dst}") |
| 114 | + tmp_path.rename(dst) |
| 115 | + except: |
| 116 | + tmp_path.unlink(missing_ok=True) |
| 117 | + raise |
| 118 | + |
| 119 | + |
| 120 | +def main(): |
| 121 | + if len(sys.argv) != 2 or not re.fullmatch(r"[0-9]+", sys.argv[1]): |
| 122 | + print(f"Usage: {sys.argv[0]} <pr-number>", file=sys.stderr) |
| 123 | + sys.exit(1) |
| 124 | + |
| 125 | + pr_number = sys.argv[1] |
| 126 | + samples_dir = Path(os.environ.get("SAMPLES", "fate-suite")) |
| 127 | + |
| 128 | + pr = fetch_json(f"{FORGEJO_API}/{pr_number}") |
| 129 | + assets = {a["name"]: a["browser_download_url"] for a in pr.get("assets", [])} |
| 130 | + paths = parse_fate_samples(pr.get("body", "")) |
| 131 | + |
| 132 | + if not paths: |
| 133 | + sys.exit(0) |
| 134 | + |
| 135 | + new_samples = False |
| 136 | + |
| 137 | + for path in paths: |
| 138 | + try: |
| 139 | + validate_path(path) |
| 140 | + except ValueError as e: |
| 141 | + print(f"fate-samples: {e}", file=sys.stderr) |
| 142 | + sys.exit(1) |
| 143 | + |
| 144 | + name = PurePosixPath(path).name |
| 145 | + url = assets.get(name) |
| 146 | + if url is None: |
| 147 | + print(f"fate-samples: no attachment named {name!r}", file=sys.stderr) |
| 148 | + sys.exit(1) |
| 149 | + |
| 150 | + try: |
| 151 | + validate_url(url) |
| 152 | + except ValueError as e: |
| 153 | + print(f"fate-samples: {e}", file=sys.stderr) |
| 154 | + sys.exit(1) |
| 155 | + |
| 156 | + dst = samples_dir / path |
| 157 | + is_new = not dst.exists() |
| 158 | + try: |
| 159 | + download(url, dst) |
| 160 | + except ValueError as e: |
| 161 | + print(f"fate-samples: {e}", file=sys.stderr) |
| 162 | + sys.exit(1) |
| 163 | + if is_new: |
| 164 | + new_samples = True |
| 165 | + print(f"Injected: {path}") |
| 166 | + |
| 167 | + output_file = os.environ.get("FORGEJO_OUTPUT") |
| 168 | + if output_file: |
| 169 | + with open(output_file, "a") as f: |
| 170 | + print(f"new_samples={'true' if new_samples else 'false'}", file=f) |
| 171 | + |
| 172 | + |
| 173 | +if __name__ == "__main__": |
| 174 | + main() |
0 commit comments