tools/lib/python/kdoc/python_version.py

Source file repositories/reference/linux-study-clean/tools/lib/python/kdoc/python_version.py

File Facts

System
Linux kernel
Corpus path
tools/lib/python/kdoc/python_version.py
Extension
.py
Size
5872 bytes
Lines
191
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: tools
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2017-2025 Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

"""
Handle Python version check logic.

Not all Python versions are supported by scripts. Yet, on some cases,
like during documentation build, a newer version of python could be
available.

This class allows checking if the minimal requirements are followed.

Better than that, PythonVersion.check_python() not only checks the minimal
requirements, but it automatically switches to a the newest available
Python version if present.

"""

import os
import re
import subprocess
import shlex
import sys

from glob import glob
from textwrap import indent

class PythonVersion:
    """
    Ancillary methods that checks for missing dependencies for different
    types of types, like binaries, python modules, rpm deps, etc.
    """

    def __init__(self, version):
        """
        Ïnitialize self.version tuple from a version string.
        """
        self.version = self.parse_version(version)

    @staticmethod
    def parse_version(version):
        """
        Convert a major.minor.patch version into a tuple.
        """
        return tuple(int(x) for x in version.split("."))

    @staticmethod
    def ver_str(version):
        """
        Returns a version tuple as major.minor.patch.
        """
        return ".".join([str(x) for x in version])

    @staticmethod
    def cmd_print(cmd, max_len=80):
        """
        Outputs a command line, repecting maximum width.
        """

        cmd_line = []

        for w in cmd:
            w = shlex.quote(w)

            if cmd_line:
                if not max_len or len(cmd_line[-1]) + len(w) < max_len:
                    cmd_line[-1] += " " + w
                    continue
                else:

Annotation

Implementation Notes