Debian Codegraph @

packaging/verify_translations.py

raw
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 """Check that every built wheel carries each compiled translation catalog.
29
30 Run it from the repository root after building:
31
32 python packaging/verify_translations.py
33
34 - Apprise only reads compiled ``.mo`` files. Without them, every language
35 falls back to English.
36 - Every ``.po`` file in the source tree needs a matching ``.mo`` in each
37 wheel in ``dist/``.
38 - Anything missing is listed and the script exits with an error.
39 """
40
41 from __future__ import annotations
42
43 import argparse
44 from glob import glob
45 from os.path import abspath, basename, dirname, join
46 import sys
47 from typing import Optional
48 import zipfile
49
50 # The repository root, one level up from this script
51 ROOT_DIR = abspath(join(dirname(__file__), ".."))
52
53 # Where our translations live in the source tree
54 I18N_DIR = join(ROOT_DIR, "apprise", "i18n")
55
56
57 def expected_catalogs() -> list[str]:
58 """The wheel path of each compiled catalog a release must carry."""
59 # One per language with a catalog in the source tree
60 catalogs = []
61 for po_path in sorted(glob(join(I18N_DIR, "*", "LC_MESSAGES", "*.po"))):
62 # e.g. apprise/i18n/fr/LC_MESSAGES/apprise.mo
63 language = basename(dirname(dirname(po_path)))
64 name = basename(po_path)[:-3] + ".mo"
65 catalogs.append(f"apprise/i18n/{language}/LC_MESSAGES/{name}")
66
67 return catalogs
68
69
70 def missing_catalogs(wheel_path: str, expected: list[str]) -> list[str]:
71 """The expected compiled catalogs that ``wheel_path`` does not carry."""
72 # Read the wheel's file listing
73 with zipfile.ZipFile(wheel_path) as wheel:
74 present = set(wheel.namelist())
75
76 return [catalog for catalog in expected if catalog not in present]
77
78
79 def main(argv: Optional[list[str]] = None) -> int:
80 """Checks every wheel in the dist folder and reports the outcome."""
81 # Prepare our arguments
82 parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
83 parser.add_argument(
84 "dist",
85 nargs="?",
86 default=join(ROOT_DIR, "dist"),
87 help="folder holding the built wheels (default: dist)",
88 )
89 args = parser.parse_args(argv)
90
91 # There is nothing to check without a wheel
92 wheels = sorted(glob(join(args.dist, "*.whl")))
93 if not wheels:
94 print(f"No wheel found in {args.dist}")
95 return 1
96
97 # Every wheel must carry every language
98 expected = expected_catalogs()
99 failed = False
100 for wheel_path in wheels:
101 missing = missing_catalogs(wheel_path, expected)
102 if missing:
103 failed = True
104 name = basename(wheel_path)
105 print(f"{name} is missing {len(missing)} catalogs:")
106 for catalog in missing:
107 print(f" - {catalog}")
108
109 else:
110 print(
111 f"{basename(wheel_path)} carries all "
112 f"{len(expected)} translation catalogs"
113 )
114
115 return 1 if failed else 0
116
117
118 if __name__ == "__main__":
119 sys.exit(main())