From 3980c2dd016a5e0821fa5c157af83a4e64fea893 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Thu, 9 Jan 2020 20:21:44 -0500 Subject: [PATCH] Check version of LTTng Python module and raise error if below 2.10.7 --- .../tracetools_trace/tools/lttng.py | 9 +++++++++ .../tracetools_trace/tools/lttng_impl.py | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/tracetools_trace/tracetools_trace/tools/lttng.py b/tracetools_trace/tracetools_trace/tools/lttng.py index aeb38ff..6bcd0cb 100644 --- a/tracetools_trace/tracetools_trace/tools/lttng.py +++ b/tracetools_trace/tracetools_trace/tools/lttng.py @@ -23,6 +23,15 @@ try: from . import lttng_impl _lttng = lttng_impl + + # Check lttng module version + from distutils.version import StrictVersion + current_version = _lttng.get_version() + LTTNG_MIN_VERSION = '2.10.7' + if current_version is None or current_version < StrictVersion(LTTNG_MIN_VERSION): + raise RuntimeError( + f'lttng module version >={LTTNG_MIN_VERSION} required, found {str(current_version)}' + ) except ImportError: # Fall back on stub functions so that this still passes linter checks from . import lttng_stub diff --git a/tracetools_trace/tracetools_trace/tools/lttng_impl.py b/tracetools_trace/tracetools_trace/tools/lttng_impl.py index 9e488d8..0a959eb 100644 --- a/tracetools_trace/tracetools_trace/tools/lttng_impl.py +++ b/tracetools_trace/tracetools_trace/tools/lttng_impl.py @@ -14,8 +14,11 @@ """Implementation of the interface for tracing with LTTng.""" +from distutils.version import StrictVersion +import re from typing import List from typing import Optional +from typing import Union import lttng @@ -26,6 +29,23 @@ from .path import DEFAULT_BASE_PATH from .path import get_full_session_path +def get_version() -> Union[StrictVersion, None]: + """ + Get the version of the lttng module. + + The module does not have a __version__ attribute, but the version is mentioned in its __doc__, + and seems to be written in a consistent way across versions. + + :return: the version as a StrictVersion object, or `None` if it cannot be extracted + """ + doc_lines = lttng.__doc__.split('\n') + first_line = list(filter(None, doc_lines))[0] + version_string = first_line.split(' ')[1] + if not re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+$').match(version_string): + return None + return StrictVersion(version_string) + + def setup( session_name: str, base_path: str = DEFAULT_BASE_PATH,