Chromium Code Reviews| Index: tools/omahaproxy.py |
| diff --git a/tools/omahaproxy.py b/tools/omahaproxy.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..9ba956f70da7377b14aa71a62eafb393bf72d44c |
| --- /dev/null |
| +++ b/tools/omahaproxy.py |
| @@ -0,0 +1,109 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Chrome Version Tool |
| + |
| +A simple script to scrape the Chrome channel information and print out the |
|
M-A Ruel
2013/01/09 23:45:21
Scrapes the ..
I know it's simple. :)
scherkus (not reviewing)
2013/01/10 01:03:48
Hah! Done!
|
| +requested nugget of information. |
| +""" |
| + |
| +import optparse |
| +import os.path |
|
M-A Ruel
2013/01/09 23:45:21
Sort.
and in general we just "import os".
scherkus (not reviewing)
2013/01/10 01:03:48
Done.
|
| +import json |
| +import urllib |
| +import string |
| +import sys |
| + |
| +URL = 'http://omahaproxy.appspot.com/json' |
|
M-A Ruel
2013/01/09 23:45:21
https?
scherkus (not reviewing)
2013/01/10 01:03:48
Done.
|
| + |
|
M-A Ruel
2013/01/09 23:45:21
2 lines
scherkus (not reviewing)
2013/01/10 01:03:48
Done.
|
| +def main(): |
| + try: |
| + data = json.load(urllib.urlopen(URL)) |
| + except: |
|
M-A Ruel
2013/01/09 23:45:21
except Exception as e:
then print str(e)? It's use
scherkus (not reviewing)
2013/01/10 01:03:48
Done.
|
| + print 'Error: could not load %s' % URL |
| + return 1 |
| + |
| + # Iterate to find out valid values for OS, channel, and field options. |
| + oses = set() |
| + channels = set() |
| + fields = set() |
| + |
| + for os_versions in data: |
| + oses.add(os_versions['os']) |
| + |
| + for version in os_versions['versions']: |
| + for field in version: |
| + if field == 'channel': |
| + channels.add(version['channel']) |
| + else: |
| + fields.add(field) |
| + |
| + oses = sorted(oses) |
| + channels = sorted(channels) |
| + fields = sorted(fields) |
| + |
| + # Command line parsing fun begins! |
| + usage = ('%prog [options]\n' |
| + 'Print out information about a particular Chrome channel.') |
| + parser = optparse.OptionParser(usage=usage) |
| + |
| + parser.add_option('-o', '--os', |
| + choices = oses, |
|
M-A Ruel
2013/01/09 23:45:21
No space around =
scherkus (not reviewing)
2013/01/10 01:03:48
Done.
|
| + default = 'win', |
| + help = 'The operating system of interest: %s ' |
| + '[default: %%default]' % ', '.join(oses)) |
| + parser.add_option('-c', '--channel', |
| + choices = channels, |
| + help = 'The channel of interest: %s' % ', '.join(channels)) |
| + parser.add_option('-f', '--field', |
| + choices = fields, |
| + default = 'version', |
| + help = 'The field of interest: %s ' |
| + '[default: %%default] ' % ', '.join(fields)) |
| + (opts, args) = parser.parse_args() |
| + |
| + if len(sys.argv) == 1: |
| + parser.print_help() |
| + return 1 |
| + |
| + if opts.os is None: |
|
M-A Ruel
2013/01/09 23:45:21
It can't be None? If someone specifies --os foo, I
scherkus (not reviewing)
2013/01/10 01:03:48
You are correct!
This is also means I don't need
|
| + print 'Error: invalid OS' |
| + parser.print_help() |
| + return 1 |
| + |
| + if opts.channel is None: |
| + print 'Error: invalid channel' |
|
M-A Ruel
2013/01/09 23:45:21
parser.error('Please use --channel')
No need to r
scherkus (not reviewing)
2013/01/10 01:03:48
No longer needed.
|
| + parser.print_help() |
| + return 1 |
| + |
| + if opts.field is None: |
| + print 'Error: invalid field' |
|
M-A Ruel
2013/01/09 23:45:21
Same
scherkus (not reviewing)
2013/01/10 01:03:48
No longer needed.
|
| + parser.print_help() |
| + return 1 |
| + |
| + # Print out requested data if available. |
| + for os_versions in data: |
| + if os_versions['os'] != opts.os: |
| + continue |
| + |
| + for version in os_versions['versions']: |
| + if version['channel'] != opts.channel: |
| + continue |
| + |
| + if opts.field not in version: |
| + continue |
| + |
| + print version[opts.field] |
| + return 0 |
| + |
| + print 'Error: unable to find %s for Chrome %s %s.' % ( |
| + opts.field, opts.os, opts.channel) |
| + return 1 |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |