-
Notifications
You must be signed in to change notification settings - Fork 0
/
gettext_rs.py
executable file
·83 lines (62 loc) · 2.58 KB
/
gettext_rs.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
#!/usr/bin/env python3
import os
import subprocess
from pathlib import Path
from typing import Optional
import utils
from utils import info, c_input
class Project:
def __init__(self, directory: Path, src_dir: Path, build_dir: Path):
self.directory = directory
self.src_dir = src_dir
self.build_dir = build_dir
self.project_name = self._get_project_name()
def _get_project_name(self) -> Optional[str]:
matches = utils.find_in_file(r"project\([\r\n]*.*'(.*)'", self.directory / 'meson.build')
if len(matches) < 1:
return None
return matches[0]
def replace_gettext_macros(self) -> None:
info("Replacing 'gettext!' with 'gettext'...")
subprocess.run(
["find", self.src_dir, "-type", "f", "-exec",
"sed", "-i", "s/gettext!/gettext/g", "{}", ";"],
check=True
)
info("Successfully replaced 'gettext!' with 'gettext'")
def generate_pot_files(self) -> None:
info("Generating pot file...")
subprocess.run(["ninja", "-C", self.build_dir, f"{self.project_name}-pot"], check=True)
info("Pot file has been successfully generated")
def restore_directory(self) -> None:
info("Restoring src directory...")
subprocess.run(["git", "restore", self.src_dir], check=True)
info("The src directory has been restored to previous state")
def main(src_dir: Path, build_dir: Path) -> None:
if c_input(
"Commit or stash unsaved changes before proceeding. Proceed? [y/N]"
) not in ("y", "Y"):
return
project_dir = src_dir.parent
project = Project(project_dir, src_dir, build_dir)
try:
project.replace_gettext_macros()
project.generate_pot_files()
except subprocess.CalledProcessError as error:
info(f"An error has occured: {error}")
finally:
project.restore_directory()
info(f"Project src dir found was {project.src_dir}")
info(f"Project build dir found was {project.build_dir}")
info(f"Project name found was {project.project_name}")
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--src-dir', type=Path, required=False,
default=Path(os.getcwd()) / 'src',
help="The source directory")
parser.add_argument('-b', '--build-dir', type=Path, required=False,
default=Path(os.getcwd()) / '_build',
help="The building directory")
args = parser.parse_args()
main(args.src_dir, args.build_dir)