1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
# -*- coding: utf-8 -*-
"""Helper to check for availability and version of dependencies."""
import configparser
import os
import re
class DependencyDefinition(object):
"""Dependency definition.
Attributes:
dpkg_name (str): name of the dpkg package that provides the dependency.
is_optional (bool): True if the dependency is optional.
l2tbinaries_name (str): name of the l2tbinaries package that provides
the dependency.
maximum_version (str): maximum supported version, a greater or equal
version is not supported.
minimum_version (str): minimum supported version, a lesser version is
not supported.
name (str): name of (the Python module that provides) the dependency.
pypi_name (str): name of the PyPI package that provides the dependency.
python2_only (bool): True if the dependency is only supported by Python 2.
python3_only (bool): True if the dependency is only supported by Python 3.
rpm_name (str): name of the rpm package that provides the dependency.
skip_check (bool): True if the dependency should be skipped by the
CheckDependencies or CheckTestDependencies methods of DependencyHelper.
skip_requires (bool): True if the dependency should be excluded from
requirements.txt or setup.py install_requires.
version_property (str): name of the version attribute or function.
"""
def __init__(self, name):
"""Initializes a dependency configuration.
Args:
name (str): name of the dependency.
"""
super(DependencyDefinition, self).__init__()
self.dpkg_name = None
self.is_optional = False
self.l2tbinaries_name = None
self.maximum_version = None
self.minimum_version = None
self.name = name
self.pypi_name = None
self.python2_only = False
self.python3_only = False
self.rpm_name = None
self.skip_check = None
self.skip_requires = None
self.version_property = None
class DependencyDefinitionReader(object):
"""Dependency definition reader."""
_VALUE_NAMES = frozenset([
'dpkg_name',
'is_optional',
'l2tbinaries_name',
'maximum_version',
'minimum_version',
'pypi_name',
'python2_only',
'python3_only',
'rpm_name',
'skip_check',
'skip_requires',
'version_property'])
def _GetConfigValue(self, config_parser, section_name, value_name):
"""Retrieves a value from the config parser.
Args:
config_parser (ConfigParser): configuration parser.
section_name (str): name of the section that contains the value.
value_name (str): name of the value.
Returns:
object: configuration value or None if the value does not exists.
"""
try:
return config_parser.get(section_name, value_name)
except configparser.NoOptionError:
return None
def Read(self, file_object):
"""Reads dependency definitions.
Args:
file_object (file): file-like object to read from.
Yields:
DependencyDefinition: dependency definition.
"""
config_parser = configparser.ConfigParser(interpolation=None)
config_parser.read_file(file_object)
for section_name in config_parser.sections():
dependency_definition = DependencyDefinition(section_name)
for value_name in self._VALUE_NAMES:
value = self._GetConfigValue(config_parser, section_name, value_name)
setattr(dependency_definition, value_name, value)
yield dependency_definition
class DependencyHelper(object):
"""Dependency helper.
Attributes:
dependencies (dict[str, DependencyDefinition]): dependencies.
"""
_VERSION_NUMBERS_REGEX = re.compile(r'[0-9.]+')
_VERSION_SPLIT_REGEX = re.compile(r'\.|\-')
def __init__(
self, dependencies_file='dependencies.ini',
test_dependencies_file='test_dependencies.ini'):
"""Initializes a dependency helper.
Args:
dependencies_file (Optional[str]): path to the dependencies configuration
file.
test_dependencies_file (Optional[str]): path to the test dependencies
configuration file.
"""
super(DependencyHelper, self).__init__()
self._test_dependencies = {}
self.dependencies = {}
dependency_reader = DependencyDefinitionReader()
with open(dependencies_file, 'r', encoding='utf-8') as file_object:
for dependency in dependency_reader.Read(file_object):
self.dependencies[dependency.name] = dependency
if os.path.exists(test_dependencies_file):
with open(test_dependencies_file, 'r', encoding='utf-8') as file_object:
for dependency in dependency_reader.Read(file_object):
self._test_dependencies[dependency.name] = dependency
def _CheckPythonModule(self, dependency):
"""Checks the availability of a Python module.
Args:
dependency (DependencyDefinition): dependency definition.
Returns:
tuple: containing:
bool: True if the Python module is available and conforms to
the minimum required version, False otherwise.
str: status message.
"""
module_object = self._ImportPythonModule(dependency.name)
if not module_object:
return False, f'missing: {dependency.name:s}'
if not dependency.version_property:
return True, dependency.name
return self._CheckPythonModuleVersion(
dependency.name, module_object, dependency.version_property,
dependency.minimum_version, dependency.maximum_version)
def _CheckPythonModuleVersion(
self, module_name, module_object, version_property, minimum_version,
maximum_version):
"""Checks the version of a Python module.
Args:
module_object (module): Python module.
module_name (str): name of the Python module.
version_property (str): version attribute or function.
minimum_version (str): minimum version.
maximum_version (str): maximum version.
Returns:
tuple: containing:
bool: True if the Python module is available and conforms to
the minimum required version, False otherwise.
str: status message.
"""
module_version = None
if not version_property.endswith('()'):
module_version = getattr(module_object, version_property, None)
else:
version_method = getattr(
module_object, version_property[:-2], None)
if version_method:
module_version = version_method()
if not module_version:
return False, (
f'unable to determine version information for: {module_name:s}')
# Make sure the module version is a string.
module_version = f'{module_version!s}'
# Split the version string and convert every digit into an integer.
# A string compare of both version strings will yield an incorrect result.
# Strip any semantic suffixes such as a1, b1, pre, post, rc, dev.
module_version = self._VERSION_NUMBERS_REGEX.findall(module_version)[0]
if module_version[-1] == '.':
module_version = module_version[:-1]
try:
module_version_map = list(
map(int, self._VERSION_SPLIT_REGEX.split(module_version)))
except ValueError:
return False, (
f'unable to parse module version: {module_name:s} {module_version:s}')
if minimum_version:
try:
minimum_version_map = list(
map(int, self._VERSION_SPLIT_REGEX.split(minimum_version)))
except ValueError:
return False, (
f'unable to parse minimum version: {module_name:s} '
f'{minimum_version:s}')
if module_version_map < minimum_version_map:
return False, (
f'{module_name:s} version: {module_version!s} is too old, '
f'{minimum_version!s} or later required')
if maximum_version:
try:
maximum_version_map = list(
map(int, self._VERSION_SPLIT_REGEX.split(maximum_version)))
except ValueError:
return False, (
f'unable to parse maximum version: {module_name:s} '
f'{maximum_version:s}')
if module_version_map > maximum_version_map:
return False, (
f'{module_name:s} version: {module_version!s} is too recent, '
f'{maximum_version!s} or earlier required')
return True, f'{module_name:s} version: {module_version!s}'
def _ImportPythonModule(self, module_name):
"""Imports a Python module.
Args:
module_name (str): name of the module.
Returns:
module: Python module or None if the module cannot be imported.
"""
try:
module_object = list(map(__import__, [module_name]))[0]
except ImportError:
return None
# If the module name contains dots get the upper most module object.
if '.' in module_name:
for submodule_name in module_name.split('.')[1:]:
module_object = getattr(module_object, submodule_name, None)
return module_object
def _PrintCheckDependencyStatus(
self, dependency, result, status_message, verbose_output=True):
"""Prints the check dependency status.
Args:
dependency (DependencyDefinition): dependency definition.
result (bool): True if the Python module is available and conforms to
the minimum required version, False otherwise.
status_message (str): status message.
verbose_output (Optional[bool]): True if output should be verbose.
"""
if not result or dependency.is_optional:
if dependency.is_optional:
status_indicator = '[OPTIONAL]'
else:
status_indicator = '[FAILURE]'
print(f'{status_indicator:s}\t{status_message:s}')
elif verbose_output:
print(f'[OK]\t\t{status_message:s}')
def CheckDependencies(self, verbose_output=True):
"""Checks the availability of the dependencies.
Args:
verbose_output (Optional[bool]): True if output should be verbose.
Returns:
bool: True if the dependencies are available, False otherwise.
"""
print('Checking availability and versions of dependencies.')
check_result = True
for _, dependency in sorted(self.dependencies.items()):
if dependency.skip_check:
continue
result, status_message = self._CheckPythonModule(dependency)
if not result and not dependency.is_optional:
check_result = False
self._PrintCheckDependencyStatus(
dependency, result, status_message, verbose_output=verbose_output)
if check_result and not verbose_output:
print('[OK]')
print('')
return check_result
def CheckTestDependencies(self, verbose_output=True):
"""Checks the availability of the dependencies when running tests.
Args:
verbose_output (Optional[bool]): True if output should be verbose.
Returns:
bool: True if the dependencies are available, False otherwise.
"""
if not self.CheckDependencies(verbose_output=verbose_output):
return False
print('Checking availability and versions of test dependencies.')
check_result = True
for dependency in sorted(
self._test_dependencies.values(),
key=lambda dependency: dependency.name):
if dependency.skip_check:
continue
result, status_message = self._CheckPythonModule(dependency)
if not result and not dependency.is_optional:
check_result = False
self._PrintCheckDependencyStatus(
dependency, result, status_message, verbose_output=verbose_output)
if check_result and not verbose_output:
print('[OK]')
print('')
return check_result
|