forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_site.py
150 lines (131 loc) · 4.86 KB
/
build_site.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
import contextlib
import json
import os
import pathlib
import shutil
import subprocess
from datetime import datetime, timezone
PYTHON_BUILD_DIR = pathlib.Path(__file__).parent
WHEEL_DIR = PYTHON_BUILD_DIR / "wheels"
SITE_OUTPUT_DIR = PYTHON_BUILD_DIR / "site"
shutil.rmtree(SITE_OUTPUT_DIR, ignore_errors=True)
SITE_OUTPUT_DIR.mkdir(parents=True)
SITE_ARTIFACT_WHEEL_DIR = SITE_OUTPUT_DIR / "artifacts" / "wheels"
SITE_ARTIFACT_WHEEL_DIR.mkdir(parents=True)
for wheel_file in WHEEL_DIR.glob("*"):
shutil.copy(wheel_file, SITE_ARTIFACT_WHEEL_DIR)
def package_name(wheel_file: pathlib.Path) -> str:
return wheel_file.name.split("-")[0].replace("_", "-")
# Get some extra context about the build
ts = datetime.now(timezone.utc).isoformat()
context_info: dict = {
"timestamp": ts,
}
# Get branch info.
with contextlib.suppress(Exception):
if branch_info := os.getenv("GITHUB_HEAD_REF"):
pass
else:
branch_info = subprocess.check_output(
["git", "branch", "--show-current"], text=True
)
context_info["branch"] = branch_info.strip()
# Get commit info.
with contextlib.suppress(Exception):
commit_info = subprocess.check_output(
["git", "log", "-1", "--pretty=%H%n%B"], text=True
)
commit_hash, commit_msg = commit_info.strip().split("\n", 1)
context_info["commit"] = {
"hash": commit_hash,
"message": commit_msg.strip(),
}
# Get PR info.
with contextlib.suppress(Exception):
pr_info = "unknown"
if github_ref := os.getenv("GITHUB_REF"):
# e.g. GITHUB_REF=refs/pull/12157/merge
parts = github_ref.split("/")
if parts[1] == "pull":
pull_number = parts[2]
pr_info = json.loads(
subprocess.check_output(
["gh", "pr", "view", pull_number, "--json", "title,number,url"],
text=True,
)
)
else:
# The `gh` CLI might be able to figure it out.
pr_info = json.loads(
subprocess.check_output(
["gh", "pr", "view", "--json", "title,number,url"], text=True
)
)
context_info["pr"] = pr_info
newline = "\n"
(SITE_OUTPUT_DIR / "index.html").write_text(
f"""
<html>
<head>
<title>DataHub Python Builds</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css" integrity="sha256-cDGQ39yChhpN5vzgHbjIdGEtQ5kXE9tttCsI7VR9TuY=" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js" integrity="sha256-fN8vcX2ULyTDspVTHEteK8hd3rQAb5thNiwakjAW75Q=" crossorigin="anonymous"></script>
<!-- CDN example (jsDelivr) -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dayjs.min.js" integrity="sha256-nP25Pzivzy0Har7NZtMr/TODzfGWdlTrwmomYF2vQXM=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/plugin/relativeTime.js" integrity="sha256-muryXOPFkVJcJO1YFmhuKyXYmGDT2TYVxivG0MCgRzg=" crossorigin="anonymous"></script>
<script>dayjs.extend(window.dayjs_plugin_relativeTime)</script>
</head>
<body>
<div class="ui container">
<h1 class="ui header" style="padding-top: 1.5em;">DataHub Python Builds</h1>
<p>
These prebuilt wheel files can be used to install our Python packages as of a specific commit.
</p>
<h2>Build context</h2>
<p>
Built <span id="build-timestamp">at {ts}</span>.
</p>
<pre id="context-info">{json.dumps(context_info, indent=2)}</pre>
<h2>Usage</h2>
<p>
Current base URL: <span class="base-url">unknown</span>
</p>
<table class="ui celled table">
<thead>
<tr>
<th>Package</th>
<th>Size</th>
<th>Install command</th>
</tr>
</thead>
<tbody>
{
newline.join(
f'''
<tr>
<td><code>{package_name(wheel_file)}</code></td>
<td>{wheel_file.stat().st_size / 1024 / 1024:.3f} MB</td>
<td><code>uv pip install '{package_name(wheel_file)} @ <span class="base-url"><base-url></span>/artifacts/wheels/{wheel_file.name}'</code></td>
</tr>
'''
for wheel_file in sorted(WHEEL_DIR.glob("*.whl"))
)
}
</tbody>
</table>
</div>
</body>
<script>
const baseUrl = window.location.href.split('/').slice(0, -1).join('/');
document.querySelectorAll(".base-url").forEach(el => {{
el.textContent = baseUrl;
}});
const buildTimestamp = document.getElementById("build-timestamp");
const buildTimestampDate = dayjs('{ts}');
buildTimestamp.textContent = buildTimestampDate.fromNow();
</script>
</html>
"""
)
print("DataHub Python wheel site built in", SITE_OUTPUT_DIR)