OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 import json | |
Peter Lundblad
2014/05/22 00:28:25
Care to provide a minimal docstring?
David Tseng
2014/05/22 02:42:04
Done.
| |
8 import optparse | |
9 import sys | |
10 | |
11 def MergeManifests(base, override, output): | |
12 ''' Takes two extension manifests and merges them into an output manifest. | |
Peter Lundblad
2014/05/22 00:28:25
Nit: space after '''
David Tseng
2014/05/22 02:42:04
Remove it?
+ ''' Takes two extension manifests an
| |
13 | |
14 Args: | |
15 base: path to a extension manifest. | |
16 override: path to a extension manifest. | |
17 output: path to the result of merging base and override. | |
18 ''' | |
19 base_file = open(base) | |
20 override_file = open(override) | |
Peter Lundblad
2014/05/22 00:28:25
Consider using with to have these automatically cl
David Tseng
2014/05/22 02:42:04
Done. (Nice construct).
| |
21 base_manifest = json.load(base_file) | |
22 override_manifest = json.load(override_file) | |
23 MergeDicts(base_manifest, override_manifest) | |
24 json.dump(base_manifest, open(output, 'w'), sort_keys=True, indent=2) | |
Peter Lundblad
2014/05/22 00:28:25
Can you put the open call out of line and close th
David Tseng
2014/05/22 02:42:04
Done.
| |
25 | |
26 | |
27 def MergeDicts(base, override): | |
28 '''Merges override into base. | |
29 | |
30 Args: | |
31 base: dictionary with base key values. | |
32 override: dictionary with key values to insert (and possibly overwrite) in | |
33 base. | |
34 ''' | |
35 for key in override: | |
36 if type(override[key]) is dict: | |
37 MergeDicts(base, override[key]) | |
Peter Lundblad
2014/05/22 00:28:25
Shouldn't you merge override into base[key], creat
David Tseng
2014/05/22 02:42:04
yYup; goofed on this case; also handle the case wh
| |
38 else: | |
39 base[key] = override[key] | |
40 | |
41 | |
42 def main(): | |
43 parser = optparse.OptionParser(description=__doc__) | |
44 parser.usage = ('%prog [options] <base_manifest_path>' | |
45 ' <override_manifest_path> <output_manifest_path>') | |
Peter Lundblad
2014/05/22 00:28:25
I recommend making the out file an -o option to be
| |
46 _, args = parser.parse_args() | |
47 if len(args) != 3: | |
48 print >>sys.stderr, 'Expected exactly three arguments' | |
49 sys.exit(1) | |
50 MergeManifests(args[0], args[1], args[2]) | |
51 | |
52 if __name__ == '__main__': | |
53 main() | |
OLD | NEW |