OLD | NEW |
1 # Copyright 2015 The LUCI Authors. All rights reserved. | 1 # Copyright 2015 The LUCI Authors. All rights reserved. |
2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
4 | 4 |
5 import copy | 5 import copy |
6 import difflib | 6 import difflib |
7 import json | 7 import json |
8 import logging | 8 import logging |
9 import operator | 9 import operator |
10 import os | 10 import os |
11 import subprocess | 11 import subprocess |
12 import sys | 12 import sys |
13 | 13 |
14 from . import env | 14 from . import env |
15 | 15 |
16 from google.protobuf import text_format | |
17 from google.protobuf import json_format | 16 from google.protobuf import json_format |
18 from . import package_pb2 | 17 from . import package_pb2 |
19 from . import fetch | 18 from . import fetch |
20 | 19 |
21 | 20 |
22 class InconsistentDependencyGraphError(Exception): | 21 class InconsistentDependencyGraphError(Exception): |
23 def __init__(self, project_id, specs): | 22 def __init__(self, project_id, specs): |
24 self.project_id = project_id | 23 self.project_id = project_id |
25 self.specs = specs | 24 self.specs = specs |
26 | 25 |
(...skipping 30 matching lines...) Expand all Loading... |
57 os.path.abspath(recipes_cfg)))) # recipes.cfg | 56 os.path.abspath(recipes_cfg)))) # recipes.cfg |
58 | 57 |
59 | 58 |
60 class ProtoFile(object): | 59 class ProtoFile(object): |
61 """A collection of functions operating on a proto path. | 60 """A collection of functions operating on a proto path. |
62 | 61 |
63 This is an object so that it can be mocked in the tests. | 62 This is an object so that it can be mocked in the tests. |
64 """ | 63 """ |
65 def __init__(self, path): | 64 def __init__(self, path): |
66 self._path = path | 65 self._path = path |
67 # TODO(iannucci): remove text proto support | |
68 self._is_json = False | |
69 try: | |
70 self._is_json = self.read_raw().lstrip().startswith('{') | |
71 except IOError: | |
72 pass | |
73 | 66 |
74 @property | 67 @property |
75 def path(self): | 68 def path(self): |
76 return os.path.realpath(self._path) | 69 return os.path.realpath(self._path) |
77 | 70 |
78 def read_raw(self): | 71 def read_raw(self): |
79 with open(self._path, 'r') as fh: | 72 with open(self._path, 'r') as fh: |
80 return fh.read() | 73 return fh.read() |
81 | 74 |
82 def read(self): | 75 def read(self): |
83 text = self.read_raw() | 76 text = self.read_raw() |
84 buf = package_pb2.Package() | 77 buf = package_pb2.Package() |
85 if text.lstrip().startswith('{'): | 78 json_format.Parse(text, buf, ignore_unknown_fields=True) |
86 self._is_json = True | |
87 json_format.Parse(text, buf, ignore_unknown_fields=True) | |
88 else: | |
89 text_format.Merge(text, buf) | |
90 return buf | 79 return buf |
91 | 80 |
92 def to_raw(self, buf): | 81 def to_raw(self, buf): |
93 if self._is_json: | 82 obj = json_format.MessageToDict(buf, preserving_proto_field_name=True) |
94 obj = json_format.MessageToDict(buf, preserving_proto_field_name=True) | 83 return json.dumps(obj, indent=2, sort_keys=True).replace(' \n', '\n') |
95 return json.dumps(obj, indent=2, sort_keys=True).replace(' \n', '\n') | |
96 else: | |
97 return text_format.MessageToString(buf) | |
98 | 84 |
99 def write(self, buf): | 85 def write(self, buf): |
100 with open(self._path, 'w') as fh: | 86 with open(self._path, 'w') as fh: |
101 fh.write(self.to_raw(buf)) | 87 fh.write(self.to_raw(buf)) |
102 | 88 |
103 | 89 |
104 class PackageContext(object): | 90 class PackageContext(object): |
105 """Contains information about where the root package and its dependency | 91 """Contains information about where the root package and its dependency |
106 checkouts live. | 92 checkouts live. |
107 | 93 |
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 >>> d = { 'x': 1, 'y': 2 } | 692 >>> d = { 'x': 1, 'y': 2 } |
707 >>> sorted(_updated(d, { 'y': 3, 'z': 4 }).items()) | 693 >>> sorted(_updated(d, { 'y': 3, 'z': 4 }).items()) |
708 [('x', 1), ('y', 3), ('z', 4)] | 694 [('x', 1), ('y', 3), ('z', 4)] |
709 >>> sorted(d.items()) | 695 >>> sorted(d.items()) |
710 [('x', 1), ('y', 2)] | 696 [('x', 1), ('y', 2)] |
711 """ | 697 """ |
712 | 698 |
713 d = copy.copy(d) | 699 d = copy.copy(d) |
714 d.update(updates) | 700 d.update(updates) |
715 return d | 701 return d |
OLD | NEW |