-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·182 lines (122 loc) · 4.64 KB
/
run.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
import __builtin__
import sys
import os.path
import argparse
import logging
__builtin__.log = logging.getLogger(__name__)
from pydaemon import Daemon
from libs.libs import Library
from libs.dbhandler import DBHandler
pidfile = '/var/run/quakealyzer.pid'
class Runner(Daemon):
lib = Library()
db_handler = DBHandler()
engine = None
def database(self, database):
self.db_connection = self.db_handler.connect(database)
def create_database(self, create_database):
self.database(create_database)
self.db_handler.db_create_models()
def database_help(self):
self.db_handler.db_help()
@property
def mod_list(self):
return self.lib.mod_list
@property
def get_db_connection(self):
return self.db_connection
def set_mod(self, mod):
return self.lib.set_mod(mod)
def set_parsing_file(self, log_file):
# transfer engine
self.lib.set_engine(self.db_handler.get_engine())
self.log_file = log_file
def run_file_import(self):
self.lib.start_import(self.log_file)
def run_parser(self):
# transfer engine
self.lib.set_engine(self.db_handler.get_engine())
# start realtime logfile parsing as Daemon
self.lib.start_parsing(self.log_file)
def run(self):
self.run_parser()
if __name__ == "__main__":
runner = Runner(pidfile)
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
parser.add_argument("startparam", choices=['start', 'stop'], default='start', help="handles the run of Quakealyzer")
group.add_argument("--databasehelp", help="gives Information about the structure of a Database-Connect", action="store_true")
group.add_argument("--listmods", help="lists all avaliable Game Mods", action="store_true")
group.add_argument("--file", help="the File to Parse")
group.add_argument("--fileimport", help="(ONLY FOR TESTS, -> incorrect start timestamps) imports a complete File")
parser.add_argument("--mod", default="baseq3", help="set the Mod which writes the Logfile")
group.add_argument("--createdatabase", help="creates Databasemodel (empty Database has to exist)")
parser.add_argument("--database", help="connects to Database (Models have to be created)")
parser.add_argument("--nodaemon", choices=['yes','no'], default='no', help="force process not to run as daemon")
parser.add_argument("--logfile", default='/var/log/quakealyzer.log', help="the Logfile for write Processlogs in")
parser.add_argument("--loglevel", default='ERROR', choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'], help="the Loglevel to write")
args = parser.parse_args()
if args.startparam == 'stop':
runner.stop()
sys.exit(0)
loglevel = args.loglevel
if loglevel == 'DEBUG':
logformat="%(asctime)s [%(levelname)-8s] - File: %(filename)s - Function: %(funcName)s - %(message)s"
else:
logformat="%(asctime)s [%(levelname)-8s] %(message)s"
# set Logfile and Loglevel
log.setLevel(loglevel)
logging.basicConfig(
filename=args.logfile,
level=loglevel,
format=logformat,
datefmt="%Y.%m.%d %H:%M:%S"
)
log.info("Startparameter: " + str(args))
if args.database:
runner.database(args.database)
if args.createdatabase:
runner.create_database(args.createdatabase)
sys.exit(0)
if args.databasehelp:
runner.database_help()
sys.exit(0)
if args.listmods:
print runner.mod_list
sys.exit(0)
# is mod existing? then try to set
if not args.mod:
log.error("missing mod Argument")
sys.exit(1)
elif not runner.set_mod(args.mod):
log.error("cannot find mod")
print "Avaliable Mods:"
print runner.mod_list
sys.exit(1)
if not runner.get_db_connection:
log.error("Missing Database Connection!")
sys.exit(1)
# can logfile open?
if not args.file or not os.path.isfile(args.file):
if not args.fileimport or not os.path.isfile(args.fileimport):
log.error("cannot open File")
sys.exit(1)
else:
parse_file = args.fileimport
else:
parse_file = args.file
runner.set_parsing_file(parse_file)
# import logfile content instead of running parser
if args.fileimport:
runner.run_file_import()
sys.exit(0)
# run in nodaemon mode
if args.nodaemon == 'yes':
runner.run()
sys.exit(0)
#start as daemon
if args.startparam == 'start':
runner.start()
sys.exit(0)
sys.exit(0)