1# Copyright (C) 2018 and later: Unicode, Inc. and others.
2# License & terms of use: http://www.unicode.org/copyright.html
3
4# Python 2/3 Compatibility (ICU-20299)
5# TODO(ICU-20301): Remove this.
6from __future__ import print_function
7
8import sys
9
10from . import *
11
12
13def dir_for(file):
14    if isinstance(file, LocalFile):
15        return get_local_dirname(file.dirname)
16    if isinstance(file, SrcFile):
17        return "{SRC_DIR}"
18    if isinstance(file, InFile):
19        return "{IN_DIR}"
20    if isinstance(file, TmpFile):
21        return "{TMP_DIR}"
22    if isinstance(file, OutFile):
23        return "{OUT_DIR}"
24    if isinstance(file, PkgFile):
25        return "{PKG_DIR}"
26    assert False
27
28
29LOCAL_DIRNAME_SUBSTITUTIONS = {
30    "SRC": "{SRC_DIR}",
31    "FILTERS": "{FILTERS_DIR}",
32    "CWD": "{CWD_DIR}"
33}
34
35
36def get_local_dirname(dirname):
37    if dirname.startswith("/"):
38        return dirname
39    elif dirname.startswith("$"):
40        # Note: directory separator substitution happens later
41        sep_idx = dirname.find("/")
42        if sep_idx == -1:
43            sep_idx = len(dirname)
44        variable = dirname[1:sep_idx]
45        if variable in LOCAL_DIRNAME_SUBSTITUTIONS:
46            return LOCAL_DIRNAME_SUBSTITUTIONS[variable] + dirname[sep_idx:]
47    print(
48        "Error: Local directory must be absolute, or relative to one of: " +
49        (", ".join("$%s" % v for v in LOCAL_DIRNAME_SUBSTITUTIONS.keys())),
50        file=sys.stderr
51    )
52    exit(1)
53
54
55ALL_TREES = [
56    "locales",
57    "curr",
58    "lang",
59    "region",
60    "zone",
61    "unit",
62    "coll",
63    "brkitr",
64    "rbnf",
65]
66
67
68def concat_dicts(*dicts):
69    # There is not a super great way to do this in Python:
70    new_dict = {}
71    for dict in dicts:
72        new_dict.update(dict)
73    return new_dict
74
75
76def repeated_execution_request_looper(request):
77    # dictionary of lists to list of dictionaries:
78    ld = [
79        dict(zip(request.repeat_with, t))
80        for t in zip(*request.repeat_with.values())
81    ]
82    if not ld:
83        # No special options given in repeat_with
84        ld = [{} for _ in range(len(request.input_files))]
85    return zip(ld, request.specific_dep_files, request.input_files, request.output_files)
86
87
88def format_single_request_command(request, cmd_template, common_vars):
89    return cmd_template.format(
90        ARGS = request.args.format(
91            INPUT_FILES = [file.filename for file in request.input_files],
92            OUTPUT_FILES = [file.filename for file in request.output_files],
93            **concat_dicts(common_vars, request.format_with)
94        )
95    )
96
97
98def format_repeated_request_command(request, cmd_template, loop_vars, common_vars):
99    (iter_vars, _, input_file, output_file) = loop_vars
100    return cmd_template.format(
101        ARGS = request.args.format(
102            INPUT_FILE = input_file.filename,
103            OUTPUT_FILE = output_file.filename,
104            **concat_dicts(common_vars, request.format_with, iter_vars)
105        )
106    )
107
108
109def flatten_requests(requests, config, common_vars):
110    result = []
111    for request in requests:
112        result += request.flatten(config, requests, common_vars)
113    return result
114
115
116def get_all_output_files(requests, include_tmp=False):
117    files = []
118    for request in requests:
119        files += request.all_output_files()
120
121    # Filter out all files but those in OUT_DIR if necessary.
122    # It is also easy to filter for uniqueness; do it right now and return.
123    if not include_tmp:
124        files = (file for file in files if isinstance(file, OutFile))
125        return list(set(files))
126
127    # Filter for unique values.  NOTE: Cannot use set() because we need to accept same filename as
128    # OutFile and TmpFile as different, and by default they evaluate as equal.
129    return [f for _, f in set((type(f), f) for f in files)]
130
131
132def compute_directories(requests):
133    dirs = set()
134    for file in get_all_output_files(requests, include_tmp=True):
135        path = "%s/%s" % (dir_for(file), file.filename)
136        dirs.add(path[:path.rfind("/")])
137    return list(sorted(dirs))
138
139
140class SpaceSeparatedList(list):
141    """A list that joins itself with spaces when converted to a string."""
142    def __str__(self):
143        return " ".join(self)
144