forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor_builders.py
96 lines (79 loc) · 3.36 KB
/
editor_builders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Functions used to generate source files during build time"""
import os
import os.path
import subprocess
import tempfile
import uuid
import methods
def make_doc_header(target, source, env):
buffer = b"".join([methods.get_buffer(src) for src in map(str, source)])
decomp_size = len(buffer)
buffer = methods.compress_buffer(buffer)
with methods.generated_wrapper(str(target[0])) as file:
file.write(f"""\
inline constexpr const char *_doc_data_hash = "{hash(buffer)}";
inline constexpr int _doc_data_compressed_size = {len(buffer)};
inline constexpr int _doc_data_uncompressed_size = {decomp_size};
inline constexpr const unsigned char _doc_data_compressed[] = {{
{methods.format_buffer(buffer, 1)}
}};
""")
def make_translations_header(target, source, env):
category = os.path.basename(str(target[0])).split("_")[0]
sorted_paths = sorted([src.abspath for src in source], key=lambda path: os.path.splitext(os.path.basename(path))[0])
xl_names = []
msgfmt = env.Detect("msgfmt")
if not msgfmt:
methods.print_warning("msgfmt not found, using .po files instead of .mo")
with methods.generated_wrapper(str(target[0])) as file:
for path in sorted_paths:
name = os.path.splitext(os.path.basename(path))[0]
# msgfmt erases non-translated messages, so avoid using it if exporting the POT.
if msgfmt and name != category:
mo_path = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".mo")
cmd = f"{msgfmt} {path} --no-hash -o {mo_path}"
try:
subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()
buffer = methods.get_buffer(mo_path)
except OSError as e:
methods.print_warning(
"msgfmt execution failed, using .po file instead of .mo: path=%r; [%s] %s"
% (path, e.__class__.__name__, e)
)
buffer = methods.get_buffer(path)
finally:
try:
if os.path.exists(mo_path):
os.remove(mo_path)
except OSError as e:
# Do not fail the entire build if it cannot delete a temporary file.
methods.print_warning(
"Could not delete temporary .mo file: path=%r; [%s] %s" % (mo_path, e.__class__.__name__, e)
)
else:
buffer = methods.get_buffer(path)
if name == category:
name = "source"
decomp_size = len(buffer)
buffer = methods.compress_buffer(buffer)
file.write(f"""\
inline constexpr const unsigned char _{category}_translation_{name}_compressed[] = {{
{methods.format_buffer(buffer, 1)}
}};
""")
xl_names.append([name, len(buffer), decomp_size])
file.write(f"""\
struct {category.capitalize()}TranslationList {{
const char* lang;
int comp_size;
int uncomp_size;
const unsigned char* data;
}};
inline constexpr {category.capitalize()}TranslationList _{category}_translations[] = {{
""")
for x in xl_names:
file.write(f'\t{{ "{x[0]}", {x[1]}, {x[2]}, _{category}_translation_{x[0]}_compressed }},\n')
file.write("""\
{ nullptr, 0, 0, nullptr },
};
""")