summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/scripts/integrate-relay-locations.py
blob: e99b6a211e478db631cf9982afa2c9aaabfbfa66 (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
#!/usr/bin/env python3

"""
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__))

# The name of the relay locations gettext catalogue template file
RELAY_LOCATIONS_POT_FILENAME = "relay-locations.pot"

# The directory with the generated content
GENERATED_CONTENT_OUTPUT_PATH = path.join(SCRIPT_DIR, "out")

# The directory with the generated localizations content
GENERATED_TRANSLATIONS_PATH = path.join(GENERATED_CONTENT_OUTPUT_PATH, "locales")

# The directory with the existing app localizations
APP_TRANSLATIONS_PATH = path.realpath(path.join(SCRIPT_DIR, "../locales"))

def merge_relay_locations_catalogue_template():
  existing_pot_file = path.join(APP_TRANSLATIONS_PATH, RELAY_LOCATIONS_POT_FILENAME)
  generated_pot_file = path.join(GENERATED_TRANSLATIONS_PATH, RELAY_LOCATIONS_POT_FILENAME)

  merge_gettext_catalogues(existing_pot_file, generated_pot_file)


def merge_gettext_catalogues(existing_catalogue_file, generated_catalogue_file):
  if path.exists(existing_catalogue_file):
    args = (
      existing_catalogue_file, generated_catalogue_file,

      "--output-file", existing_catalogue_file,

      # ensure that the first occurrence takes precedence in merge conflict
      "--use-first",

      # sort by msgid
      "--sort-output",

      # disable wrapping long strings because crowdin does not do that
      "--no-wrap"
    )

    (exit_code, errors) = run_program("msgcat", *args)

    if exit_code == 0:
      print(c.green("Merged {} into {}.".format(generated_catalogue_file, existing_catalogue_file)))
    else:
      print(c.red("msgcat exited with {}: {}".format(exit_code, errors.decode().strip())))
  else:
    print(c.orange("The existing catalogue does not exist. Copying {} to {}")
      .format(generated_catalogue_file, existing_catalogue_file))
    shutil.copyfile(generated_catalogue_file, existing_catalogue_file)


def run_program(*args):
  with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as subproc:
    print("Run: {}".format(' '.join(args)))

    errors = subproc.communicate()[1]
    return (subproc.returncode, errors)


# Program main()

def main():
  merge_relay_locations_catalogue_template()

main()