bito-code-review[bot] commented on code in PR #39443: URL: https://github.com/apache/superset/pull/39443#discussion_r3277946735
########## scripts/translations/check_translation_regression.py: ########## @@ -0,0 +1,226 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Check that source-code changes don't cause translation regressions. + +Usage +----- +Count non-fuzzy translated entries in all .po files and write JSON to stdout: + + python check_translation_regression.py --count + +Compare the current .po state against a previously-recorded baseline and fail +if any language lost translations: + + python check_translation_regression.py --compare /path/to/before.json + +Optionally write a markdown report to a file (used by CI to post a PR comment): + + python check_translation_regression.py --compare before.json --report report.md + +Use a translations directory other than the repo default (used by CI to count +against a separate base-branch worktree): + + python check_translation_regression.py --count \\ + --translations-dir /tmp/base-worktree/superset/translations + +Typical CI workflow +------------------- +1. Create a base-branch worktree alongside the PR worktree +2. Run babel_update.sh in the base worktree (extract from BASE source) +3. Record baseline: python ... --count --translations-dir BASE_TREE > before.json +4. Run babel_update.sh in the PR worktree (extract from PR source) starting + from the same pristine BASE translations +5. Compare: python ... --compare before.json [--report report.md] + +Comparing two babel_update outputs that started from the same BASE .po files +isolates regressions caused by the PR's source diff from any pre-existing +drift on the base branch. +""" + +import argparse +import json +import re +import subprocess +import sys +from pathlib import Path +from typing import Optional + +DEFAULT_TRANSLATIONS_DIR = ( + Path(__file__).resolve().parent.parent.parent / "superset" / "translations" +) + +# English .po files use empty msgstr by convention (source language == target), +# so they always show 0 translated entries and should not be checked. +SKIP_LANGS = {"en"} + + +def count_translated(po_file: Path) -> int: + """Return the number of non-fuzzy translated messages in a .po file.""" + import shutil # noqa: PLC0415 + + msgfmt = shutil.which("msgfmt") or "msgfmt" + result = subprocess.run( # noqa: S603 + [msgfmt, "--statistics", "-o", "/dev/null", str(po_file)], + capture_output=True, + text=True, + ) Review Comment: <div> <div id="suggestion"> <div id="issue"><b>subprocess.run missing explicit check argument</b></div> <div id="fix"> Add `check=True` to the `subprocess.run()` call on line 78 to explicitly handle non-zero exit codes, or add `check=False` if the current behavior is intentional. </div> <details> <summary> <b>Code suggestion</b> </summary> <blockquote>Check the AI-generated fix before applying</blockquote> <div id="code"> ````suggestion result = subprocess.run( # noqa: S603 [msgfmt, "--statistics", "-o", "/dev/null", str(po_file)], capture_output=True, text=True, check=False, ) ```` </div> </details> </div> <small><i>Code Review Run #5b05d6</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
