Debian Codegraph @

third_party/python/moz.l10n/moz/l10n/bin/build_file.py

raw
1 # Copyright Mozilla Foundation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from __future__ import annotations
16
17 import logging
18 from argparse import ArgumentParser, RawDescriptionHelpFormatter
19 from os import makedirs
20 from os.path import dirname, exists
21 from shutil import copyfile
22 from textwrap import dedent
23
24 from moz.l10n.bin.build import write_target_file
25 from moz.l10n.resource import UnsupportedResource, parse_resource
26
27 log = logging.getLogger(__name__)
28
29
30 def cli() -> None:
31 parser = ArgumentParser(
32 description=dedent(
33 """
34 Build one localization file for release.
35
36 Uses the --source file as a baseline, applying --l10n localizations to build --target.
37
38 Trims out all comments and messages not in the source file.
39 """
40 ),
41 formatter_class=RawDescriptionHelpFormatter,
42 )
43 parser.add_argument(
44 "-v", "--verbose", action="count", default=0, help="increase logging verbosity"
45 )
46 parser.add_argument("--source", metavar="PATH", required=True, help="source file")
47 parser.add_argument(
48 "--l10n", metavar="PATH", required=True, help="localization file"
49 )
50 parser.add_argument("--target", metavar="PATH", required=True, help="output target")
51 args = parser.parse_args()
52
53 log_level = (
54 logging.WARNING
55 if args.verbose == 0
56 else logging.INFO
57 if args.verbose == 1
58 else logging.DEBUG
59 )
60 logging.basicConfig(format="%(message)s", level=log_level)
61
62 try:
63 try:
64 source_res = parse_resource(args.source)
65 except UnsupportedResource:
66 source_res = None
67 makedirs(dirname(args.target), exist_ok=True)
68 if source_res is None:
69 from_path = args.l10n if exists(args.l10n) else args.source
70 copyfile(from_path, args.target)
71 else:
72 write_target_file(args.source, source_res, args.l10n, args.target)
73 except (OSError, UnsupportedResource) as error:
74 raise SystemExit(error)
75
76
77 if __name__ == "__main__":
78 cli()