TS3-Translation_zh-CN/maker.py

142 lines
5.1 KiB
Python
Raw Normal View History

2019-01-28 22:31:19 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
2019-02-01 15:07:02 +08:00
import re
2019-01-28 22:31:19 +08:00
import sys
import zipfile
import subprocess
2019-02-15 18:55:29 +08:00
from urllib import request, parse, error
2019-01-28 22:31:19 +08:00
from time import strftime, localtime
target_version = "3.6.2"
2019-01-28 22:31:19 +08:00
language = "zh"
cwd = sys.path[0]
src = f"{cwd + os.sep}src{os.sep}"
dist = f"{cwd + os.sep}dist{os.sep}"
ini = f"{dist}package.ini"
def make_release():
if os.name == 'nt':
lrelease = os.popen("where lrelease").read().strip() # set in path.
2019-02-09 20:49:43 +08:00
else:
# os.name == 'posix'
lrelease = os.popen("which lrelease").read().strip()
2019-01-28 22:31:19 +08:00
# source_file = [f[:-3] for f in os.listdir(src) if os.path.isfile(os.path.join(src, f)) and f[-5:-3] == language]
source_file = [f[:-3] for f in os.listdir(src) if f[-5:-3] == language]
2019-02-09 20:49:43 +08:00
if len(source_file) == 0:
print("Input file not found. Exit.")
2019-01-28 22:31:19 +08:00
release_file = []
2019-02-01 15:07:02 +08:00
translated_count = 0
total_count = 0
2019-02-09 20:49:43 +08:00
if not os.path.exists(dist):
print("Warning: \"dist\" folder not found. Creating folder...\n")
os.makedirs(dist)
2019-02-01 15:07:02 +08:00
2019-02-15 18:55:29 +08:00
translated = re.compile(r"(?:Generated\s)(\d+)(?: translation)")
untranslated = re.compile(r"(?:Ignored\s)(\d+)(?: untranslated)")
error = False
2019-01-28 22:31:19 +08:00
for i in source_file:
print(i)
result = subprocess.run([lrelease, f'{src+i}.ts', '-qm', f'{dist+i}.qm'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2019-02-09 20:49:43 +08:00
try:
result_info = result.stdout.decode("utf-8")
except UnicodeDecodeError:
result_info = result.stdout.decode("gbk") # 中文系统可能会遇到编码问题
2019-02-01 15:07:02 +08:00
print(result_info)
2019-01-28 22:31:19 +08:00
if result.returncode == 0:
2019-02-09 20:49:43 +08:00
release_file.append(f"{i}.qm")
2019-02-01 15:07:02 +08:00
if i != "qt_zh":
translated_count += int(translated.findall(result_info)[0])
total_count += int(translated.findall(result_info)[0])
untranslated_count = untranslated.findall(result_info)
total_count += int(untranslated_count[0]) if len(untranslated_count) != 0 else 0
2019-01-28 22:31:19 +08:00
else:
error = True
2019-02-09 20:49:43 +08:00
try:
2019-02-15 18:55:29 +08:00
print(f"发生错误:\n{result.stderr.decode('utf-8')}")
2019-02-09 21:06:15 +08:00
telegram_push(f"发生错误:\n{i}\n{result.stderr.decode('utf-8')}")
2019-02-09 20:49:43 +08:00
except UnicodeDecodeError:
2019-02-15 18:55:29 +08:00
print(f"发生错误:\n{result.stderr.decode('gbk')}") # 中文系统可能会遇到编码问题
2019-02-09 21:06:15 +08:00
telegram_push(f"发生错误:\n{i}\n{result.stderr.decode('gbk')}")
2019-01-28 22:31:19 +08:00
2019-02-01 15:07:02 +08:00
# except subprocess.CalledProcessError as err:
2019-01-28 22:31:19 +08:00
# print("lrelease error:")
# print(err)'''
if error:
raise RuntimeError
2019-02-01 15:07:02 +08:00
send_progress(translated_count, total_count)
2019-01-28 22:31:19 +08:00
return release_file
2019-02-01 15:07:02 +08:00
def send_progress(done, total):
2019-02-07 16:11:41 +08:00
percentage = round(done*100/total, 2)
2019-02-09 20:49:43 +08:00
output = f"当前进度:\n{done}/{total}\n{percentage}%\n"
2019-02-07 16:11:41 +08:00
print(output)
try:
2019-02-09 21:06:15 +08:00
assert 0 == telegram_push(output, 1)
2019-02-09 20:49:43 +08:00
print("推送成功\n")
except AssertionError:
print("推送被取消\n")
2019-02-15 18:55:29 +08:00
# except Exception as err:
# print(f"发生错误,推送失败\n错误信息{err}\n")
2019-02-07 16:11:41 +08:00
2019-02-09 21:06:15 +08:00
def telegram_push(string, debug=0):
2019-02-15 18:55:29 +08:00
if not string:
if debug:
print('No message to send.')
return 1
2019-02-07 16:11:41 +08:00
querystring = parse.urlencode({"text": string.encode('utf-8')})
2019-02-01 15:07:02 +08:00
tg_api = os.getenv('TG_API')
group_id = os.getenv('TG_GROUP_ID')
2019-02-15 19:00:20 +08:00
if not tg_api or not group_id:
2019-02-09 21:06:15 +08:00
if debug:
print("Telegram api key or chat(group) id not found.")
print("You need to set TG_API and TG_GROUP_ID in the environment variable.")
2019-02-09 20:49:43 +08:00
return 1
2019-02-01 15:07:02 +08:00
url = f"https://api.telegram.org/bot{tg_api}/sendMessage?chat_id={group_id}&"
2019-02-15 18:55:29 +08:00
try:
request.urlopen(url + querystring)
return 0
except error.URLError:
if debug:
print("Unable to connect to telegram server, skip message sending operation.")
return 1
2019-02-01 15:07:02 +08:00
2019-01-28 22:31:19 +08:00
def make_package(release_list):
timestamp = strftime("%Y%m%d%H%M%S", localtime())
print("Write package info to package.ini ...")
with open(ini, "w", encoding="utf-8") as f:
package_info = [f"Name = TeamSpeak 3 简体中文汉化包 软件版本:{target_version}",
"Type = Translation",
2019-02-07 16:11:41 +08:00
"Author = 寂听 & EdisonJwa",
2019-02-01 15:07:02 +08:00
f"Version = {timestamp}",
2019-01-28 22:31:19 +08:00
"Platforms = ",
2022-08-15 23:26:28 +08:00
'Description = 源代码: https://github.com/VigorousPro/TS3-Translation_zh-CN']
2019-01-28 22:31:19 +08:00
f.write("\n".join(package_info))
2019-01-29 12:03:10 +08:00
file_name = 'Chinese_Translation_zh-CN.ts3_translation'
2019-01-28 22:31:19 +08:00
print("Zip package ...")
with zipfile.ZipFile(file_name, 'w', zipfile.ZIP_DEFLATED) as release:
release.write(ini, "package.ini")
for i in release_list:
release.write(dist+i, f"translations/{i}")
print("语言包生成成功")
2019-01-28 22:31:19 +08:00
if __name__ == '__main__':
2019-02-07 16:11:41 +08:00
if len(sys.argv) <= 1:
print("Making .qm translations file ...")
release_file_list = make_release()
print("Making .ts3_translation release package ...")
make_package(release_file_list)
else:
if sys.argv[1] == "1":
telegram_push("构建成功")