From b88b34c82fffdedbe218f62ca5176d4d6ea6f2a3 Mon Sep 17 00:00:00 2001 From: Antheas Kapenekakis Date: Sun, 4 Aug 2024 17:13:38 +0300 Subject: [PATCH] add formatter overrides in action --- .github/workflows/online_test_deck.yml | 10 +++- 3_chunk.sh | 21 +++++-- action.yml | 14 +++++ src/rechunk/__main__.py | 20 ++++++- src/rechunk/alg.py | 2 + src/rechunk/utils.py | 81 +++++++++++++++++++++----- 6 files changed, 126 insertions(+), 22 deletions(-) diff --git a/.github/workflows/online_test_deck.yml b/.github/workflows/online_test_deck.yml index 2202039..1a1520b 100644 --- a/.github/workflows/online_test_deck.yml +++ b/.github/workflows/online_test_deck.yml @@ -38,7 +38,7 @@ jobs: sudo podman pull ghcr.io/ublue-os/bazzite-deck:${{ github.event.inputs.ref }} - name: Run Rechunker id: rechunk - uses: ./. # IMPORTANT: Use a tag, e.g.: "hhd-dev/rechunk@v0.6.2" + uses: ./. # IMPORTANT: Use a tag, e.g.: "hhd-dev/rechunk@v0.6.4" with: ref: 'ghcr.io/ublue-os/bazzite-deck:${{ github.event.inputs.ref }}' prev-ref: ghcr.io/hhd-dev/bazzite-automated-deck:stable @@ -92,6 +92,14 @@ jobs: Package Changes: + formatters: | + commits.none=No changes\n + commits.commit=- **[](https://github.com/hhd-dev/rechunk/commit/)** \n + pkgupd.none=No package updates\n + pkgupd.add=- **** Added at \n + pkgupd.update=- **** \n + pkgupd.remove=- **** → Removed\n + - name: Upload Changelog uses: actions/upload-artifact@v4 with: diff --git a/3_chunk.sh b/3_chunk.sh index 940e8e7..cc6323b 100755 --- a/3_chunk.sh +++ b/3_chunk.sh @@ -85,30 +85,41 @@ if [ -n "$CLEAR_PLAN" ]; then PREV_ARG+=("--clear-plan") fi -LABEL_ARR=() +ARG_ARR=() if [ -n "$LABELS" ]; then IFS=$'\n' for label in $LABELS; do if [ -z "$label" ]; then continue fi - LABEL_ARR+=("--label" "$label") + ARG_ARR+=("--label" "$label") done unset IFS fi +if [ -n "$FORMATTERS" ]; then + IFS=$'\n' + for form in $FORMATTERS; do + if [ -z "$form" ]; then + continue + fi + ARG_ARR+=("--formatter" "$(echo $form)") + done + unset IFS +fi + if [ -n "$DESCRIPTION" ]; then echo "Writing description to 'org.opencontainers.image.description'" - LABEL_ARR+=("--label" "org.opencontainers.image.description=$DESCRIPTION") + ARG_ARR+=("--label" "org.opencontainers.image.description=$DESCRIPTION") fi echo Executing command: echo $RECHUNK -r "$REPO" -b "$OUT_TAG" -c "$CONTENT_META" \ --changelog-fn "${OUT_NAME}.changelog.txt" \ - "${PREV_ARG[@]}" "${LABEL_ARR[@]}" + "${PREV_ARG[@]}" "${ARG_ARR[@]}" $RECHUNK -r "$REPO" -b "$OUT_TAG" -c "$CONTENT_META" \ --changelog-fn "${OUT_NAME}.changelog.txt" \ - "${PREV_ARG[@]}" "${LABEL_ARR[@]}" + "${PREV_ARG[@]}" "${ARG_ARR[@]}" PREV_ARG="" diff --git a/action.yml b/action.yml index 292efbc..9e2cfe2 100644 --- a/action.yml +++ b/action.yml @@ -79,6 +79,19 @@ inputs: tag along with the git path. If is not used, providing it as part of a "org.opencontainers.image.revision" is the same. + formatters: + description: | + The formatters to use for the changelog. The format is as follows: + ``` + formatter= + ``` + Formatter is a string up to the first = and defines the formatter name. + After that and until the end of the line is the format. + Use \n for newlines. Different formatters use different + substitutions (e.g., ), refer to the examples for specifics. + + You can use the characters `<`, `>`, `=` freely. Substitution is only + performed on exact tag matches. outputs: ref: @@ -165,6 +178,7 @@ runs: -e PREV_REF="${{ inputs.prev-ref }}" \ -e OUT_NAME="$OUT_NAME" \ -e LABELS="${{ inputs.labels }}" \ + -e FORMATTERS="${{ inputs.formatters }}" \ -e VERSION="${{ inputs.version }}" \ -e VERSION_FN="/workspace/version.txt" \ -e PRETTY="${{ inputs.pretty }}" \ diff --git a/src/rechunk/__main__.py b/src/rechunk/__main__.py index 27e4d94..a356f6d 100644 --- a/src/rechunk/__main__.py +++ b/src/rechunk/__main__.py @@ -62,7 +62,15 @@ def argparse_func(): default=None, ) parser.add_argument( - "-l", "--label", help="Add labels to the output image.", action="append" + "-l", + "--label", + help="Add labels to the output image (`label=var`).", + action="append", + ) + parser.add_argument( + "--formatter", + help="Override string formatters for tags for i18n (`formatter=var`).", + action="append", ) parser.add_argument("--pretty", help="Pretty version string.", default=None) parser.add_argument( @@ -126,6 +134,15 @@ def argparse_func(): ) args = parser.parse_args() + + # Parse formatters + formatters = {} + for line in args.formatter: + if "=" not in line: + continue + idx = line.index("=") + formatters[line[:idx]] = line[idx + 1 :] + alg_main( repo=args.repo, ref=args.ref, @@ -145,6 +162,7 @@ def argparse_func(): changelog=args.changelog, changelog_fn=args.changelog_fn, clear_plan=args.clear_plan, + formatters=formatters, ) diff --git a/src/rechunk/alg.py b/src/rechunk/alg.py index e2862e2..78ffc87 100644 --- a/src/rechunk/alg.py +++ b/src/rechunk/alg.py @@ -547,6 +547,7 @@ def main( changelog: str | None = None, changelog_fn: str | None = None, clear_plan: bool = False, + formatters: dict[str, str] = {}, ): if not meta_fn: meta_fn = get_default_meta_yaml() @@ -655,6 +656,7 @@ def main( changelog_template=changelog, changelog_fn=changelog_fn, info=info, + formatters=formatters, ) if contentmeta_fn: diff --git a/src/rechunk/utils.py b/src/rechunk/utils.py index 037e2d7..2ffb85f 100644 --- a/src/rechunk/utils.py +++ b/src/rechunk/utils.py @@ -18,6 +18,15 @@ VERSION_TAG = "org.opencontainers.image.version" REVISION_TAG = "org.opencontainers.image.revision" +DEFAULT_FORMATTERS = { + "commits.none": "-\n", + "commits.commit": "- **** \n", + "pkgupd.none": "-\n", + "pkgupd.update": " - ****: \n", + "pkgupd.add": " - ****: x → \n", + "pkgupd.remove": " - ****: → x\n", +} + class tqdm(tqdm_orig): def __init__(self, *args, **kwargs): @@ -136,14 +145,19 @@ def get_update_matrix(packages: list[MetaPackage], biweekly: bool = True): return p_upd -def get_commits(git_dir: str | None, revision: str | None, prev_rev: str | None): +def get_commits( + git_dir: str | None, + revision: str | None, + prev_rev: str | None, + formatters: dict[str, str], +): logger.info(f"Getting commits from '{prev_rev}' to '{revision}' in '{git_dir}'") if not git_dir or not revision or not prev_rev: return "" out = "" try: - cmd = f"git --git-dir='{git_dir}/.git' log --format=\"%t/%s\" --no-merges {prev_rev}..{revision}" + cmd = f"git --git-dir='{git_dir}/.git' log --format=\"%t/%T/%s\" --no-merges {prev_rev}..{revision}" for commit in ( subprocess.run(cmd, stdout=subprocess.PIPE, shell=True) .stdout.decode("utf-8") @@ -152,12 +166,27 @@ def get_commits(git_dir: str | None, revision: str | None, prev_rev: str | None) if not "/" in commit: continue idx = commit.index("/") - out += f" - **{commit[:idx]}** {commit[idx+1:]}\n" + short = commit[:idx] + rest = commit[idx + 1 :] + idx = rest.index("/") + hash = rest[:idx] + commit = rest[idx + 1 :] + out += ( + formatters["commits.commit"] + .replace("", short) + .replace("", commit) + .replace("", hash) + ) except Exception as e: logger.error(f"Failed to get commits: {e}") return out -def get_package_update_str(base_pkg: Sequence[Package] | None, info: ExportInfo | None): + +def get_package_update_str( + base_pkg: Sequence[Package] | None, + info: ExportInfo | None, + formatters: dict[str, str], +): if not base_pkg or not info or not info.get("packages", None): return "" @@ -169,33 +198,51 @@ def get_package_update_str(base_pkg: Sequence[Package] | None, info: ExportInfo if p.name in seen: continue seen.add(p.name) - + if p.name not in previous: - out += f" - {p.name}: x → {p.version}\n" + out += ( + formatters["pkgupd.add"] + .replace("", p.version) + .replace("", p.name) + ) else: pv = previous[p.name] # Skip release for package version updates if "-" in pv: prel = pv[pv.rindex("-") + 1 :] - pv = pv[:pv.rindex("-")] + pv = pv[: pv.rindex("-")] else: prel = None if p.version != pv: - out += f" - {p.name}: {pv} → {p.version}\n" + prev = pv + newv = p.version elif prel and p.release != prel: - out += f" - {p.name}: {pv}-{prel} → {p.version}-{p.release}\n" - + prev = f"{pv}-{prel}" + newv = f"{p.version}-{p.release}" + else: + continue + + out += ( + formatters["pkgupd.update"] + .replace("", prev) + .replace("", newv) + .replace("", p.name) + ) + for p in previous: if p not in seen: pv = previous[p] if "-" in pv: - pv = pv[:pv.rindex("-")] - out += f" - {p}: {pv} → x\n" + pv = pv[: pv.rindex("-")] + out += ( + formatters["pkgupd.remove"].replace("", pv).replace("", p) + ) seen.add(p) return out + def get_labels( labels: Sequence[str], version: str | None, @@ -209,13 +256,16 @@ def get_labels( changelog_template: str | None, changelog_fn: str | None, info: ExportInfo | None, + formatters: dict[str, str] = {}, ) -> tuple[dict[str, str], str]: + formatters = {**DEFAULT_FORMATTERS, **formatters} + # Date format is YYMMDD # Timestamp format is YYYY-MM-DDTHH:MM:SSZ now = datetime.now() date = now.strftime("%y%m%d") timestamp = now.strftime("%Y-%m-%dT%H:%M:%SZ") - pkgupd = get_package_update_str(base_pkg, info) + pkgupd = get_package_update_str(base_pkg, info, formatters) prev_labels = prev_manifest.get("Labels", {}) if prev_manifest else {} prev_version = prev_labels.get(VERSION_TAG, None) if prev_labels else None @@ -266,6 +316,7 @@ def get_labels( git_dir, revision, (info or {}).get("revision", None) or prev_labels.get(REVISION_TAG, None), + formatters=formatters, ) def process_label(key: str, value: str): @@ -285,9 +336,9 @@ def process_label(key: str, value: str): value = value.replace("", imginfo) blacklist[key] = BLACKLIST_KEY if "" in value: - value = value.replace("", commit_str or "-") + value = value.replace("", commit_str or formatters["commits.none"]) if "" in value: - value = value.replace("", pkgupd or "-") + value = value.replace("", pkgupd or formatters["pkgupd.none"]) if base_pkg: for pkg in base_pkg: