yelphelper.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 subprocess
  13. import shutil
  14. import argparse
  15. from .. import mlog
  16. from ..mesonlib import has_path_sep
  17. from . import destdir_join
  18. from .gettext import read_linguas
  19. parser = argparse.ArgumentParser()
  20. parser.add_argument('command')
  21. parser.add_argument('--id', dest='project_id')
  22. parser.add_argument('--subdir', dest='subdir')
  23. parser.add_argument('--installdir', dest='install_dir')
  24. parser.add_argument('--sources', dest='sources')
  25. parser.add_argument('--media', dest='media', default='')
  26. parser.add_argument('--langs', dest='langs', default='')
  27. parser.add_argument('--symlinks', type=bool, dest='symlinks', default=False)
  28. def build_pot(srcdir, project_id, sources):
  29. # Must be relative paths
  30. sources = [os.path.join('C', source) for source in sources]
  31. outfile = os.path.join(srcdir, project_id + '.pot')
  32. subprocess.call(['itstool', '-o', outfile] + sources)
  33. def update_po(srcdir, project_id, langs):
  34. potfile = os.path.join(srcdir, project_id + '.pot')
  35. for lang in langs:
  36. pofile = os.path.join(srcdir, lang, lang + '.po')
  37. subprocess.call(['msgmerge', '-q', '-o', pofile, pofile, potfile])
  38. def build_translations(srcdir, blddir, langs):
  39. for lang in langs:
  40. outdir = os.path.join(blddir, lang)
  41. os.makedirs(outdir, exist_ok=True)
  42. subprocess.call([
  43. 'msgfmt', os.path.join(srcdir, lang, lang + '.po'),
  44. '-o', os.path.join(outdir, lang + '.gmo')
  45. ])
  46. def merge_translations(blddir, sources, langs):
  47. for lang in langs:
  48. subprocess.call([
  49. 'itstool', '-m', os.path.join(blddir, lang, lang + '.gmo'),
  50. '-o', os.path.join(blddir, lang)
  51. ] + sources)
  52. def install_help(srcdir, blddir, sources, media, langs, install_dir, destdir, project_id, symlinks):
  53. c_install_dir = os.path.join(install_dir, 'C', project_id)
  54. for lang in langs + ['C']:
  55. indir = destdir_join(destdir, os.path.join(install_dir, lang, project_id))
  56. os.makedirs(indir, exist_ok=True)
  57. for source in sources:
  58. infile = os.path.join(srcdir if lang == 'C' else blddir, lang, source)
  59. outfile = os.path.join(indir, source)
  60. mlog.log('Installing %s to %s' % (infile, outfile))
  61. shutil.copyfile(infile, outfile)
  62. shutil.copystat(infile, outfile)
  63. for m in media:
  64. infile = os.path.join(srcdir, lang, m)
  65. outfile = os.path.join(indir, m)
  66. if not os.path.exists(infile):
  67. if lang == 'C':
  68. mlog.warning('Media file "%s" did not exist in C directory' % m)
  69. continue
  70. elif symlinks:
  71. srcfile = os.path.join(c_install_dir, m)
  72. mlog.log('Symlinking %s to %s.' % (outfile, srcfile))
  73. if has_path_sep(m):
  74. os.makedirs(os.path.dirname(outfile), exist_ok=True)
  75. try:
  76. try:
  77. os.symlink(srcfile, outfile)
  78. except FileExistsError:
  79. os.remove(outfile)
  80. os.symlink(srcfile, outfile)
  81. continue
  82. except (NotImplementedError, OSError):
  83. mlog.warning('Symlinking not supported, falling back to copying')
  84. else:
  85. # Lang doesn't have media file so copy it over 'C' one
  86. infile = os.path.join(srcdir, 'C', m)
  87. mlog.log('Installing %s to %s' % (infile, outfile))
  88. if has_path_sep(m):
  89. os.makedirs(os.path.dirname(outfile), exist_ok=True)
  90. shutil.copyfile(infile, outfile)
  91. shutil.copystat(infile, outfile)
  92. def run(args):
  93. options = parser.parse_args(args)
  94. langs = options.langs.split('@@') if options.langs else []
  95. media = options.media.split('@@') if options.media else []
  96. sources = options.sources.split('@@')
  97. destdir = os.environ.get('DESTDIR', '')
  98. src_subdir = os.path.join(os.environ['MESON_SOURCE_ROOT'], options.subdir)
  99. build_subdir = os.path.join(os.environ['MESON_BUILD_ROOT'], options.subdir)
  100. abs_sources = [os.path.join(src_subdir, 'C', source) for source in sources]
  101. if not langs:
  102. langs = read_linguas(src_subdir)
  103. if options.command == 'pot':
  104. build_pot(src_subdir, options.project_id, sources)
  105. elif options.command == 'update-po':
  106. build_pot(src_subdir, options.project_id, sources)
  107. update_po(src_subdir, options.project_id, langs)
  108. elif options.command == 'build':
  109. if langs:
  110. build_translations(src_subdir, build_subdir, langs)
  111. elif options.command == 'install':
  112. install_dir = os.path.join(os.environ['MESON_INSTALL_PREFIX'], options.install_dir)
  113. if langs:
  114. build_translations(src_subdir, build_subdir, langs)
  115. merge_translations(build_subdir, abs_sources, langs)
  116. install_help(src_subdir, build_subdir, sources, media, langs, install_dir,
  117. destdir, options.project_id, options.symlinks)