summaryrefslogtreecommitdiffstats
path: root/comm/python/l10n/missing_ftl/__init__.py
blob: 1e90db6d5bf18901ff87fa77e6179a3353897b08 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this,
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os


def walk_path(folder_path, prefix):
    file_list = []
    for root, dirs, files in os.walk(folder_path, followlinks=True):
        for file_name in files:
            if os.path.splitext(file_name)[1] == ".ftl":
                file_name = os.path.relpath(os.path.join(root, file_name), folder_path)
                file_name = os.path.join(prefix, file_name)
                file_list.append(file_name)
    file_list.sort()

    return file_list


def get_source_ftls(comm_src_dir):
    """Find ftl files in en-US mail and calendar."""
    file_list = []
    for d in ["mail", "calendar"]:
        folder_path = os.path.join(comm_src_dir, d, "locales/en-US")
        file_list += walk_path(folder_path, d)
    return file_list


def get_lang_ftls(l10n_path):
    """Find ftl files in the merge directory."""
    file_list = []
    for d in ["mail", "calendar"]:
        folder_path = os.path.join(l10n_path, d)
        file_list += walk_path(folder_path, d)
    return file_list


def add_missing_ftls(l10n_path, source_files, locale_files):
    """
    For any ftl files that are in source_files but missing in locale_files,
    create a placeholder file.
    """
    for file_name in source_files:
        if file_name not in locale_files:
            full_file_name = os.path.join(l10n_path, file_name)
            file_path = os.path.dirname(full_file_name)
            if not os.path.isdir(file_path):
                # Create missing folder
                print("Creating missing folder: {}".format(os.path.relpath(file_path, l10n_path)))
                os.makedirs(file_path)

            print("Adding missing file: {}".format(file_name))
            with open(full_file_name, "w") as f:
                f.write(
                    "# This Source Code Form is subject to the terms of the Mozilla Public\n"
                    "# License, v. 2.0. If a copy of the MPL was not distributed with this\n"
                    "# file, You can obtain one at http://mozilla.org/MPL/2.0/.\n"
                )