Last active
August 29, 2015 13:56
-
-
Save corpix/9194343 to your computer and use it in GitHub Desktop.
SImple wrapper around sudo on Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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