-
Notifications
You must be signed in to change notification settings - Fork 7
/
config_migrate.py
executable file
·77 lines (63 loc) · 1.8 KB
/
config_migrate.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
#!/usr/bin/env python
#
# Script to migrate a v1.7 configuration file to a v1.xx configuration
# file
import os, sys
# Parse command line
if len(sys.argv) != 4:
print 'Usage :- %s old_config old_action new_config' % sys.argv[0]
old_cnf_path = sys.argv[1]
old_act_path = sys.argv[2]
new_cnf_path = sys.argv[3]
# Load OLD config
try:
execfile(old_cnf_path)
except Exception, e:
print 'ERROR: failed to load old configuration [e=%s]' % e
sys.exit(1)
# Process new config file
lines = []
try:
fp = open(new_cnf_path, 'r')
for l in fp.readlines():
l = l.strip()
# Ignore blank, comment and section line
if not l or l[0] in [ '#', '[' ]:
lines.append(l)
# Interval
elif l.startswith('interval'):
lines.append('interval = %0.2f' % INTERVAL_SECONDS)
# Absent
elif l.startswith('absent'):
lines.append('absent = %0.2f' % ABSENT_SECONDS)
# Grace
elif l.startswith('grace'):
lines.append('grace = %0.2f' % GRACE_SECONDS)
# Debug
elif l.startswith('debug'):
lines.append('debug = %d' % DEBUG)
# Action
elif l.startswith('action_enter_sleep'):
if os.access(old_act_path, os.X_OK):
lines.append("action_enter_sleep = '%s'" % os.path.abspath(old_act_path))
else: lines.append(l)
# Monitors
elif l.startswith('monitors'):
monitors = "monitors = [ \"InputMonitor()\""
for m in MONITORED_PROCESSES:
monitors += ", \"ProcessMonitor({'regex':'%s'})\"" % m
monitors += " ]"
lines.append(monitors)
# All else
else: lines.append(l)
fp.close()
# Store data
fp = open(new_cnf_path, 'w')
for l in lines:
fp.write(l + '\n')
fp.close()
except Exception, e:
print 'ERROR: failed to update configuration [e=%s]' % e
sys.exit(1)
# Done
print 'Configuration successfully updated'