1#!/usr/bin/env python
2# Copyright (c) 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"""Chrome Version Tool
7
8Scrapes Chrome channel information and prints out the requested nugget of
9information.
10"""
11
12import json
13import optparse
14import os
15import string
16import sys
17import urllib
18
19URL = 'https://omahaproxy.appspot.com/json'
20
21
22def main():
23  try:
24    data = json.load(urllib.urlopen(URL))
25  except Exception as e:
26    print 'Error: could not load %s\n\n%s' % (URL, str(e))
27    return 1
28
29  # Iterate to find out valid values for OS, channel, and field options.
30  oses = set()
31  channels = set()
32  fields = set()
33
34  for os_versions in data:
35    oses.add(os_versions['os'])
36
37    for version in os_versions['versions']:
38      for field in version:
39        if field == 'channel':
40          channels.add(version['channel'])
41        else:
42          fields.add(field)
43
44  oses = sorted(oses)
45  channels = sorted(channels)
46  fields = sorted(fields)
47
48  # Command line parsing fun begins!
49  usage = ('%prog [options]\n'
50           'Print out information about a particular Chrome channel.')
51  parser = optparse.OptionParser(usage=usage)
52
53  parser.add_option('-o', '--os',
54                    choices=oses,
55                    default='win',
56                    help='The operating system of interest: %s '
57                         '[default: %%default]' % ', '.join(oses))
58  parser.add_option('-c', '--channel',
59                    choices=channels,
60                    default='stable',
61                    help='The channel of interest: %s '
62                         '[default: %%default]' % ', '.join(channels))
63  parser.add_option('-f', '--field',
64                    choices=fields,
65                    default='version',
66                    help='The field of interest: %s '
67                         '[default: %%default] ' % ', '.join(fields))
68  (opts, args) = parser.parse_args()
69
70  # Print out requested data if available.
71  for os_versions in data:
72    if os_versions['os'] != opts.os:
73      continue
74
75    for version in os_versions['versions']:
76      if version['channel'] != opts.channel:
77        continue
78
79      if opts.field not in version:
80        continue
81
82      print version[opts.field]
83      return 0
84
85  print 'Error: unable to find %s for Chrome %s %s.' % (
86      opts.field, opts.os, opts.channel)
87  return 1
88
89if __name__ == '__main__':
90  sys.exit(main())
91