| 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 tempfile |
| 10 |
import shlex |
| 11 |
import shutil |
| 12 |
import typing as T |
| 13 |
|
| 14 |
parser = argparse.ArgumentParser() |
| 15 |
parser.add_argument('command') |
| 16 |
parser.add_argument('--build-dir', default='') |
| 17 |
parser.add_argument('-i', '--input', default='') |
| 18 |
parser.add_argument('-o', '--output', default='') |
| 19 |
parser.add_argument('--itstool', default='itstool') |
| 20 |
parser.add_argument('--its', action='append', default=[]) |
| 21 |
parser.add_argument('mo_files', nargs='+') |
| 22 |
|
| 23 |
|
| 24 |
def run_join(build_dir: str, itstool: str, its_files: T.List[str], mo_files: T.List[str], |
| 25 |
in_fname: str, out_fname: str) -> int: |
| 26 |
if not mo_files: |
| 27 |
print('No mo files specified to use for translation.') |
| 28 |
return 1 |
| 29 |
|
| 30 |
with tempfile.TemporaryDirectory(prefix=os.path.basename(in_fname), dir=build_dir) as tmp_dir: |
| 31 |
# copy mo files to have the right names so itstool can infer their locale |
| 32 |
locale_mo_files = [] |
| 33 |
for mo_file in mo_files: |
| 34 |
if not os.path.exists(mo_file): |
| 35 |
print(f'Could not find mo file {mo_file}') |
| 36 |
return 1 |
| 37 |
if not mo_file.endswith('.mo'): |
| 38 |
print(f'File is not a mo file: {mo_file}') |
| 39 |
return 1 |
| 40 |
# determine locale of this mo file |
| 41 |
parts = mo_file.partition('LC_MESSAGES') |
| 42 |
if parts[0].endswith((os.sep, '/')): |
| 43 |
locale = os.path.basename(parts[0][:-1]) |
| 44 |
else: |
| 45 |
locale = os.path.basename(parts[0]) |
| 46 |
tmp_mo_fname = os.path.join(tmp_dir, locale + '.mo') |
| 47 |
shutil.copy(mo_file, tmp_mo_fname) |
| 48 |
locale_mo_files.append(tmp_mo_fname) |
| 49 |
|
| 50 |
cmd = shlex.split(itstool) |
| 51 |
if its_files: |
| 52 |
for fname in its_files: |
| 53 |
cmd.extend(['-i', fname]) |
| 54 |
cmd.extend(['-j', in_fname, |
| 55 |
'-o', out_fname]) |
| 56 |
cmd.extend(locale_mo_files) |
| 57 |
|
| 58 |
return subprocess.call(cmd) |
| 59 |
|
| 60 |
|
| 61 |
def run(args: T.List[str]) -> int: |
| 62 |
options = parser.parse_args(args) |
| 63 |
command = options.command |
| 64 |
build_dir = os.environ.get('MESON_BUILD_ROOT', os.getcwd()) |
| 65 |
if options.build_dir: |
| 66 |
build_dir = options.build_dir |
| 67 |
|
| 68 |
if command == 'join': |
| 69 |
return run_join(build_dir, |
| 70 |
options.itstool, |
| 71 |
options.its, |
| 72 |
options.mo_files, |
| 73 |
options.input, |
| 74 |
options.output) |
| 75 |
else: |
| 76 |
print('Unknown subcommand.') |
| 77 |
return 1 |