From c7a75ffec65c1feb7872d1fc5e61b1bc97184293 Mon Sep 17 00:00:00 2001 From: "pavel.muhortov" Date: Sun, 8 Jan 2023 16:19:45 +0300 Subject: [PATCH] update template --- script.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 script.py diff --git a/script.py b/script.py new file mode 100644 index 0000000..c9bfc6d --- /dev/null +++ b/script.py @@ -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!")