-
Notifications
You must be signed in to change notification settings - Fork 14.5k
/
fetch_inventories.py
167 lines (145 loc) · 6.38 KB
/
fetch_inventories.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
from __future__ import annotations
import concurrent
import concurrent.futures
import datetime
import itertools
import os
import shutil
import sys
import traceback
from collections.abc import Iterator
from tempfile import NamedTemporaryFile
import requests
import urllib3.exceptions
from requests.adapters import DEFAULT_POOLSIZE
from sphinx.util.inventory import InventoryFileReader
from airflow.utils.helpers import partition
from docs.exts.docs_build.docs_builder import get_available_providers_packages
from docs.exts.docs_build.third_party_inventories import THIRD_PARTY_INDEXES
CURRENT_DIR = os.path.dirname(__file__)
ROOT_DIR = os.path.abspath(os.path.join(CURRENT_DIR, os.pardir, os.pardir, os.pardir))
DOCS_DIR = os.path.join(ROOT_DIR, "docs")
CACHE_DIR = os.path.join(DOCS_DIR, "_inventory_cache")
EXPIRATION_DATE_PATH = os.path.join(DOCS_DIR, "_inventory_cache", "expiration-date")
S3_DOC_URL = "http://apache-airflow-docs.s3-website.eu-central-1.amazonaws.com"
S3_DOC_URL_VERSIONED = S3_DOC_URL + "/docs/{package_name}/stable/objects.inv"
S3_DOC_URL_NON_VERSIONED = S3_DOC_URL + "/docs/{package_name}/objects.inv"
def _fetch_file(session: requests.Session, package_name: str, url: str, path: str) -> tuple[str, bool]:
"""
Download a file, validate Sphinx Inventory headers and returns status information as a tuple with package
name and success status(bool value).
"""
try:
response = session.get(url, allow_redirects=True, stream=True)
except (requests.RequestException, urllib3.exceptions.HTTPError):
print(f"{package_name}: Failed to fetch inventory: {url}")
traceback.print_exc(file=sys.stderr)
return package_name, False
if not response.ok:
print(f"{package_name}: Failed to fetch inventory: {url}")
print(f"{package_name}: Failed with status: {response.status_code}", file=sys.stderr)
return package_name, False
if response.url != url:
print(f"{package_name}: {url} redirected to {response.url}")
with NamedTemporaryFile(suffix=package_name, mode="wb+") as tf:
for chunk in response.iter_content(chunk_size=4096):
tf.write(chunk)
tf.flush()
tf.seek(0, 0)
line = InventoryFileReader(tf).readline()
if not line.startswith("# Sphinx inventory version"):
print(f"{package_name}: Response contain unexpected Sphinx Inventory header: {line!r}.")
return package_name, False
tf.seek(0, 0)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "wb") as f:
shutil.copyfileobj(tf, f)
print(f"{package_name}: Fetched inventory: {response.url}")
return package_name, True
def _is_outdated(path: str):
if not os.path.exists(path):
return True
delta = datetime.datetime.now() - datetime.datetime.fromtimestamp(os.path.getmtime(path))
return delta > datetime.timedelta(hours=12)
def fetch_inventories():
"""Fetch all inventories for Airflow documentation packages and store in cache."""
os.makedirs(os.path.dirname(CACHE_DIR), exist_ok=True)
to_download: list[tuple[str, str, str]] = []
for pkg_name in get_available_providers_packages():
to_download.append(
(
pkg_name,
S3_DOC_URL_VERSIONED.format(package_name=pkg_name),
f"{CACHE_DIR}/{pkg_name}/objects.inv",
)
)
for pkg_name in ["apache-airflow", "helm-chart"]:
to_download.append(
(
pkg_name,
S3_DOC_URL_VERSIONED.format(package_name=pkg_name),
f"{CACHE_DIR}/{pkg_name}/objects.inv",
)
)
for pkg_name in ["apache-airflow-providers", "docker-stack"]:
to_download.append(
(
pkg_name,
S3_DOC_URL_NON_VERSIONED.format(package_name=pkg_name),
f"{CACHE_DIR}/{pkg_name}/objects.inv",
)
)
to_download.extend(
(
pkg_name,
f"{doc_url}/objects.inv",
f"{CACHE_DIR}/{pkg_name}/objects.inv",
)
for pkg_name, doc_url in THIRD_PARTY_INDEXES.items()
)
to_download = [(pkg_name, url, path) for pkg_name, url, path in to_download if _is_outdated(path)]
if not to_download:
print("Nothing to do")
return []
print(f"To download {len(to_download)} inventorie(s)")
with requests.Session() as session, concurrent.futures.ThreadPoolExecutor(DEFAULT_POOLSIZE) as pool:
download_results: Iterator[tuple[str, bool]] = pool.map(
_fetch_file,
itertools.repeat(session, len(to_download)),
(pkg_name for pkg_name, _, _ in to_download),
(url for _, url, _ in to_download),
(path for _, _, path in to_download),
)
failed, success = partition(lambda d: d[1], download_results)
failed, success = list(failed), list(success)
print(f"Result: {len(success)} success, {len(failed)} failed")
if failed:
terminate = False
print("Failed packages:")
for pkg_no, (pkg_name, _) in enumerate(failed, start=1):
print(f"{pkg_no}. {pkg_name}")
if not terminate and not pkg_name.startswith("apache-airflow"):
# For solve situation that newly created Community Provider doesn't upload inventory yet.
# And we terminate execution only if any error happen during fetching
# third party intersphinx inventories.
terminate = True
if terminate:
print("Terminate execution.")
raise SystemExit(1)
return [pkg_name for pkg_name, status in failed]