| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 | 66 # 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 | 67 |
| 74 @property | 68 @property |
| 75 def path(self): | 69 def path(self): |
| 76 return os.path.realpath(self._path) | 70 return os.path.realpath(self._path) |
| 77 | 71 |
| 78 def read_raw(self): | 72 def read_raw(self): |
| 79 with open(self._path, 'r') as fh: | 73 with open(self._path, 'r') as fh: |
| 80 return fh.read() | 74 return fh.read() |
| 81 | 75 |
| 82 def read(self): | 76 def read(self): |
| 83 text = self.read_raw() | 77 text = self.read_raw() |
| 84 buf = package_pb2.Package() | 78 buf = package_pb2.Package() |
| 85 if text.lstrip().startswith('{'): | 79 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 | 80 return buf |
| 91 | 81 |
| 92 def to_raw(self, buf): | 82 def to_raw(self, buf): |
| 93 if self._is_json: | 83 obj = json_format.MessageToDict(buf, preserving_proto_field_name=True) |
| 94 obj = json_format.MessageToDict(buf, preserving_proto_field_name=True) | 84 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 | 85 |
| 99 def write(self, buf): | 86 def write(self, buf): |
| 100 with open(self._path, 'w') as fh: | 87 with open(self._path, 'w') as fh: |
| 101 fh.write(self.to_raw(buf)) | 88 fh.write(self.to_raw(buf)) |
| 102 | 89 |
| 103 | 90 |
| 104 class PackageContext(object): | 91 class PackageContext(object): |
| 105 """Contains information about where the root package and its dependency | 92 """Contains information about where the root package and its dependency |
| 106 checkouts live. | 93 checkouts live. |
| 107 | 94 |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 706 >>> d = { 'x': 1, 'y': 2 } | 693 >>> d = { 'x': 1, 'y': 2 } |
| 707 >>> sorted(_updated(d, { 'y': 3, 'z': 4 }).items()) | 694 >>> sorted(_updated(d, { 'y': 3, 'z': 4 }).items()) |
| 708 [('x', 1), ('y', 3), ('z', 4)] | 695 [('x', 1), ('y', 3), ('z', 4)] |
| 709 >>> sorted(d.items()) | 696 >>> sorted(d.items()) |
| 710 [('x', 1), ('y', 2)] | 697 [('x', 1), ('y', 2)] |
| 711 """ | 698 """ |
| 712 | 699 |
| 713 d = copy.copy(d) | 700 d = copy.copy(d) |
| 714 d.update(updates) | 701 d.update(updates) |
| 715 return d | 702 return d |
| OLD | NEW |