Debian Codegraph @

mesonbuild/scripts/gettext.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2016 The Meson development team
3
4 from __future__ import annotations
5
6 import os
7 import argparse
8 import subprocess
9 import typing as T
10
11 from .meson_exe import run_exe
12 from ..utils.core import ExecutableSerialisation
13
14 parser = argparse.ArgumentParser()
15 parser.add_argument('command')
16 parser.add_argument('--pkgname', default='')
17 parser.add_argument('--datadirs', default='')
18 parser.add_argument('--langs', default='')
19 parser.add_argument('--localedir', default='')
20 parser.add_argument('--source-root', default='')
21 parser.add_argument('--subdir', default='')
22 parser.add_argument('--xgettext', default='xgettext')
23 parser.add_argument('--msgmerge', default='msgmerge')
24 parser.add_argument('--msginit', default='msginit')
25 parser.add_argument('--extra-args', default='')
26
27 def read_linguas(src_sub: str) -> T.List[str]:
28 # Syntax of this file is documented here:
29 # https://www.gnu.org/software/gettext/manual/html_node/po_002fLINGUAS.html
30 linguas = os.path.join(src_sub, 'LINGUAS')
31 try:
32 langs = []
33 with open(linguas, encoding='utf-8') as f:
34 for line in f:
35 line = line.strip()
36 if line and not line.startswith('#'):
37 langs += line.split()
38 return langs
39 except (FileNotFoundError, PermissionError):
40 print(f'Could not find file LINGUAS in {src_sub}')
41 return []
42
43 def run_potgen(src_sub: str, xgettext: str, pkgname: str, datadirs: str, args: T.List[str], source_root: str) -> int:
44 listfile = os.path.join(src_sub, 'POTFILES.in')
45 if not os.path.exists(listfile):
46 listfile = os.path.join(src_sub, 'POTFILES')
47 if not os.path.exists(listfile):
48 print('Could not find file POTFILES in %s' % src_sub)
49 return 1
50
51 child_env = os.environ.copy()
52 if datadirs:
53 child_env['GETTEXTDATADIRS'] = datadirs
54
55 ofile = os.path.join(src_sub, pkgname + '.pot')
56 return subprocess.call([xgettext, '--package-name=' + pkgname, '-p', src_sub, '-f', listfile,
57 '-D', source_root, '-k_', '-o', ofile] + args,
58 env=child_env)
59
60 def update_po(src_sub: str, msgmerge: str, msginit: str, pkgname: str, langs: T.List[str]) -> int:
61 potfile = os.path.join(src_sub, pkgname + '.pot')
62 for l in langs:
63 pofile = os.path.join(src_sub, l + '.po')
64 if os.path.exists(pofile):
65 exe = ExecutableSerialisation([msgmerge, '-q', pofile, potfile], capture=pofile)
66 if run_exe(exe) != 0:
67 return 1
68 else:
69 subprocess.check_call([msginit, '--input', potfile, '--output-file', pofile, '--locale', l, '--no-translator'])
70 return 0
71
72 def run(args: T.List[str]) -> int:
73 options = parser.parse_args(args)
74 subcmd = options.command
75 langs = options.langs.split('@@') if options.langs else None
76 extra_args = options.extra_args.split('@@') if options.extra_args else []
77 subdir = options.subdir
78 src_sub = os.path.join(options.source_root, subdir)
79
80 if not langs:
81 langs = read_linguas(src_sub)
82
83 if subcmd == 'pot':
84 return run_potgen(src_sub, options.xgettext, options.pkgname, options.datadirs, extra_args, options.source_root)
85 elif subcmd == 'update_po':
86 if run_potgen(src_sub, options.xgettext, options.pkgname, options.datadirs, extra_args, options.source_root) != 0:
87 return 1
88 return update_po(src_sub, options.msgmerge, options.msginit, options.pkgname, langs)
89 else:
90 print('Unknown subcommand.')
91 return 1