Blame view

digger.py 6.05 KB
1
2
#! /usr/bin/env python3
3
4
5
6
7
8
import sys

sys.path.append('modules')
sys.path.append('vars')
sys.path.append('logger')
9
10
11
12
13
import locale
from dialog import Dialog
import glob
import os
import subprocess as sp
Imanol-Mikel Barba Sabariego authored
14
import tomb
15
import traceback
16
17
18
import logger
from consolelogger import ConsoleLogger
from filelogger import FileLogger
19
from hashlib import sha256
20
21
22
23
24
25
26
27
28
29
30

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)
31
            instance = module.getInstance()
32
33
34
35
36
37
38
39
40
            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):
41
42
43
44
45
    try:
        module = __import__(moduleName).getInstance()
        module.getVars()
        return module
    except Exception as e:
46
47
        raise
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def sha256sum(file):
    f = open(file, 'rb')
    result = sha256(f.read()).hexdigest()
    f.close()
    return result

def bagAndTag():
    tombPath = tomb.getPath()
    hashes = open(tombPath + "hashes.txt",'a+')

    for root, dirnames, filenames in os.walk(tombPath):
        for dir in dirnames:
            print(dir)
            for subroot, subdirnames, subfilenames in os.walk(tombPath + dir):
                print(subfilenames)
                for filename in subfilenames:
                    hashes.write(sha256sum(subroot + "/" + filename) + " " + subroot + "/" + filename + "\n")

def finish(allSuccessful):
    bagAndTag()
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    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)
83
    logger.msgLog("Finished excavation of tomb: " + tomb.getPath(),"digger",logger.TYPE_INFO)
84
85
86
    exit(0)

def showContinueDialog(d,msg):
87
88
89
90
91
92
    code, tag = d.menu(msg + "\n\nDo you want to retry module configuration or continue execution without the module?",
                       choices=[("retry", "Retry configuration"),
                                ("continue", "Continue without module")])
    if code != d.OK:
        return "abort"
    return tag
93
94
95
96
97
98
99
100

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")
Imanol-Mikel Barba Sabariego authored
101
    if code == d.OK:
102
103
104
        tomb._MACHINE_NAME= value
        logger.logSystems.append(ConsoleLogger())
        logger.logSystems.append(FileLogger(tomb.getPath() + "log.txt"))
105
        logger.msgLog("Beginning excavation of tomb: " + tomb.getPath(),"digger",logger.TYPE_INFO)
106
107
108
109
110
111
        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 = []
Imanol-Mikel Barba Sabariego authored
112
            if "all" in tags:
113
                logger.msgLog("Selected modules: " + repr(moduleList), "digger", logger.TYPE_INFO)
Imanol-Mikel Barba Sabariego authored
114
                for module in moduleList:
115
116
117
118
119
120
121
122
123
124
125
                    while True:
                        try:
                            mod = prepareModule(module[0])
                            runlist.append(mod)
                            break
                        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)
                            ans = showContinueDialog(d,msg)
                            if ans == "abort":
126
                                finish(False)
127
                            elif ans == "continue":
Imanol-Mikel Barba Sabariego authored
128
                                logger.msgLog("Skipping module " + module[0], "digger", logger.TYPE_INFO)
129
                                break
Imanol-Mikel Barba Sabariego authored
130
            else:
131
                logger.msgLog("Selected modules: " + repr(tags), "digger", logger.TYPE_INFO)
Imanol-Mikel Barba Sabariego authored
132
                for tag in tags:
133
134
135
136
137
138
139
140
141
142
143
                    while True:
                        try:
                            mod = prepareModule(tag)
                            runlist.append(mod)
                            break
                        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)
                            ans = showContinueDialog(d, msg)
                            if ans == "abort":
144
                                finish(False)
145
146
147
                            elif ans == "continue":
                                logger.msgLog("Skipping module " + tag, "digger", logger.TYPE_INFO)
                                break
148
Imanol-Mikel Barba Sabariego authored
149
150
            sp.call('clear', shell=True)
            for module in runlist:
151
                logger.msgLog("Running " + module.name + "...", "digger", logger.TYPE_INFO)
Imanol-Mikel Barba Sabariego authored
152
153
154
                try:
                    module.run()
                except Exception as e:
155
156
                    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)
157
Imanol-Mikel Barba Sabariego authored
158
159
            finish(True)