This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.py
64 lines (56 loc) · 2.17 KB
/
fetch.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
# encoding: utf-8
import yaml
import requests
from contextlib import closing
import os
from urllib.parse import urlparse
# 转自https://www.zhihu.com/question/41132103/answer/93438156
def wget(url, file_name):
with closing(requests.get(url, stream=True)) as response:
chunk_size = 4096 # 单次请求最大值
print(file_name+" downloading...")
with open(file_name, "wb") as file:
for data in response.iter_content(chunk_size=chunk_size):
file.write(data)
def main():
root = os.path.join(os.getcwd(), 'docs')
if not os.path.exists(root):
os.mkdir(root)
chart_url = os.environ.get(
"CHARTS_URL", "https://kubernetes-charts.storage.googleapis.com/")
repo_url = os.environ.get("GIT_REPO")
if repo_url is None:
raise RuntimeError("You must specify a git repo!")
print("parse url...")
p = urlparse(repo_url)
git_user = p.path.split("/")[-2]
repo_name = p.path.split("/")[-1].split(".")[0]
default_mirror = "https://%s.github.io/%s/" % (git_user.lower(), repo_name)
mirror_url = os.environ.get("MIRROR_URL", default_mirror)
index_file = "index.yaml"
wget(chart_url + index_file, index_file)
with open(index_file) as f:
index = yaml.load(f)
entries = index["entries"]
new = index.copy()
for name, charts in entries.items():
for chart, new_chart in zip(charts, new["entries"][name]):
url = chart["urls"][0]
tar_name = url.split("/")[-1]
target = os.path.join(root, tar_name)
new_chart["urls"][0] = "/".join(
[mirror_url[:-1] if mirror_url.endswith("/") else mirror_url, tar_name])
# datetime format issure
new_chart["created"] = new_chart["created"].strftime('%Y-%m-%dT%H:%M:%S.%f000Z')
if os.path.exists(target):
continue
wget(url, target)
new["generated"] = new["generated"].strftime('%Y-%m-%dT%H:%M:%S.%f000Z')
with open(os.path.join(root, "index.yaml"), "w") as f:
yaml.dump(new, stream=f)
if __name__ == "__main__":
try:
main()
except Exception as error:
print(error)
exit(1)