Page MenuHomePhabricator
Paste P2370

cmd.py
ActivePublic

Authored by mmodell on Dec 3 2015, 5:05 PM.
# -*- coding: utf-8 -*-
"""
scap.env
~~~~~~~~~~~
command execution
"""
import scap.config as config
class Command(object):
def __init__(self, *args):
self.cmd = args
def __call__(self, *args, **kwargs):
return " ".join(self.args)
class arg(Command):
def __init__(self, name, cmd):
self.name = name
self.cmd = cmd
def required(self, required=True):
self._required = required
return self
def __call__(self, **values):
if self.name in values and values[self.name]:
val = values[self.name]
return self.cmd.format(val)
else:
return ''
class ComposableCommand(Command):
def __init__(self, *cmds):
self.cmds = cmds
def __call__(self, *args, **values):
result = []
parts = self.cmds + args
for cmd in parts:
rendered = cmd
if callable(cmd):
rendered = cmd(**values)
if rendered:
if type(rendered) is str:
result.append(rendered)
else:
result.extend(rendered)
return result
class SubCommand(ComposableCommand):
pass
env = config.Environment()
SSH_USER = arg('user', '-oUser={}')
SUDO_USER = arg('user', '-u {}')
SSH = ComposableCommand(env['ssh_command'], SSH_USER)
SUDO = ComposableCommand('sudo', SUDO_USER, '-n', '--')