OLD | NEW |
| (Empty) |
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 | |
7 dynamically from builder_name_schema.json. """ | |
8 | |
9 | |
10 import json | |
11 import os | |
12 | |
13 | |
14 # All of these global variables are filled in by _LoadSchema(). | |
15 | |
16 # The full schema. | |
17 BUILDER_NAME_SCHEMA = None | |
18 | |
19 # Character which separates parts of a builder name. | |
20 BUILDER_NAME_SEP = None | |
21 | |
22 # Builder roles. | |
23 BUILDER_ROLE_CANARY = 'Canary' | |
24 BUILDER_ROLE_BUILD = 'Build' | |
25 BUILDER_ROLE_HOUSEKEEPER = 'Housekeeper' | |
26 BUILDER_ROLE_INFRA = 'Infra' | |
27 BUILDER_ROLE_PERF = 'Perf' | |
28 BUILDER_ROLE_TEST = 'Test' | |
29 BUILDER_ROLES = (BUILDER_ROLE_CANARY, | |
30 BUILDER_ROLE_BUILD, | |
31 BUILDER_ROLE_HOUSEKEEPER, | |
32 BUILDER_ROLE_INFRA, | |
33 BUILDER_ROLE_PERF, | |
34 BUILDER_ROLE_TEST) | |
35 | |
36 # Suffix which distinguishes trybots from normal bots. | |
37 TRYBOT_NAME_SUFFIX = None | |
38 | |
39 | |
40 def _LoadSchema(): | |
41 """ Load the builder naming schema from the JSON file. """ | |
42 | |
43 def _UnicodeToStr(obj): | |
44 """ Convert all unicode strings in obj to Python strings. """ | |
45 if isinstance(obj, unicode): | |
46 return str(obj) | |
47 elif isinstance(obj, dict): | |
48 return dict(map(_UnicodeToStr, obj.iteritems())) | |
49 elif isinstance(obj, list): | |
50 return list(map(_UnicodeToStr, obj)) | |
51 elif isinstance(obj, tuple): | |
52 return tuple(map(_UnicodeToStr, obj)) | |
53 else: | |
54 return obj | |
55 | |
56 builder_name_json_filename = os.path.join( | |
57 os.path.dirname(__file__), 'builder_name_schema.json') | |
58 builder_name_schema_json = json.load(open(builder_name_json_filename)) | |
59 | |
60 global BUILDER_NAME_SCHEMA | |
61 BUILDER_NAME_SCHEMA = _UnicodeToStr( | |
62 builder_name_schema_json['builder_name_schema']) | |
63 | |
64 global BUILDER_NAME_SEP | |
65 BUILDER_NAME_SEP = _UnicodeToStr( | |
66 builder_name_schema_json['builder_name_sep']) | |
67 | |
68 global TRYBOT_NAME_SUFFIX | |
69 TRYBOT_NAME_SUFFIX = _UnicodeToStr( | |
70 builder_name_schema_json['trybot_name_suffix']) | |
71 | |
72 # Since the builder roles are dictionary keys, just assert that the global | |
73 # variables above account for all of them. | |
74 assert len(BUILDER_ROLES) == len(BUILDER_NAME_SCHEMA) | |
75 for role in BUILDER_ROLES: | |
76 assert role in BUILDER_NAME_SCHEMA | |
77 | |
78 | |
79 _LoadSchema() | |
80 | |
81 | |
82 def MakeBuilderName(role, extra_config=None, is_trybot=False, **kwargs): | |
83 schema = BUILDER_NAME_SCHEMA.get(role) | |
84 if not schema: | |
85 raise ValueError('%s is not a recognized role.' % role) | |
86 for k, v in kwargs.iteritems(): | |
87 if BUILDER_NAME_SEP in v: | |
88 raise ValueError('%s not allowed in %s.' % (BUILDER_NAME_SEP, v)) | |
89 if not k in schema: | |
90 raise ValueError('Schema does not contain "%s": %s' %(k, schema)) | |
91 if extra_config and BUILDER_NAME_SEP in extra_config: | |
92 raise ValueError('%s not allowed in %s.' % (BUILDER_NAME_SEP, | |
93 extra_config)) | |
94 name_parts = [role] | |
95 name_parts.extend([kwargs[attribute] for attribute in schema]) | |
96 if extra_config: | |
97 name_parts.append(extra_config) | |
98 if is_trybot: | |
99 name_parts.append(TRYBOT_NAME_SUFFIX) | |
100 return BUILDER_NAME_SEP.join(name_parts) | |
101 | |
102 | |
103 def BuilderNameFromObject(obj, is_trybot=False): | |
104 """Create a builder name based on properties of the given object. | |
105 | |
106 Args: | |
107 obj: the object from which to create the builder name. The object must | |
108 have as properties: | |
109 - A valid builder role, as defined in the JSON file | |
110 - All properties listed in the JSON file for that role | |
111 - Optionally, an extra_config property | |
112 is_trybot: bool; whether or not the builder is a trybot. | |
113 Returns: | |
114 string which combines the properties of the given object into a valid | |
115 builder name. | |
116 """ | |
117 schema = BUILDER_NAME_SCHEMA.get(obj.role) | |
118 if not schema: | |
119 raise ValueError('%s is not a recognized role.' % obj.role) | |
120 name_parts = [obj.role] | |
121 for attr_name in schema: | |
122 attr_val = getattr(obj, attr_name) | |
123 name_parts.append(attr_val) | |
124 extra_config = getattr(obj, 'extra_config', None) | |
125 if extra_config: | |
126 name_parts.append(extra_config) | |
127 if is_trybot: | |
128 name_parts.append(TRYBOT_NAME_SUFFIX) | |
129 return BUILDER_NAME_SEP.join(name_parts) | |
130 | |
131 | |
132 def IsTrybot(builder_name): | |
133 """ Returns true if builder_name refers to a trybot (as opposed to a | |
134 waterfall bot). """ | |
135 return builder_name.endswith(TRYBOT_NAME_SUFFIX) | |
136 | |
137 | |
138 def GetWaterfallBot(builder_name): | |
139 """Returns the name of the waterfall bot for this builder. If it is not a | |
140 trybot, builder_name is returned unchanged. If it is a trybot the name is | |
141 returned without the trybot suffix.""" | |
142 if not IsTrybot(builder_name): | |
143 return builder_name | |
144 return _WithoutSuffix(builder_name, BUILDER_NAME_SEP + TRYBOT_NAME_SUFFIX) | |
145 | |
146 | |
147 def TrybotName(builder_name): | |
148 """Returns the name of the trybot clone of this builder. | |
149 | |
150 If the given builder is a trybot, the name is returned unchanged. If not, the | |
151 TRYBOT_NAME_SUFFIX is appended. | |
152 """ | |
153 if builder_name.endswith(TRYBOT_NAME_SUFFIX): | |
154 return builder_name | |
155 return builder_name + BUILDER_NAME_SEP + TRYBOT_NAME_SUFFIX | |
156 | |
157 | |
158 def _WithoutSuffix(string, suffix): | |
159 """ Returns a copy of string 'string', but with suffix 'suffix' removed. | |
160 Raises ValueError if string does not end with suffix. """ | |
161 if not string.endswith(suffix): | |
162 raise ValueError('_WithoutSuffix: string %s does not end with suffix %s' % ( | |
163 string, suffix)) | |
164 return string[:-len(suffix)] | |
165 | |
166 | |
167 def DictForBuilderName(builder_name): | |
168 """Makes a dictionary containing details about the builder from its name.""" | |
169 split_name = builder_name.split(BUILDER_NAME_SEP) | |
170 | |
171 def pop_front(): | |
172 try: | |
173 return split_name.pop(0) | |
174 except: | |
175 raise ValueError('Invalid builder name: %s' % builder_name) | |
176 | |
177 result = {'is_trybot': False} | |
178 | |
179 if split_name[-1] == TRYBOT_NAME_SUFFIX: | |
180 result['is_trybot'] = True | |
181 split_name.pop() | |
182 | |
183 if split_name[0] in BUILDER_NAME_SCHEMA.keys(): | |
184 key_list = BUILDER_NAME_SCHEMA[split_name[0]] | |
185 result['role'] = pop_front() | |
186 for key in key_list: | |
187 result[key] = pop_front() | |
188 if split_name: | |
189 result['extra_config'] = pop_front() | |
190 if split_name: | |
191 raise ValueError('Invalid builder name: %s' % builder_name) | |
192 else: | |
193 raise ValueError('Invalid builder name: %s' % builder_name) | |
194 return result | |
195 | |
196 | |
OLD | NEW |