forked from pulp/pulp_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs-publisher.py
More file actions
executable file
·211 lines (188 loc) · 7.08 KB
/
Copy pathdocs-publisher.py
File metadata and controls
executable file
·211 lines (188 loc) · 7.08 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
# WARNING: DO NOT EDIT!
#
# This file was generated by plugin_template, and is managed by it. Please use
# './plugin-template --github pulp_python' to update this file.
#
# For more info visit https://github.com/pulp/plugin_template
import argparse
import subprocess
import os
import re
from shutil import rmtree
import tempfile
import requests
import json
from packaging import version
WORKING_DIR = os.environ["WORKSPACE"]
VERSION_REGEX = r"(\s*)(version)(\s*)(=)(\s*)(['\"])(.*)(['\"])(.*)"
RELEASE_REGEX = r"(\s*)(release)(\s*)(=)(\s*)(['\"])(.*)(['\"])(.*)"
USERNAME = "doc_builder_pulp_python"
HOSTNAME = "8.43.85.236"
SITE_ROOT = "/var/www/docs.pulpproject.org/pulp_python/"
def make_directory_with_rsync(remote_paths_list):
"""
Ensure the remote directory path exists.
:param remote_paths_list: The list of parameters. e.g. ['en', 'latest'] to be en/latest on the
remote.
:type remote_paths_list: a list of strings, with each string representing a directory.
"""
try:
tempdir_path = tempfile.mkdtemp()
cwd = os.getcwd()
os.chdir(tempdir_path)
os.makedirs(os.sep.join(remote_paths_list))
remote_path_arg = "%s@%s:%s%s" % (
USERNAME,
HOSTNAME,
SITE_ROOT,
remote_paths_list[0],
)
local_path_arg = tempdir_path + os.sep + remote_paths_list[0] + os.sep
rsync_command = ["rsync", "-avzh", local_path_arg, remote_path_arg]
exit_code = subprocess.call(rsync_command)
if exit_code != 0:
raise RuntimeError("An error occurred while creating remote directories.")
finally:
rmtree(tempdir_path)
os.chdir(cwd)
def ensure_dir(target_dir, clean=True):
"""
Ensure that the directory specified exists and is empty.
By default this will delete the directory if it already exists
:param target_dir: The directory to process
:type target_dir: str
:param clean: Whether or not the directory should be removed and recreated
:type clean: bool
"""
if clean:
rmtree(target_dir, ignore_errors=True)
try:
os.makedirs(target_dir)
except OSError:
pass
def main():
"""
Builds documentation using the 'make html' command and rsyncs to docs.pulpproject.org.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--build-type", required=True, help="Build type: nightly or beta.")
parser.add_argument("--branch", required=True, help="Branch or tag name.")
opts = parser.parse_args()
if opts.build_type not in ["nightly", "tag"]:
raise RuntimeError("Build type must be either 'nightly' or 'tag'.")
build_type = opts.build_type
branch = opts.branch
ga_build = False
publish_at_root = False
if (not re.search("[a-zA-Z]", branch) or "post" in branch) and len(branch.split(".")) > 2:
ga_build = True
# Only publish docs at the root if this is the latest version
r = requests.get("https://pypi.org/pypi/pulp-python/json")
latest_version = version.parse(json.loads(r.text)["info"]["version"])
docs_version = version.parse(branch)
# This is to mitigate delays on PyPI which doesn't update metadata in timely manner.
# It doesn't prevent incorrect docs being published at root if 2 releases are done close
# to each other, within PyPI delay. E.g. Release 3.11.0 an then 3.10.1 immediately after.
if docs_version >= latest_version:
publish_at_root = True
# Post releases should use the x.y.z part of the version string to form a path
if "post" in branch:
branch = ".".join(branch.split(".")[:-1])
# rsync the docs
print("rsync the docs")
docs_directory = os.sep.join([WORKING_DIR, "docs"])
local_path_arg = os.sep.join([docs_directory, "_build", "html"]) + os.sep
if build_type != "tag":
# This is a nightly build
remote_path_arg = "%s@%s:%sen/%s/%s/" % (
USERNAME,
HOSTNAME,
SITE_ROOT,
branch,
build_type,
)
make_directory_with_rsync(["en", branch, build_type])
rsync_command = ["rsync", "-avzh", "--delete", local_path_arg, remote_path_arg]
exit_code = subprocess.call(rsync_command, cwd=docs_directory)
if exit_code != 0:
raise RuntimeError("An error occurred while pushing docs.")
elif ga_build:
# This is a GA build.
# publish to the root of docs.pulpproject.org
if publish_at_root:
version_components = branch.split(".")
x_y_version = "{}.{}".format(version_components[0], version_components[1])
remote_path_arg = "%s@%s:%s" % (USERNAME, HOSTNAME, SITE_ROOT)
rsync_command = [
"rsync",
"-avzh",
"--delete",
"--exclude",
"en",
"--omit-dir-times",
local_path_arg,
remote_path_arg,
]
exit_code = subprocess.call(rsync_command, cwd=docs_directory)
if exit_code != 0:
raise RuntimeError("An error occurred while pushing docs.")
# publish to docs.pulpproject.org/en/3.y/
make_directory_with_rsync(["en", x_y_version])
remote_path_arg = "%s@%s:%sen/%s/" % (
USERNAME,
HOSTNAME,
SITE_ROOT,
x_y_version,
)
rsync_command = [
"rsync",
"-avzh",
"--delete",
"--omit-dir-times",
local_path_arg,
remote_path_arg,
]
exit_code = subprocess.call(rsync_command, cwd=docs_directory)
if exit_code != 0:
raise RuntimeError("An error occurred while pushing docs.")
# publish to docs.pulpproject.org/en/3.y.z/
make_directory_with_rsync(["en", branch])
remote_path_arg = "%s@%s:%sen/%s/" % (USERNAME, HOSTNAME, SITE_ROOT, branch)
rsync_command = [
"rsync",
"-avzh",
"--delete",
"--omit-dir-times",
local_path_arg,
remote_path_arg,
]
exit_code = subprocess.call(rsync_command, cwd=docs_directory)
if exit_code != 0:
raise RuntimeError("An error occurred while pushing docs.")
else:
# This is a pre-release
make_directory_with_rsync(["en", branch])
remote_path_arg = "%s@%s:%sen/%s/%s/" % (
USERNAME,
HOSTNAME,
SITE_ROOT,
branch,
build_type,
)
rsync_command = [
"rsync",
"-avzh",
"--delete",
"--exclude",
"nightly",
"--exclude",
"testing",
local_path_arg,
remote_path_arg,
]
exit_code = subprocess.call(rsync_command, cwd=docs_directory)
if exit_code != 0:
raise RuntimeError("An error occurred while pushing docs.")
if __name__ == "__main__":
main()