Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sitemap.xml.gz slightly more reproducible #3460

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion mkdocs/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def _build_theme_template(
log.debug(f"Gzipping template: {template_name}")
gz_filename = f'{output_path}.gz'
with open(gz_filename, 'wb') as f:
timestamp = utils.get_build_timestamp()
timestamp = utils.get_build_timestamp(
pages=[f.page for f in files.documentation_pages() if f.page is not None]
)
with gzip.GzipFile(
fileobj=f, filename=gz_filename, mode='wb', mtime=timestamp
) as gz_buf:
Expand Down
2 changes: 1 addition & 1 deletion mkdocs/structure/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, title: str | None, file: File, config: MkDocsConfig) -> None:
self.next_page = None
self.active = False

self.update_date = get_build_date()
self.update_date: str = get_build_date()

self._set_canonical_url(config.get('site_url', None))
self._set_edit_url(
Expand Down
8 changes: 4 additions & 4 deletions mkdocs/tests/build_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_extra_context(self):
def test_build_theme_template(self, mock_build_template, mock_write_file):
cfg = load_config()
env = cfg.theme.get_env()
build._build_theme_template('main.html', env, mock.Mock(), cfg, mock.Mock())
build._build_theme_template('main.html', env, Files([]), cfg, mock.Mock())
mock_write_file.assert_called_once()
mock_build_template.assert_called_once()

Expand All @@ -246,7 +246,7 @@ def test_build_sitemap_template(
):
cfg = load_config(site_dir=site_dir)
env = cfg.theme.get_env()
build._build_theme_template('sitemap.xml', env, mock.Mock(), cfg, mock.Mock())
build._build_theme_template('sitemap.xml', env, Files([]), cfg, mock.Mock())
mock_write_file.assert_called_once()
mock_build_template.assert_called_once()
mock_gzip_gzipfile.assert_called_once()
Expand All @@ -257,7 +257,7 @@ def test_skip_missing_theme_template(self, mock_build_template, mock_write_file)
cfg = load_config()
env = cfg.theme.get_env()
with self.assertLogs('mkdocs') as cm:
build._build_theme_template('missing.html', env, mock.Mock(), cfg, mock.Mock())
build._build_theme_template('missing.html', env, Files([]), cfg, mock.Mock())
self.assertEqual(
'\n'.join(cm.output),
"WARNING:mkdocs.commands.build:Template skipped: 'missing.html' not found in theme directories.",
Expand All @@ -271,7 +271,7 @@ def test_skip_theme_template_empty_output(self, mock_build_template, mock_write_
cfg = load_config()
env = cfg.theme.get_env()
with self.assertLogs('mkdocs') as cm:
build._build_theme_template('main.html', env, mock.Mock(), cfg, mock.Mock())
build._build_theme_template('main.html', env, Files([]), cfg, mock.Mock())
self.assertEqual(
'\n'.join(cm.output),
"INFO:mkdocs.commands.build:Template skipped: 'main.html' generated empty output.",
Expand Down
19 changes: 10 additions & 9 deletions mkdocs/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@
)


def get_build_timestamp() -> int:
def get_build_timestamp(*, pages: Collection[Page] | None = None) -> int:
"""
Returns the number of seconds since the epoch.
Returns the number of seconds since the epoch for the latest updated page.

Support SOURCE_DATE_EPOCH environment variable for reproducible builds.
See https://reproducible-builds.org/specs/source-date-epoch/
In reality this is just today's date because that's how pages' update time is populated.
"""
source_date_epoch = os.environ.get('SOURCE_DATE_EPOCH')
if source_date_epoch is None:
return int(datetime.now(timezone.utc).timestamp())

return int(source_date_epoch)
if pages:
# Lexicographic comparison is OK for ISO date.
date_string = max(p.update_date for p in pages)
dt = datetime.fromisoformat(date_string)
else:
dt = get_build_datetime()
return int(dt.timestamp())


def get_build_datetime() -> datetime:
Expand Down