OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 import copy | 5 import copy |
6 import hashlib | 6 import hashlib |
7 import json | 7 import json |
8 import sys | 8 import sys |
9 | 9 |
10 MANIFEST_VERSION = 2 | 10 MANIFEST_VERSION = 2 |
(...skipping 29 matching lines...) Expand all Loading... |
40 def GetHostOS(): | 40 def GetHostOS(): |
41 '''Returns the host_os value that corresponds to the current host OS''' | 41 '''Returns the host_os value that corresponds to the current host OS''' |
42 return { | 42 return { |
43 'linux2': 'linux', | 43 'linux2': 'linux', |
44 'darwin': 'mac', | 44 'darwin': 'mac', |
45 'cygwin': 'win', | 45 'cygwin': 'win', |
46 'win32': 'win' | 46 'win32': 'win' |
47 }[sys.platform] | 47 }[sys.platform] |
48 | 48 |
49 | 49 |
50 def DictToJSON(dict): | 50 def DictToJSON(pydict): |
51 """Convert a dict to a JSON-formatted string.""" | 51 """Convert a dict to a JSON-formatted string.""" |
52 pretty_string = json.dumps(dict, sort_keys=False, indent=2) | 52 pretty_string = json.dumps(pydict, sort_keys=False, indent=2) |
53 # json.dumps sometimes returns trailing whitespace and does not put | 53 # json.dumps sometimes returns trailing whitespace and does not put |
54 # a newline at the end. This code fixes these problems. | 54 # a newline at the end. This code fixes these problems. |
55 pretty_lines = pretty_string.split('\n') | 55 pretty_lines = pretty_string.split('\n') |
56 return '\n'.join([line.rstrip() for line in pretty_lines]) + '\n' | 56 return '\n'.join([line.rstrip() for line in pretty_lines]) + '\n' |
57 | 57 |
58 | 58 |
59 def DownloadAndComputeHash(from_stream, to_stream=None, progress_func=None): | 59 def DownloadAndComputeHash(from_stream, to_stream=None, progress_func=None): |
60 ''' Download the archive data from from-stream and generate sha1 and | 60 ''' Download the archive data from from-stream and generate sha1 and |
61 size info. | 61 size info. |
62 | 62 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 101 |
102 | 102 |
103 class Archive(dict): | 103 class Archive(dict): |
104 """A placeholder for sdk archive information. We derive Archive from | 104 """A placeholder for sdk archive information. We derive Archive from |
105 dict so that it is easily serializable. """ | 105 dict so that it is easily serializable. """ |
106 | 106 |
107 def __init__(self, host_os_name): | 107 def __init__(self, host_os_name): |
108 """ Create a new archive for the given host-os name. """ | 108 """ Create a new archive for the given host-os name. """ |
109 self['host_os'] = host_os_name | 109 self['host_os'] = host_os_name |
110 | 110 |
111 def CopyFrom(self, dict): | 111 def CopyFrom(self, src): |
112 """Update the content of the archive by copying values from the given | 112 """Update the content of the archive by copying values from the given |
113 dictionary. | 113 dictionary. |
114 | 114 |
115 Args: | 115 Args: |
116 dict: The dictionary whose values must be copied to the archive.""" | 116 src: The dictionary whose values must be copied to the archive.""" |
117 for key, value in dict.items(): | 117 for key, value in src.items(): |
118 self[key] = value | 118 self[key] = value |
119 | 119 |
120 def Validate(self): | 120 def Validate(self): |
121 """Validate the content of the archive object. Raise an Error if | 121 """Validate the content of the archive object. Raise an Error if |
122 an invalid or missing field is found. | 122 an invalid or missing field is found. |
123 | 123 |
124 Returns: True if self is a valid bundle. | 124 Returns: True if self is a valid bundle. |
125 """ | 125 """ |
126 host_os = self.get('host_os', None) | 126 host_os = self.get('host_os', None) |
127 if host_os and host_os not in HOST_OS_LITERALS: | 127 if host_os and host_os not in HOST_OS_LITERALS: |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 new_bundle.CopyFrom(b) | 513 new_bundle.CopyFrom(b) |
514 bundles.append(new_bundle) | 514 bundles.append(new_bundle) |
515 self._manifest_data[key] = bundles | 515 self._manifest_data[key] = bundles |
516 else: | 516 else: |
517 self._manifest_data[key] = value | 517 self._manifest_data[key] = value |
518 self.Validate() | 518 self.Validate() |
519 | 519 |
520 def GetDataAsString(self): | 520 def GetDataAsString(self): |
521 """Returns the current JSON manifest object, pretty-printed""" | 521 """Returns the current JSON manifest object, pretty-printed""" |
522 return DictToJSON(self._manifest_data) | 522 return DictToJSON(self._manifest_data) |
OLD | NEW |