blob: 61e0b5d30ea2c0a0a9750bb9910b727dbf42f1dc [file] [log] [blame]
[email protected]a3d7c4b2013-11-20 02:14:081#!/usr/bin/env python
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Wraps gclient calls with annotated output.
7
8Note that you will have to use -- to stop option parsing for gclient flags.
9
10To run `gclient sync --gclientfile=.gclient` and annotate got_v8_revision:
11 `annotated_gclient.py --revision-mapping='{"src/v8": "got_v8_revision"}' --
12 sync --gclientfile=.gclient`
13"""
14
15import contextlib
16import json
17import optparse
18import os
19import subprocess
20import sys
21import tempfile
22
23
24@contextlib.contextmanager
25def temp_filename(suffix='', prefix='tmp'):
26 output_fd, output_file = tempfile.mkstemp(suffix=suffix, prefix=prefix)
27 os.close(output_fd)
28
29 yield output_file
30
31 try:
32 os.remove(output_file)
33 except OSError as e:
34 print 'Error cleaning up temp file %s: %s' % (output_file, e)
35
36
37def parse_got_revision(filename, revision_mapping):
38 result = {}
39 with open(filename) as f:
40 data = json.load(f)
41
42 for path, info in data['solutions'].iteritems():
43 # gclient json paths always end with a slash
44 path = path.rstrip('/')
45 if path in revision_mapping:
46 propname = revision_mapping[path]
47 result[propname] = info['revision']
48
49 return result
50
51
52def emit_buildprops(got_revisions):
53 for prop, revision in got_revisions.iteritems():
[email protected]2b2b3702013-11-21 07:25:4754 print '@@@SET_BUILD_PROPERTY@%s@%s@@@' % (prop, json.dumps(revision))
[email protected]a3d7c4b2013-11-20 02:14:0855
56
57def main():
58 parser = optparse.OptionParser(
59 description=('Runs gclient and annotates the output with any '
60 'got_revisions.'))
61 parser.add_option('--revision-mapping', default='{}',
62 help='json dict of directory-to-property mappings.')
63 parser.add_option('--suffix', default='gclient',
64 help='tempfile suffix')
65 opts, args = parser.parse_args()
66
67 revision_mapping = json.loads(opts.revision_mapping)
68
69 if not args:
70 parser.error('Must provide arguments to gclient.')
71
72 if any(a.startswith('--output-json') for a in args):
73 parser.error('Can\'t call annotated_gclient with --output-json.')
74
75 with temp_filename(opts.suffix) as f:
76 cmd = ['gclient']
77 cmd.extend(args)
78 cmd.extend(['--output-json', f])
79 p = subprocess.Popen(cmd)
80 p.wait()
81
82 if p.returncode == 0:
83 revisions = parse_got_revision(f, revision_mapping)
84 emit_buildprops(revisions)
85 return p.returncode
86
87
88if __name__ == '__main__':
89 sys.exit(main())