|
32 | 32 | """
|
33 | 33 |
|
34 | 34 | from bisect import bisect_left as bisect
|
| 35 | +from contextlib import contextmanager, suppress |
35 | 36 | import filecmp
|
36 | 37 | import logging
|
37 | 38 | import logging.handlers
|
38 | 39 | import os
|
39 |
| -import pathlib |
| 40 | +from pathlib import Path |
40 | 41 | import re
|
41 | 42 | from shlex import quote
|
42 | 43 | import shutil
|
@@ -157,7 +158,7 @@ def changed_files(left, right):
|
157 | 158 | changed = []
|
158 | 159 |
|
159 | 160 | def traverse(dircmp_result):
|
160 |
| - base = pathlib.Path(dircmp_result.left).relative_to(left) |
| 161 | + base = Path(dircmp_result.left).relative_to(left) |
161 | 162 | changed.extend(str(base / file) for file in dircmp_result.diff_files)
|
162 | 163 | for dircomp in dircmp_result.subdirs.values():
|
163 | 164 | traverse(dircomp)
|
@@ -255,38 +256,38 @@ def translation_branch(locale_repo, locale_clone_dir, needed_version):
|
255 | 256 | return locate_nearest_version(branches, needed_version)
|
256 | 257 |
|
257 | 258 |
|
| 259 | +@contextmanager |
| 260 | +def edit(file): |
| 261 | + """Context manager to edit a file "in place", use it as: |
| 262 | + with edit("/etc/hosts") as i, o: |
| 263 | + for line in i: |
| 264 | + o.write(line.replace("localhoat", "localhost")) |
| 265 | + """ |
| 266 | + temporary = file.with_name(file.name + ".tmp") |
| 267 | + with suppress(OSError): |
| 268 | + os.unlink(temporary) |
| 269 | + with open(file) as input_file: |
| 270 | + with open(temporary, "w") as output_file: |
| 271 | + yield input_file, output_file |
| 272 | + os.rename(temporary, file) |
| 273 | + |
| 274 | + |
258 | 275 | def setup_switchers(html_root):
|
259 | 276 | """Setup cross-links between cpython versions:
|
260 | 277 | - Cross-link various languages in a language switcher
|
261 | 278 | - Cross-link various versions in a version switcher
|
262 | 279 | """
|
263 | 280 | shutil.copy("switchers.js", os.path.join(html_root, "_static"))
|
264 |
| - shell_out( |
265 |
| - " ".join( |
266 |
| - [ |
267 |
| - "sed", |
268 |
| - "-i", |
269 |
| - quote( |
270 |
| - r's#\(^ *\)</body>$#\1<script type="text/javascript" src="_static/switchers.js"></script>\n\0#' |
271 |
| - ), |
272 |
| - os.path.join(html_root, "*.html"), |
273 |
| - ] |
274 |
| - ), |
275 |
| - shell=True, |
276 |
| - ) |
277 |
| - shell_out( |
278 |
| - " ".join( |
279 |
| - [ |
280 |
| - "sed", |
281 |
| - "-i", |
282 |
| - quote( |
283 |
| - r's#\(^ *\)</body>$#\1<script type="text/javascript" src="../_static/switchers.js"></script>\n\0#' |
284 |
| - ), |
285 |
| - os.path.join(html_root, "*/*.html"), |
286 |
| - ] |
287 |
| - ), |
288 |
| - shell=True, |
289 |
| - ) |
| 281 | + for file in Path(html_root).glob("**/*.html"): |
| 282 | + depth = len(file.relative_to(html_root).parts) - 1 |
| 283 | + script = f""" <script type="text/javascript" src="{'../' * depth}_static/switchers.js"></script>\n""" |
| 284 | + with edit(file) as (i, o): |
| 285 | + for line in i: |
| 286 | + if line == script: |
| 287 | + continue |
| 288 | + if line == " </body>\n": |
| 289 | + o.write(script) |
| 290 | + o.write(line) |
290 | 291 |
|
291 | 292 |
|
292 | 293 | def build_one(
|
|
0 commit comments