update template
This commit is contained in:
parent
0a36399509
commit
c7a75ffec6
107
script.py
Normal file
107
script.py
Normal file
|
@ -0,0 +1,107 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
from os import path, chdir
|
||||
from sys import platform
|
||||
from getpass import getuser
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
|
||||
def addtolog(message: str) -> None:
|
||||
"""
|
||||
Simple logger
|
||||
:param message: string for print to stdout and write to log
|
||||
:return: None
|
||||
"""
|
||||
print(message)
|
||||
if conf['logs']:
|
||||
with open(conf['logs'], 'a') as file:
|
||||
file.write(datetime.now().strftime("%Y.%m.%d-%H:%M:%S") + " " + message + "\n")
|
||||
|
||||
def execpause() -> None:
|
||||
"""
|
||||
Simple pause
|
||||
:return: None
|
||||
"""
|
||||
input("Press [ENTER] to continue...")
|
||||
|
||||
def execquite(code: int) -> None:
|
||||
"""
|
||||
Exit handler
|
||||
:param code: integer to exitcode
|
||||
:return: None
|
||||
"""
|
||||
if args['show'] != 'qn':
|
||||
execpause()
|
||||
addtolog("execution time is " + str((datetime.now() - start).seconds) + " seconds, exit")
|
||||
exit(code)
|
||||
|
||||
def execerror(message: str) -> None:
|
||||
"""
|
||||
Error handler
|
||||
:param message: string for print to stdout and write to log
|
||||
:return: None
|
||||
"""
|
||||
addtolog("error: " + message)
|
||||
execquite(1)
|
||||
|
||||
def getconfig(config: str) -> dict:
|
||||
"""
|
||||
Simple config reader
|
||||
:param config: path to configuration file
|
||||
:return: dictionary as "key":"value"
|
||||
"""
|
||||
dictionary = {}
|
||||
dictionary['logs'] = None
|
||||
if path.exists(config):
|
||||
with open(config) as file:
|
||||
raw = file.read()
|
||||
for line in raw.splitlines():
|
||||
if "logs=" in line:
|
||||
dictionary['logs'] = line.split('=')[1].strip()
|
||||
return dictionary
|
||||
|
||||
def checkroot() -> bool:
|
||||
"""
|
||||
Crossplatform privileged rights checker
|
||||
:return: "true" or "false"
|
||||
"""
|
||||
if platform.startswith('linux') or platform.startswith('darwin'):
|
||||
from os import geteuid
|
||||
if geteuid() != 0:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
elif platform.startswith('win32'):
|
||||
import ctypes
|
||||
return ctypes.windll.shell32.IsUserAnAdmin()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from argparse import ArgumentParser
|
||||
|
||||
args = ArgumentParser(
|
||||
prog='script',
|
||||
description='Checking privileged rights',
|
||||
epilog='Dependencies: '
|
||||
'Python 3 (tested version 3.9.5)'
|
||||
)
|
||||
args.add_argument('-s', '--show', type=str, default=None, required=False,
|
||||
help='qn - execution without pauses')
|
||||
args.add_argument('-c', '--conf', type=str, default=None, required=False,
|
||||
help='path to configuration file')
|
||||
args = vars(args.parse_args())
|
||||
if not args['conf']:
|
||||
conf = path.abspath(__file__).replace('.py', '.conf')
|
||||
else:
|
||||
conf = args['conf']
|
||||
conf = getconfig(conf)
|
||||
|
||||
start = datetime.now()
|
||||
chdir(path.split(path.abspath(__file__))[0])
|
||||
if checkroot():
|
||||
print('Running as', getuser())
|
||||
execquite(0)
|
||||
else:
|
||||
execerror("Restart this as root!")
|
Loading…
Reference in New Issue
Block a user