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

import locale
from dialog import Dialog
import glob
import os
import sys
import subprocess as sp
import tomb
import traceback

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

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 Exception("[" + moduleName + "] " + str(e))

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
    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 = []
        try:
            if "all" in tags:
                for module in moduleList:
                    runlist.append(prepareModule(module[0]))
            else:
                for tag in tags:
                    runlist.append(prepareModule(tag))

            sp.call('clear', shell=True)

            for module in runlist:
                print("Running " + module.name + "...")
                try:
                    module.run()
                except Exception as e:
                    print("Exception raised while running " + module.name + ": " + str(e))
                    traceback.print_exc()

            code, tag = d.menu("All modules finished execution",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)


        except Exception as e:
            print("Exception raised while preparing module: " + str(e))
            traceback.print_exc()