digger.py 4.23 KB
#! /usr/bin/env python3

import sys

sys.path.append('modules')
sys.path.append('vars')
sys.path.append('logger')

import locale
from dialog import Dialog
import glob
import os
import subprocess as sp
import tomb
import traceback
import logger
from consolelogger import ConsoleLogger
from filelogger import FileLogger

def getModules():
    choiceList = []
    modules = []
    for filename in glob.glob('./modules/*.py'):
        modules.append(os.path.splitext(os.path.basename(filename))[0])
    modules.remove('__init__')

    for modname in modules:
        try:
            module = __import__(modname)
            instance = module.getInstance()
            choiceList.append((instance.name,instance.description,False))
            del instance
            del module
            sys.modules.pop(modname)
        except Exception as e:
            print("Exception raised while importing " + modname)
    return choiceList

def prepareModule(moduleName):
    try:
        module = __import__(moduleName).getInstance()
        module.getVars()
        return module
    except Exception as e:
        raise

def showFinishDialog(allSuccessful):
    msg = ""
    if(allSuccessful):
        msg = "All modules finished execution"
    else:
        msg = "Some or all modules failed execution. Please check the logs."

    code, tag = d.menu(msg, choices=[("Poweroff", "Shutdown the computer"),
                                     ("Reboot", "Reboot the computer"),
                                     ("Restart", "Run Gravedigger again")])
    if (code == d.OK):
        if tag == "Poweroff":
            sp.call("poweroff", shell=True)
        elif tag == "Reboot":
            sp.call("reboot", shell=True)

    exit(0)

def showContinueDialog(d,msg):
    ans = d.yesno(msg + "\n\nDo you want to continue execution without the module?")
    return ans == d.OK

if __name__ == "__main__":
    locale.setlocale(locale.LC_ALL, '')
    d = Dialog(dialog="dialog",autowidgetsize=True)
    d.set_background_title("Gravedigger")

    moduleList = getModules()
    code,value = d.inputbox("Input computer's name")
    if code == d.OK:
        tomb._MACHINE_NAME= value
        logger.logSystems.append(ConsoleLogger())
        logger.logSystems.append(FileLogger(tomb.getPath() + "log.txt"))
        d.set_background_title("Gravedigger - " + value)
        code, tags = d.checklist("Select modules to execute",
                             choices=moduleList + [("all","Execute all modules",False)],
                             title="Module selection")
        if code == d.OK:
            runlist = []
            if "all" in tags:
                for module in moduleList:
                    try:
                        mod = prepareModule(module[0])
                        runlist.append(mod)
                    except Exception as e:
                        msg = "Exception raised while preparing module \"" + module[0] + "\": " + str(e)
                        logger.msgLog(msg, module[0], logger.TYPE_ERROR)
                        #logger.msgLog(traceback.format_exc(), module[0],  logger.TYPE_ERROR)
                        if(not showContinueDialog(d,msg)):
                            showFinishDialog(False)
            else:
                for tag in tags:
                    try:
                        mod = prepareModule(tag)
                        runlist.append(mod)
                    except Exception as e:
                        msg = "Exception raised while preparing module \"" + tag + "\": " + str(e)
                        logger.msgLog(msg,tag,logger.TYPE_ERROR)
                        #logger.msgLog(traceback.format_exc(), tag logger.TYPE_ERROR)
                        if(not showContinueDialog(d,msg)):
                            showFinishDialog(False)

            sp.call('clear', shell=True)
            for module in runlist:
                logger.msgLog("Running " + module.name + "...", "digger", logger.TYPE_INFO)
                try:
                    module.run()
                except Exception as e:
                    logger.msgLog("Exception raised while running \"" + module.name + "\": " + str(e), module.name, logger.TYPE_ERROR)
                    #logger.msgLog(traceback.format_exc(), module.name, logger.TYPE_ERROR)


            showFinishDialog(True)