-
Notifications
You must be signed in to change notification settings - Fork 796
/
Copy pathsync_website.py
118 lines (89 loc) · 3.6 KB
/
sync_website.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
from __future__ import annotations
import argparse
import os
from typing import TYPE_CHECKING
from tools import fs
if TYPE_CHECKING:
from pathlib import Path
from typing import Literal
DOC_REPO_ORG: Literal["altair-viz"] = "altair-viz"
GITHUB: Literal["github"] = "github"
WEBSITE: str = f"{DOC_REPO_ORG}.{GITHUB}.io"
DOC_REPO_URL: str = f"https://{GITHUB}.com/{DOC_REPO_ORG}/{WEBSITE}.git"
DOC_DIR: Path = fs.REPO_ROOT / "doc"
DOC_BUILD_DIR: Path = DOC_DIR / "_build"
DOC_REPO_DIR: Path = DOC_BUILD_DIR / WEBSITE
DOC_HTML_DIR: Path = DOC_BUILD_DIR / "html"
DOC_BUILD_INFO: Path = DOC_HTML_DIR / ".buildinfo"
CMD_CLONE = f"git clone --filter=blob:none {DOC_REPO_URL}"
"""Partial clone, as we only keep ``./.git/**``."""
CMD_PULL = "git pull"
CMD_HEAD_HASH = "git rev-parse HEAD"
CMD_ADD = "git add . --all --force"
CMD_COMMIT = "git commit -m"
CMD_PUSH = "git push origin master"
COMMIT_MSG_PREFIX = "doc build for commit"
UNTRACKED = ".git"
def clone_or_sync_repo() -> None:
os.chdir(DOC_BUILD_DIR)
if not DOC_REPO_DIR.exists():
print(f"Cloning repo {WEBSITE!r}\n -> {fs.path_repr(DOC_REPO_DIR)}")
fs.run_stream_stdout(CMD_CLONE)
else:
print(f"Using existing cloned altair directory {fs.path_repr(DOC_REPO_DIR)}")
os.chdir(DOC_REPO_DIR)
print(f"Syncing {WEBSITE!r}\n -> {fs.path_repr(DOC_REPO_DIR)} ...")
fs.run_stream_stdout(CMD_PULL)
def remove_tracked_files() -> None:
os.chdir(DOC_REPO_DIR)
print(f"Removing all tracked files from {fs.path_repr(DOC_REPO_DIR)} ...")
for fp in DOC_REPO_DIR.iterdir():
if fp.name == UNTRACKED:
continue
fs.rm(fp)
def sync_from_html_build() -> None:
print(f"Syncing files from {fs.path_repr(DOC_HTML_DIR)} ...")
copy_ret = fs.copytree(DOC_HTML_DIR, DOC_REPO_DIR)
print(f"Successful copy to: {fs.path_repr(copy_ret)}")
def generate_commit_message() -> str:
os.chdir(DOC_REPO_DIR)
print("Generating commit message ...")
return f"{COMMIT_MSG_PREFIX} {fs.run_check(CMD_HEAD_HASH).stdout.strip()}"
def current_branch() -> str:
return fs.run_check(["git", "branch", "--show-current"]).stdout.rstrip()
def add_commit_push_github(msg: str, /) -> None:
os.chdir(DOC_REPO_DIR)
print("Pushing ...")
# NOTE: Ensures the message uses cross-platform escaping
cmd_commit = *CMD_COMMIT.split(" "), msg
commands = (CMD_ADD, cmd_commit, CMD_PUSH)
for command in commands:
fs.run_stream_stdout(command)
def ensure_build_html() -> None:
if not fs.dir_exists(DOC_HTML_DIR):
raise FileNotFoundError(DOC_HTML_DIR)
if not DOC_BUILD_INFO.exists():
raise FileNotFoundError(DOC_BUILD_INFO)
fs.mkdir(DOC_REPO_DIR)
time = fs.modified_time(DOC_BUILD_INFO).isoformat(" ", "seconds")
print(f"Docs last build time: {time!r}")
def main(*, no_commit: bool = False) -> None:
ensure_build_html()
commit_message = generate_commit_message()
clone_or_sync_repo()
remove_tracked_files()
sync_from_html_build()
branch = current_branch()
if no_commit:
print(f"Unused commit message:\n {commit_message!r}")
elif branch != "main":
# FIXME: Unsure how to reproduce the RELEASING.md steps from this PR
# https://github.com/vega/altair/blob/main/RELEASING.md
print(f"Unable to push from {branch!r}.\n" "Must be on 'main'.")
else:
add_commit_push_github(commit_message)
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="sync_website.py")
parser.add_argument("--no-commit", action="store_true")
args = parser.parse_args()
main(no_commit=args.no_commit)