omahaproxy.py 2.5 KB

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