-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhatch_build.py
More file actions
59 lines (44 loc) · 1.87 KB
/
hatch_build.py
File metadata and controls
59 lines (44 loc) · 1.87 KB
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
import shutil
import subprocess
from pathlib import Path
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
class TranslationFilesHook(BuildHookInterface):
"""Compile the GNU gettext translation files from their po-format into
their binary representing mo-format using 'msgfmt'.
"""
# Command used to compile po into mo files.
COMPILE_COMMAND = 'msgfmt'
def _check_compile_command(self):
"""Check if "msgfmt" is available."""
if not shutil.which(type(self).COMPILE_COMMAND):
raise OSError(
'Executable "{cmd}" (from GNU gettext tools) is not '
'available. Please install it via a package manager of '
'your trust. In most cases "{cmd}" is part of "gettext".'
.format(cmd=type(self).COMPILE_COMMAND)
)
def _compile_po_to_mo(self, po_file: Path, mo_file: Path):
""" Compile po-file to mo-file using "msgfmt".
"""
# Build command
cmd = [
type(self).COMPILE_COMMAND,
'--output-file={}'.format(mo_file),
po_file
]
# Execute command
rc = subprocess.run(cmd, check=False, text=True, capture_output=True)
# Validate output
if rc.stderr:
raise RuntimeError(rc.stderr)
def initialize(self, version, build_data):
print('|==-- Preparing translation files. --==|')
self._check_compile_command()
pkg_path = Path.cwd()
for in_file in pkg_path.glob('po/*.po'):
print(f'Processing "{in_file.name}"')
out_file = pkg_path / 'share/locale' / in_file.stem / 'LC_MESSAGES' / 'offpunk.mo'
# Create folder for output file
out_file.parent.mkdir(parents=True, exist_ok=True)
self._compile_po_to_mo(in_file, out_file)
print('|==-- Completed. --==|')