diff --git a/scripts/talos/talos_from_code.py b/scripts/talos/talos_from_code.py new file mode 100644 index 000000000..8b54dda3b --- /dev/null +++ b/scripts/talos/talos_from_code.py @@ -0,0 +1,84 @@ +#! /usr/bin/env python +from optparse import OptionParser +try: + import json +except: + import simplejson as json +import re +import urllib2 +import urlparse + +def main(): + parser = OptionParser() + parser.add_option("--talos-json-url", dest="talos_json_url", type="string", + help="It indicates from where to download the talos.json file.") + (options, args) = parser.parse_args() + + if options.talos_json_url == None: + print "You need to specify --talos-json-url." + sys.exit(1) + + # json file with info on which talos.zip to use + # the json file URL should look like this: + # %(repo_path)s/raw-file/%(revision)s/testing/talos/talos.json + try: + jsonFilename = download_file(options.talos_json_url) + except: + print "ERROR: We have been unable to download the talos.zip indicated " + \ + "in the talos.json file." + print "ERROR: %s" % str(e) + sys.exit(1) + + print "INFO: talos.json URL: %s" % options.talos_json_url + talos_zip_url = get_value(jsonFilename, "talos_zip") + print "INFO: talos.zip URL: '%s'" % talos_zip_url + if options.talos_json_url.startswith("http://hg.mozilla.org/try/") == False: + p = re.compile('^http://build.mozilla.org/talos/zips/talos.*?\.zip$') + m = p.match(talos_zip_url) + if m == None: + print "ERROR: You have tried to download a talos.zip from a location " + \ + "different than http://build.mozilla.org/talos/zips" + print "ERROR: This is only allowed for the 'try' branch." + exit(1) + try: + download_file(talos_zip_url, "talos.zip") + except: + print "ERROR: We have been unable to download the talos.zip indicated " + \ + "in the talos.json file." + print "ERROR: %s" % str(e) + sys.exit(1) + +def get_filename_from_url(url): + ''' + This returns the filename of the file we're trying to download + ''' + parsed = urlparse.urlsplit(url.rstrip('/')) + if parsed.path != '': + return parsed.path.rsplit('/', 1)[-1] + else: + print "ERROR: We were trying to download a file from %s " + \ + "but the URL seems to be incorrect." + sys.exit(1) + +def download_file(url, saveAs=None): + ''' + It downloads a file from the URL indicated and can be saved locally with + a different name if needed. + ''' + req = urllib2.Request(url) + filename = get_filename_from_url(url) + f = urllib2.urlopen(req) + local_file = open(saveAs if saveAs else filename, 'wb') + local_file.write(f.read()) + local_file.close() + return filename + +def get_value(json_filename, key): + ''' + It loads up a JSON file and returns the value for the given string + ''' + f = open(json_filename, 'r') + return json.load(f)[key] + +if __name__ == '__main__': + main()