Skip to content

Commit 42a6546

Browse files
committed
Move _file_checksum() to sphinx.builders.html._assets
1 parent 44a7820 commit 42a6546

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

sphinx/builders/html/__init__.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import re
1010
import sys
1111
import warnings
12-
import zlib
1312
from datetime import datetime, timezone
1413
from os import path
1514
from typing import IO, TYPE_CHECKING, Any
@@ -27,6 +26,7 @@
2726
from sphinx import version_info as sphinx_version
2827
from sphinx.application import Sphinx
2928
from sphinx.builders import Builder
29+
from sphinx.builders.html._assets import _file_checksum
3030
from sphinx.config import ENUM, Config
3131
from sphinx.domains import Domain, Index, IndexEntry
3232
from sphinx.environment import BuildEnvironment
@@ -1254,29 +1254,6 @@ def js_tag(js: JavaScript) -> str:
12541254
context['js_tag'] = js_tag
12551255

12561256

1257-
def _file_checksum(outdir: str | os.PathLike[str], filename: str | os.PathLike[str]) -> str:
1258-
filename = os.fspath(filename)
1259-
# Don't generate checksums for HTTP URIs
1260-
if '://' in filename:
1261-
return ''
1262-
# Some themes and extensions have used query strings
1263-
# for a similar asset checksum feature.
1264-
# As we cannot safely strip the query string,
1265-
# raise an error to the user.
1266-
if '?' in filename:
1267-
msg = f'Local asset file paths must not contain query strings: {filename!r}'
1268-
raise ThemeError(msg)
1269-
try:
1270-
# Ensure universal newline mode is used to avoid checksum differences
1271-
with open(path.join(outdir, filename), encoding='utf-8') as f:
1272-
content = f.read().encode(encoding='utf-8')
1273-
except FileNotFoundError:
1274-
return ''
1275-
if not content:
1276-
return ''
1277-
return f'{zlib.crc32(content):08x}'
1278-
1279-
12801257
def setup_resource_paths(app: Sphinx, pagename: str, templatename: str,
12811258
context: dict, doctree: Node) -> None:
12821259
"""Set up relative resource paths."""

sphinx/builders/html/_assets.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import zlib
5+
from typing import TYPE_CHECKING
6+
7+
from sphinx.errors import ThemeError
8+
9+
if TYPE_CHECKING:
10+
from pathlib import Path
11+
12+
13+
def _file_checksum(outdir: Path, filename: str | os.PathLike[str]) -> str:
14+
filename = os.fspath(filename)
15+
# Don't generate checksums for HTTP URIs
16+
if '://' in filename:
17+
return ''
18+
# Some themes and extensions have used query strings
19+
# for a similar asset checksum feature.
20+
# As we cannot safely strip the query string,
21+
# raise an error to the user.
22+
if '?' in filename:
23+
msg = f'Local asset file paths must not contain query strings: {filename!r}'
24+
raise ThemeError(msg)
25+
try:
26+
# Remove all carriage returns to avoid checksum differences
27+
content = outdir.joinpath(filename).read_bytes().translate(None, b'\r')
28+
except FileNotFoundError:
29+
return ''
30+
if not content:
31+
return ''
32+
return f'{zlib.crc32(content):08x}'

tests/test_build_html.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
import sphinx.builders.html
1414
from sphinx.builders.html import (
15-
_file_checksum,
1615
validate_html_extra_path,
1716
validate_html_static_path,
1817
)
18+
from sphinx.builders.html._assets import _file_checksum
1919
from sphinx.errors import ConfigError, ThemeError
2020
from sphinx.testing.util import strip_escseq
2121
from sphinx.util.inventory import InventoryFile
@@ -1248,10 +1248,10 @@ def test_file_checksum(app):
12481248

12491249
def test_file_checksum_query_string():
12501250
with pytest.raises(ThemeError, match='Local asset file paths must not contain query strings'):
1251-
_file_checksum('', 'with_query_string.css?dead_parrots=1')
1251+
_file_checksum(Path(), 'with_query_string.css?dead_parrots=1')
12521252

12531253
with pytest.raises(ThemeError, match='Local asset file paths must not contain query strings'):
1254-
_file_checksum('', 'with_query_string.js?dead_parrots=1')
1254+
_file_checksum(Path(), 'with_query_string.js?dead_parrots=1')
12551255

12561256
with pytest.raises(ThemeError, match='Local asset file paths must not contain query strings'):
12571257
_file_checksum(Path.cwd(), '_static/with_query_string.css?dead_parrots=1')

0 commit comments

Comments
 (0)