File: get-solr-download-url.py

package info (click to toggle)
python-pysolr 3.8.1-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 312 kB
  • sloc: python: 1,704; sh: 166; makefile: 9
file content (56 lines) | stat: -rwxr-xr-x 1,888 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# encoding: utf-8

from __future__ import absolute_import, division, print_function, unicode_literals

from itertools import chain
import sys

import requests

# Try to import urllib from the Python 3 reorganized stdlib first:
try:
    from urllib.parse import urljoin
except ImportError:
    try:
        from urlparse.parse import urljoin
    except ImportError:
        from urlparse import urljoin


if len(sys.argv) != 2:
    print('Usage: %s SOLR_VERSION' % sys.argv[0], file=sys.stderr)
    sys.exit(1)

solr_version = sys.argv[1]
tarball = 'solr-{0}.tgz'.format(solr_version)
dist_path = 'lucene/solr/{0}/{1}'.format(solr_version, tarball)

download_url = urljoin('https://archive.apache.org/dist/', dist_path)
mirror_response = requests.get("https://www.apache.org/dyn/mirrors/mirrors.cgi/%s?asjson=1" % dist_path)

if not mirror_response.ok:
    print('Apache mirror request returned HTTP %d' % mirror_response.status_code, file=sys.stderr)
    sys.exit(1)

mirror_data = mirror_response.json()

# Since the Apache mirrors are often unreliable and releases may disappear without notice we'll
# try the preferred mirror, all of the alternates and backups, and fall back to the main Apache
# archive server:
for base_url in chain((mirror_data['preferred'], ), mirror_data['http'], mirror_data['backup'],
                      ('https://archive.apache.org/dist/', )):
    test_url = urljoin(base_url, mirror_data['path_info'])

    # The Apache mirror script's response format has recently changed to exclude the actual file paths:
    if not test_url.endswith(tarball):
        test_url = urljoin(test_url, dist_path)

    if requests.head(test_url, allow_redirects=True).status_code == 200:
        download_url = test_url
        break
else:
    print('None of the Apache mirrors have %s' % dist_path, file=sys.stderr)
    sys.exit(1)

print(download_url)