Avoid unnecessary computations
This refactors the script for cleaning the translations to get the same result by doing less. This is achieved by the following changes: - Use glob-patterns for finding the files to clean more efficiently, without the need to exclude collected files again. - Only write files which are supposed to be modified (previously all portable object files did get rewritten by this script, no matter if it did change something or not). - Stop searching for sections in files to clean up, once they are already passed.
This commit is contained in:
parent
c59030857d
commit
57308bb847
@ -27,51 +27,64 @@ However that needs to be fixed on the transifex side, see rP25896. For now
|
|||||||
strip the e-mails using this script.
|
strip the e-mails using this script.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import fileinput
|
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
|
|
||||||
from i18n_helper import L10N_FOLDER_NAME, PROJECT_ROOT_DIRECTORY, TRANSIFEX_CLIENT_FOLDER
|
from i18n_helper import L10N_FOLDER_NAME, PROJECT_ROOT_DIRECTORY, TRANSIFEX_CLIENT_FOLDER
|
||||||
|
|
||||||
|
|
||||||
|
TRANSLATOR_REGEX = re.compile(r"^(#\s+[^,<]*)\s+<.*>(.*)$")
|
||||||
|
LAST_TRANSLATION_REGEX = re.compile(r"^(\"Last-Translator:[^,<]*)\s+<.*>(.*)$")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
translator_match = re.compile(r"^(#\s+[^,<]*)\s+<.*>(.*)")
|
for folder in glob.iglob(
|
||||||
last_translator_match = re.compile(r"^(\"Last-Translator:[^,<]*)\s+<.*>(.*)")
|
f"**/{L10N_FOLDER_NAME}/{TRANSIFEX_CLIENT_FOLDER}/",
|
||||||
|
root_dir=PROJECT_ROOT_DIRECTORY,
|
||||||
|
recursive=True,
|
||||||
|
):
|
||||||
|
for file in glob.iglob(
|
||||||
|
f"{os.path.join(folder, os.pardir)}/*.po", root_dir=PROJECT_ROOT_DIRECTORY
|
||||||
|
):
|
||||||
|
absolute_file_path = os.path.abspath(f"{PROJECT_ROOT_DIRECTORY}/{file}")
|
||||||
|
|
||||||
for root, folders, _ in os.walk(PROJECT_ROOT_DIRECTORY):
|
file_content = []
|
||||||
for folder in folders:
|
usernames = []
|
||||||
if folder != L10N_FOLDER_NAME:
|
changes = False
|
||||||
continue
|
in_translators = False
|
||||||
|
found_last_translator = False
|
||||||
if not os.path.exists(os.path.join(root, folder, TRANSIFEX_CLIENT_FOLDER)):
|
with open(absolute_file_path, "r+", encoding="utf-8") as fd:
|
||||||
continue
|
for line in fd:
|
||||||
|
if line.strip() == "# Translators:":
|
||||||
path = os.path.join(root, folder, "*.po")
|
in_translators = True
|
||||||
files = glob.glob(path)
|
elif not line.strip().startswith("#"):
|
||||||
for file in files:
|
in_translators = False
|
||||||
usernames = []
|
elif in_translators:
|
||||||
reached = False
|
|
||||||
for line in fileinput.input(
|
|
||||||
file.replace("\\", "/"), inplace=True, encoding="utf-8"
|
|
||||||
):
|
|
||||||
if reached:
|
|
||||||
if line == "# \n":
|
if line == "# \n":
|
||||||
line = ""
|
changes = True
|
||||||
m = translator_match.match(line)
|
continue
|
||||||
if m:
|
translator_match = TRANSLATOR_REGEX.match(line)
|
||||||
if m.group(1) in usernames:
|
if translator_match:
|
||||||
line = ""
|
changes = True
|
||||||
else:
|
if translator_match.group(1) in usernames:
|
||||||
line = m.group(1) + m.group(2) + "\n"
|
continue
|
||||||
usernames.append(m.group(1))
|
line = TRANSLATOR_REGEX.sub(r"\1\2", line)
|
||||||
m2 = last_translator_match.match(line)
|
usernames.append(translator_match.group(1))
|
||||||
if m2:
|
|
||||||
line = re.sub(last_translator_match, r"\1\2", line)
|
if not in_translators and not found_last_translator:
|
||||||
elif line.strip() == "# Translators:":
|
last_translator_match = LAST_TRANSLATION_REGEX.match(line)
|
||||||
reached = True
|
if last_translator_match:
|
||||||
sys.stdout.write(line)
|
found_last_translator = True
|
||||||
|
changes = True
|
||||||
|
line = LAST_TRANSLATION_REGEX.sub(r"\1\2", line)
|
||||||
|
|
||||||
|
file_content.append(line)
|
||||||
|
|
||||||
|
if changes:
|
||||||
|
fd.seek(0)
|
||||||
|
fd.truncate()
|
||||||
|
fd.writelines(file_content)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user