Merge branch 'handle-no-lttng-python-module-found' into 'master'

Improve handling and reporting of LTTng Python module status

See merge request micro-ROS/ros_tracing/ros2_tracing!132
This commit is contained in:
Christophe Bedard 2020-01-10 16:22:46 +00:00
commit a900bd6f71
3 changed files with 40 additions and 5 deletions

View file

@ -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

View file

@ -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,

View file

@ -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)