mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-22 04:43:00 +00:00
DockerCommand now is an Executable
This commit is contained in:
parent
8c2cdbda3a
commit
70b04f039a
2 changed files with 28 additions and 21 deletions
|
@ -3,33 +3,39 @@ import subprocess
|
||||||
from .executable import Executable
|
from .executable import Executable
|
||||||
|
|
||||||
|
|
||||||
class DockerCommand:
|
class DockerCommand(Executable):
|
||||||
__requires_root = None
|
__requires_root = None
|
||||||
__exe = None
|
|
||||||
|
|
||||||
def __init__(self, exe_name):
|
def __init__(self, exe_name):
|
||||||
|
super().__init__(exe_name)
|
||||||
|
|
||||||
if DockerCommand.__requires_root is None:
|
if DockerCommand.__requires_root is None:
|
||||||
try:
|
try:
|
||||||
Executable('docker').run(
|
Executable('docker').run(
|
||||||
['ps'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
['ps'],
|
||||||
|
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
||||||
)
|
)
|
||||||
DockerCommand.__requires_root = False
|
DockerCommand.__requires_root = False
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
DockerCommand.__requires_root = True
|
DockerCommand.__requires_root = True
|
||||||
|
|
||||||
self.__exe = Executable(exe_name, DockerCommand.__requires_root)
|
def run(self, args, **kwargs):
|
||||||
|
# equivalent to 'super().run' but agnostic of nested class construct
|
||||||
def __getattr__(self, item):
|
super().__getattr__("run")(
|
||||||
return getattr(self.__exe, item)
|
args, DockerCommand.__requires_root,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
def run_less(self, args, **kwargs):
|
def run_less(self, args, **kwargs):
|
||||||
process = self.Popen(
|
process = self.Popen(
|
||||||
args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
args, DockerCommand.__requires_root,
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
less_process = Executable('less').run(
|
less_process = Executable('less').run(
|
||||||
['-R', '+G'], stdin=process.stdout
|
['-R', '+G'],
|
||||||
|
stdin=process.stdout
|
||||||
)
|
)
|
||||||
|
|
||||||
process.communicate()
|
process.communicate()
|
||||||
|
|
|
@ -20,35 +20,36 @@ def find_exe_file(exe_name):
|
||||||
|
|
||||||
|
|
||||||
class Executable:
|
class Executable:
|
||||||
__exe_name = None
|
|
||||||
__instances = {}
|
|
||||||
|
|
||||||
class __Executable:
|
class __Executable:
|
||||||
__cmd = None
|
__exe_path = None
|
||||||
|
|
||||||
def __init__(self, exe_name, requires_root=False):
|
def __init__(self, exe_name, requires_root=False):
|
||||||
self.__cmd = [find_exe_file(exe_name)]
|
self.__exe_path = find_exe_file(exe_name)
|
||||||
|
|
||||||
|
def __build_cmd(self, args, requires_root=False, **kwargs):
|
||||||
|
cmd = [self.__exe_path, *args]
|
||||||
|
|
||||||
if requires_root:
|
if requires_root:
|
||||||
self.__cmd = [find_exe_file("sudo"), *self.__cmd]
|
self.__exe_path = [find_exe_file("sudo"), self.__exe_path]
|
||||||
|
|
||||||
def __build_cmd(self, args, **kwargs):
|
|
||||||
cmd = [*self.__cmd, *args]
|
|
||||||
logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
|
logging.debug(f"Executable cmd{cmd}, kwargs{kwargs}")
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
def run(self, args, **kwargs):
|
def run(self, args, requires_root=False, **kwargs):
|
||||||
return subprocess.run(
|
return subprocess.run(
|
||||||
self.__build_cmd(args, **kwargs),
|
self.__build_cmd(args, requires_root, **kwargs),
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def Popen(self, args, **kwargs):
|
def Popen(self, args, requires_root=False, **kwargs):
|
||||||
return subprocess.Popen(
|
return subprocess.Popen(
|
||||||
self.__build_cmd(args, **kwargs),
|
self.__build_cmd(args, requires_root, **kwargs),
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
|
__exe_name = None
|
||||||
|
__instances = {}
|
||||||
|
|
||||||
def __init__(self, exe_name, requires_root=False):
|
def __init__(self, exe_name, requires_root=False):
|
||||||
self.__exe_name = exe_name
|
self.__exe_name = exe_name
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue