Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions build_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,8 @@ def build_docs(args: argparse.Namespace) -> int:

build_sitemap(versions, languages, args.www_root, args.group)
build_404(args.www_root, args.group)
copy_robots_txt(
build_robots_txt(
versions,
args.www_root,
args.group,
args.skip_cache_invalidation,
Expand Down Expand Up @@ -1338,20 +1339,23 @@ def build_404(www_root: Path, group: str) -> None:
chgrp(not_found_file, group=group)


def copy_robots_txt(
def build_robots_txt(
versions: Versions,
www_root: Path,
group: str,
skip_cache_invalidation: bool,
http: urllib3.PoolManager,
) -> None:
"""Copy robots.txt to www_root."""
"""Build robots.txt to www_root."""
if not www_root.exists():
logging.info("Skipping copying robots.txt (www root does not even exist).")
logging.info("Skipping robots.txt generation (www root does not even exist).")
return
logging.info("Copying robots.txt...")
logging.info("Starting robots.txt generation...")
template_path = HERE / "templates" / "robots.txt"
template = jinja2.Template(template_path.read_text(encoding="UTF-8"))
rendered_template = template.render(versions=versions)
robots_path = www_root / "robots.txt"
shutil.copyfile(template_path, robots_path)
robots_path.write_text(rendered_template, encoding="UTF-8")
robots_path.chmod(0o775)
chgrp(robots_path, group=group)
if not skip_cache_invalidation:
Expand Down
16 changes: 5 additions & 11 deletions templates/robots.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ Disallow: /2.2/
Disallow: /2.3/
Disallow: /2.4/
Disallow: /2.5/
Disallow: /2.6/
Disallow: /2.7/
Disallow: /3.0/
Disallow: /3.1/
Disallow: /3.2/
Disallow: /3.3/
Disallow: /3.4/
Disallow: /3.5/
Disallow: /3.6/
Disallow: /3.7/
Disallow: /3.8/
{% for version in versions -%}
{% if version.status == "EOL" -%}
Disallow: /{{ version.name }}/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this include all of /2/ and /2.0/ - /3.10/?

Will it add any others?

https://peps.python.org/api/release-cycle.json only goes back to 2.6.

https://peps.python.org/api/python-releases.json has more but is more plumbing. We could hardcode the oldest ones, and the newer ones will be updated automatically.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted to hardcode the old ones, with that, it adds one line:

+ Disallow: /3.9/

{% endif -%}
{% endfor %}
24 changes: 23 additions & 1 deletion tests/test_build_docs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from unittest.mock import patch

import pytest

from build_docs import format_seconds
from build_docs import Version, Versions, build_robots_txt, format_seconds


@pytest.mark.parametrize(
Expand All @@ -24,3 +26,23 @@
)
def test_format_seconds(seconds: float, expected: str) -> None:
assert format_seconds(seconds) == expected


@patch("build_docs.chgrp")
def test_build_robots_txt(mock_chgrp, tmp_path) -> None:
versions = Versions([
Version(name="3.14", status="EOL", branch_or_tag="3.14"),
Version(name="3.15", status="EOL", branch_or_tag="3.15"),
Version(name="3.16", status="security-fixes", branch_or_tag="3.16"),
Version(name="3.17", status="stable", branch_or_tag="2.17"),
])

build_robots_txt(
versions, tmp_path, group="", skip_cache_invalidation=True, http=None
)

result = (tmp_path / "robots.txt").read_text()
assert "Disallow: /3.14/" in result
assert "Disallow: /3.15/" in result
assert "/3.16/" not in result
assert "/3.17/" not in result
Loading