UBUNTU: [Packaging] provide a wrapper module for python-perf

BugLink: https://bugs.launchpad.net/bugs/2051560

Provide a virtual python module wrapper to load the actual versioned
python perf module, based on the running kernel version.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
This commit is contained in:
Andrea Righi
2024-03-22 07:54:42 +01:00
committed by Roxana Nicolescu
parent 48357b9b6d
commit 51c8aee421
3 changed files with 41 additions and 1 deletions
+32
View File
@@ -0,0 +1,32 @@
import os
import importlib.util
from glob import glob
class KernelNotFoundError(Exception):
def __init__(self):
kernel_version = os.uname().release
super().__init__(f"WARNING: python perf module not found for kernel {kernel_version}\n\n"
f"You may need to install the following packages for this specific kernel:\n"
f" linux-tools-{kernel_version}-generic\n"
f"You may also want to install of the following package to keep up to date:\n"
f" linux-tools-generic")
# Extract ABI number from kernel version
def _get_abi_version():
_kernel_version = os.uname().release
_parts = _kernel_version.split("-")
return "-".join(_parts[:-1])
# Load the actual python-perf module for the running kernel
_abi_version = _get_abi_version()
_perf_dir = f"/usr/lib/python3/dist-packages/linux-tools-{_abi_version}"
if not os.path.exists(_perf_dir):
raise KernelNotFoundError()
_perf_lib = glob(os.path.join(_perf_dir, "*.so"))[-1]
_spec = importlib.util.spec_from_file_location("perf", _perf_lib)
_perf = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_perf)
# Expose the 'perf' module.
__all__ = ['perf']