1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2025-12-07 17:13:09 +00:00
kiwi-scp/src/kiwi/subcommands/utils/dockercommand.py

45 lines
1.2 KiB
Python
Raw Normal View History

2020-08-12 15:46:50 +00:00
# system
import subprocess
2020-08-12 15:46:50 +00:00
# local
from .executable import Executable
2020-08-12 15:39:55 +00:00
class DockerCommand(Executable):
__requires_root = None
def __init__(self, exe_name):
2020-08-12 15:39:55 +00:00
super().__init__(exe_name)
2020-08-10 15:40:56 +00:00
if DockerCommand.__requires_root is None:
try:
Executable('docker').run(
2020-08-12 15:39:55 +00:00
['ps'],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
2020-08-10 15:40:56 +00:00
DockerCommand.__requires_root = False
except subprocess.CalledProcessError:
2020-08-10 15:40:56 +00:00
DockerCommand.__requires_root = True
2020-08-12 15:39:55 +00:00
def run(self, args, **kwargs):
# equivalent to 'super().run' but agnostic of nested class construct
super().__getattr__("run")(
args, DockerCommand.__requires_root,
**kwargs
)
def run_less(self, args, **kwargs):
process = self.Popen(
2020-08-12 15:39:55 +00:00
args, DockerCommand.__requires_root,
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
**kwargs
)
less_process = Executable('less').run(
2020-08-12 15:39:55 +00:00
['-R', '+G'],
stdin=process.stdout
)
process.communicate()
return less_process