sau/sau/helpers.py

47 lines
1.3 KiB
Python

import logging
import os
import subprocess
import time
import sau
def exec_cmd(cmd, timeout=900, env = None):
my_env = os.environ.copy()
if env:
my_env.update(env)
log = logging.getLogger(sau.LOGNAME)
log.debug('Executing "{}"'.format(' '.join(cmd)))
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env = my_env)
out = b""
err = b""
try:
out, err = proc.communicate(timeout=timeout)
except subprocess.TimeoutExpired as ex:
log.error('Command "{}" timed out, killing it.'.format(' '.join(cmd)))
proc.kill()
time.sleep(30)
if proc.poll() != None:
log.error('Command "{}" would not be killed, forcing a termination'.format(' '.join(cmd)))
proc.terminate()
time.sleep(5)
return (proc.returncode, out.decode('utf-8'), err.decode('utf-8'))
def version_diff(new, old):
""" This will return the number of common fields in the version scheme,
counted from the left. """
new_list = list(new.split('.'))
old_list = list(old.split('.'))
common = min(len(new_list), len(old_list))
for i in range(0, common):
if new_list[i] == old_list[i]:
continue
return i
return common