Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 8 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!
| |
| 9 requested nugget of information. | |
| 10 """ | |
| 11 | |
| 12 import optparse | |
| 13 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.
| |
| 14 import json | |
| 15 import urllib | |
| 16 import string | |
| 17 import sys | |
| 18 | |
| 19 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.
| |
| 20 | |
|
M-A Ruel
2013/01/09 23:45:21
2 lines
scherkus (not reviewing)
2013/01/10 01:03:48
Done.
| |
| 21 def main(): | |
| 22 try: | |
| 23 data = json.load(urllib.urlopen(URL)) | |
| 24 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.
| |
| 25 print 'Error: could not load %s' % URL | |
| 26 return 1 | |
| 27 | |
| 28 # Iterate to find out valid values for OS, channel, and field options. | |
| 29 oses = set() | |
| 30 channels = set() | |
| 31 fields = set() | |
| 32 | |
| 33 for os_versions in data: | |
| 34 oses.add(os_versions['os']) | |
| 35 | |
| 36 for version in os_versions['versions']: | |
| 37 for field in version: | |
| 38 if field == 'channel': | |
| 39 channels.add(version['channel']) | |
| 40 else: | |
| 41 fields.add(field) | |
| 42 | |
| 43 oses = sorted(oses) | |
| 44 channels = sorted(channels) | |
| 45 fields = sorted(fields) | |
| 46 | |
| 47 # Command line parsing fun begins! | |
| 48 usage = ('%prog [options]\n' | |
| 49 'Print out information about a particular Chrome channel.') | |
| 50 parser = optparse.OptionParser(usage=usage) | |
| 51 | |
| 52 parser.add_option('-o', '--os', | |
| 53 choices = oses, | |
|
M-A Ruel
2013/01/09 23:45:21
No space around =
scherkus (not reviewing)
2013/01/10 01:03:48
Done.
| |
| 54 default = 'win', | |
| 55 help = 'The operating system of interest: %s ' | |
| 56 '[default: %%default]' % ', '.join(oses)) | |
| 57 parser.add_option('-c', '--channel', | |
| 58 choices = channels, | |
| 59 help = 'The channel of interest: %s' % ', '.join(channels)) | |
| 60 parser.add_option('-f', '--field', | |
| 61 choices = fields, | |
| 62 default = 'version', | |
| 63 help = 'The field of interest: %s ' | |
| 64 '[default: %%default] ' % ', '.join(fields)) | |
| 65 (opts, args) = parser.parse_args() | |
| 66 | |
| 67 if len(sys.argv) == 1: | |
| 68 parser.print_help() | |
| 69 return 1 | |
| 70 | |
| 71 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
| |
| 72 print 'Error: invalid OS' | |
| 73 print | |
| 74 parser.print_help() | |
| 75 return 1 | |
| 76 | |
| 77 if opts.channel is None: | |
| 78 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.
| |
| 79 print | |
| 80 parser.print_help() | |
| 81 return 1 | |
| 82 | |
| 83 if opts.field is None: | |
| 84 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.
| |
| 85 print | |
| 86 parser.print_help() | |
| 87 return 1 | |
| 88 | |
| 89 # Print out requested data if available. | |
| 90 for os_versions in data: | |
| 91 if os_versions['os'] != opts.os: | |
| 92 continue | |
| 93 | |
| 94 for version in os_versions['versions']: | |
| 95 if version['channel'] != opts.channel: | |
| 96 continue | |
| 97 | |
| 98 if opts.field not in version: | |
| 99 continue | |
| 100 | |
| 101 print version[opts.field] | |
| 102 return 0 | |
| 103 | |
| 104 print 'Error: unable to find %s for Chrome %s %s.' % ( | |
| 105 opts.field, opts.os, opts.channel) | |
| 106 return 1 | |
| 107 | |
| 108 if __name__ == '__main__': | |
| 109 sys.exit(main()) | |
| OLD | NEW |