From daced4dd7dcd951ec1dda4f09ac999524918e26e Mon Sep 17 00:00:00 2001 From: ldericher Date: Wed, 12 Aug 2020 16:43:13 +0200 Subject: [PATCH] Package subcommands.utils --- src/kiwi/subcommands/_subcommand.py | 18 ++++++++ src/kiwi/subcommands/init.py | 3 +- src/kiwi/subcommands/logs.py | 3 +- src/kiwi/subcommands/show.py | 2 +- src/kiwi/subcommands/utils/__init__.py | 1 + .../{_utils.py => utils/dockercommand.py} | 42 ++----------------- src/kiwi/subcommands/utils/executable.py | 21 ++++++++++ 7 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 src/kiwi/subcommands/_subcommand.py create mode 100644 src/kiwi/subcommands/utils/__init__.py rename src/kiwi/subcommands/{_utils.py => utils/dockercommand.py} (68%) create mode 100644 src/kiwi/subcommands/utils/executable.py diff --git a/src/kiwi/subcommands/_subcommand.py b/src/kiwi/subcommands/_subcommand.py new file mode 100644 index 0000000..d6c890d --- /dev/null +++ b/src/kiwi/subcommands/_subcommand.py @@ -0,0 +1,18 @@ +from ..parser import Parser + + +class SubCommand: + __name = None + _sub_parser = None + + def __init__(self, name, **kwargs): + self.__name = name + self._sub_parser = Parser().get_subparsers().add_parser(name, **kwargs) + + def __str__(self): + return self.__name + + def run(self, config, args): + pass + + diff --git a/src/kiwi/subcommands/init.py b/src/kiwi/subcommands/init.py index bba50be..c2e5469 100644 --- a/src/kiwi/subcommands/init.py +++ b/src/kiwi/subcommands/init.py @@ -4,7 +4,8 @@ import os from .._constants import KIWI_CONF_NAME from ..config import DefaultConfig -from ._utils import SubCommand, is_executable, find_exe_file, get_exe_key +from ._subcommand import SubCommand +from .utils.executable import get_exe_key, is_executable, find_exe_file def user_input(config, key, prompt): diff --git a/src/kiwi/subcommands/logs.py b/src/kiwi/subcommands/logs.py index 5ae932a..5755611 100644 --- a/src/kiwi/subcommands/logs.py +++ b/src/kiwi/subcommands/logs.py @@ -1,6 +1,7 @@ import logging -from ._utils import SubCommand, DockerCommand +from ._subcommand import SubCommand +from .utils.dockercommand import DockerCommand class LogsCommand(SubCommand): diff --git a/src/kiwi/subcommands/show.py b/src/kiwi/subcommands/show.py index 1deb830..3cb9069 100644 --- a/src/kiwi/subcommands/show.py +++ b/src/kiwi/subcommands/show.py @@ -1,4 +1,4 @@ -from ._utils import SubCommand +from ._subcommand import SubCommand class ShowCommand(SubCommand): diff --git a/src/kiwi/subcommands/utils/__init__.py b/src/kiwi/subcommands/utils/__init__.py new file mode 100644 index 0000000..5a65bd6 --- /dev/null +++ b/src/kiwi/subcommands/utils/__init__.py @@ -0,0 +1 @@ +# git keep diff --git a/src/kiwi/subcommands/_utils.py b/src/kiwi/subcommands/utils/dockercommand.py similarity index 68% rename from src/kiwi/subcommands/_utils.py rename to src/kiwi/subcommands/utils/dockercommand.py index 6de617b..725ac5d 100644 --- a/src/kiwi/subcommands/_utils.py +++ b/src/kiwi/subcommands/utils/dockercommand.py @@ -1,44 +1,8 @@ import logging -import os import subprocess -from ..parser import Parser -from ..config import LoadedConfig - - -def is_executable(filename): - if filename is None: - return False - - return os.path.isfile(filename) and os.access(filename, os.X_OK) - - -def find_exe_file(exe_name): - for path in os.environ['PATH'].split(os.pathsep): - exe_file = os.path.join(path, exe_name) - if is_executable(exe_file): - return exe_file - - return None - - -def get_exe_key(exe_name): - return f'executables:{exe_name}' - - -class SubCommand: - __name = None - _sub_parser = None - - def __init__(self, name, **kwargs): - self.__name = name - self._sub_parser = Parser().get_subparsers().add_parser(name, **kwargs) - - def __str__(self): - return self.__name - - def run(self, config, args): - pass +from ...config import LoadedConfig +from .executable import get_exe_key class DockerCommand: @@ -100,4 +64,4 @@ class DockerCommand: DockerCommand.__instances[exe_name] = DockerCommand.__DockerCommand(exe_name) def __getattr__(self, item): - return getattr(self.__instances[self.__exe_name], item) + return getattr(self.__instances[self.__exe_name], item) \ No newline at end of file diff --git a/src/kiwi/subcommands/utils/executable.py b/src/kiwi/subcommands/utils/executable.py new file mode 100644 index 0000000..58e2d1e --- /dev/null +++ b/src/kiwi/subcommands/utils/executable.py @@ -0,0 +1,21 @@ +import os + + +def get_exe_key(exe_name): + return f'executables:{exe_name}' + + +def is_executable(filename): + if filename is None: + return False + + return os.path.isfile(filename) and os.access(filename, os.X_OK) + + +def find_exe_file(exe_name): + for path in os.environ['PATH'].split(os.pathsep): + exe_file = os.path.join(path, exe_name) + if is_executable(exe_file): + return exe_file + + return None \ No newline at end of file