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