-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathobserver.py
More file actions
81 lines (55 loc) · 2.16 KB
/
observer.py
File metadata and controls
81 lines (55 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from typing import List, Dict
import logging
from pe.r2helper import r2_disas
from config import config
logger = logging.getLogger("Observer")
class Observer():
"""Central class to store all logs and files created during the build process"""
def __init__(self):
self.cmd_output = [] # output of external programs (cmdoutput.log)
self.logs: List[str] = [] # internal log messages (supermega.log)
self.files = [] # content of generated files
self.active = True
def reset(self):
self.cmd_output = []
self.logs = []
self.files = []
self.idx = 0
def add_cmd_output(self, cmd_output):
self.cmd_output.append(cmd_output)
def get_cmd_output(self):
return self.cmd_output
def add_log(self, log: str):
self.logs.append(log)
def get_logs(self):
return self.logs
def add_text_file(self, name, data):
self.files.append((name + ".txt", data))
def add_code_file(self, name, data: bytes):
if not config.has_r2:
return
ret = r2_disas(data)
self.files.append((name + ".disas.ascii", ret['color']))
#self.write_to_file(name + ".disas.txt", ret['text'])
#self.write_to_file(name + ".disas.ascii", ret['color'])
#self.write_to_file(name + ".hex", ret['hexdump'])
#self.write_to_file_bin(name + ".bin", data)
def write_logs(self, working_dir: str):
# Our log output
with open(f"{working_dir}log-supermega.log", "w") as f:
for line in observer.get_logs():
try:
f.write(line + "\n")
except Exception as e:
logger.warning("Error: {}".format(e))
# Stdout of executed commands
with open(f"{working_dir}log-cmdoutput.log", "w") as f:
for line in observer.get_cmd_output():
f.write(line)
# Write all files
idx = 0
for name, data in observer.files:
with open(f"{working_dir}log-{idx}-{name}", "w") as f:
f.write(data)
idx += 1
observer = Observer()