|
| 1 | +import itertools |
| 2 | +import json |
| 3 | +import os |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | +from typing import NoReturn |
| 9 | + |
| 10 | +EXTENSION_REGEX = re.compile(r"^src/(?P<lang>\w+)/(?P<extension>\w+)") |
| 11 | +MULTISRC_LIB_REGEX = re.compile(r"^lib-multisrc/(?P<multisrc>\w+)") |
| 12 | +LIB_REGEX = re.compile(r"^lib/(?P<lib>\w+)") |
| 13 | +MODULE_REGEX = re.compile(r"^:src:(?P<lang>\w+):(?P<extension>\w+)$") |
| 14 | + |
| 15 | +def run_command(command: str) -> str: |
| 16 | + result = subprocess.run(command, capture_output=True, text=True, shell=True) |
| 17 | + if result.returncode != 0: |
| 18 | + print(result.stderr.strip()) |
| 19 | + sys.exit(result.returncode) |
| 20 | + return result.stdout.strip() |
| 21 | + |
| 22 | +def get_module_list(ref: str) -> tuple[list[str], list[str]]: |
| 23 | + changed_files = run_command(f"git diff --name-only {ref}").splitlines() |
| 24 | + |
| 25 | + modules = set() |
| 26 | + libs = set() |
| 27 | + deleted = set() |
| 28 | + |
| 29 | + for file in map(lambda x: Path(x).as_posix(), changed_files): |
| 30 | + if match := EXTENSION_REGEX.search(file): |
| 31 | + lang = match.group("lang") |
| 32 | + extension = match.group("extension") |
| 33 | + if Path("src", lang, extension).is_dir(): |
| 34 | + modules.add(f':src:{lang}:{extension}') |
| 35 | + deleted.add(f"{lang}.{extension}") |
| 36 | + elif match := MULTISRC_LIB_REGEX.search(file): |
| 37 | + multisrc = match.group("multisrc") |
| 38 | + if Path("lib-multisrc", multisrc).is_dir(): |
| 39 | + libs.add(f":lib-multisrc:{multisrc}:printDependentExtensions") |
| 40 | + elif match := LIB_REGEX.search(file): |
| 41 | + lib = match.group("lib") |
| 42 | + if Path("lib", lib).is_dir(): |
| 43 | + libs.add(f":lib:{lib}:printDependentExtensions") |
| 44 | + |
| 45 | + def is_extension_module(module: str) -> bool: |
| 46 | + if not (match := MODULE_REGEX.search(module)): |
| 47 | + return False |
| 48 | + lang = match.group("lang") |
| 49 | + extension = match.group("extension") |
| 50 | + deleted.add(f"{lang}.{extension}") |
| 51 | + return True |
| 52 | + |
| 53 | + modules.update([ |
| 54 | + module for module in |
| 55 | + run_command("./gradlew -q " + " ".join(libs)).splitlines() |
| 56 | + if is_extension_module(module) |
| 57 | + ]) |
| 58 | + |
| 59 | + if os.getenv("IS_PR_CHECK") != "true": |
| 60 | + with Path.cwd().joinpath(".github/always_build.json").open() as always_build_file: |
| 61 | + always_build = json.load(always_build_file) |
| 62 | + for extension in always_build: |
| 63 | + modules.add(":src:" + extension.replace(".", ":")) |
| 64 | + deleted.add(extension) |
| 65 | + |
| 66 | + return list(modules), list(deleted) |
| 67 | + |
| 68 | +def main() -> NoReturn: |
| 69 | + _, ref, build_type = sys.argv |
| 70 | + modules, deleted = get_module_list(ref) |
| 71 | + |
| 72 | + chunked = { |
| 73 | + "chunk": [ |
| 74 | + {"number": i + 1, "modules": modules} |
| 75 | + for i, modules in |
| 76 | + enumerate(itertools.batched( |
| 77 | + map(lambda x: f"{x}:assemble{build_type}", modules), |
| 78 | + int(os.getenv("CI_CHUNK_SIZE", 65)) |
| 79 | + )) |
| 80 | + ] |
| 81 | + } |
| 82 | + |
| 83 | + print(f"Module chunks to build:\n{json.dumps(chunked, indent=2)}\n\nModule to delete:\n{json.dumps(deleted, indent=2)}") |
| 84 | + |
| 85 | + if os.getenv("CI") == "true": |
| 86 | + with open(os.getenv("GITHUB_OUTPUT"), 'a') as out_file: |
| 87 | + out_file.write(f"matrix={json.dumps(chunked)}\n") |
| 88 | + out_file.write(f"delete={json.dumps(deleted)}\n") |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + main() |
0 commit comments