From: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
To: Jonathan Corbet <corbet@lwn.net>,
Linux Doc Mailing List <linux-doc@vger.kernel.org>
Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
linux-kernel@vger.kernel.org
Subject: [PATCH v2 2/3] tools/docs: python_version: allow check for alternatives and bail out
Date: Wed, 3 Sep 2025 18:24:15 +0200 [thread overview]
Message-ID: <6c2155e62b71e5e10fd4b17ecc33fb1187f7dca6.1756916565.git.mchehab+huawei@kernel.org> (raw)
In-Reply-To: <cover.1756916565.git.mchehab+huawei@kernel.org>
The caller script may not want an automatic execution of the new
version. Add two parameters to allow showing alternatives and to
bail out if version is incompatible.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
tools/docs/lib/python_version.py | 43 ++++++++++++++++++++++++++------
1 file changed, 35 insertions(+), 8 deletions(-)
diff --git a/tools/docs/lib/python_version.py b/tools/docs/lib/python_version.py
index 660bfe7d23fa..a9fda2470a26 100644
--- a/tools/docs/lib/python_version.py
+++ b/tools/docs/lib/python_version.py
@@ -85,10 +85,12 @@ class PythonVersion:
may need to update it one day, hopefully on a distant future.
"""
patterns = [
- "python3.[0-9]",
"python3.[0-9][0-9]",
+ "python3.[0-9]",
]
+ python_cmd = []
+
# Seek for a python binary newer than min_version
for path in os.getenv("PATH", "").split(":"):
for pattern in patterns:
@@ -96,12 +98,13 @@ class PythonVersion:
if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
version = PythonVersion.get_python_version(cmd)
if version >= min_version:
- return cmd
+ python_cmd.append((version, cmd))
- return None
+ return sorted(python_cmd, reverse=True)
@staticmethod
- def check_python(min_version):
+ def check_python(min_version, show_alternatives=False, bail_out=False,
+ success_on_error=False):
"""
Check if the current python binary satisfies our minimal requirement
for Sphinx build. If not, re-run with a newer version if found.
@@ -113,18 +116,42 @@ class PythonVersion:
python_ver = PythonVersion.ver_str(cur_ver)
- new_python_cmd = PythonVersion.find_python(min_version)
- if not new_python_cmd:
+ available_versions = PythonVersion.find_python(min_version)
+ if not available_versions:
print(f"ERROR: Python version {python_ver} is not spported anymore\n")
print(" Can't find a new version. This script may fail")
return
- # Restart script using the newer version
script_path = os.path.abspath(sys.argv[0])
- args = [new_python_cmd, script_path] + sys.argv[1:]
+
+ # Check possible alternatives
+ if available_versions:
+ new_python_cmd = available_versions[0][1]
+ else:
+ new_python_cmd = None
+
+ if show_alternatives:
+ print("You could run, instead:")
+ for _, cmd in available_versions:
+ args = [cmd, script_path] + sys.argv[1:]
+
+ cmd_str = " ".join(args)
+ print(f" {cmd_str}")
+ print()
+
+ if bail_out:
+ msg = f"Python {python_ver} not supported. Bailing out"
+ if success_on_error:
+ print(msg, file=sys.stderr)
+ sys.exit(0)
+ else:
+ sys.exit(msg)
print(f"Python {python_ver} not supported. Changing to {new_python_cmd}")
+ # Restart script using the newer version
+ args = [new_python_cmd, script_path] + sys.argv[1:]
+
try:
os.execv(new_python_cmd, args)
except OSError as e:
--
2.51.0
next prev parent reply other threads:[~2025-09-03 16:24 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-03 16:24 [PATCH v2 0/3] Break building docs on distros where python3==python3.6 or older Mauro Carvalho Chehab
2025-09-03 16:24 ` [PATCH v2 1/3] tools/docs: python_version.py: drop a debug print Mauro Carvalho Chehab
2025-09-03 16:24 ` Mauro Carvalho Chehab [this message]
2025-09-03 16:24 ` [PATCH v2 3/3] tools/docs: sphinx-* break documentation bulds on openSUSE Mauro Carvalho Chehab
2025-09-03 17:38 ` Matthew Wilcox
2025-09-04 5:18 ` Mauro Carvalho Chehab
2025-09-03 17:41 ` Randy Dunlap
2025-09-03 19:17 ` Mauro Carvalho Chehab
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6c2155e62b71e5e10fd4b17ecc33fb1187f7dca6.1756916565.git.mchehab+huawei@kernel.org \
--to=mchehab+huawei@kernel.org \
--cc=corbet@lwn.net \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®