misc: add sudo function (wip)
This commit is contained in:
parent
cc8f8b09a1
commit
ea9f64b1b9
|
@ -1,9 +1,9 @@
|
||||||
import grp, hashlib, os, platform, random, signal, socket, statistics, string, time, timeit
|
import grp, hashlib, os, platform, random, shlex, signal, socket, statistics, string, time, timeit
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from getpass import getpass, getuser
|
from getpass import getpass, getuser
|
||||||
from importlib import util
|
from importlib import util
|
||||||
from pathlib import Path
|
from subprocess import Popen, PIPE
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from . import izzylog
|
from . import izzylog
|
||||||
|
@ -26,6 +26,7 @@ __all__ = [
|
||||||
'random_gen',
|
'random_gen',
|
||||||
'remove',
|
'remove',
|
||||||
'signal_handler',
|
'signal_handler',
|
||||||
|
'sudo',
|
||||||
'time_function',
|
'time_function',
|
||||||
'time_function_pprint',
|
'time_function_pprint',
|
||||||
'timestamp',
|
'timestamp',
|
||||||
|
@ -221,7 +222,7 @@ def nfs_check(path):
|
||||||
return
|
return
|
||||||
|
|
||||||
proc = Path('/proc/mounts')
|
proc = Path('/proc/mounts')
|
||||||
path = Path(path).resolve
|
path = Path(path).resolve()
|
||||||
|
|
||||||
if not proc.exists:
|
if not proc.exists:
|
||||||
return True
|
return True
|
||||||
|
@ -284,7 +285,7 @@ def prompt(prompt, default=None, valtype=str, options=[], password=False):
|
||||||
Arguments:
|
Arguments:
|
||||||
prompt (str): The string to display to the user
|
prompt (str): The string to display to the user
|
||||||
default (various): The value that should be returned if there is no user input
|
default (various): The value that should be returned if there is no user input
|
||||||
valtype (str): The type the value should be returned as
|
valtype (function): The type the value should be returned as
|
||||||
options (list(str)): If set, these are the only values the user can select
|
options (list(str)): If set, these are the only values the user can select
|
||||||
password (bool): If set to True, the input will be treated like a password and not show the user's input on screen
|
password (bool): If set to True, the input will be treated like a password and not show the user's input on screen
|
||||||
|
|
||||||
|
@ -376,6 +377,22 @@ def signal_handler(func=None, *args, original_args=False, **kwargs):
|
||||||
signal.signal(signal.SIGTERM, handler)
|
signal.signal(signal.SIGTERM, handler)
|
||||||
|
|
||||||
|
|
||||||
|
# This currently uses os.system instead of subprocess.Popen which I don't want
|
||||||
|
def sudo(command, argstring, password=None):
|
||||||
|
if command.startswith('/'):
|
||||||
|
assert Path(command).isfile
|
||||||
|
|
||||||
|
while not password:
|
||||||
|
password = prompt(f'[PySudo] password for {getuser()}', password=True)
|
||||||
|
|
||||||
|
#proc = Popen(['sudo', command, *shlex.split(argstring), *args], stdin=PIPE)
|
||||||
|
#proc.communicate(bytes(password + '\n', 'utf-8'))
|
||||||
|
|
||||||
|
#return proc
|
||||||
|
|
||||||
|
return os.system(f'echo {password} | sudo -S -- {command} {argstring}')
|
||||||
|
|
||||||
|
|
||||||
def time_function(func, *args, passes=1, use_gc=True, **kwargs):
|
def time_function(func, *args, passes=1, use_gc=True, **kwargs):
|
||||||
'''Run a function and return the time it took
|
'''Run a function and return the time it took
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue