Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Fail the version bump on version directives older than 3 years (#8219)

Adrian4aa119becd03
diff --git a/docs/topics/settings.rst b/docs/topics/settings.rst
index 8117086f4..2f7da6224 100644
--- a/docs/topics/settings.rst
+++ b/docs/topics/settings.rst
@@ -105,8 +105,6 @@ and settings set there should use the ``"spider"`` priority explicitly:
super().update_settings(settings)
settings.set("SOME_SETTING", "some value", priority="spider")
-.. versionadded:: 2.11
-
It's also possible to modify the settings in the
:meth:`~scrapy.Spider.from_crawler` method, e.g. based on :ref:`spider
arguments <spiderargs>` or other logic:
diff --git a/docs/topics/spiders.rst b/docs/topics/spiders.rst
index 09a76d8d8..75b28cf56 100644
--- a/docs/topics/spiders.rst
+++ b/docs/topics/spiders.rst
@@ -119,20 +119,16 @@ scrapy.Spider
attributes in the new instance so they can be accessed later inside the
spider's code.
- .. versionchanged:: 2.11
-
- The settings in ``crawler.settings`` can now be modified in this
- method, which is handy if you want to modify them based on
- arguments. As a consequence, these settings aren't the final values
- as they can be modified later by e.g. :ref:`add-ons
- <topics-addons>`. For the same reason, most of the
- :class:`~scrapy.crawler.Crawler` attributes aren't initialized at
- this point.
-
- The final settings and the initialized
- :class:`~scrapy.crawler.Crawler` attributes are available in the
- :meth:`start` method, handlers of the
- :signal:`engine_started` signal and later.
+ The settings in ``crawler.settings`` can be modified in this method,
+ which is handy if you want to modify them based on arguments. As a
+ consequence, these settings aren't the final values as they can be
+ modified later by e.g. :ref:`add-ons <topics-addons>`. For the same
+ reason, most of the :class:`~scrapy.crawler.Crawler` attributes aren't
+ initialized at this point.
+
+ The final settings and the initialized :class:`~scrapy.crawler.Crawler`
+ attributes are available in the :meth:`start` method, handlers of the
+ :signal:`engine_started` signal and later.
:param crawler: crawler to which the spider will be bound
:type crawler: :class:`~scrapy.crawler.Crawler` instance
diff --git a/extras/pre-bump.py b/extras/pre-bump.py
new file mode 100644
index 000000000..f2f4a23b4
--- /dev/null
+++ b/extras/pre-bump.py
@@ -0,0 +1,68 @@
+import re
+import subprocess
+import sys
+from datetime import UTC, date, datetime
+from pathlib import Path
+
+TITLE = re.compile(
+ r"^Scrapy (\d+\.\d+\.\d+) \((unreleased|\d{4}-\d{2}-\d{2})\)$", re.MULTILINE
+)
+
+
+def _git_grep(pattern: str) -> list[str]:
+ return subprocess.run( # noqa: S603
+ ["git", "grep", "-nE", pattern, "--", "docs", "scrapy"], # noqa: S607
+ capture_output=True,
+ check=False,
+ text=True,
+ ).stdout.splitlines()
+
+
+def placeholders() -> list[str]:
+ """Return the ``VERSION`` and ``PREVIOUS_VERSION`` placeholders, which
+ must be replaced with actual versions before a release.
+ """
+ return _git_grep(r"(^|[^/\"_A-Z`])(PREVIOUS_)?VERSION\b")
+
+
+def old_directives(today: date) -> list[str]:
+ """Return the ``versionadded`` and ``versionchanged`` directives that a
+ major or minor release made on *today* must remove, i.e. those of
+ releases more than 3 years old.
+ """
+ news = Path("docs/news.rst").read_text(encoding="utf-8")
+ new_version = next((m[1] for m in TITLE.finditer(news) if m[2] == "unreleased"), "")
+ if not new_version.endswith(".0"):
+ return []
+ for path in Path("docs/news").glob("*.rst"):
+ news += path.read_text(encoding="utf-8")
+ released = {
+ m[1]: date.fromisoformat(m[2])
+ for m in TITLE.finditer(news)
+ if m[2] != "unreleased"
+ }
+ # February 29 becomes February 28.
+ cutoff = date(
+ today.year - 3, today.month, min(today.day, 28 if today.month == 2 else 31)
+ )
+ return [
+ line
+ for line in _git_grep(r"\.\. version(added|changed):: *[0-9]")
+ if (m := re.search(r":: *(\d+\.\d+)(\.\d+)?", line))
+ and released.get(f"{m[1]}{m[2] or '.0'}", today) < cutoff
+ ]
+
+
+if __name__ == "__main__":
+ failed = False
+ for message, lines in (
+ ("Replace these placeholders:", placeholders()),
+ (
+ "Remove these directives, older than 3 years:",
+ old_directives(datetime.now(UTC).date()),
+ ),
+ ):
+ if lines:
+ print(message, *lines, sep="\n")
+ failed = True
+ sys.exit(failed)
diff --git a/pyproject.toml b/pyproject.toml
index c98a7f8ee..aeabe0c47 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -157,9 +157,7 @@ current_version = "2.19.0"
commit = true
tag = true
tag_name = "{new_version}"
-# Fail on VERSION and PREVIOUS_VERSION placeholders left in docs or code.
-allow_shell_hooks = true
-setup_hooks = ['''! git grep -nE '(^|[^/"_A-Z`])(PREVIOUS_)?VERSION\b' -- docs scrapy''']
+setup_hooks = ["python3 extras/pre-bump.py"]
[[tool.bumpversion.files]]
filename = "docs/news.rst"