from subprocess import call
[docs]def truthy(val):
if isinstance(val, basestring):
if val.lower() == 'true':
return True
return False
return val
[docs]class BaseDriver(object):
def __init__(self, config):
self.config = config
[docs] def use_sudo(self, cmd):
use_sudo = truthy(self.config.get('use_sudo'))
if use_sudo:
return ['sudo'] + [cmd]
return cmd
[docs] def working_dir(self, cmd):
command = ' '.join(cmd)
working_dir = self.config.get('directory')
if working_dir:
command = 'cd %s && %s' % (working_dir, command)
return command
[docs] def do(self, cmd):
cmd = self.command(cmd)
call(cmd)
[docs] def command(self):
raise NotImplementedError()