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>
12 from optparse
import OptionParser
22 This script downloads a talos.json file which indicates which files to download
24 See a talos.json file for a better understand:
25 http://hg.mozilla.org/mozilla-central/raw-file/default/testing/talos/talos.json
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."
37 # 2) try to download the talos.json file
39 jsonFilename
= download_file(options
.talos_json_url
)
41 print "ERROR: We tried to download the talos.json file but something failed."
42 print "ERROR: %s" % str(e
)
45 # 3) download the necessary files
46 print "INFO: talos.json URL: %s" % options
.talos_json_url
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
)
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."
61 print "ERROR: %s" % str(e
)
64 def passesRestrictions(talosJsonUrl
, fileUrl
):
66 Only certain branches are exempted from having to host their downloadable files
67 in talos-bundles.pvt.build.mozilla.org
69 if talosJsonUrl
.startswith("http://hg.mozilla.org/try/") == True or \
70 talosJsonUrl
.startswith("http://hg.mozilla.org/projects/pine/") == True:
73 p
= re
.compile('^http://talos-bundles.pvt.build.mozilla.org/')
79 def get_filename_from_url(url
):
81 This returns the filename of the file we're trying to download
83 parsed
= urlparse
.urlsplit(url
.rstrip('/'))
85 return parsed
.path
.rsplit('/', 1)[-1]
87 print "ERROR: We were trying to download a file from %s " + \
88 "but the URL seems to be incorrect."
91 def download_file(url
, path
="", saveAs
=None):
93 It downloads a file from URL to the indicated path
95 req
= urllib2
.Request(url
)
96 f
= urllib2
.urlopen(req
)
97 if path
!= "" and not os
.path
.isdir(path
):
100 print "INFO: directory %s created" % path
102 print "ERROR: %s" % str(e
)
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())
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__':