| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import argparse |
| 10 |
import hashlib |
| 11 |
import json |
| 12 |
import os |
| 13 |
import subprocess |
| 14 |
import tempfile |
| 15 |
import zipfile |
| 16 |
|
| 17 |
MANIFEST_NAME = "opencc-resource-manifest.json" |
| 18 |
|
| 19 |
|
| 20 |
def parse_args(): |
| 21 |
parser = argparse.ArgumentParser( |
| 22 |
description="Convert a text-backed resource zip to an ocd2-backed resource zip." |
| 23 |
) |
| 24 |
parser.add_argument( |
| 25 |
"--input", required=True, help="Input resource zip file (text-backed)" |
| 26 |
) |
| 27 |
parser.add_argument( |
| 28 |
"--output", required=True, help="Output resource zip file (ocd2-backed)" |
| 29 |
) |
| 30 |
parser.add_argument( |
| 31 |
"--opencc-dict", required=True, help="Path to opencc_dict executable" |
| 32 |
) |
| 33 |
return parser.parse_args() |
| 34 |
|
| 35 |
|
| 36 |
def convert_dict_references(value): |
| 37 |
if isinstance(value, dict): |
| 38 |
converted = {} |
| 39 |
for key, child in value.items(): |
| 40 |
if key == "type" and child == "text": |
| 41 |
converted[key] = "ocd2" |
| 42 |
elif ( |
| 43 |
key == "file" |
| 44 |
and isinstance(child, str) |
| 45 |
and child.endswith(".txt") |
| 46 |
): |
| 47 |
converted[key] = child[:-4] + ".ocd2" |
| 48 |
else: |
| 49 |
converted[key] = convert_dict_references(child) |
| 50 |
return converted |
| 51 |
if isinstance(value, list): |
| 52 |
return [convert_dict_references(child) for child in value] |
| 53 |
return value |
| 54 |
|
| 55 |
|
| 56 |
def sha256_hash(data): |
| 57 |
return hashlib.sha256(data).hexdigest() |
| 58 |
|
| 59 |
|
| 60 |
def main(): |
| 61 |
args = parse_args() |
| 62 |
|
| 63 |
if not os.path.exists(args.input): |
| 64 |
raise FileNotFoundError(f"Input zip file not found: {args.input}") |
| 65 |
|
| 66 |
output_dir = os.path.dirname(args.output) |
| 67 |
if output_dir: |
| 68 |
os.makedirs(output_dir, exist_ok=True) |
| 69 |
|
| 70 |
with zipfile.ZipFile(args.input, "r") as in_zip: |
| 71 |
|
| 72 |
try: |
| 73 |
manifest_content = in_zip.read(MANIFEST_NAME).decode("utf-8") |
| 74 |
except KeyError: |
| 75 |
raise ValueError( |
| 76 |
f"Manifest file {MANIFEST_NAME} not found in the input zip" |
| 77 |
) |
| 78 |
|
| 79 |
manifest = json.loads(manifest_content) |
| 80 |
in_names = in_zip.namelist() |
| 81 |
|
| 82 |
new_entries = {} |
| 83 |
out_contents = {} |
| 84 |
out_zipinfos = {} |
| 85 |
|
| 86 |
with tempfile.TemporaryDirectory() as temp_dir: |
| 87 |
|
| 88 |
txt_files = [name for name in in_names if name.endswith(".txt")] |
| 89 |
for name in txt_files: |
| 90 |
in_zip.extract(name, temp_dir) |
| 91 |
|
| 92 |
|
| 93 |
for name in txt_files: |
| 94 |
base_name = name[:-4] |
| 95 |
ocd2_name = base_name + ".ocd2" |
| 96 |
|
| 97 |
txt_path = os.path.join(temp_dir, name) |
| 98 |
ocd2_path = os.path.join(temp_dir, ocd2_name) |
| 99 |
|
| 100 |
|
| 101 |
subprocess.run( |
| 102 |
[ |
| 103 |
args.opencc_dict, |
| 104 |
"-i", |
| 105 |
txt_path, |
| 106 |
"-o", |
| 107 |
ocd2_path, |
| 108 |
"-f", |
| 109 |
"text", |
| 110 |
"-t", |
| 111 |
"ocd2", |
| 112 |
], |
| 113 |
check=True, |
| 114 |
) |
| 115 |
|
| 116 |
|
| 117 |
with open(ocd2_path, "rb") as f: |
| 118 |
ocd2_data = f.read() |
| 119 |
|
| 120 |
out_contents[ocd2_name] = ocd2_data |
| 121 |
|
| 122 |
|
| 123 |
orig_info = in_zip.getinfo(name) |
| 124 |
info = zipfile.ZipInfo(ocd2_name) |
| 125 |
info.date_time = orig_info.date_time |
| 126 |
info.compress_type = zipfile.ZIP_STORED |
| 127 |
info.external_attr = 0o644 << 16 |
| 128 |
out_zipinfos[ocd2_name] = info |
| 129 |
|
| 130 |
new_entries[ocd2_name] = { |
| 131 |
"sha256": sha256_hash(ocd2_data), |
| 132 |
"size": len(ocd2_data), |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
json_files = [ |
| 137 |
name |
| 138 |
for name in in_names |
| 139 |
if name.endswith(".json") and name != MANIFEST_NAME |
| 140 |
] |
| 141 |
for name in json_files: |
| 142 |
config_content = in_zip.read(name).decode("utf-8") |
| 143 |
config = json.loads(config_content) |
| 144 |
converted_config = convert_dict_references(config) |
| 145 |
|
| 146 |
|
| 147 |
new_json_str = ( |
| 148 |
json.dumps(converted_config, ensure_ascii=False, indent=2) |
| 149 |
+ "\n" |
| 150 |
) |
| 151 |
new_json_bytes = new_json_str.encode("utf-8") |
| 152 |
|
| 153 |
out_contents[name] = new_json_bytes |
| 154 |
|
| 155 |
orig_info = in_zip.getinfo(name) |
| 156 |
info = zipfile.ZipInfo(name) |
| 157 |
info.date_time = orig_info.date_time |
| 158 |
info.compress_type = zipfile.ZIP_STORED |
| 159 |
info.external_attr = 0o644 << 16 |
| 160 |
out_zipinfos[name] = info |
| 161 |
|
| 162 |
new_entries[name] = { |
| 163 |
"sha256": sha256_hash(new_json_bytes), |
| 164 |
"size": len(new_json_bytes), |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
new_manifest = { |
| 169 |
"manifest_version": manifest.get("manifest_version", 1), |
| 170 |
"source_url": manifest.get("source_url", ""), |
| 171 |
"commit_id": manifest.get("commit_id", ""), |
| 172 |
"source_dirty": manifest.get("source_dirty", False), |
| 173 |
"build_time_utc": manifest.get("build_time_utc", ""), |
| 174 |
"hash_algorithm": "sha256", |
| 175 |
"entries": new_entries, |
| 176 |
} |
| 177 |
|
| 178 |
new_manifest_str = ( |
| 179 |
json.dumps( |
| 180 |
new_manifest, ensure_ascii=False, indent=2, sort_keys=True |
| 181 |
) |
| 182 |
+ "\n" |
| 183 |
) |
| 184 |
new_manifest_bytes = new_manifest_str.encode("utf-8") |
| 185 |
|
| 186 |
|
| 187 |
with zipfile.ZipFile(args.output, "w") as out_zip: |
| 188 |
|
| 189 |
for name in sorted(out_contents.keys()): |
| 190 |
out_zip.writestr(out_zipinfos[name], out_contents[name]) |
| 191 |
|
| 192 |
|
| 193 |
manifest_info = zipfile.ZipInfo(MANIFEST_NAME) |
| 194 |
manifest_info.date_time = in_zip.getinfo(MANIFEST_NAME).date_time |
| 195 |
manifest_info.compress_type = zipfile.ZIP_STORED |
| 196 |
manifest_info.external_attr = 0o644 << 16 |
| 197 |
out_zip.writestr(manifest_info, new_manifest_bytes) |
| 198 |
|
| 199 |
|
| 200 |
if __name__ == "__main__": |
| 201 |
main() |