OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 """ Utilities for dealing with builder names. This module obtains its attributes | 6 """ Utilities for dealing with builder names. This module obtains its attributes |
7 dynamically from builder_name_schema.json. """ | 7 dynamically from builder_name_schema.json. """ |
8 | 8 |
9 | 9 |
10 import json | 10 import json |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 """ Convert all unicode strings in obj to Python strings. """ | 44 """ Convert all unicode strings in obj to Python strings. """ |
45 if isinstance(obj, unicode): | 45 if isinstance(obj, unicode): |
46 return str(obj) | 46 return str(obj) |
47 elif isinstance(obj, dict): | 47 elif isinstance(obj, dict): |
48 return dict(map(_UnicodeToStr, obj.iteritems())) | 48 return dict(map(_UnicodeToStr, obj.iteritems())) |
49 elif isinstance(obj, list): | 49 elif isinstance(obj, list): |
50 return list(map(_UnicodeToStr, obj)) | 50 return list(map(_UnicodeToStr, obj)) |
51 elif isinstance(obj, tuple): | 51 elif isinstance(obj, tuple): |
52 return tuple(map(_UnicodeToStr, obj)) | 52 return tuple(map(_UnicodeToStr, obj)) |
53 else: | 53 else: |
54 return obj | 54 return obj # pragma: no cover |
55 | 55 |
56 builder_name_json_filename = os.path.join( | 56 builder_name_json_filename = os.path.join( |
57 os.path.dirname(__file__), 'builder_name_schema.json') | 57 os.path.dirname(__file__), 'builder_name_schema.json') |
58 builder_name_schema_json = json.load(open(builder_name_json_filename)) | 58 builder_name_schema_json = json.load(open(builder_name_json_filename)) |
59 | 59 |
60 global BUILDER_NAME_SCHEMA | 60 global BUILDER_NAME_SCHEMA |
61 BUILDER_NAME_SCHEMA = _UnicodeToStr( | 61 BUILDER_NAME_SCHEMA = _UnicodeToStr( |
62 builder_name_schema_json['builder_name_schema']) | 62 builder_name_schema_json['builder_name_schema']) |
63 | 63 |
64 global BUILDER_NAME_SEP | 64 global BUILDER_NAME_SEP |
65 BUILDER_NAME_SEP = _UnicodeToStr( | 65 BUILDER_NAME_SEP = _UnicodeToStr( |
66 builder_name_schema_json['builder_name_sep']) | 66 builder_name_schema_json['builder_name_sep']) |
67 | 67 |
68 global TRYBOT_NAME_SUFFIX | 68 global TRYBOT_NAME_SUFFIX |
69 TRYBOT_NAME_SUFFIX = _UnicodeToStr( | 69 TRYBOT_NAME_SUFFIX = _UnicodeToStr( |
70 builder_name_schema_json['trybot_name_suffix']) | 70 builder_name_schema_json['trybot_name_suffix']) |
71 | 71 |
72 # Since the builder roles are dictionary keys, just assert that the global | 72 # Since the builder roles are dictionary keys, just assert that the global |
73 # variables above account for all of them. | 73 # variables above account for all of them. |
74 assert len(BUILDER_ROLES) == len(BUILDER_NAME_SCHEMA) | 74 assert len(BUILDER_ROLES) == len(BUILDER_NAME_SCHEMA) |
75 for role in BUILDER_ROLES: | 75 for role in BUILDER_ROLES: |
76 assert role in BUILDER_NAME_SCHEMA | 76 assert role in BUILDER_NAME_SCHEMA |
77 | 77 |
78 | 78 |
79 _LoadSchema() | 79 _LoadSchema() |
80 | 80 |
81 | 81 |
82 def MakeBuilderName(role, extra_config=None, is_trybot=False, **kwargs): | 82 def MakeBuilderName(role, extra_config=None, is_trybot=False, **kwargs): |
83 schema = BUILDER_NAME_SCHEMA.get(role) | 83 schema = BUILDER_NAME_SCHEMA.get(role) |
84 if not schema: | 84 if not schema: # pragma: no cover |
85 raise ValueError('%s is not a recognized role.' % role) | 85 raise ValueError('%s is not a recognized role.' % role) |
86 for k, v in kwargs.iteritems(): | 86 for k, v in kwargs.iteritems(): |
87 if BUILDER_NAME_SEP in v: | 87 if BUILDER_NAME_SEP in v: # pragma: no cover |
88 raise ValueError('%s not allowed in %s.' % (BUILDER_NAME_SEP, v)) | 88 raise ValueError('%s not allowed in %s.' % (BUILDER_NAME_SEP, v)) |
89 if not k in schema: | 89 if not k in schema: # pragma: no cover |
90 raise ValueError('Schema does not contain "%s": %s' %(k, schema)) | 90 raise ValueError('Schema does not contain "%s": %s' %(k, schema)) |
91 if extra_config and BUILDER_NAME_SEP in extra_config: | 91 if extra_config and BUILDER_NAME_SEP in extra_config: # pragma: no cover |
92 raise ValueError('%s not allowed in %s.' % (BUILDER_NAME_SEP, | 92 raise ValueError('%s not allowed in %s.' % (BUILDER_NAME_SEP, |
93 extra_config)) | 93 extra_config)) |
94 name_parts = [role] | 94 name_parts = [role] |
95 name_parts.extend([kwargs[attribute] for attribute in schema]) | 95 name_parts.extend([kwargs[attribute] for attribute in schema]) |
96 if extra_config: | 96 if extra_config: |
97 name_parts.append(extra_config) | 97 name_parts.append(extra_config) |
98 if is_trybot: | 98 if is_trybot: |
99 name_parts.append(TRYBOT_NAME_SUFFIX) | 99 name_parts.append(TRYBOT_NAME_SUFFIX) |
100 return BUILDER_NAME_SEP.join(name_parts) | 100 return BUILDER_NAME_SEP.join(name_parts) |
101 | 101 |
102 | 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): | 103 def IsTrybot(builder_name): |
133 """ Returns true if builder_name refers to a trybot (as opposed to a | 104 """ Returns true if builder_name refers to a trybot (as opposed to a |
134 waterfall bot). """ | 105 waterfall bot). """ |
135 return builder_name.endswith(TRYBOT_NAME_SUFFIX) | 106 return builder_name.endswith(TRYBOT_NAME_SUFFIX) |
136 | 107 |
137 | 108 |
138 def GetWaterfallBot(builder_name): | 109 def GetWaterfallBot(builder_name): # pragma: no cover |
139 """Returns the name of the waterfall bot for this builder. If it is not a | 110 """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 | 111 trybot, builder_name is returned unchanged. If it is a trybot the name is |
141 returned without the trybot suffix.""" | 112 returned without the trybot suffix.""" |
142 if not IsTrybot(builder_name): | 113 if not IsTrybot(builder_name): |
143 return builder_name | 114 return builder_name |
144 return _WithoutSuffix(builder_name, BUILDER_NAME_SEP + TRYBOT_NAME_SUFFIX) | 115 return _WithoutSuffix(builder_name, BUILDER_NAME_SEP + TRYBOT_NAME_SUFFIX) |
145 | 116 |
146 | 117 |
147 def TrybotName(builder_name): | 118 def TrybotName(builder_name): # pragma: no cover |
148 """Returns the name of the trybot clone of this builder. | 119 """Returns the name of the trybot clone of this builder. |
149 | 120 |
150 If the given builder is a trybot, the name is returned unchanged. If not, the | 121 If the given builder is a trybot, the name is returned unchanged. If not, the |
151 TRYBOT_NAME_SUFFIX is appended. | 122 TRYBOT_NAME_SUFFIX is appended. |
152 """ | 123 """ |
153 if builder_name.endswith(TRYBOT_NAME_SUFFIX): | 124 if builder_name.endswith(TRYBOT_NAME_SUFFIX): |
154 return builder_name | 125 return builder_name |
155 return builder_name + BUILDER_NAME_SEP + TRYBOT_NAME_SUFFIX | 126 return builder_name + BUILDER_NAME_SEP + TRYBOT_NAME_SUFFIX |
156 | 127 |
157 | 128 |
158 def _WithoutSuffix(string, suffix): | 129 def _WithoutSuffix(string, suffix): # pragma: no cover |
159 """ Returns a copy of string 'string', but with suffix 'suffix' removed. | 130 """ Returns a copy of string 'string', but with suffix 'suffix' removed. |
160 Raises ValueError if string does not end with suffix. """ | 131 Raises ValueError if string does not end with suffix. """ |
161 if not string.endswith(suffix): | 132 if not string.endswith(suffix): |
162 raise ValueError('_WithoutSuffix: string %s does not end with suffix %s' % ( | 133 raise ValueError('_WithoutSuffix: string %s does not end with suffix %s' % ( |
163 string, suffix)) | 134 string, suffix)) |
164 return string[:-len(suffix)] | 135 return string[:-len(suffix)] |
165 | 136 |
166 | 137 |
167 def DictForBuilderName(builder_name): | 138 def DictForBuilderName(builder_name): |
168 """Makes a dictionary containing details about the builder from its name.""" | 139 """Makes a dictionary containing details about the builder from its name.""" |
169 split_name = builder_name.split(BUILDER_NAME_SEP) | 140 split_name = builder_name.split(BUILDER_NAME_SEP) |
170 | 141 |
171 def pop_front(): | 142 def pop_front(): |
172 try: | 143 try: |
173 return split_name.pop(0) | 144 return split_name.pop(0) |
174 except: | 145 except: # pragma: no cover |
175 raise ValueError('Invalid builder name: %s' % builder_name) | 146 raise ValueError('Invalid builder name: %s' % builder_name) |
176 | 147 |
177 result = {'is_trybot': False} | 148 result = {'is_trybot': False} |
178 | 149 |
179 if split_name[-1] == TRYBOT_NAME_SUFFIX: | 150 if split_name[-1] == TRYBOT_NAME_SUFFIX: |
180 result['is_trybot'] = True | 151 result['is_trybot'] = True |
181 split_name.pop() | 152 split_name.pop() |
182 | 153 |
183 if split_name[0] in BUILDER_NAME_SCHEMA.keys(): | 154 if split_name[0] in BUILDER_NAME_SCHEMA.keys(): |
184 key_list = BUILDER_NAME_SCHEMA[split_name[0]] | 155 key_list = BUILDER_NAME_SCHEMA[split_name[0]] |
185 result['role'] = pop_front() | 156 result['role'] = pop_front() |
186 for key in key_list: | 157 for key in key_list: |
187 result[key] = pop_front() | 158 result[key] = pop_front() |
188 if split_name: | 159 if split_name: |
189 result['extra_config'] = pop_front() | 160 result['extra_config'] = pop_front() |
190 if split_name: | 161 if split_name: # pragma: no cover |
191 raise ValueError('Invalid builder name: %s' % builder_name) | 162 raise ValueError('Invalid builder name: %s' % builder_name) |
192 else: | 163 else: # pragma: no cover |
193 raise ValueError('Invalid builder name: %s' % builder_name) | 164 raise ValueError('Invalid builder name: %s' % builder_name) |
194 return result | 165 return result |
195 | 166 |
196 | 167 |
OLD | NEW |