Use python iconv if system iconv is broken (#1156)

* use python iconv if system iconv is broken

* black
This commit is contained in:
Alex Bates 2024-01-21 20:37:54 +00:00 committed by GitHub
parent 260b96faf9
commit 1d0c818088
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View File

@ -357,6 +357,24 @@ def write_ninja_for_tools(ninja: ninja_syntax.Writer):
ninja.build(CRC_TOOL, "cc_tool", f"{BUILD_TOOLS}/rom/n64crc.c") ninja.build(CRC_TOOL, "cc_tool", f"{BUILD_TOOLS}/rom/n64crc.c")
def does_iconv_work() -> bool:
# run iconv and see if it works
stdin = "エリア _".encode("utf-8")
def run(command, stdin):
sub = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, input=stdin)
return sub.stdout
expected_stdout = run(["tools/build/iconv.py", "UTF-8", "CP932"], stdin)
actual_stdout = run(["iconv", "--from", "UTF-8", "--to", "CP932"], stdin)
return expected_stdout == actual_stdout
use_python_iconv = not does_iconv_work()
if use_python_iconv:
print("warning: iconv doesn't work, using python implementation")
class Configure: class Configure:
def __init__(self, version: str): def __init__(self, version: str):
self.version = version self.version = version
@ -677,7 +695,10 @@ class Configure:
if version == "ique": if version == "ique":
encoding = "EUC-JP" encoding = "EUC-JP"
iconv = f"iconv --from UTF-8 --to {encoding}" if use_python_iconv:
iconv = f"tools/build/iconv.py UTF-8 {encoding}"
else:
iconv = f"iconv --from UTF-8 --to {encoding}"
# use tools/sjis-escape.py for src/battle/area/tik2/area.c # use tools/sjis-escape.py for src/battle/area/tik2/area.c
if version != "ique" and seg.dir.parts[-3:] == ("battle", "area", "tik2") and seg.name == "area": if version != "ique" and seg.dir.parts[-3:] == ("battle", "area", "tik2") and seg.name == "area":

23
tools/build/iconv.py Executable file
View File

@ -0,0 +1,23 @@
#!/usr/bin/python3
import argparse
import sys
parser = argparse.ArgumentParser(description="Convert a file from one encoding to another")
parser.add_argument("f")
parser.add_argument("t")
parser.add_argument("infile", nargs="?", type=argparse.FileType("r"), default=sys.stdin)
parser.add_argument("outfile", nargs="?", type=argparse.FileType("w"), default=sys.stdout)
args = parser.parse_args()
def main(args):
sys.stdin.reconfigure(encoding=args.f)
in_data = args.infile.read()
sys.stdout.reconfigure(encoding=args.t)
args.outfile.write(in_data)
if __name__ == "__main__":
main(parser.parse_args())