1#!/usr/bin/python
2# Copyright 2015 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"""Gets information about one commit from gitiles.
7
8Example usage:
9  ./fetch_revision_info.py 343b531d31 chromium
10  ./fetch_revision_info.py 17b4e7450d v8
11"""
12
13import argparse
14import json
15import urllib2
16
17from bisect_lib import depot_map
18
19_GITILES_PADDING = ')]}\'\n'
20_URL_TEMPLATE = 'https://chromium.googlesource.com/%s/+/%s?format=json'
21
22def FetchRevisionInfo(commit_hash, depot_name):
23  """Gets information about a chromium revision."""
24  path = depot_map.DEPOT_PATH_MAP[depot_name]
25  url = _URL_TEMPLATE % (path, commit_hash)
26  response = urllib2.urlopen(url).read()
27  response_json = response[len(_GITILES_PADDING):]
28  response_dict = json.loads(response_json)
29  message = response_dict['message'].splitlines()
30  subject = message[0]
31  body = '\n'.join(message[1:])
32  result = {
33      'author': response_dict['author']['name'],
34      'email': response_dict['author']['email'],
35      'subject': subject,
36      'body': body,
37      'date': response_dict['committer']['time'],
38  }
39  return result
40
41
42def Main():
43  parser = argparse.ArgumentParser()
44  parser.add_argument('commit_hash')
45  parser.add_argument('depot', choices=list(depot_map.DEPOT_PATH_MAP))
46  args = parser.parse_args()
47  revision_info = FetchRevisionInfo(args.commit_hash, args.depot)
48  print json.dumps(revision_info)
49
50
51if __name__ == '__main__':
52  Main()
53