diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2019-04-09 15:42:36 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2019-04-09 15:42:36 +0200 |
| commit | b427aa1adca8fcfeca7f777499f0b4e1c84e3c6e (patch) | |
| tree | b096c6eb2a2bf3885436910894249452ccb1d813 /gui/scripts/integrate-into-app.py | |
| parent | 88031a653167df93396433fcf53a1422c2f995fd (diff) | |
| parent | 6405e1eebbe12313bed10f3f8bd1a0f051e83d24 (diff) | |
| download | mullvadvpn-b427aa1adca8fcfeca7f777499f0b4e1c84e3c6e.tar.xz mullvadvpn-b427aa1adca8fcfeca7f777499f0b4e1c84e3c6e.zip | |
Merge branch 'multi-catalogue'
Diffstat (limited to 'gui/scripts/integrate-into-app.py')
| -rw-r--r-- | gui/scripts/integrate-into-app.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/gui/scripts/integrate-into-app.py b/gui/scripts/integrate-into-app.py new file mode 100644 index 0000000000..9a853573c7 --- /dev/null +++ b/gui/scripts/integrate-into-app.py @@ -0,0 +1,102 @@ +""" +A helper script to integrate the generated geo data into the app. +""" + +import os +from os import path +from subprocess import Popen, PIPE +import shutil +import colorful as c + +SCRIPT_DIR = path.dirname(path.realpath(__file__)) +SOURCE_DIR = path.join(SCRIPT_DIR, "out") +GEO_ASSETS_DEST_DIR = path.realpath(path.join(SCRIPT_DIR, "../assets/geo")) +TRANSLATIONS_SOURCE_DIR = path.join(SOURCE_DIR, "locales") +TRANSLATIONS_DEST_DIR = path.realpath(path.join(SCRIPT_DIR, "../locales")) + +GEO_ASSETS_TO_COPY = [ + "cities.rbush.json", + "countries.rbush.json", + "geometry.json", + "geometry.rbush.json", + "states-provinces-lines.json", + "states-provinces-lines.rbush.json", +] + +TRANSLATIONS_TO_COPY = [ + "cities.po", + "countries.po" +] + +TRANSLATIONS_TO_MERGE = [ + "relay-locations.po" +] + +def remove_common_prefix(source, destination): + prefix_len = len(path.commonprefix((source, destination))) + return (source[prefix_len:], destination[prefix_len:]) + +def run_program(args): + p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) + errors = p.communicate()[1] + return (p.returncode, errors) + +def copy_geo_assets(): + for f in GEO_ASSETS_TO_COPY: + src = path.join(SOURCE_DIR, f) + dst = path.join(GEO_ASSETS_DEST_DIR, f) + + print "Copying {} to {}".format(*remove_common_prefix(src, dst)) + + shutil.copyfile(src, dst) + +def copy_and_merge_translations(): + for f in os.listdir(TRANSLATIONS_SOURCE_DIR): + src = path.join(TRANSLATIONS_SOURCE_DIR, f) + dst = path.join(TRANSLATIONS_DEST_DIR, f) + + if path.isdir(src): + merge_single_locale_folder(src, dst) + else: + print "Copying {} to {}".format(*remove_common_prefix(src, dst)) + shutil.copyfile(src, dst) + +def merge_single_locale_folder(src, dst): + for f in os.listdir(src): + src_po = path.join(src, f) + dst_po = path.join(dst, f) + + if f in TRANSLATIONS_TO_COPY: + print "Copying {} to {}".format(*remove_common_prefix(src_po, dst_po)) + shutil.copyfile(src_po, dst_po) + elif f in TRANSLATIONS_TO_MERGE: + if path.exists(dst_po): + pot_basename = path.basename(path.splitext(dst_po)[0]) + pot_path = path.join(TRANSLATIONS_DEST_DIR, pot_basename + ".pot") + + (msgmerge_code, msgmerge_errors) = run_program([ + "msgmerge", "--update", "--no-fuzzy-matching", dst_po, pot_path]) + + if msgmerge_code == 0: + (msgcat_code, msgcat_errors) = run_program([ + "msgcat", src_po, dst_po, "--output-file", dst_po]) + + if msgcat_code == 0: + print c.green("Merged and concatenated the catalogues.") + else: + print c.red("msgcat exited with {}: {}".format( + msgcat_code, msgcat_errors.decode('utf-8').strip())) + else: + print c.red("msgmerge exited with {}: {}".format( + msgmerge_code, msgmerge_errors.decode('utf-8').strip())) + else: + shutil.copy(src_po, dst_po) + else: + print c.orange("Unexpected file: {}".format(src_po)) + + +if not path.exists(GEO_ASSETS_DEST_DIR): + os.makedirs(GEO_ASSETS_DEST_DIR) + +copy_geo_assets() +copy_and_merge_translations() |
