Skip to content

Instantly share code, notes, and snippets.

@corpix
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corpix/9194343 to your computer and use it in GitHub Desktop.
Save corpix/9194343 to your computer and use it in GitHub Desktop.
SImple wrapper around sudo on Python
from subprocess import call
import types
def sudo(cmd):
cmd = 'sudo %s' % cmd
return call(cmd, shell=True)
class SudoExecutor(type):
def __getattr__(self, name):
def execute(self, params=[]):
exec_name = name
safe = True
if name.startswith('must_'):
exec_name = name.replace('must_', '', 1)
safe = False
cmd = '%s %s' % (exec_name, ' '.join(params))
res = sudo(cmd) is 0
if not safe and not res:
raise RuntimeError('Non zero exit code while `%s`' % cmd)
return res
meth = types.MethodType(execute, self, SudoExecutor)
return meth
class Sudo:
__metaclass__ = SudoExecutor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment