diff --git a/tracetools_trace/tracetools_trace/tools/lttng.py b/tracetools_trace/tracetools_trace/tools/lttng.py index d358475..782b9cf 100644 --- a/tracetools_trace/tracetools_trace/tools/lttng.py +++ b/tracetools_trace/tracetools_trace/tools/lttng.py @@ -14,6 +14,8 @@ """Interface for tracing with LTTng.""" +import sys + from typing import List from typing import Optional @@ -21,8 +23,18 @@ 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): + print( + f'lttng module version >={LTTNG_MIN_VERSION} required, found {str(current_version)}', + file=sys.stderr, + ) except ImportError: - # Fall back on empty functions + # Fall back on stub functions so that this still passes linter checks from . import lttng_stub _lttng = 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, diff --git a/tracetools_trace/tracetools_trace/tools/lttng_stub.py b/tracetools_trace/tracetools_trace/tools/lttng_stub.py index 47c0c63..ce7ecc7 100644 --- a/tracetools_trace/tracetools_trace/tools/lttng_stub.py +++ b/tracetools_trace/tracetools_trace/tools/lttng_stub.py @@ -15,17 +15,20 @@ """Stub version of the interface for tracing with LTTng.""" +ERROR_MESSAGE = 'lttng module not found, but still tried to use it' + + def setup(*args, **kwargs) -> None: - pass + raise RuntimeError(ERROR_MESSAGE) def start(*args, **kwargs) -> None: - pass + raise RuntimeError(ERROR_MESSAGE) def stop(*args, **kwargs) -> None: - pass + raise RuntimeError(ERROR_MESSAGE) def destroy(*args, **kwargs) -> None: - pass + raise RuntimeError(ERROR_MESSAGE)