-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.py
More file actions
284 lines (244 loc) · 9.66 KB
/
Copy pathpreview.py
File metadata and controls
284 lines (244 loc) · 9.66 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
This script starts a live-reloading Jekyll preview server for the documentation repository.
It will open a browser window to the local preview webpage and rebuild the site when files change.
If Ruby, Bundler, or the Jekyll theme is missing, it will attempt to install them automatically.
"""
import os
import signal
import shutil
import subprocess
import sys
import time
import webbrowser
from pathlib import Path
# Locate the repository and set the fixed local preview address
ROOT = Path(__file__).resolve().parent
ON_WINDOWS = os.name == "nt"
HOST = "127.0.0.1"
PORT = "4000"
URL = f"http://{HOST}:{PORT}"
SERVE_PROCESS = None
# Message shown when Ruby cannot be installed automatically
RUBY_INSTALL_HINT = (
"Unable to install Ruby automatically. Please install it and re-run this script.\n"
"Windows: https://rubyinstaller.org/\n"
"macOS/Linux: https://www.ruby-lang.org/en/documentation/installation/"
)
# Run a command from the repository root
def run(command, check=True):
resolved_command = command[:]
executable = shutil.which(command[0])
if executable:
resolved_command[0] = executable
return subprocess.run(resolved_command, cwd=ROOT, check=check)
# Run a Ruby-installed command without using Windows batch wrappers
def ruby(command):
return ["ruby", "-S"] + command
# Start Jekyll without using Windows batch wrappers
def run_server(command):
resolved_command = command[:]
executable = shutil.which(command[0])
if executable:
resolved_command[0] = executable
popen_options = {"cwd": ROOT}
if ON_WINDOWS:
popen_options["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
else:
popen_options["start_new_session"] = True
return subprocess.Popen(resolved_command, **popen_options)
# Wait in short intervals so Ctrl+C is handled by Python
def wait_for_server(process):
while process.poll() is None:
time.sleep(0.2)
return process.returncode
# Stop the preview server and any child processes it started
def stop_server(process):
if not process or process.poll() is not None:
return
if ON_WINDOWS:
subprocess.run(
["taskkill", "/PID", str(process.pid), "/T", "/F"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
else:
os.killpg(process.pid, signal.SIGTERM)
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
# Make a newly installed tool available to this script
def add_to_path(path):
path = str(path)
current_paths = os.environ.get("PATH", "").split(os.pathsep)
if path not in current_paths:
os.environ["PATH"] = path + os.pathsep + os.environ.get("PATH", "")
# Use sudo for Linux package managers when it is available and needed
def with_sudo(command):
if ON_WINDOWS:
return command
if hasattr(os, "geteuid") and os.geteuid() == 0:
return command
if shutil.which("sudo"):
return ["sudo"] + command
return command
# Some installers update PATH for future terminals, but not this one
def refresh_ruby_path():
if ON_WINDOWS:
search_roots = [Path("C:/"), Path(os.environ.get("LOCALAPPDATA", ""))]
ruby_bins = []
for root in search_roots:
if root.exists():
ruby_bins.extend(root.glob("Ruby*/bin"))
ruby_bins.extend(root.glob("Programs/Ruby*/bin"))
for ruby_bin in sorted(ruby_bins, reverse=True):
if (ruby_bin / "ruby.exe").exists():
add_to_path(ruby_bin)
return
if sys.platform == "darwin" and shutil.which("brew"):
result = subprocess.run(
["brew", "--prefix", "ruby"],
cwd=ROOT,
capture_output=True,
text=True,
check=False,
)
ruby_bin = Path(result.stdout.strip()) / "bin"
if result.returncode == 0 and ruby_bin.exists():
add_to_path(ruby_bin)
# Make sure the script is being run from the documentation repository
if not (ROOT / "Gemfile").exists():
print("Gemfile not found. Run this script from the documentation repository.")
raise SystemExit(1)
try:
# Check for Ruby before trying to install anything
refresh_ruby_path()
if not shutil.which("ruby"):
print("Ruby was not found. Installing Ruby...")
# Build a list of platform-appropriate Ruby installation attempts
install_plans = []
if ON_WINDOWS:
# RubyInstaller with DevKit is the most reliable Jekyll path on Windows
if shutil.which("winget"):
for ruby_version in ["3.4", "3.3", "3.2"]:
install_plans.append(
[
[
"winget",
"install",
"--id",
f"RubyInstallerTeam.RubyWithDevKit.{ruby_version}",
"--exact",
"--source",
"winget",
"--accept-package-agreements",
"--accept-source-agreements",
]
]
)
if shutil.which("choco"):
install_plans.append([["choco", "install", "ruby", "-y"]])
elif sys.platform == "darwin":
if shutil.which("brew"):
install_plans.append([["brew", "install", "ruby"]])
else:
# Jekyll gems often need native extensions, so include build tools
if shutil.which("apt-get"):
install_plans.append(
[
with_sudo(["apt-get", "update"]),
with_sudo(
[
"apt-get",
"install",
"-y",
"ruby-full",
"build-essential",
"zlib1g-dev",
]
),
]
)
elif shutil.which("dnf"):
install_plans.append(
[with_sudo(["dnf", "install", "-y", "ruby", "ruby-devel", "gcc", "make"])]
)
elif shutil.which("yum"):
install_plans.append(
[with_sudo(["yum", "install", "-y", "ruby", "ruby-devel", "gcc", "make"])]
)
elif shutil.which("pacman"):
install_plans.append(
[with_sudo(["pacman", "-S", "--needed", "--noconfirm", "ruby", "base-devel"])]
)
elif shutil.which("zypper"):
install_plans.append(
[with_sudo(["zypper", "install", "-y", "ruby", "ruby-devel", "gcc", "make"])]
)
elif shutil.which("apk"):
install_plans.append([with_sudo(["apk", "add", "ruby", "ruby-dev", "build-base"])])
if not install_plans:
print("No supported package manager was found for automatic Ruby installation.")
print(RUBY_INSTALL_HINT)
raise SystemExit(1)
# Try each install plan until one succeeds and Ruby appears on PATH
for plan in install_plans:
plan_failed = False
for command in plan:
result = run(command, check=False)
if result.returncode != 0:
plan_failed = True
break
refresh_ruby_path()
if not plan_failed and shutil.which("ruby"):
break
# If Ruby still is not visible, the user may need a fresh terminal session
if not shutil.which("ruby"):
print("Ruby installation did not complete, or Ruby is not available in this terminal yet.")
print("Try closing and reopening your terminal, then run this script again.")
print(RUBY_INSTALL_HINT)
raise SystemExit(1)
# Install Bundler when Ruby is present but Bundler is missing
if run(ruby(["bundle", "--version"]), check=False).returncode != 0:
print("Bundler was not found. Installing it...")
run(ruby(["gem", "install", "bundler"]))
# Install the Jekyll theme and other gems from the Gemfile when needed
if run(ruby(["bundle", "check"]), check=False).returncode != 0:
print("Installing site dependencies...")
run(ruby(["bundle", "install"]))
# Open the browser shortly after Jekyll starts so the first page can load
print(f"Opening {URL}")
webbrowser.open(URL)
# Start the live-reloading Jekyll preview server
print(f"Serving site at {URL}")
print("Press Ctrl+C to stop.")
SERVE_PROCESS = run_server(
ruby(
[
"bundle",
"exec",
"jekyll",
"serve",
"--livereload",
"--host",
HOST,
"--port",
PORT,
]
)
)
exit_code = wait_for_server(SERVE_PROCESS)
if exit_code:
raise SystemExit(exit_code)
# Keep Ctrl+C friendly when stopping the preview server
except KeyboardInterrupt:
print("\nStopping preview server...")
stop_server(SERVE_PROCESS)
print("Stopped preview server.")
raise SystemExit(0)
# Report command failures without a Python traceback
except subprocess.CalledProcessError as error:
print(f"Command failed with exit code {error.returncode}: {subprocess.list2cmdline(error.cmd)}")
raise SystemExit(error.returncode)