-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbuild.py
More file actions
127 lines (100 loc) · 4 KB
/
Copy pathbuild.py
File metadata and controls
127 lines (100 loc) · 4 KB
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
import os
import re
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
# pod2markdown keeps the line wrapping of the POD source inside inline code spans (C<...>) and
# backslash-escapes markdown punctuation that ends up at the start of a wrapped line. Material
# renders inline code with `white-space: pre-wrap`, so the wrap shows up as a line break and the
# backslash is displayed literally. Such spans are joined back into a single line.
INLINE_CODE_SPAN = re.compile(r"(?<!`)(`+)(?!`)(.+?)(?<!`)\1(?!`)", flags=re.S)
WRAPPED_LINE = re.compile(r"\n[ \t]*(?:\\(?=[-+*#>|]))?")
def join_inline_code_span(match: re.Match) -> str:
ticks, body = match.groups()
if "\n\n" in body:
# inline code spans never contain blank lines; this matched across paragraphs
return match.group()
return f"{ticks}{WRAPPED_LINE.sub(' ', body)}{ticks}"
def join_inline_code_spans(markdown: str) -> str:
return INLINE_CODE_SPAN.sub(join_inline_code_span, markdown)
def get_version_from_branch(branch: str) -> str:
if branch == "master":
return "master"
if match := re.match(r"openssl-([34]\.[0-9]+)", branch):
return match.group(1)
if branch == "OpenSSL_1_1_1-stable":
return "1.1.1"
if branch == "OpenSSL_1_0_2-stable":
return "1.0.2"
print(f"Incorrect branch {branch}")
raise SystemExit(1)
def clone(branch: str, tmp_dir: str) -> None:
subprocess.run(
["git", "clone", "--single-branch", "-b", branch, "--depth", "1", "https://github.com/openssl/openssl", tmp_dir]
)
def get_commit_hash(tmp_dir: str) -> str:
ps = subprocess.run(["git", "rev-parse", "HEAD"], cwd=tmp_dir, capture_output=True)
if ps.returncode != 0:
raise SystemExit(ps.returncode)
return ps.stdout.decode("utf-8").strip()
def build_manpages(tmp_dir: str):
if return_code := subprocess.run(["sh", "config"], cwd=tmp_dir).returncode != 0:
raise SystemExit(return_code)
cmd = ["make", "-j", str(os.cpu_count()), "build_man_docs"]
if return_code := subprocess.run(cmd, cwd=tmp_dir).returncode != 0:
raise SystemExit(return_code)
def clean_docs():
try:
shutil.rmtree("docs/")
except FileNotFoundError:
pass
def create_dirs():
os.makedirs("docs/man1", exist_ok=True)
os.makedirs("docs/man3", exist_ok=True)
os.makedirs("docs/man5", exist_ok=True)
os.makedirs("docs/man7", exist_ok=True)
def convert_pod_to_md(tmp_dir: str):
# for openssl 1.0.2
dir_map = {
"apps": "man1",
"crypto": "man3",
"ssl": "man3",
"man1": "man1",
"man3": "man3",
"man5": "man5",
"man7": "man7",
}
for pod in Path(f"{tmp_dir}/doc").glob("**/*.pod"):
if "internal" in pod.parent.parts:
continue
target = f"docs/{dir_map[pod.parent.name]}/{pod.stem}.md"
ps = subprocess.run(
["pod2markdown", "--html-encode-chars", "1", "--man-url-prefix", "../../man", str(pod), target]
)
if ps.returncode != 0:
raise SystemExit(ps.returncode)
markdown = Path(target).read_text(encoding="utf-8")
Path(target).write_text(join_inline_code_spans(markdown), encoding="utf-8")
def copy_images(tmp_dir: str):
shutil.copytree(f"{tmp_dir}/doc/man7/img", "docs/man7/img")
def build_site(commit: str, version: str):
commit_msg = f"deploy openssl/openssl@{commit} to {version}"
return subprocess.run(["mike", "deploy", "-m", commit_msg, version]).returncode
def main():
branch = sys.argv[1]
version = get_version_from_branch(branch)
clean_docs()
create_dirs()
with tempfile.TemporaryDirectory() as tmp_dir:
clone(branch, tmp_dir)
commit = get_commit_hash(tmp_dir)
if version not in ["1.0.2", "1.1.1"]:
build_manpages(tmp_dir)
convert_pod_to_md(tmp_dir)
if version not in ["1.0.2", "1.1.1"]:
copy_images(tmp_dir)
return build_site(commit, version)
if __name__ == "__main__":
sys.exit(main())