Last active
January 6, 2024 04:52
-
-
Save barneygale/8ff070659178135b10b5e202a1ecaa3f to your computer and use it in GitHub Desktop.
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
import sys, marshal, functools, subprocess | |
child_script = """ | |
import marshal, sys, types; | |
fn, args, kwargs = marshal.load(sys.stdin) | |
marshal.dump( | |
types.FunctionType(fn, globals())(*args, **kwargs), | |
sys.stdout) | |
""" | |
def sudo(fn): | |
@functools.wraps(fn) | |
def inner(*args, **kwargs): | |
proc_args = [ | |
"sudo", | |
sys.executable, | |
"-c", | |
child_script] | |
proc = subprocess.Popen( | |
proc_args, | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE) | |
send_data = marshal.dumps(( | |
fn.func_code, | |
args, | |
kwargs)) | |
recv_data = proc.communicate(send_data)[0] | |
return marshal.loads(recv_data) | |
return inner | |
@sudo | |
def whoami(): | |
import os | |
return "I am: %d" % os.getuid() | |
if __name__ == '__main__': | |
print whoami() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful, I took @mohsenBanan code to add support to all kind of objects by using the pickle library.
The only drawback is that you should include the import or definition of what you want inside the function's definition.