From c80bee258f796ab8b884585da669095d0d261bce Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Thu, 6 Apr 2023 14:11:33 +0200 Subject: [PATCH] create_nojekyll_and_cname: clean-up CNAME file I noticed that while running `pytest --random` and the extension should wipe CNAME file if a configuration changes and it should not be created. Related: #11285 --- sphinx/ext/githubpages.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/sphinx/ext/githubpages.py b/sphinx/ext/githubpages.py index 1e0cdc968b9..5f8a1e8d73c 100644 --- a/sphinx/ext/githubpages.py +++ b/sphinx/ext/githubpages.py @@ -2,8 +2,8 @@ from __future__ import annotations -import os import urllib +from pathlib import Path from typing import Any import sphinx @@ -13,17 +13,18 @@ def create_nojekyll_and_cname(app: Sphinx, env: BuildEnvironment) -> None: if app.builder.format == 'html': - open(os.path.join(app.builder.outdir, '.nojekyll'), 'wb').close() + (Path(app.builder.outdir) / '.nojekyll').touch() html_baseurl = app.config.html_baseurl - if html_baseurl: - domain = urllib.parse.urlparse(html_baseurl).hostname - if domain and not domain.endswith(".github.io"): - with open(os.path.join(app.builder.outdir, 'CNAME'), 'w', - encoding="utf-8") as f: - # NOTE: don't write a trailing newline. The `CNAME` file that's - # auto-generated by the Github UI doesn't have one. - f.write(domain) + cname_path = Path(app.builder.outdir) / 'CNAME' + domain = urllib.parse.urlparse(html_baseurl).hostname if html_baseurl else None + if domain and not domain.endswith(".github.io"): + with cname_path.open('w', encoding="utf-8") as f: + # NOTE: don't write a trailing newline. The `CNAME` file that's + # auto-generated by the Github UI doesn't have one. + f.write(domain) + else: + cname_path.unlink(missing_ok=True) def setup(app: Sphinx) -> dict[str, Any]: