-
Notifications
You must be signed in to change notification settings - Fork 3
/
noxfile.py
296 lines (243 loc) · 8.82 KB
/
noxfile.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
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
285
286
287
288
289
290
291
292
293
294
295
296
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
import glob
import errno
import os
import shutil
import subprocess
import nox
import psutil
import py.path
_SHUTIL_RMTREE_IGNORE_ERRORS = functools.partial(
shutil.rmtree, ignore_errors=True
)
nox.options.error_on_external_run = True
DEFAULT_INTERPRETER = "3.7"
PRINT_SEP = "=" * 60
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
INPUT_DIR = os.path.join(BASE_DIR, "content")
OUTPUT_DIR = os.path.join(BASE_DIR, "output")
CONF_FILE = os.path.join(BASE_DIR, "pelicanconf.py")
ALT_CONF_FILE = os.path.join(BASE_DIR, "pelicanconf_with_pagination.py")
DEBUG = "DEBUG" in os.environ
PORT = os.environ.get("PORT")
def get_path(*names):
return os.path.join(BASE_DIR, *names)
def _render(session, env=None):
# I will typically run this via
# PATH="${PATH}:${HOME}/.nodenv/versions/${VERSION}/bin" nox -s render
# because I don't have a ``node`` executable on my default ``${PATH}``.
if py.path.local.sysfind("node") is None:
session.skip("`node` must be installed")
if py.path.local.sysfind("npm") is None:
session.skip("`npm` must be installed")
session.run("npm", "install", external=True)
script = get_path("render_jinja2_templates.py")
session.run("python", script, env=env)
@nox.session(py=DEFAULT_INTERPRETER)
def render(session):
"""Render blog posts from templates.
If the post has already been rendered, this will check the file hash against
a stored mapping of hashes and do nothing if confirmed.
"""
session.install("--requirement", "render-requirements.txt")
_render(session)
@nox.session(py=DEFAULT_INTERPRETER)
def rerender(session):
"""Re-render blog posts from templates."""
session.install("--requirement", "render-requirements.txt")
_render(session, env={"FORCE_RENDER": "true"})
def _generate(
session, pelican_opts, regenerate=False, conf_file=CONF_FILE, env=None
):
args = [os.path.join(session.bin, "pelican")]
if regenerate:
args.append("-r")
args.extend([INPUT_DIR, "-o", OUTPUT_DIR, "-s", conf_file])
args.extend(pelican_opts)
session.run(*args, env=env)
def get_pelican_opts():
pelican_opts = []
if DEBUG:
pelican_opts.append("-D")
return pelican_opts
@nox.session(py=DEFAULT_INTERPRETER)
def html(session):
"""(Re)-generate the web site."""
pelican_opts = get_pelican_opts()
session.install("--requirement", "html-requirements.txt")
# 1. Render
print("Rendering templates...")
print(PRINT_SEP)
_render(session)
print(PRINT_SEP)
# 2. Build HTML with paging.
print("Making first pass with paging")
print(PRINT_SEP)
env = {"PYTHONPATH": get_path()}
_generate(session, pelican_opts, conf_file=ALT_CONF_FILE, env=env)
print(PRINT_SEP)
# 3. Keep around the paged index files and nothing else.
print("Storing paging index*.html files for re-use")
print(" and removing paged output.")
print(PRINT_SEP)
index_files = glob.glob(os.path.join(OUTPUT_DIR, "index*.html"))
for filename in index_files:
session.run(shutil.move, filename, BASE_DIR)
session.run(_SHUTIL_RMTREE_IGNORE_ERRORS, OUTPUT_DIR)
print(PRINT_SEP)
# 4. Build HTML without paging.
print("Making second pass without paging")
print(PRINT_SEP)
_generate(session, pelican_opts, env=env)
print(PRINT_SEP)
# 5. Add back paging information.
print("Putting back paging index*.html files")
print(PRINT_SEP)
session.run(os.remove, os.path.join(OUTPUT_DIR, "index.html"))
index_files = glob.glob(os.path.join(BASE_DIR, "index*.html"))
for filename in index_files:
session.run(shutil.move, filename, OUTPUT_DIR)
print(PRINT_SEP)
# 6. Delete generated pages that are unused
print("Removing unwanted pages")
print(PRINT_SEP)
session.run(remove_file, os.path.join(OUTPUT_DIR, "authors.html"))
session.run(
_SHUTIL_RMTREE_IGNORE_ERRORS,
os.path.join(OUTPUT_DIR, "author"),
)
session.run(remove_file, os.path.join(OUTPUT_DIR, "categories.html"))
session.run(
_SHUTIL_RMTREE_IGNORE_ERRORS,
os.path.join(OUTPUT_DIR, "category"),
)
session.run(remove_file, os.path.join(OUTPUT_DIR, "tags.html"))
print(PRINT_SEP)
# 7. Rewrite URL paths for the pagination feature.
print("Rewriting paths for paging index*.html files.")
print(PRINT_SEP)
script = get_path("rewrite_custom_pagination.py")
session.run("python", script)
print(PRINT_SEP)
def remove_file(filename):
try:
os.remove(filename)
except OSError as exc:
# errno.ENOENT = no such file or directory
if exc.errno != errno.ENOENT:
raise
@nox.session(py=DEFAULT_INTERPRETER)
def regenerate(session):
"""Regenerate files upon modification.
This runs a daemon that waits on file changes and updates generated
content when files are updated.
"""
pelican_opts = get_pelican_opts()
session.install("--requirement", "html-requirements.txt")
env = {"PYTHONPATH": get_path()}
_generate(session, pelican_opts, regenerate=True, env=env)
@nox.session(py=DEFAULT_INTERPRETER)
def serve(session):
"""Serve site at http://localhost:${PORT}."""
script = get_path("pelican_server.py")
session.cd(OUTPUT_DIR)
if PORT is None:
session.run("python", script)
else:
session.run("python", script, PORT)
@nox.session(py=DEFAULT_INTERPRETER)
def serve_local(session):
"""Serve at http://192.168.XX.YY:8001."""
script = get_path("get_local_ip.py")
local_ip = session.run("python", script, silent=True)
script = get_path("pelican_server.py")
session.cd(OUTPUT_DIR)
# ``root`` doesn't know about our virtualenv.
py_exe = os.path.join(session.bin, "python")
session.run(py_exe, script, "8001", local_ip.strip())
@nox.session(py=DEFAULT_INTERPRETER)
def dev_server(session):
"""Start / restart ``develop_server.sh``.
Uses ``${PORT}`` environment variable.
"""
script = get_path("develop_server.sh")
if PORT is None:
session.run(script, "restart")
else:
session.run(script, "restart", PORT)
def get_pelican_pid():
try:
with open(get_path("pelican.pid"), "r") as fh:
return int(fh.read())
except (OSError, ValueError):
return None
def get_srv_pid():
try:
with open(get_path("srv.pid"), "r") as fh:
return int(fh.read())
except (OSError, ValueError):
return None
@nox.session(py=False)
def stop_server(session):
"""Stop local server."""
pelican_pid = session.run(get_pelican_pid)
srv_pid = session.run(get_srv_pid)
if pelican_pid is None:
if srv_pid is None:
session.error("`pelican.pid` and `srv.pid` files invalid")
else:
session.error("`pelican.pid` file invalid")
if srv_pid is None:
session.error("srv.pid` file invalid")
pelican_proc = psutil.Process(pelican_pid)
srv_proc = psutil.Process(srv_pid)
session.run(pelican_proc.kill)
session.run(srv_proc.kill)
@nox.session(py=DEFAULT_INTERPRETER)
def update_requirements(session):
if py.path.local.sysfind("git") is None:
session.skip("`git` must be installed")
# Install all dependencies.
session.install("pip-tools")
# Update all of the requirements file(s).
names = ("render", "html")
for name in names:
in_name = "{}-requirements.in".format(name)
txt_name = "{}-requirements.txt".format(name)
session.run("rm", "-f", txt_name, external=True)
session.run(
"pip-compile",
"--generate-hashes",
"--output-file",
txt_name,
in_name,
)
session.run("git", "add", txt_name, external=True)
@nox.session(python=DEFAULT_INTERPRETER)
def blacken(session):
session.install("black")
file_list_str = subprocess.check_output(["git", "ls-files", "*.py"])
file_list = file_list_str.decode("ascii").strip().split("\n")
session.run("black", "--line-length=79", *file_list)
@nox.session(py=False)
def clean(session):
"""Remove the generated files."""
dir_paths = (
OUTPUT_DIR,
get_path("__pycache__"),
get_path("node_modules"),
get_path("pelican-plugins", "__pycache__"),
)
for dir_path in dir_paths:
session.run(_SHUTIL_RMTREE_IGNORE_ERRORS, dir_path)