talos_from_code.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #! /usr/bin/env python
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. #
  6. # Script name: talos_from_code.py
  7. # Purpose: Read from a talos.json file the different files to download for a talos job
  8. # Author(s): Zambrano Gasparnian, Armen <armenzg@mozilla.com>
  9. # Target: Python 2.5
  10. #
  11. from optparse import OptionParser
  12. import json
  13. import re
  14. import urllib2
  15. import urlparse
  16. import sys
  17. import os
  18. def main():
  19. '''
  20. This script downloads a talos.json file which indicates which files to download
  21. for a talos job.
  22. See a talos.json file for a better understand:
  23. http://hg.mozilla.org/mozilla-central/raw-file/default/testing/talos/talos.json
  24. '''
  25. parser = OptionParser()
  26. parser.add_option("--talos-json-url", dest="talos_json_url", type="string",
  27. help="It indicates from where to download the talos.json file.")
  28. (options, args) = parser.parse_args()
  29. # 1) check that the url was passed
  30. if options.talos_json_url is None:
  31. print("You need to specify --talos-json-url.")
  32. sys.exit(1)
  33. # 2) try to download the talos.json file
  34. try:
  35. jsonFilename = download_file(options.talos_json_url)
  36. except Exception as e:
  37. print("ERROR: We tried to download the talos.json file but something failed.")
  38. print("ERROR: %s" % str(e))
  39. sys.exit(1)
  40. # 3) download the necessary files
  41. print("INFO: talos.json URL: %s" % options.talos_json_url)
  42. try:
  43. key = 'talos.zip'
  44. entity = get_value(jsonFilename, key)
  45. if passesRestrictions(options.talos_json_url, entity["url"]):
  46. # the key is at the same time the filename e.g. talos.zip
  47. print("INFO: Downloading %s as %s" %
  48. (entity["url"], os.path.join(entity["path"], key)))
  49. download_file(entity["url"], entity["path"], key)
  50. else:
  51. print("ERROR: You have tried to download a file " +
  52. "from: %s " % entity["url"] +
  53. "which is a location different than http://talos-bundles.pvt.build.mozilla.org/")
  54. print("ERROR: This is only allowed for the certain branches.")
  55. sys.exit(1)
  56. except Exception as e:
  57. print("ERROR: %s" % str(e))
  58. sys.exit(1)
  59. def passesRestrictions(talosJsonUrl, fileUrl):
  60. '''
  61. Only certain branches are exempted from having to host their downloadable files
  62. in talos-bundles.pvt.build.mozilla.org
  63. '''
  64. if talosJsonUrl.startswith("http://hg.mozilla.org/try/") or \
  65. talosJsonUrl.startswith("https://hg.mozilla.org/try/") or \
  66. talosJsonUrl.startswith("http://hg.mozilla.org/projects/pine/") or \
  67. talosJsonUrl.startswith("https://hg.mozilla.org/projects/pine/") or \
  68. talosJsonUrl.startswith("http://hg.mozilla.org/projects/ash/") or \
  69. talosJsonUrl.startswith("https://hg.mozilla.org/projects/ash/"):
  70. return True
  71. else:
  72. p = re.compile('^http://talos-bundles.pvt.build.mozilla.org/')
  73. m = p.match(fileUrl)
  74. if m is None:
  75. return False
  76. return True
  77. def get_filename_from_url(url):
  78. '''
  79. This returns the filename of the file we're trying to download
  80. '''
  81. parsed = urlparse.urlsplit(url.rstrip('/'))
  82. if parsed.path != '':
  83. return parsed.path.rsplit('/', 1)[-1]
  84. else:
  85. print("ERROR: We were trying to download a file from %s " +
  86. "but the URL seems to be incorrect.")
  87. sys.exit(1)
  88. def download_file(url, path="", saveAs=None):
  89. '''
  90. It downloads a file from URL to the indicated path
  91. '''
  92. req = urllib2.Request(url)
  93. f = urllib2.urlopen(req)
  94. if path != "" and not os.path.isdir(path):
  95. try:
  96. os.makedirs(path)
  97. print("INFO: directory %s created" % path)
  98. except Exception as e:
  99. print("ERROR: %s" % str(e))
  100. sys.exit(1)
  101. filename = saveAs if saveAs else get_filename_from_url(url)
  102. local_file = open(os.path.join(path, filename), 'wb')
  103. local_file.write(f.read())
  104. local_file.close()
  105. return filename
  106. def get_value(json_filename, key):
  107. '''
  108. It loads up a JSON file and returns the value for the given string
  109. '''
  110. f = open(json_filename, 'r')
  111. return json.load(f)[key]
  112. if __name__ == '__main__':
  113. main()