File: get-solr-download-url.py

package info (click to toggle)
django-haystack 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: python: 23,475; xml: 1,708; sh: 74; makefile: 71
file content (55 lines) | stat: -rwxr-xr-x 1,684 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
#!/usr/bin/env python
import sys
from itertools import chain
from urllib.parse import urljoin

import requests

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)

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

print(download_url)