summaryrefslogtreecommitdiffhomepage
path: root/gui/scripts/integrate-into-app.py
blob: 18725fac800ebc0eeda9a0b0e0b3051ad1db421d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
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)

  print "Run: {}".format(' '.join(args))

  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()