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>
16 from optparse
import OptionParser
23 This script downloads a talos.json file which indicates which files to download
25 See a talos.json file for a better understand:
26 https://hg.mozilla.org/mozilla-central/raw-file/default/testing/talos/talos.json
28 parser
= OptionParser()
31 dest
="talos_json_url",
33 help="It indicates from where to download the talos.json file.",
35 (options
, args
) = parser
.parse_args()
37 # 1) check that the url was passed
38 if options
.talos_json_url
is None:
39 print("You need to specify --talos-json-url.")
42 # 2) try to download the talos.json file
44 jsonFilename
= download_file(options
.talos_json_url
)
45 except Exception as e
:
46 print("ERROR: We tried to download the talos.json file but something failed.")
47 print("ERROR: %s" % str(e
))
50 # 3) download the necessary files
51 print("INFO: talos.json URL: %s" % options
.talos_json_url
)
54 entity
= get_value(jsonFilename
, key
)
55 if passesRestrictions(options
.talos_json_url
, entity
["url"]):
56 # the key is at the same time the filename e.g. talos.zip
58 "INFO: Downloading %s as %s"
59 % (entity
["url"], os
.path
.join(entity
["path"], key
))
61 download_file(entity
["url"], entity
["path"], key
)
64 "ERROR: You have tried to download a file "
65 + "from: %s " % entity
["url"]
66 + "which is a location different than http://talos-bundles.pvt.build.mozilla.org/"
68 print("ERROR: This is only allowed for the certain branches.")
70 except Exception as e
:
71 print("ERROR: %s" % str(e
))
75 def passesRestrictions(talosJsonUrl
, fileUrl
):
77 Only certain branches are exempted from having to host their downloadable files
78 in talos-bundles.pvt.build.mozilla.org
81 talosJsonUrl
.startswith("http://hg.mozilla.org/try/")
82 or talosJsonUrl
.startswith("https://hg.mozilla.org/try/")
83 or talosJsonUrl
.startswith("http://hg.mozilla.org/projects/pine/")
84 or talosJsonUrl
.startswith("https://hg.mozilla.org/projects/pine/")
85 or talosJsonUrl
.startswith("http://hg.mozilla.org/projects/ash/")
86 or talosJsonUrl
.startswith("https://hg.mozilla.org/projects/ash/")
90 p
= re
.compile("^http://talos-bundles.pvt.build.mozilla.org/")
97 def get_filename_from_url(url
):
99 This returns the filename of the file we're trying to download
101 parsed
= six
.moves
.urllib
.parse
.urlsplit(url
.rstrip("/"))
102 if parsed
.path
!= "":
103 return parsed
.path
.rsplit("/", 1)[-1]
106 "ERROR: We were trying to download a file from %s "
107 + "but the URL seems to be incorrect."
112 def download_file(url
, path
="", saveAs
=None):
114 It downloads a file from URL to the indicated path
116 req
= six
.moves
.urllib
.request
.Request(url
)
117 f
= six
.moves
.urllib
.request
.urlopen(req
)
118 if path
!= "" and not os
.path
.isdir(path
):
121 print("INFO: directory %s created" % path
)
122 except Exception as e
:
123 print("ERROR: %s" % str(e
))
125 filename
= saveAs
if saveAs
else get_filename_from_url(url
)
126 local_file
= open(os
.path
.join(path
, filename
), "wb")
127 local_file
.write(f
.read())
132 def get_value(json_filename
, key
):
134 It loads up a JSON file and returns the value for the given string
136 f
= open(json_filename
, "r")
137 return json
.load(f
)[key
]
140 if __name__
== "__main__":