Flutter Engine
The Flutter Engine
builder_name_schema.py
Go to the documentation of this file.
1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6""" Utilities for dealing with builder names. This module obtains its attributes
7dynamically from builder_name_schema.json. """
8
9
10import json
11import os
12
13
14# All of these global variables are filled in by _LoadSchema().
15
16# The full schema.
17BUILDER_NAME_SCHEMA = None
18
19# Character which separates parts of a builder name.
20BUILDER_NAME_SEP = None
21
22# Builder roles.
23BUILDER_ROLE_BAZELBUILD = 'BazelBuild'
24BUILDER_ROLE_BAZELTEST = 'BazelTest'
25BUILDER_ROLE_BUILD = 'Build'
26BUILDER_ROLE_BUILDSTATS = 'BuildStats'
27BUILDER_ROLE_CANARY = 'Canary'
28BUILDER_ROLE_CODESIZE = 'CodeSize'
29BUILDER_ROLE_HOUSEKEEPER = 'Housekeeper'
30BUILDER_ROLE_INFRA = 'Infra'
31BUILDER_ROLE_PERF = 'Perf'
32BUILDER_ROLE_TEST = 'Test'
33BUILDER_ROLE_UPLOAD = 'Upload'
34BUILDER_ROLES = (BUILDER_ROLE_BAZELBUILD,
35 BUILDER_ROLE_BAZELTEST,
36 BUILDER_ROLE_BUILD,
37 BUILDER_ROLE_BUILDSTATS,
38 BUILDER_ROLE_CANARY,
39 BUILDER_ROLE_CODESIZE,
40 BUILDER_ROLE_HOUSEKEEPER,
41 BUILDER_ROLE_INFRA,
42 BUILDER_ROLE_PERF,
43 BUILDER_ROLE_TEST,
44 BUILDER_ROLE_UPLOAD)
45
46
47def _LoadSchema():
48 """ Load the builder naming schema from the JSON file. """
49
50 def ToStr(obj):
51 """ Convert all unicode strings in obj to Python strings. """
52 if isinstance(obj, str):
53 return obj # pragma: nocover
54 elif isinstance(obj, dict):
55 return dict(map(ToStr, obj.items()))
56 elif isinstance(obj, list):
57 return list(map(ToStr, obj))
58 elif isinstance(obj, tuple):
59 return tuple(map(ToStr, obj))
60 else:
61 return obj.decode('utf-8') # pragma: nocover
62
63 builder_name_json_filename = os.path.join(
64 os.path.dirname(__file__), 'builder_name_schema.json')
65 builder_name_schema_json = json.load(open(builder_name_json_filename))
66
67 global BUILDER_NAME_SCHEMA
68 BUILDER_NAME_SCHEMA = ToStr(
69 builder_name_schema_json['builder_name_schema'])
70
71 global BUILDER_NAME_SEP
72 BUILDER_NAME_SEP = ToStr(
73 builder_name_schema_json['builder_name_sep'])
74
75 # Since the builder roles are dictionary keys, just assert that the global
76 # variables above account for all of them.
77 assert len(BUILDER_ROLES) == len(BUILDER_NAME_SCHEMA)
78 for role in BUILDER_ROLES:
79 assert role in BUILDER_NAME_SCHEMA
80
81
82_LoadSchema()
83
84
85def MakeBuilderName(**parts):
86 for v in parts.values():
87 if BUILDER_NAME_SEP in v:
88 raise ValueError('Parts cannot contain "%s"' % BUILDER_NAME_SEP)
89
90 rv_parts = []
91
92 def process(depth, parts):
93 role_key = 'role'
94 if depth != 0:
95 role_key = 'sub-role-%d' % depth
96 role = parts.get(role_key)
97 if not role:
98 raise ValueError('Invalid parts; missing key %s' % role_key)
99 s = BUILDER_NAME_SCHEMA.get(role)
100 if not s:
101 raise ValueError('Invalid parts; unknown role %s' % role)
102 rv_parts.append(role)
103 del parts[role_key]
104
105 for key in s.get('keys', []):
106 value = parts.get(key)
107 if not value:
108 raise ValueError('Invalid parts; missing %s' % key)
109 rv_parts.append(value)
110 del parts[key]
111
112 recurse_roles = s.get('recurse_roles', [])
113 if len(recurse_roles) > 0:
114 sub_role_key = 'sub-role-%d' % (depth+1)
115 sub_role = parts.get(sub_role_key)
116 if not sub_role:
117 raise ValueError('Invalid parts; missing %s' % sub_role_key)
118
119 found = False
120 for recurse_role in recurse_roles:
121 if recurse_role == sub_role:
122 found = True
123 parts = process(depth+1, parts)
124 break
125 if not found:
126 raise ValueError('Invalid parts; unknown sub-role %s' % sub_role)
127
128 for key in s.get('optional_keys', []):
129 if parts.get(key):
130 rv_parts.append(parts[key])
131 del parts[key]
132
133 if len(parts) > 0:
134 raise ValueError('Invalid parts; too many parts: %s' % parts)
135
136 return parts
137
138 process(0, parts)
139
140 return BUILDER_NAME_SEP.join(rv_parts)
141
142
143def DictForBuilderName(builder_name):
144 """Makes a dictionary containing details about the builder from its name."""
145 split = builder_name.split(BUILDER_NAME_SEP)
146
147 def pop_front(items):
148 try:
149 return items.pop(0), items
150 except:
151 raise ValueError(
152 'Invalid builder name: %s (not enough parts)' % builder_name)
153
154 result = {}
155
156 def _parse(depth, role, parts):
157 schema = BUILDER_NAME_SCHEMA.get(role)
158 if not schema:
159 raise ValueError('Invalid builder name: %s' % builder_name)
160 if depth == 0:
161 result['role'] = str(role)
162 else:
163 result['sub-role-%d' % depth] = str(role)
164 for key in schema.get('keys', []):
165 value, parts = pop_front(parts)
166 result[key] = str(value)
167 for sub_role in schema.get('recurse_roles', []):
168 if len(parts) > 0 and sub_role == parts[0]:
169 parts = _parse(depth+1, parts[0], parts[1:])
170 for key in schema.get('optional_keys', []):
171 if parts:
172 value, parts = pop_front(parts)
173 result[key] = str(value)
174 if parts:
175 raise ValueError('Invalid builder name: %s' % builder_name)
176 return parts
177
178 _parse(0, split[0], split[1:])
179
180 return result
SI auto map(std::index_sequence< I... >, Fn &&fn, const Args &... args) -> skvx::Vec< sizeof...(I), decltype(fn(args[0]...))>
Definition: SkVx.h:680
static void process(const char *inPath, const char *lexer, const char *token, const char *hPath, const char *cppPath)
Definition: Main.cpp:114