]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/data/buildtool/utils.py
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / data / buildtool / utils.py
... / ...
CommitLineData
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 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
29def concat_dicts(*dicts):
30 # There is not a super great way to do this in Python:
31 new_dict = {}
32 for dict in dicts:
33 new_dict.update(dict)
34 return new_dict
35
36
37def repeated_execution_request_looper(request):
38 # dictionary of lists to list of dictionaries:
39 ld = [
40 dict(zip(request.repeat_with, t))
41 for t in zip(*request.repeat_with.values())
42 ]
43 if not ld:
44 # No special options given in repeat_with
45 ld = [{} for _ in range(len(request.input_files))]
46 return zip(ld, request.specific_dep_files, request.input_files, request.output_files)
47
48
49def format_single_request_command(request, cmd_template, common_vars):
50 return cmd_template.format(
51 ARGS = request.args.format(
52 INPUT_FILES = [file.filename for file in request.input_files],
53 OUTPUT_FILES = [file.filename for file in request.output_files],
54 **concat_dicts(common_vars, request.format_with)
55 )
56 )
57
58
59def format_repeated_request_command(request, cmd_template, loop_vars, common_vars):
60 (iter_vars, _, input_file, output_file) = loop_vars
61 return cmd_template.format(
62 ARGS = request.args.format(
63 INPUT_FILE = input_file.filename,
64 OUTPUT_FILE = output_file.filename,
65 **concat_dicts(common_vars, request.format_with, iter_vars)
66 )
67 )
68
69
70def flatten_requests(requests, config, common_vars):
71 result = []
72 for request in requests:
73 result += request.flatten(config, requests, common_vars)
74 return result
75
76
77def get_all_output_files(requests, include_tmp=False):
78 files = []
79 for request in requests:
80 files += request.all_output_files()
81
82 # Filter out all files but those in OUT_DIR if necessary.
83 # It is also easy to filter for uniqueness; do it right now and return.
84 if not include_tmp:
85 files = (file for file in files if isinstance(file, OutFile))
86 return list(set(files))
87
88 # Filter for unique values. NOTE: Cannot use set() because we need to accept same filename as
89 # OutFile and TmpFile as different, and by default they evaluate as equal.
90 return [f for _, f in set((type(f), f) for f in files)]
91
92
93def compute_directories(requests):
94 dirs = set()
95 for file in get_all_output_files(requests, include_tmp=True):
96 path = "%s/%s" % (dir_for(file), file.filename)
97 dirs.add(path[:path.rfind("/")])
98 return list(sorted(dirs))
99
100
101class SpaceSeparatedList(list):
102 """A list that joins itself with spaces when converted to a string."""
103 def __str__(self):
104 return " ".join(self)