2017-04-26 07:51:11 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-12-09 19:29:16 +01:00
|
|
|
import shlex
|
2017-04-02 08:31:23 +02:00
|
|
|
import logging
|
2016-12-09 19:29:16 +01:00
|
|
|
import subprocess
|
|
|
|
|
|
|
|
try:
|
|
|
|
from exceptions import RuntimeError
|
|
|
|
except ImportError:
|
|
|
|
# Python3 doesn't require this anymore
|
|
|
|
pass
|
|
|
|
|
2017-07-08 06:44:08 +02:00
|
|
|
|
|
|
|
def asbool(val):
|
|
|
|
if val is None:
|
|
|
|
return False
|
|
|
|
if isinstance(val, bool):
|
|
|
|
return val
|
|
|
|
val = str(val).strip().lower()
|
|
|
|
return val in ("t", "true", "y", "yes", "on", "1")
|
|
|
|
|
2019-12-11 12:23:57 +01:00
|
|
|
def aslist(val):
|
|
|
|
if val is None:
|
|
|
|
return []
|
|
|
|
if isinstance(val, list):
|
|
|
|
return val
|
2019-12-11 21:23:07 +01:00
|
|
|
return str(val).replace(' ', '').split(',')
|
2019-12-11 12:23:57 +01:00
|
|
|
|
2016-12-09 19:29:16 +01:00
|
|
|
def execute(cmd, wait=True):
|
2019-01-26 19:40:08 +01:00
|
|
|
logging.info("executing command '{}'".format(cmd))
|
2016-12-09 19:29:16 +01:00
|
|
|
args = shlex.split(cmd)
|
|
|
|
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2017-04-02 08:31:23 +02:00
|
|
|
rv = None
|
2016-12-09 19:29:16 +01:00
|
|
|
|
|
|
|
if wait:
|
|
|
|
out, _ = proc.communicate()
|
|
|
|
if proc.returncode != 0:
|
|
|
|
raise RuntimeError("{} exited with {}".format(cmd, proc.returncode))
|
2017-04-26 07:51:11 +02:00
|
|
|
|
|
|
|
if hasattr(out, "decode"):
|
2019-09-25 19:00:08 +02:00
|
|
|
rv = out.decode("utf-8", "ignore")
|
2017-04-26 07:51:11 +02:00
|
|
|
else:
|
|
|
|
rv = out
|
|
|
|
|
2019-01-26 19:40:08 +01:00
|
|
|
logging.info(u"command returned '{}'".format("" if not rv else rv))
|
2017-04-02 08:31:23 +02:00
|
|
|
return rv
|
2016-12-09 19:29:16 +01:00
|
|
|
|
2016-12-10 18:21:01 +01:00
|
|
|
def bytefmt(num):
|
2017-10-13 17:06:18 +02:00
|
|
|
for unit in ["", "Ki", "Mi", "Gi"]:
|
2016-12-10 18:21:01 +01:00
|
|
|
if num < 1024.0:
|
|
|
|
return "{:.2f}{}B".format(num, unit)
|
|
|
|
num /= 1024.0
|
2016-12-17 09:00:49 +01:00
|
|
|
return "{:.2f}GiB".format(num*1024.0)
|
2016-12-10 18:21:01 +01:00
|
|
|
|
2017-07-26 16:41:30 +02:00
|
|
|
def durationfmt(duration, shorten=False, suffix=False):
|
|
|
|
duration = int(duration)
|
2016-12-10 07:47:24 +01:00
|
|
|
minutes, seconds = divmod(duration, 60)
|
|
|
|
hours, minutes = divmod(minutes, 60)
|
2017-07-26 16:41:30 +02:00
|
|
|
suf = "m"
|
2016-12-10 07:47:24 +01:00
|
|
|
res = "{:02d}:{:02d}".format(minutes, seconds)
|
2017-07-26 16:41:30 +02:00
|
|
|
if hours > 0:
|
|
|
|
if shorten:
|
|
|
|
res = "{:02d}:{:02d}".format(hours, minutes)
|
|
|
|
else:
|
|
|
|
res = "{:02d}:{}".format(hours, res)
|
|
|
|
suf = "h"
|
2016-12-10 07:47:24 +01:00
|
|
|
|
2017-07-26 16:41:30 +02:00
|
|
|
return "{}{}".format(res, suf if suffix else "")
|
2016-12-10 07:47:24 +01:00
|
|
|
|
2018-08-24 10:22:28 +02:00
|
|
|
def which(program):
|
|
|
|
import os
|
|
|
|
def is_exe(fpath):
|
|
|
|
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
|
|
|
|
|
|
|
|
fpath, fname = os.path.split(program)
|
|
|
|
if fpath:
|
|
|
|
if is_exe(program):
|
|
|
|
return program
|
|
|
|
else:
|
2018-08-24 10:44:42 +02:00
|
|
|
localPATH = os.environ["PATH"].split(os.pathsep)
|
|
|
|
localPATH += ["/sbin", "/usr/sbin/", "/usr/local/sbin"]
|
|
|
|
for path in localPATH:
|
2018-08-24 10:22:28 +02:00
|
|
|
exe_file = os.path.join(path, program)
|
|
|
|
if is_exe(exe_file):
|
|
|
|
return exe_file
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2016-12-09 19:29:16 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|