2016-11-26 13:57:33 +01:00
|
|
|
import shlex
|
|
|
|
import subprocess
|
2016-11-27 18:33:37 +01:00
|
|
|
try:
|
|
|
|
from exceptions import RuntimeError
|
|
|
|
except ImportError:
|
|
|
|
# Python3 doesn't require this anymore
|
|
|
|
pass
|
2016-10-31 13:34:48 +01:00
|
|
|
|
|
|
|
def bytefmt(num):
|
|
|
|
for unit in [ "", "Ki", "Mi", "Gi" ]:
|
|
|
|
if num < 1024.0:
|
|
|
|
return "{:.2f}{}B".format(num, unit)
|
|
|
|
num /= 1024.0
|
|
|
|
return "{:05.2f%}{}GiB".format(num)
|
|
|
|
|
2016-11-12 08:34:21 +01:00
|
|
|
def durationfmt(duration):
|
|
|
|
minutes, seconds = divmod(duration, 60)
|
|
|
|
hours, minutes = divmod(minutes, 60)
|
|
|
|
res = "{:02d}:{:02d}".format(minutes, seconds)
|
|
|
|
if hours > 0: res = "{:02d}:{}".format(hours, res)
|
|
|
|
|
|
|
|
return res
|
2016-11-26 13:57:33 +01:00
|
|
|
|
2016-12-02 17:52:05 +01:00
|
|
|
def execute(cmd, wait=True):
|
2016-11-26 13:57:33 +01:00
|
|
|
args = shlex.split(cmd)
|
|
|
|
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
|
|
|
2016-12-02 17:52:05 +01:00
|
|
|
if wait:
|
|
|
|
out, err = p.communicate()
|
|
|
|
if p.returncode != 0:
|
|
|
|
raise RuntimeError("{} exited with {}".format(cmd, p.returncode))
|
|
|
|
return out.decode("utf-8")
|
|
|
|
return None
|