editor_builders.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """Functions used to generate source files during build time"""
  2. import os
  3. import os.path
  4. import shutil
  5. import subprocess
  6. import tempfile
  7. import uuid
  8. import zlib
  9. from methods import print_warning
  10. def make_doc_header(target, source, env):
  11. dst = str(target[0])
  12. with open(dst, "w", encoding="utf-8", newline="\n") as g:
  13. buf = ""
  14. docbegin = ""
  15. docend = ""
  16. for src in source:
  17. src = str(src)
  18. if not src.endswith(".xml"):
  19. continue
  20. with open(src, "r", encoding="utf-8") as f:
  21. content = f.read()
  22. buf += content
  23. buf = (docbegin + buf + docend).encode("utf-8")
  24. decomp_size = len(buf)
  25. # Use maximum zlib compression level to further reduce file size
  26. # (at the cost of initial build times).
  27. buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
  28. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  29. g.write("#ifndef _DOC_DATA_RAW_H\n")
  30. g.write("#define _DOC_DATA_RAW_H\n")
  31. g.write('static const char *_doc_data_hash = "' + str(hash(buf)) + '";\n')
  32. g.write("static const int _doc_data_compressed_size = " + str(len(buf)) + ";\n")
  33. g.write("static const int _doc_data_uncompressed_size = " + str(decomp_size) + ";\n")
  34. g.write("static const unsigned char _doc_data_compressed[] = {\n")
  35. for i in range(len(buf)):
  36. g.write("\t" + str(buf[i]) + ",\n")
  37. g.write("};\n")
  38. g.write("#endif")
  39. def make_translations_header(target, source, env, category):
  40. dst = str(target[0])
  41. with open(dst, "w", encoding="utf-8", newline="\n") as g:
  42. g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  43. g.write("#ifndef _{}_TRANSLATIONS_H\n".format(category.upper()))
  44. g.write("#define _{}_TRANSLATIONS_H\n".format(category.upper()))
  45. sorted_paths = sorted([str(x) for x in source], key=lambda path: os.path.splitext(os.path.basename(path))[0])
  46. msgfmt_available = shutil.which("msgfmt") is not None
  47. if not msgfmt_available:
  48. print_warning("msgfmt is not found, using .po files instead of .mo")
  49. xl_names = []
  50. for i in range(len(sorted_paths)):
  51. name = os.path.splitext(os.path.basename(sorted_paths[i]))[0]
  52. # msgfmt erases non-translated messages, so avoid using it if exporting the POT.
  53. if msgfmt_available and name != category:
  54. mo_path = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".mo")
  55. cmd = "msgfmt " + sorted_paths[i] + " --no-hash -o " + mo_path
  56. try:
  57. subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
  58. with open(mo_path, "rb") as f:
  59. buf = f.read()
  60. except OSError as e:
  61. print_warning(
  62. "msgfmt execution failed, using .po file instead of .mo: path=%r; [%s] %s"
  63. % (sorted_paths[i], e.__class__.__name__, e)
  64. )
  65. with open(sorted_paths[i], "rb") as f:
  66. buf = f.read()
  67. finally:
  68. try:
  69. os.remove(mo_path)
  70. except OSError as e:
  71. # Do not fail the entire build if it cannot delete a temporary file.
  72. print_warning(
  73. "Could not delete temporary .mo file: path=%r; [%s] %s" % (mo_path, e.__class__.__name__, e)
  74. )
  75. else:
  76. with open(sorted_paths[i], "rb") as f:
  77. buf = f.read()
  78. if name == category:
  79. name = "source"
  80. decomp_size = len(buf)
  81. # Use maximum zlib compression level to further reduce file size
  82. # (at the cost of initial build times).
  83. buf = zlib.compress(buf, zlib.Z_BEST_COMPRESSION)
  84. g.write("static const unsigned char _{}_translation_{}_compressed[] = {{\n".format(category, name))
  85. for j in range(len(buf)):
  86. g.write("\t" + str(buf[j]) + ",\n")
  87. g.write("};\n")
  88. xl_names.append([name, len(buf), str(decomp_size)])
  89. g.write("struct {}TranslationList {{\n".format(category.capitalize()))
  90. g.write("\tconst char* lang;\n")
  91. g.write("\tint comp_size;\n")
  92. g.write("\tint uncomp_size;\n")
  93. g.write("\tconst unsigned char* data;\n")
  94. g.write("};\n\n")
  95. g.write("static {}TranslationList _{}_translations[] = {{\n".format(category.capitalize(), category))
  96. for x in xl_names:
  97. g.write(
  98. '\t{{ "{}", {}, {}, _{}_translation_{}_compressed }},\n'.format(
  99. x[0], str(x[1]), str(x[2]), category, x[0]
  100. )
  101. )
  102. g.write("\t{nullptr, 0, 0, nullptr}\n")
  103. g.write("};\n")
  104. g.write("#endif")
  105. def make_editor_translations_header(target, source, env):
  106. make_translations_header(target, source, env, "editor")
  107. def make_property_translations_header(target, source, env):
  108. make_translations_header(target, source, env, "property")
  109. def make_doc_translations_header(target, source, env):
  110. make_translations_header(target, source, env, "doc")
  111. def make_extractable_translations_header(target, source, env):
  112. make_translations_header(target, source, env, "extractable")