kiwi-el8/kiwi/utils/command_capabilities.py
Alberto Planas 0c095a98b1 Replace @staticmethod with @classmethod when needed
@classmethod are used in Python to represent methods that can
query and update the class (cls parameter). Is expected to be
used for metaprograming, or advanced techniques that require the
access to the class itself, before we have an instance.

@staticmethod are used to associate a function to a class. It will
not be have access to the instance (self) not the class (cls). In
other programming languages are known as class methods.

This patch replace all the @classmethod with @staticmethod when
there is not need to access to the cls parameter, because the
intention is to be used as normal functions.
2019-03-26 16:27:43 +01:00

119 lines
4.2 KiB
Python

# Copyright (c) 2015 SUSE Linux GmbH. All rights reserved.
#
# This file is part of kiwi.
#
# kiwi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# kiwi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with kiwi. If not, see <http://www.gnu.org/licenses/>
#
import re
# project
from kiwi.command import Command
from kiwi.logger import log
from kiwi.exceptions import KiwiCommandCapabilitiesError
class CommandCapabilities(object):
"""
**Validation of command version flags or version**
Performs commands calls and parses the output
so it can look specific flags on help message, check
command version, etc.
"""
@staticmethod
def has_option_in_help(
call, flag, help_flags=None,
root=None, raise_on_error=True
):
"""
Checks if the given flag is present in the help output
of the given command.
:param str call: the command the check
:param str flag: the flag or substring to find in stdout
:param list help_flags: a list with the required command arguments.
:param str root: root directory of the env to validate
:raises KiwiCommandCapabilitiesError: if command execution fails
:return: True if the flag is found, False in any other case
:rtype: bool
"""
help_args = help_flags or ['--help']
if root:
arguments = ['chroot', root, call] + help_args
else:
arguments = [call] + help_args
try:
command = Command.run(arguments)
for line in command.output.splitlines():
if flag in line:
return True
for line in command.error.splitlines():
if flag in line:
return True
except Exception:
message = 'Could not parse {} output'.format(call)
if raise_on_error:
raise KiwiCommandCapabilitiesError(message)
log.warning(message)
return False
@staticmethod
def check_version(
call, version_waterline, version_flags=None,
root=None, raise_on_error=True
):
"""
Checks if the given command version is equal or higher than
the given version tuple.
:param str call: the command the check
:param tuple version_waterline: minimum desired version of the command
:param list version_flags: a list with the required command arguments.
:param str root: root directory of the env to validate
:param bool raise_on_error: control error behavior
:raises KiwiCommandCapabilitiesError: if raise_on_error is True and
command execution fails or version can't be parsed.
:return: True if the current command version is equal or higher to
version_waterline
:rtype: bool
"""
version_args = version_flags or ['--version']
if root:
arguments = ['chroot', root, call] + version_args
else:
arguments = [call] + version_args
version_info = None
try:
command = Command.run(arguments)
for line in command.output.splitlines():
match = re.search('[0-9]+(\.[0-9]+)*', line)
if match:
version_info = tuple(
int(elt) for elt in match.group(0).split('.')
)
break
if version_info is None:
raise Exception
except Exception:
message = 'Could not parse {0} version'.format(call)
if raise_on_error:
raise KiwiCommandCapabilitiesError(message)
log.warning(message)
return False
return version_info >= version_waterline