2020-08-08 17:41:11 +00:00
|
|
|
import subprocess
|
|
|
|
|
|
2020-08-12 15:18:20 +00:00
|
|
|
from .executable import Executable
|
2020-08-08 17:41:11 +00:00
|
|
|
|
|
|
|
|
|
2020-08-10 15:40:56 +00:00
|
|
|
class DockerCommand:
|
2020-08-08 17:41:11 +00:00
|
|
|
__requires_root = None
|
2020-08-12 15:18:20 +00:00
|
|
|
__exe = None
|
2020-08-08 17:41:11 +00:00
|
|
|
|
2020-08-10 14:28:42 +00:00
|
|
|
def __init__(self, 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(
|
|
|
|
|
['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:18:20 +00:00
|
|
|
self.__exe = Executable(exe_name, DockerCommand.__requires_root)
|
2020-08-08 17:41:11 +00:00
|
|
|
|
2020-08-10 14:28:42 +00:00
|
|
|
def __getattr__(self, item):
|
2020-08-12 15:18:20 +00:00
|
|
|
return getattr(self.__exe, item)
|
|
|
|
|
|
|
|
|
|
def run_less(self, args, **kwargs):
|
|
|
|
|
process = self.Popen(
|
|
|
|
|
args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
|
|
|
|
|
**kwargs
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
less_process = Executable('less').run(
|
|
|
|
|
['-R', '+G'], stdin=process.stdout
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
process.communicate()
|
|
|
|
|
return less_process
|