Path: blob/master/tools/lib/python/kdoc/python_version.py
38186 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.0-or-later2# Copyright (c) 2017-2025 Mauro Carvalho Chehab <[email protected]>34"""5Handle Python version check logic.67Not all Python versions are supported by scripts. Yet, on some cases,8like during documentation build, a newer version of python could be9available.1011This class allows checking if the minimal requirements are followed.1213Better than that, PythonVersion.check_python() not only checks the minimal14requirements, but it automatically switches to a the newest available15Python version if present.1617"""1819import os20import re21import subprocess22import shlex23import sys2425from glob import glob26from textwrap import indent2728class PythonVersion:29"""30Ancillary methods that checks for missing dependencies for different31types of types, like binaries, python modules, rpm deps, etc.32"""3334def __init__(self, version):35"""Ïnitialize self.version tuple from a version string"""36self.version = self.parse_version(version)3738@staticmethod39def parse_version(version):40"""Convert a major.minor.patch version into a tuple"""41return tuple(int(x) for x in version.split("."))4243@staticmethod44def ver_str(version):45"""Returns a version tuple as major.minor.patch"""46return ".".join([str(x) for x in version])4748@staticmethod49def cmd_print(cmd, max_len=80):50cmd_line = []5152for w in cmd:53w = shlex.quote(w)5455if cmd_line:56if not max_len or len(cmd_line[-1]) + len(w) < max_len:57cmd_line[-1] += " " + w58continue59else:60cmd_line[-1] += " \\"61cmd_line.append(w)62else:63cmd_line.append(w)6465return "\n ".join(cmd_line)6667def __str__(self):68"""Returns a version tuple as major.minor.patch from self.version"""69return self.ver_str(self.version)7071@staticmethod72def get_python_version(cmd):73"""74Get python version from a Python binary. As we need to detect if75are out there newer python binaries, we can't rely on sys.release here.76"""7778kwargs = {}79if sys.version_info < (3, 7):80kwargs['universal_newlines'] = True81else:82kwargs['text'] = True8384result = subprocess.run([cmd, "--version"],85stdout = subprocess.PIPE,86stderr = subprocess.PIPE,87**kwargs, check=False)8889version = result.stdout.strip()9091match = re.search(r"(\d+\.\d+\.\d+)", version)92if match:93return PythonVersion.parse_version(match.group(1))9495print(f"Can't parse version {version}")96return (0, 0, 0)9798@staticmethod99def find_python(min_version):100"""101Detect if are out there any python 3.xy version newer than the102current one.103104Note: this routine is limited to up to 2 digits for python3. We105may need to update it one day, hopefully on a distant future.106"""107patterns = [108"python3.[0-9][0-9]",109"python3.[0-9]",110]111112python_cmd = []113114# Seek for a python binary newer than min_version115for path in os.getenv("PATH", "").split(":"):116for pattern in patterns:117for cmd in glob(os.path.join(path, pattern)):118if os.path.isfile(cmd) and os.access(cmd, os.X_OK):119version = PythonVersion.get_python_version(cmd)120if version >= min_version:121python_cmd.append((version, cmd))122123return sorted(python_cmd, reverse=True)124125@staticmethod126def check_python(min_version, show_alternatives=False, bail_out=False,127success_on_error=False):128"""129Check if the current python binary satisfies our minimal requirement130for Sphinx build. If not, re-run with a newer version if found.131"""132cur_ver = sys.version_info[:3]133if cur_ver >= min_version:134ver = PythonVersion.ver_str(cur_ver)135return136137python_ver = PythonVersion.ver_str(cur_ver)138139available_versions = PythonVersion.find_python(min_version)140if not available_versions:141print(f"ERROR: Python version {python_ver} is not supported anymore\n")142print(" Can't find a new version. This script may fail")143return144145script_path = os.path.abspath(sys.argv[0])146147# Check possible alternatives148if available_versions:149new_python_cmd = available_versions[0][1]150else:151new_python_cmd = None152153if show_alternatives and available_versions:154print("You could run, instead:")155for _, cmd in available_versions:156args = [cmd, script_path] + sys.argv[1:]157158cmd_str = indent(PythonVersion.cmd_print(args), " ")159print(f"{cmd_str}\n")160161if bail_out:162msg = f"Python {python_ver} not supported. Bailing out"163if success_on_error:164print(msg, file=sys.stderr)165sys.exit(0)166else:167sys.exit(msg)168169print(f"Python {python_ver} not supported. Changing to {new_python_cmd}")170171# Restart script using the newer version172args = [new_python_cmd, script_path] + sys.argv[1:]173174try:175os.execv(new_python_cmd, args)176except OSError as e:177sys.exit(f"Failed to restart with {new_python_cmd}: {e}")178179180