Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 from recipe_engine import recipe_api | 5 from recipe_engine import recipe_api |
| 6 | 6 |
| 7 import base64 | 7 import base64 |
| 8 import collections | |
| 9 import re | |
| 8 import json | 10 import json |
| 9 | 11 |
| 10 class LuciConfigApi(recipe_api.RecipeApi): | 12 class LuciConfigApi(recipe_api.RecipeApi): |
| 11 def __init__(self, **kwargs): | 13 def __init__(self, **kwargs): |
| 12 super(LuciConfigApi, self).__init__(**kwargs) | 14 super(LuciConfigApi, self).__init__(**kwargs) |
| 13 self.set_config('basic') | 15 self.set_config('basic') |
| 14 | 16 |
| 15 def get_config_defaults(self): | 17 def get_config_defaults(self): |
| 16 return { | 18 return { |
| 17 'BASE_URL': 'https://luci-config.appspot.com/', | 19 'BASE_URL': 'https://luci-config.appspot.com/', |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 url = self.c.base_url + '/_ah/api/config/v1/config_sets/' | 66 url = self.c.base_url + '/_ah/api/config/v1/config_sets/' |
| 65 url += self.m.url.quote('projects/%s/refs/heads/master' % project, safe='') | 67 url += self.m.url.quote('projects/%s/refs/heads/master' % project, safe='') |
| 66 url += '/config/%s' % config | 68 url += '/config/%s' % config |
| 67 | 69 |
| 68 fetch_result = self._fetch( | 70 fetch_result = self._fetch( |
| 69 url, step_name='Get project %r config %r' % (project, config), | 71 url, step_name='Get project %r config %r' % (project, config), |
| 70 headers=self._get_headers()) | 72 headers=self._get_headers()) |
| 71 result = json.loads(fetch_result) | 73 result = json.loads(fetch_result) |
| 72 result['content'] = base64.b64decode(result['content']) | 74 result['content'] = base64.b64decode(result['content']) |
| 73 return result | 75 return result |
| 76 | |
| 77 def parse_textproto(self, lines): | |
| 78 """(badly) parses a text protobuf. | |
| 79 | |
| 80 This is not real protobuf parsing at the moment; eventually, maybe it could | |
|
dsansome-google
2017/03/03 03:48:24
ಠ_ಠ
iannucci1
2017/03/03 05:15:30
Yep, getting a protobuf library into all the envir
| |
| 81 be. For now, it's enough to just get by. | |
| 82 | |
| 83 We assume all fields are repeated since we don't have a proto spec to work | |
| 84 with. | |
| 85 | |
| 86 Args: | |
| 87 lines: a list of the lines to parse | |
| 88 Returns: | |
| 89 A recursive dictionary of lists. | |
| 90 """ | |
| 91 def parse_atom(text): | |
| 92 # NOTE: Assuming we only have numbers and strings to avoid using | |
| 93 # ast.literal_eval | |
| 94 try: | |
| 95 return int(text) | |
| 96 except ValueError: | |
| 97 return text.strip("'").strip('"') | |
| 98 | |
| 99 ret = {} | |
| 100 while lines: | |
| 101 line = lines.pop(0).strip() | |
| 102 | |
| 103 m = re.match(r'(\w+)\s*:\s*(.*)', line) | |
| 104 if m: | |
| 105 ret.setdefault(m.group(1), []).append(parse_atom(m.group(2))) | |
| 106 continue | |
| 107 | |
| 108 m = re.match(r'(\w+)\s*{', line) | |
| 109 if m: | |
| 110 subparse = self.parse_textproto(lines) | |
| 111 ret.setdefault(m.group(1), []).append(subparse) | |
| 112 continue | |
| 113 | |
| 114 if line == '}': | |
| 115 return ret | |
| 116 if line == '': | |
| 117 continue | |
| 118 | |
| 119 raise ValueError( | |
| 120 'Could not understand line: <%s>' % line) # pragma: no cover | |
| 121 return ret | |
| 122 | |
| 123 def get_project_metadata(self, project): | |
| 124 mapping = self.get_projects() | |
| 125 return mapping.get(project) | |
| 126 | |
| OLD | NEW |