gettext.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright 2016 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import os
  12. import shutil
  13. import argparse
  14. import subprocess
  15. from . import destdir_join
  16. parser = argparse.ArgumentParser()
  17. parser.add_argument('command')
  18. parser.add_argument('--pkgname', default='')
  19. parser.add_argument('--datadirs', default='')
  20. parser.add_argument('--langs', default='')
  21. parser.add_argument('--localedir', default='')
  22. parser.add_argument('--subdir', default='')
  23. parser.add_argument('--extra-args', default='')
  24. def read_linguas(src_sub):
  25. # Syntax of this file is documented here:
  26. # https://www.gnu.org/software/gettext/manual/html_node/po_002fLINGUAS.html
  27. linguas = os.path.join(src_sub, 'LINGUAS')
  28. try:
  29. langs = []
  30. with open(linguas) as f:
  31. for line in f:
  32. line = line.strip()
  33. if line and not line.startswith('#'):
  34. langs += line.split()
  35. return langs
  36. except (FileNotFoundError, PermissionError):
  37. print('Could not find file LINGUAS in {}'.format(src_sub))
  38. return []
  39. def run_potgen(src_sub, pkgname, datadirs, args):
  40. listfile = os.path.join(src_sub, 'POTFILES')
  41. if not os.path.exists(listfile):
  42. listfile = os.path.join(src_sub, 'POTFILES.in')
  43. if not os.path.exists(listfile):
  44. print('Could not find file POTFILES in %s' % src_sub)
  45. return 1
  46. child_env = os.environ.copy()
  47. if datadirs:
  48. child_env['GETTEXTDATADIRS'] = datadirs
  49. ofile = os.path.join(src_sub, pkgname + '.pot')
  50. return subprocess.call(['xgettext', '--package-name=' + pkgname, '-p', src_sub, '-f', listfile,
  51. '-D', os.environ['MESON_SOURCE_ROOT'], '-k_', '-o', ofile] + args,
  52. env=child_env)
  53. def gen_gmo(src_sub, bld_sub, langs):
  54. for l in langs:
  55. subprocess.check_call(['msgfmt', os.path.join(src_sub, l + '.po'),
  56. '-o', os.path.join(bld_sub, l + '.gmo')])
  57. return 0
  58. def update_po(src_sub, pkgname, langs):
  59. potfile = os.path.join(src_sub, pkgname + '.pot')
  60. for l in langs:
  61. pofile = os.path.join(src_sub, l + '.po')
  62. if os.path.exists(pofile):
  63. subprocess.check_call(['msgmerge', '-q', '-o', pofile, pofile, potfile])
  64. else:
  65. subprocess.check_call(['msginit', '--input', potfile, '--output-file', pofile, '--locale', l, '--no-translator'])
  66. return 0
  67. def do_install(src_sub, bld_sub, dest, pkgname, langs):
  68. for l in langs:
  69. srcfile = os.path.join(bld_sub, l + '.gmo')
  70. outfile = os.path.join(dest, l, 'LC_MESSAGES',
  71. pkgname + '.mo')
  72. tempfile = outfile + '.tmp'
  73. os.makedirs(os.path.dirname(outfile), exist_ok=True)
  74. shutil.copyfile(srcfile, tempfile)
  75. shutil.copystat(srcfile, tempfile)
  76. os.replace(tempfile, outfile)
  77. print('Installing %s to %s' % (srcfile, outfile))
  78. return 0
  79. def run(args):
  80. options = parser.parse_args(args)
  81. subcmd = options.command
  82. langs = options.langs.split('@@') if options.langs else None
  83. extra_args = options.extra_args.split('@@') if options.extra_args else []
  84. subdir = os.environ.get('MESON_SUBDIR', '')
  85. if options.subdir:
  86. subdir = options.subdir
  87. src_sub = os.path.join(os.environ['MESON_SOURCE_ROOT'], subdir)
  88. bld_sub = os.path.join(os.environ['MESON_BUILD_ROOT'], subdir)
  89. if not langs:
  90. langs = read_linguas(src_sub)
  91. if subcmd == 'pot':
  92. return run_potgen(src_sub, options.pkgname, options.datadirs, extra_args)
  93. elif subcmd == 'gen_gmo':
  94. return gen_gmo(src_sub, bld_sub, langs)
  95. elif subcmd == 'update_po':
  96. if run_potgen(src_sub, options.pkgname, options.datadirs, extra_args) != 0:
  97. return 1
  98. return update_po(src_sub, options.pkgname, langs)
  99. elif subcmd == 'install':
  100. destdir = os.environ.get('DESTDIR', '')
  101. dest = destdir_join(destdir, os.path.join(os.environ['MESON_INSTALL_PREFIX'],
  102. options.localedir))
  103. if gen_gmo(src_sub, bld_sub, langs) != 0:
  104. return 1
  105. do_install(src_sub, bld_sub, dest, options.pkgname, langs)
  106. else:
  107. print('Unknown subcommand.')
  108. return 1