2020-08-12 15:46:50 +00:00
|
|
|
# system
|
2020-08-08 17:41:11 +00:00
|
|
|
import subprocess
|
|
|
|
|
|
2020-08-12 15:46:50 +00:00
|
|
|
# local
|
2020-08-12 15:18:20 +00:00
|
|
|
from .executable import Executable
|
2020-08-08 17:41:11 +00:00
|
|
|
|
|
|
|
|
|
2020-08-12 15:39:55 +00:00
|
|
|
class DockerCommand(Executable):
|
2020-08-08 17:41:11 +00:00
|
|
|
__requires_root = None
|
|
|
|
|
|
2020-08-10 14:28:42 +00:00
|
|
|
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:
|
2020-08-08 17:41:11 +00:00
|
|
|
try:
|
2020-08-12 15:18:20 +00:00
|
|
|
Executable('docker').run(
|
2020-08-12 15:39:55 +00:00
|
|
|
['ps'],
|
|
|
|
|
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
2020-08-08 17:41:11 +00:00
|
|
|
)
|
2020-08-10 15:40:56 +00:00
|
|
|
DockerCommand.__requires_root = False
|
2020-08-08 17:41:11 +00:00
|
|
|
except subprocess.CalledProcessError:
|
2020-08-10 15:40:56 +00:00
|
|
|
DockerCommand.__requires_root = True
|
2020-08-08 17:41:11 +00:00
|
|
|
|
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
|
|
|
|
|
)
|
2020-08-12 15:18:20 +00:00
|
|
|
|
|
|
|
|
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,
|
2020-08-12 15:18:20 +00:00
|
|
|
**kwargs
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
less_process = Executable('less').run(
|
2020-08-12 15:39:55 +00:00
|
|
|
['-R', '+G'],
|
|
|
|
|
stdin=process.stdout
|
2020-08-12 15:18:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
process.communicate()
|
|
|
|
|
return less_process
|