Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve performance forproofread_canonicals()#258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
AA-Turner merged 2 commits intomainfromproofread-perf
Apr 11, 2025
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletionsbuild_docs.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,6 +23,7 @@
from __future__ import annotations

import argparse
import concurrent.futures
import dataclasses
import datetime as dt
import filecmp
Expand DownExpand Up@@ -1262,21 +1263,41 @@ def proofread_canonicals(
/3/whatsnew/3.11.html, which may not exist yet.
"""
logging.info("Checking canonical links...")
canonical_re = re.compile(
"""<link rel="canonical" href="https://docs.python.org/([^"]*)" />"""
)
for file in www_root.glob("**/*.html"):
html = file.read_text(encoding="UTF-8", errors="surrogateescape")
canonical = canonical_re.search(html)
if not canonical:
continue
target = canonical.group(1)
if not (www_root / target).exists():
logging.info("Removing broken canonical from %s to %s", file, target)
html = html.replace(canonical.group(0), "")
file.write_text(html, encoding="UTF-8", errors="surrogateescape")
if not skip_cache_invalidation:
purge(http, str(file).replace("/srv/docs.python.org/", ""))
worker_count = (os.cpu_count() or 1) + 2
with concurrent.futures.ThreadPoolExecutor(worker_count) as executor:
futures = {
executor.submit(_check_canonical_rel, file, www_root)
for file in www_root.glob("**/*.html")
}
paths_to_purge = {
res.relative_to(www_root) # strip the leading /srv/docs.python.org
for fut in concurrent.futures.as_completed(futures)
if (res := fut.result()) is not None
}
if not skip_cache_invalidation:
purge(http, *paths_to_purge)


def _check_canonical_rel(file: Path, www_root: Path):
# Check for a canonical relation link in the HTML.
# If one exists, ensure that the target exists
# or otherwise remove the canonical link element.
prefix = b'<link rel="canonical" href="https://docs.python.org/'
suffix = b'" />'
pfx_len = len(prefix)
sfx_len = len(suffix)
html = file.read_bytes()
try:
start = html.index(prefix)
end = html.index(suffix, start + pfx_len)
except ValueError:
return None
target = html[start + pfx_len : end].decode(errors="surrogateescape")
if (www_root / target).exists():
return None
logging.info("Removing broken canonical from %s to %s", file, target)
file.write_bytes(html[:start] + html[end + sfx_len :])
return file


def purge(http: urllib3.PoolManager, *paths: Path | str) -> None:
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp