2012-01-28 00:50:07 +04:00
|
|
|
#! /usr/bin/env python
|
2012-05-21 15:12:37 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2012-02-02 20:03:47 +04:00
|
|
|
#
|
|
|
|
# Script name: talos_from_code.py
|
|
|
|
# Purpose: Read from a talos.json file the different files to download for a talos job
|
|
|
|
# Author(s): Zambrano Gasparnian, Armen <armenzg@mozilla.com>
|
|
|
|
# Target: Python 2.5
|
|
|
|
#
|
2012-01-28 00:50:07 +04:00
|
|
|
from optparse import OptionParser
|
2013-10-01 18:32:58 +04:00
|
|
|
import json
|
2012-01-28 00:50:07 +04:00
|
|
|
import re
|
|
|
|
import urllib2
|
|
|
|
import urlparse
|
|
|
|
import sys
|
2012-02-02 20:03:47 +04:00
|
|
|
import os
|
2012-01-28 00:50:07 +04:00
|
|
|
|
2016-07-01 18:48:07 +03:00
|
|
|
|
2012-01-28 00:50:07 +04:00
|
|
|
def main():
|
2012-02-02 20:03:47 +04:00
|
|
|
'''
|
|
|
|
This script downloads a talos.json file which indicates which files to download
|
|
|
|
for a talos job.
|
|
|
|
See a talos.json file for a better understand:
|
|
|
|
http://hg.mozilla.org/mozilla-central/raw-file/default/testing/talos/talos.json
|
|
|
|
'''
|
2012-01-28 00:50:07 +04:00
|
|
|
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()
|
|
|
|
|
2012-02-02 20:03:47 +04:00
|
|
|
# 1) check that the url was passed
|
2016-07-01 18:48:07 +03:00
|
|
|
if options.talos_json_url is None:
|
|
|
|
print("You need to specify --talos-json-url.")
|
2012-01-28 00:50:07 +04:00
|
|
|
sys.exit(1)
|
|
|
|
|
2012-02-02 20:03:47 +04:00
|
|
|
# 2) try to download the talos.json file
|
2012-01-28 00:50:07 +04:00
|
|
|
try:
|
|
|
|
jsonFilename = download_file(options.talos_json_url)
|
2016-07-01 18:48:07 +03:00
|
|
|
except Exception as e:
|
|
|
|
print("ERROR: We tried to download the talos.json file but something failed.")
|
|
|
|
print("ERROR: %s" % str(e))
|
2012-01-28 00:50:07 +04:00
|
|
|
sys.exit(1)
|
|
|
|
|
2012-02-02 20:03:47 +04:00
|
|
|
# 3) download the necessary files
|
2016-07-01 18:48:07 +03:00
|
|
|
print("INFO: talos.json URL: %s" % options.talos_json_url)
|
2012-01-28 00:50:07 +04:00
|
|
|
try:
|
2012-03-08 18:52:51 +04:00
|
|
|
key = 'talos.zip'
|
|
|
|
entity = get_value(jsonFilename, key)
|
|
|
|
if passesRestrictions(options.talos_json_url, entity["url"]):
|
|
|
|
# the key is at the same time the filename e.g. talos.zip
|
2016-07-01 18:48:07 +03:00
|
|
|
print("INFO: Downloading %s as %s" %
|
|
|
|
(entity["url"], os.path.join(entity["path"], key)))
|
2012-03-08 18:52:51 +04:00
|
|
|
download_file(entity["url"], entity["path"], key)
|
|
|
|
else:
|
2016-07-01 18:48:07 +03:00
|
|
|
print("ERROR: You have tried to download a file " +
|
|
|
|
"from: %s " % entity["url"] +
|
|
|
|
"which is a location different than http://talos-bundles.pvt.build.mozilla.org/")
|
|
|
|
print("ERROR: This is only allowed for the certain branches.")
|
2012-03-08 18:52:51 +04:00
|
|
|
sys.exit(1)
|
2016-07-01 18:48:07 +03:00
|
|
|
except Exception as e:
|
|
|
|
print("ERROR: %s" % str(e))
|
2012-01-28 00:50:07 +04:00
|
|
|
sys.exit(1)
|
|
|
|
|
2016-07-01 18:48:07 +03:00
|
|
|
|
2012-01-28 00:50:07 +04:00
|
|
|
def passesRestrictions(talosJsonUrl, fileUrl):
|
|
|
|
'''
|
|
|
|
Only certain branches are exempted from having to host their downloadable files
|
2013-08-13 19:06:52 +04:00
|
|
|
in talos-bundles.pvt.build.mozilla.org
|
2012-01-28 00:50:07 +04:00
|
|
|
'''
|
2014-09-24 20:25:39 +04:00
|
|
|
if talosJsonUrl.startswith("http://hg.mozilla.org/try/") or \
|
|
|
|
talosJsonUrl.startswith("https://hg.mozilla.org/try/") or \
|
|
|
|
talosJsonUrl.startswith("http://hg.mozilla.org/projects/pine/") or \
|
|
|
|
talosJsonUrl.startswith("https://hg.mozilla.org/projects/pine/") or \
|
|
|
|
talosJsonUrl.startswith("http://hg.mozilla.org/projects/ash/") or \
|
|
|
|
talosJsonUrl.startswith("https://hg.mozilla.org/projects/ash/"):
|
2012-01-28 00:50:07 +04:00
|
|
|
return True
|
|
|
|
else:
|
2013-08-13 19:06:52 +04:00
|
|
|
p = re.compile('^http://talos-bundles.pvt.build.mozilla.org/')
|
2012-01-28 00:50:07 +04:00
|
|
|
m = p.match(fileUrl)
|
2016-07-01 18:48:07 +03:00
|
|
|
if m is None:
|
2012-01-28 00:50:07 +04:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2016-07-01 18:48:07 +03:00
|
|
|
|
2012-01-28 00:50:07 +04:00
|
|
|
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:
|
2016-07-01 18:48:07 +03:00
|
|
|
print("ERROR: We were trying to download a file from %s " +
|
|
|
|
"but the URL seems to be incorrect.")
|
2012-01-28 00:50:07 +04:00
|
|
|
sys.exit(1)
|
|
|
|
|
2016-07-01 18:48:07 +03:00
|
|
|
|
2012-02-02 20:03:47 +04:00
|
|
|
def download_file(url, path="", saveAs=None):
|
2012-01-28 00:50:07 +04:00
|
|
|
'''
|
2012-02-02 20:03:47 +04:00
|
|
|
It downloads a file from URL to the indicated path
|
2012-01-28 00:50:07 +04:00
|
|
|
'''
|
|
|
|
req = urllib2.Request(url)
|
|
|
|
f = urllib2.urlopen(req)
|
2012-02-02 20:03:47 +04:00
|
|
|
if path != "" and not os.path.isdir(path):
|
|
|
|
try:
|
|
|
|
os.makedirs(path)
|
2016-07-01 18:48:07 +03:00
|
|
|
print("INFO: directory %s created" % path)
|
|
|
|
except Exception as e:
|
|
|
|
print("ERROR: %s" % str(e))
|
2012-02-02 20:03:47 +04:00
|
|
|
sys.exit(1)
|
|
|
|
filename = saveAs if saveAs else get_filename_from_url(url)
|
|
|
|
local_file = open(os.path.join(path, filename), 'wb')
|
2012-01-28 00:50:07 +04:00
|
|
|
local_file.write(f.read())
|
|
|
|
local_file.close()
|
|
|
|
return filename
|
|
|
|
|
2016-07-01 18:48:07 +03:00
|
|
|
|
2012-01-28 00:50:07 +04:00
|
|
|
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()
|