From ac435adda3b69596f97ff70c59b0703d6988e845 Mon Sep 17 00:00:00 2001 From: dkanada Date: Mon, 20 Jan 2020 10:38:53 +0900 Subject: [PATCH] add a folder for useful scripts --- scripts/scdup.py | 41 +++++++++++++++++++++++++++++++++++++++++ scripts/scgen.py | 40 ++++++++++++++++++++++++++++++++++++++++ scripts/scrm.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 scripts/scdup.py create mode 100644 scripts/scgen.py create mode 100644 scripts/scrm.py diff --git a/scripts/scdup.py b/scripts/scdup.py new file mode 100644 index 000000000..468e31f14 --- /dev/null +++ b/scripts/scdup.py @@ -0,0 +1,41 @@ +import sys +import os +import json + +# load every key in the source language +# check the keys in all translations +# remove keys that only exist in translations + +cwd = os.getcwd() +langdir = cwd + '/../src/strings' +langlst = os.listdir(langdir) + +langlst.remove('en-us.json') +print(langlst) +input('press enter to continue') + +keysus = [] +with open(langdir + '/' + 'en-us.json') as en: + langus = json.load(en) + for key in langus: + keysus.append(key) + +for lang in langlst: + with open(langdir + '/' + lang, 'r') as f: + inde = 2 + if '\n \"' in f.read(): + inde = 4 + f.close() + with open(langdir + '/' + lang, 'r+') as f: + langjson = json.load(f) + langjnew = {} + for key in langjson: + if key in keysus: + langjnew[key] = langjson[key] + f.seek(0) + f.write(json.dumps(langjnew, indent=inde, sort_keys=False, ensure_ascii=False)) + f.write('\n') + f.truncate() + f.close() + +print('DONE') diff --git a/scripts/scgen.py b/scripts/scgen.py new file mode 100644 index 000000000..0d831426e --- /dev/null +++ b/scripts/scgen.py @@ -0,0 +1,40 @@ +import os +import subprocess +import json + +# load all keys in the source language +# check entire codebase for usages +# print unused keys to a text file +# TODO: dynamic string usages cause false positives + +cwd = os.getcwd() +langdir = cwd + '/../src/strings' +langlst = [] +langlst.append('en-us.json') + +# unused keys +dep = [] + +def grep(key): + command = 'grep -r -E "(\(\\\"|\(\'|\{)%s(\\\"|\'|\})" --include=\*.{js,html} --exclude-dir=../src/strings ../src' % key + p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + output = p.stdout.readlines() + if output: + print('DONE: ' + key) + return True + print('UNUSED: ' + key) + dep.append(key) + return False + +for lang in langlst: + with open(langdir + '/' + lang) as f: + langjson = json.load(f) + for key in langjson: + grep(key) + +print(dep) +print('LENGTH: ' + str(len(dep))) +with open('scout.txt', 'w') as out: + for item in dep: + out.write(item + '\n') + out.close() diff --git a/scripts/scrm.py b/scripts/scrm.py new file mode 100644 index 000000000..9bd5bc2a4 --- /dev/null +++ b/scripts/scrm.py @@ -0,0 +1,34 @@ +import sys +import os +import json + +# load text file containing unused keys +# remove the keys from all string files + +cwd = os.getcwd() +langdir = cwd + '/../src/strings' +langlst = os.listdir(langdir) + +keys = [] + +with open('scout.txt', 'r') as f: + for line in f: + keys.append(line.strip('\n')) + +for lang in langlst: + with open(langdir + '/' + lang, 'r') as f: + inde = 2 + if '\n \"' in f.read(): + inde = 4 + f.close() + with open(langdir + '/' + lang, 'r+') as f: + langjson = json.load(f) + for key in keys: + langjson.pop(key, None) + f.seek(0) + f.write(json.dumps(langjson, indent=inde, sort_keys=False, ensure_ascii=False)) + f.write('\n') + f.truncate() + f.close() + +print('DONE')