yelphelper.py 5.6 KB

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