blob: f5a2ff28684d62ed1a46829df8623cf6689c6ff7 [file] [log] [blame]
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Generate a Skylark file containing a map of hash of bazel installers."""
import gflags
import urllib2
import sys
_URL_FORMAT = "http://releases.bazel.build/{version}/release/bazel-{version}-without-jdk-installer-{platform}.sh.sha256"
_URL_EXISTS = "http://releases.bazel.build/{version}/release/index.html"
gflags.DEFINE_string("output", "bazel_hash_dict.bzl", "The output file")
gflags.DEFINE_string("map_name", "BAZEL_HASH_DICT",
"The name of the generated map in the output file")
gflags.DEFINE_multistring("platforms", ["darwin-x86_64", "linux-x86_64"],
"List of platforms to download SHA-256.")
gflags.DEFINE_string("minimum_version", "0.5.0",
"The lowest version of Bazel supported")
FLAGS = gflags.FLAGS
def get_hash_map(f):
"""Construct the hash map reading the release website, writing it to f."""
splitted_version = FLAGS.minimum_version.split(".")
if len(splitted_version) != 3:
sys.stderr.write(("Invalid version '%s', "
"expected a dot-separated version with 3 components "
"(e.g. 3.1.2)") % FLAGS.minimum_version)
sys.exit(-1)
version = [
int(splitted_version[0]),
int(splitted_version[1]),
int(splitted_version[2])
]
while True:
try:
v = "%s.%s.%s" % (version[0], version[1], version[2])
print "Getting SHA-256 for version " + v
# Force 404 before we actually add the information
urllib2.urlopen(_URL_EXISTS.format(version=v)).read()
f.write(" '%s': {\n" % v)
for platform in FLAGS.platforms:
r = urllib2.urlopen(
_URL_FORMAT.format(version=v, platform=platform))
f.write(" '%s': '%s',\n" % (platform,
r.read().split(" ", 1)[0]))
f.write(" },\n")
version[2] += 1
except urllib2.HTTPError as e:
if e.code == 404:
print " ==> Not a Bazel version"
# Current version does not exists, increase the lowest non null version number
if version[2] == 0:
if version[1] == 0:
return
version[1] = 0
version[0] += 1
else:
version[2] = 0
version[1] += 1
else:
raise e
def print_command_line(f):
"""Print the current command line."""
f.write("create_hash_dict")
for i in range(1, len(sys.argv)):
f.write(" '%s'" % (sys.argv[i].replace("'", "'\\''")))
def main(unused_argv):
with open(FLAGS.output, "w") as f:
f.write("# Automatically generated by ")
print_command_line(f)
f.write("\n\n%s = {\n" % FLAGS.map_name)
get_hash_map(f)
f.write("}\n")
if __name__ == "__main__":
main(FLAGS(sys.argv))