Path: blob/master/tools/lib/python/kdoc/python_version.py
122941 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"""36Ïnitialize self.version tuple from a version string.37"""38self.version = self.parse_version(version)3940@staticmethod41def parse_version(version):42"""43Convert a major.minor.patch version into a tuple.44"""45return tuple(int(x) for x in version.split("."))4647@staticmethod48def ver_str(version):49"""50Returns a version tuple as major.minor.patch.51"""52return ".".join([str(x) for x in version])5354@staticmethod55def cmd_print(cmd, max_len=80):56"""57Outputs a command line, repecting maximum width.58"""5960cmd_line = []6162for w in cmd:63w = shlex.quote(w)6465if cmd_line:66if not max_len or len(cmd_line[-1]) + len(w) < max_len:67cmd_line[-1] += " " + w68continue69else:70cmd_line[-1] += " \\"71cmd_line.append(w)72else:73cmd_line.append(w)7475return "\n ".join(cmd_line)7677def __str__(self):78"""79Return a version tuple as major.minor.patch from self.version.80"""81return self.ver_str(self.version)8283@staticmethod84def get_python_version(cmd):85"""86Get python version from a Python binary. As we need to detect if87are out there newer python binaries, we can't rely on sys.release here.88"""8990kwargs = {}91if sys.version_info < (3, 7):92kwargs['universal_newlines'] = True93else:94kwargs['text'] = True9596result = subprocess.run([cmd, "--version"],97stdout = subprocess.PIPE,98stderr = subprocess.PIPE,99**kwargs, check=False)100101version = result.stdout.strip()102103match = re.search(r"(\d+\.\d+\.\d+)", version)104if match:105return PythonVersion.parse_version(match.group(1))106107print(f"Can't parse version {version}")108return (0, 0, 0)109110@staticmethod111def find_python(min_version):112"""113Detect if are out there any python 3.xy version newer than the114current one.115116Note: this routine is limited to up to 2 digits for python3. We117may need to update it one day, hopefully on a distant future.118"""119patterns = [120"python3.[0-9][0-9]",121"python3.[0-9]",122]123124python_cmd = []125126# Seek for a python binary newer than min_version127for path in os.getenv("PATH", "").split(":"):128for pattern in patterns:129for cmd in glob(os.path.join(path, pattern)):130if os.path.isfile(cmd) and os.access(cmd, os.X_OK):131version = PythonVersion.get_python_version(cmd)132if version >= min_version:133python_cmd.append((version, cmd))134135return sorted(python_cmd, reverse=True)136137@staticmethod138def check_python(min_version, show_alternatives=False, bail_out=False,139success_on_error=False):140"""141Check if the current python binary satisfies our minimal requirement142for Sphinx build. If not, re-run with a newer version if found.143"""144cur_ver = sys.version_info[:3]145if cur_ver >= min_version:146ver = PythonVersion.ver_str(cur_ver)147return148149python_ver = PythonVersion.ver_str(cur_ver)150151available_versions = PythonVersion.find_python(min_version)152if not available_versions:153print(f"ERROR: Python version {python_ver} is not supported anymore\n")154print(" Can't find a new version. This script may fail")155return156157script_path = os.path.abspath(sys.argv[0])158159# Check possible alternatives160if available_versions:161new_python_cmd = available_versions[0][1]162else:163new_python_cmd = None164165if show_alternatives and available_versions:166print("You could run, instead:")167for _, cmd in available_versions:168args = [cmd, script_path] + sys.argv[1:]169170cmd_str = indent(PythonVersion.cmd_print(args), " ")171print(f"{cmd_str}\n")172173if bail_out:174msg = f"Python {python_ver} not supported. Bailing out"175if success_on_error:176print(msg, file=sys.stderr)177sys.exit(0)178else:179sys.exit(msg)180181print(f"Python {python_ver} not supported. Changing to {new_python_cmd}")182183# Restart script using the newer version184args = [new_python_cmd, script_path] + sys.argv[1:]185186try:187os.execv(new_python_cmd, args)188except OSError as e:189sys.exit(f"Failed to restart with {new_python_cmd}: {e}")190191192