-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.py
78 lines (65 loc) · 1.54 KB
/
daemon.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
import os
import sys
import time
import ctypes
import ieInit
ieInit.init()
import settingsManager
globalSettings = settingsManager.globalSettings()
errCount = 0
def init():
global errCount
errCount = 0
def getCommands():
try:
f = open('../test/commands.txt')
commands = filter(None, (line.rstrip() for line in f))
f.close()
return commands
except:
print >> sys.stderr, 'Error reading from file.'
def getCommandInfo(command):
cmd = ' '.join(command.strip().split()[1:])
compTypes = command.split()[0]
compTypes = compTypes.split(',')
return compTypes, cmd, command
def update():
commands = getCommands()
commands = [getCommandInfo(c) for c in commands]
for compTypes, cmd, command in commands:
if globalSettings.COMPUTER_TYPE in compTypes:
try:
print 'Executing %s.' % cmd
err = os.system(cmd)
if (not err):
clear(command)
else:
error(cmd, err)
except:
error(cmd, ctypes.get_errno())
else:
clear(command)
def clear(command):
f=open('../test/commands.txt')
lines = f.readlines()
f.close()
f = open('../test/commands.txt', 'w')
for line in lines:
if (line.strip() and normalizeLine(line) != normalizeLine(command)):
f.write(line)
f.close()
def normalizeLine(line):
if (line[-1] == '\n'):
return line[:-1]
return line
def error(cmd, errno):
global errCount
errCount += 1
print >> sys.stderr, 'Error on %s. Error number %d. Number of errors is: %d.' % (cmd, errno, errCount)
def runDaemon():
init()
while (1):
update()
time.sleep(15)
if __name__ == '__main__':
runDaemon()