1#!/usr/bin/env python 2# encoding: utf-8 3 4from __future__ import absolute_import, division, print_function, unicode_literals 5 6from itertools import chain 7import sys 8 9import requests 10 11# Try to import urljoin from the Python 3 reorganized stdlib first: 12try: 13 from urllib.parse import urljoin 14except ImportError: 15 from urlparse import urljoin 16 17 18if len(sys.argv) != 2: 19 print('Usage: %s SOLR_VERSION' % sys.argv[0], file=sys.stderr) 20 sys.exit(1) 21 22solr_version = sys.argv[1] 23tarball = 'solr-{0}.tgz'.format(solr_version) 24dist_path = 'lucene/solr/{0}/{1}'.format(solr_version, tarball) 25 26download_url = urljoin('https://archive.apache.org/dist/', dist_path) 27mirror_response = requests.get("https://www.apache.org/dyn/mirrors/mirrors.cgi/%s?asjson=1" % dist_path) 28 29if not mirror_response.ok: 30 print('Apache mirror request returned HTTP %d' % mirror_response.status_code, file=sys.stderr) 31 sys.exit(1) 32 33mirror_data = mirror_response.json() 34 35# Since the Apache mirrors are often unreliable and releases may disappear without notice we'll 36# try the preferred mirror, all of the alternates and backups, and fall back to the main Apache 37# archive server: 38for base_url in chain((mirror_data['preferred'], ), mirror_data['http'], mirror_data['backup'], 39 ('https://archive.apache.org/dist/', )): 40 test_url = urljoin(base_url, mirror_data['path_info']) 41 42 # The Apache mirror script's response format has recently changed to exclude the actual file paths: 43 if not test_url.endswith(tarball): 44 test_url = urljoin(test_url, dist_path) 45 46 if requests.head(test_url, allow_redirects=True).status_code == 200: 47 download_url = test_url 48 break 49else: 50 print('None of the Apache mirrors have %s' % dist_path, file=sys.stderr) 51 sys.exit(1) 52 53print(download_url) 54