languages: Add new script update-pot.py

This commit is contained in:
Guðni Már Gilbert 2022-11-19 12:20:19 +00:00
parent 65d7605623
commit 53ced0cb21
2 changed files with 82 additions and 4 deletions

View File

@ -4,13 +4,22 @@ import bisect
import codecs import codecs
import polib import polib
import regex import regex
import os
import sys import sys
import lib.charset as cs import lib.charset as cs
from pathlib import Path, PurePosixPath from pathlib import Path, PurePosixPath
BASE_DIR: Path = Path.cwd()
FILE_LIST: list[Path] = [] FILE_LIST: list[Path] = []
# Absolute path
BASE_DIR: Path = Path.cwd().resolve()
PO_DIR: Path = BASE_DIR / "po"
# Pathlib can't change the working directory yet
# The script is currently made to assume the working
# directory is ./lang/po
os.chdir(PO_DIR)
def line_warning(path, line, msg): def line_warning(path, line, msg):
print(f'{path}:{line}: {msg}', file=sys.stderr) print(f'{path}:{line}: {msg}', file=sys.stderr)
@ -267,15 +276,15 @@ def main():
for path in args.file: for path in args.file:
if not Path(path).exists(): if not Path(path).exists():
# assume its regex # assume its regex, search for files that match pattern
for file in sorted(BASE_DIR.glob(path)): for file in sorted(PO_DIR.glob(path)):
FILE_LIST.append(file) FILE_LIST.append(file)
else: else:
FILE_LIST.append(Path(path)) FILE_LIST.append(Path(path))
# Convert the path to relative and use Posix format # Convert the path to relative and use Posix format
for index, absolute_path in enumerate(FILE_LIST[:]): for index, absolute_path in enumerate(FILE_LIST[:]):
FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(BASE_DIR) FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(PO_DIR)
# extract strings # extract strings
catalog = {} catalog = {}

69
lang/update-pot.py Normal file
View File

@ -0,0 +1,69 @@
#!/usr/bin/env python3
"""
Script updates the Firmware.pot file.
The script does the following:
1. Current Firmware.pot is backed up with a copy, Firmware.pot.bak
2. Runs lang-extract.py with all the correct arguments.
"""
import sys
import os
from pathlib import Path, PurePath, PurePosixPath
import shutil
import subprocess
from subprocess import CalledProcessError
BASE_DIR: Path = Path.cwd().resolve()
PROJECT_DIR: Path = BASE_DIR.parent
PO_DIR: Path = BASE_DIR / "po"
SEARCH_REGEX: str = "[a-zA-Z]*.[ch]*"
SEARCH_PATHS: list[str] = ["./Firmware", "./Firmware/mmu2"]
def main():
# Start by creating a back-up of the current Firmware.pot
shutil.copy(PO_DIR / "Firmware.pot", PO_DIR / "Firmware.pot.bak")
# Get the relative prepend of Project directory relative to ./po directory
# This should be something like '../../'
# Note: Pathlib's relative_to() doesn't handle this case yet, so let's use os module
rel_path = os.path.relpath(PROJECT_DIR, PO_DIR)
# We want to search for the C/C++ files relative to the .po/ directory
# Lets append to the search path an absolute path.
for index, search_path in enumerate(SEARCH_PATHS[:]):
try:
# Example: Converts ./Firmware to ../../Firmware
SEARCH_PATHS[index] = PurePath(rel_path).joinpath(search_path)
# Example: Convert ../../Firmware to ../../Firmware/[a-zA-Z]*.[ch]*
SEARCH_PATHS[index] = PurePosixPath(SEARCH_PATHS[index]).joinpath(SEARCH_REGEX)
except ValueError as error:
print(error)
# Run the lang-extract.py script
try:
subprocess.check_call(
[
"python",
"lang-extract.py",
"--no-missing",
"-s",
"-o",
"./Firmware.pot",
SEARCH_PATHS[0],
SEARCH_PATHS[1],
]
)
except CalledProcessError as error:
print(error)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(-1)