-
Notifications
You must be signed in to change notification settings - Fork 5
/
swarmexecutor.py
executable file
·38 lines (29 loc) · 1.16 KB
/
swarmexecutor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import subprocess
class SwarmExecutor:
def __init__(self, logging):
self.logging = logging
def log_cmd(self, *args, **kwargs):
def dictionary_diff(a, b):
return {k: v for k, v in a.items() if k not in b or v != b[k]}
if "env" in kwargs:
extra_env = dictionary_diff(kwargs["env"], os.environ)
self.logging.info(f"Executing command: {args} with env {extra_env}")
else:
self.logging.info(f"Executing command: {args}")
@staticmethod
def prepare_sudo_command(command, env):
return ["sudo", f"--preserve-env={','.join(env.keys())}"] + command
def Popen(self, *args, **kwargs):
self.log_cmd(*args, **kwargs)
return subprocess.Popen(*args, **kwargs)
def check_call(self, *args, **kwargs):
self.log_cmd(*args, **kwargs)
return subprocess.check_call(*args, **kwargs)
def check_output(self, *args, **kwargs) -> bytes:
self.log_cmd(*args, **kwargs)
output = subprocess.check_output(*args, **kwargs)
if type(output) is bytes:
return output
else:
return output.encode('utf-8')