forked from MarlinFirmware/Marlin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request MarlinFirmware#3754 from gudnimg/languages-gudni
Update language script to run on Windows
- Loading branch information
Showing
5 changed files
with
183 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
polib==1.1.1 | ||
pyelftools==0.29 | ||
regex==2022.9.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
Portable script to update po files on most platforms | ||
""" | ||
|
||
import argparse | ||
from sys import stderr, exit | ||
import shutil | ||
from pathlib import Path | ||
import polib | ||
from polib import POFile | ||
|
||
BASE_DIR: Path = Path.absolute(Path(__file__).parent) | ||
PO_DIR: Path = BASE_DIR / "po" | ||
PO_FILE_LIST: list[Path] = [] | ||
POT_REFERENCE: POFile = polib.pofile(PO_DIR/'Firmware.pot') | ||
|
||
|
||
def main(): | ||
global PO_FILE_LIST | ||
ap = argparse.ArgumentParser() | ||
group = ap.add_mutually_exclusive_group(required=True) | ||
group.add_argument('-f', '--file', help='File path for a single PO file to update Example: ./po/Firmware_cs.po') | ||
group.add_argument('--all', action='store_true', help='Update all PO files at once') | ||
args = ap.parse_args() | ||
|
||
if args.all: | ||
PO_FILE_LIST = sorted(PO_DIR.glob('**/*.po')) | ||
elif args.file: | ||
if Path(args.file).is_file(): | ||
PO_FILE_LIST.append(Path(args.file)) | ||
else: | ||
print("{}: file does not exist or is not a regular file".format(args.file), file=stderr) | ||
return 1 | ||
|
||
for po_file in PO_FILE_LIST: | ||
# Start by creating a back-up of the .po file | ||
po_file_bak = po_file.with_suffix(".bak") | ||
shutil.copy(PO_DIR / po_file.name, PO_DIR / po_file_bak.name) | ||
po = polib.pofile(po_file) | ||
po.merge(POT_REFERENCE) | ||
po.save(po_file) | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except KeyboardInterrupt: | ||
exit(-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/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 | ||
|
||
# Constants | ||
BASE_DIR: Path = Path.absolute(Path(__file__).parent) | ||
PROJECT_DIR: Path = BASE_DIR.parent | ||
PO_DIR: Path = BASE_DIR / "po" | ||
|
||
# Regex pattern to search for source files | ||
SEARCH_REGEX: str = "[a-zA-Z]*.[ch]*" | ||
|
||
# Folders to search for messages | ||
SEARCH_PATHS: list[str] = ["./Firmware", "./Firmware/mmu2"] | ||
|
||
|
||
def main(): | ||
# List of source files to extract messages from | ||
FILE_LIST: list[Path] = [] | ||
|
||
# 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.copy()): | ||
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) | ||
|
||
# Extend the glob and append all found files into FILE_LIST | ||
for pattern in SEARCH_PATHS: | ||
for file in sorted(PO_DIR.glob(str(pattern))): | ||
FILE_LIST.append(file) | ||
|
||
# Convert the path to relative and use Posix format | ||
for index, absolute_path in enumerate(FILE_LIST.copy()): | ||
FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(PO_DIR) | ||
|
||
# Run the lang-extract.py script | ||
SCRIPT_PATH = BASE_DIR.joinpath("lang-extract.py") | ||
try: | ||
subprocess.check_call( | ||
[ | ||
"python", | ||
SCRIPT_PATH, | ||
"--no-missing", | ||
"-s", | ||
"-o", | ||
"./Firmware.pot", | ||
*FILE_LIST, | ||
] | ||
) | ||
except CalledProcessError as error: | ||
print(error) | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except KeyboardInterrupt: | ||
sys.exit(-1) |