This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 673131. talos_from_source_code.py script. NPOTB. r=coop,jmaher
- Loading branch information
Armen Zambrano Gasparnian
committed
Dec 15, 2011
1 parent
f1842be
commit 788af89
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |