|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Script to manage/update specification sub-repositories, |
| 4 | +in the style of git submodules, but without requiring |
| 5 | +them for all development situations |
| 6 | +because they are only necessary for a small subset of |
| 7 | +maintenance tasks. |
| 8 | +
|
| 9 | +This script should be runnable without non-stdlib dependencies. |
| 10 | +""" |
| 11 | + |
| 12 | +# TODO: windows support? |
| 13 | + |
| 14 | +import collections |
| 15 | +import contextlib |
| 16 | +import logging |
| 17 | +import logging.config |
| 18 | +import os |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +from pathlib import Path |
| 22 | +from typing import List |
| 23 | + |
| 24 | + |
| 25 | +_log = logging.getLogger(__name__) |
| 26 | +_ROOT_DIR = Path(__file__).parent |
| 27 | + |
| 28 | +SubRepo = collections.namedtuple("SubRepo", ["url", "rev", "path"]) |
| 29 | + |
| 30 | + |
| 31 | +def main(): |
| 32 | + logging.basicConfig( |
| 33 | + level=logging.INFO, format=">>> {message}", style="{", stream=sys.stdout |
| 34 | + ) |
| 35 | + subrepos = [ |
| 36 | + SubRepo( |
| 37 | + url="https://github.com/Open-EO/openeo-processes.git", |
| 38 | + rev="ca9e31094b863233d88459b6cf2a37416bc90d4e", |
| 39 | + path="openeo-processes", |
| 40 | + ) |
| 41 | + ] |
| 42 | + |
| 43 | + for subrepo in subrepos: |
| 44 | + ensure_subrepo(subrepo) |
| 45 | + |
| 46 | + run_command(["ls", "-al", _ROOT_DIR]) |
| 47 | + |
| 48 | + |
| 49 | +def run_command(cmd: List[str], *, return_stdout: bool = False, **kwargs): |
| 50 | + _log.info(f"Running {cmd} ({kwargs=})") |
| 51 | + if not return_stdout: |
| 52 | + return subprocess.check_call(args=cmd, **kwargs) |
| 53 | + else: |
| 54 | + return subprocess.check_output(args=cmd, **kwargs) |
| 55 | + |
| 56 | + |
| 57 | +@contextlib.contextmanager |
| 58 | +def in_cwd(path: Path): |
| 59 | + orig_cwd = Path.cwd() |
| 60 | + try: |
| 61 | + _log.info(f"Changing working directory to {path}") |
| 62 | + os.chdir(path) |
| 63 | + yield |
| 64 | + finally: |
| 65 | + _log.info(f"Resetting working directory to {path}") |
| 66 | + os.chdir(orig_cwd) |
| 67 | + |
| 68 | + |
| 69 | +def ensure_subrepo(subrepo: SubRepo): |
| 70 | + path = _ROOT_DIR / subrepo.path |
| 71 | + _log.info(f"Ensuring subrepo {subrepo}") |
| 72 | + if not path.exists(): |
| 73 | + run_command(["git", "clone", subrepo.url, str(path)]) |
| 74 | + run_command(["git", "config", "advice.detachedHead", "false"], cwd=path) |
| 75 | + |
| 76 | + if path.is_dir() and (path / ".git").is_dir(): |
| 77 | + _log.info(f"{path} already looks like a git repo") |
| 78 | + else: |
| 79 | + raise RuntimeError(f"{path} exists but does not look like a git repo") |
| 80 | + |
| 81 | + # Checkout to desired revision |
| 82 | + run_command(["git", "checkout", subrepo.rev], cwd=path) |
| 83 | + run_command(["git", "log", "-1"], cwd=path) |
| 84 | + run_command(["git", "submodule", "update", "--init", "--recursive"], cwd=path) |
| 85 | + |
| 86 | + # Check that repo is not dirty |
| 87 | + dirty = run_command( |
| 88 | + ["git", "status", "--short", "--untracked-files=all", "--ignored"], |
| 89 | + cwd=path, |
| 90 | + return_stdout=True, |
| 91 | + ) |
| 92 | + if dirty.strip(): |
| 93 | + raise RuntimeError(f"Repo {path} seems dirty: {dirty}") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments