~ubuntu-branches/ubuntu/natty/powernap/natty-proposed

1.1.10 by Andres Rodriguez
Import upstream version 1.12
1
#!/usr/bin/env python
2
#
3
# Script to migrate a v1.7 configuration file to a v1.xx configuration
4
# file
5
6
import os, sys
7
8
# Parse command line
9
if len(sys.argv) != 4:
10
  print 'Usage :- %s old_config old_action new_config' % sys.argv[0]
11
old_cnf_path = sys.argv[1]
12
old_act_path = sys.argv[2]
13
new_cnf_path = sys.argv[3]
14
15
# Load OLD config
16
try:
17
  execfile(old_cnf_path)
18
except Exception, e:
19
  print 'ERROR: failed to load old configuration [e=%s]' % e
20
  sys.exit(1)
21
22
# Process new config file
23
lines = []
24
try:
25
  fp = open(new_cnf_path, 'r')
26
  for l in fp.readlines():
27
    l = l.strip()
28
29
    # Ignore blank, comment and section line
30
    if not l or l[0] in [ '#', '[' ]:
31
      lines.append(l)
32
   
33
    # Interval
34
    elif l.startswith('interval'):
35
      lines.append('interval = %0.2f' % INTERVAL_SECONDS)
36
37
    # Absent
38
    elif l.startswith('absent'):
39
      lines.append('absent = %0.2f' % ABSENT_SECONDS)
40
 
41
    # Grace
42
    elif l.startswith('grace'):
43
      lines.append('grace = %0.2f' % GRACE_SECONDS)
44
45
    # Debug
46
    elif l.startswith('debug'):
47
      lines.append('debug = %d' % DEBUG)
48
  
49
    # Action
50
    elif l.startswith('action_enter_sleep'):
51
      if os.access(old_act_path, os.X_OK):
52
        lines.append("action_enter_sleep = '%s'" % os.path.abspath(old_act_path))
53
      else: lines.append(l)
54
55
    # Monitors
56
    elif l.startswith('monitors'):
57
      monitors = "monitors = [ \"InputMonitor()\""
58
      for m in MONITORED_PROCESSES:
59
        monitors += ", \"ProcessMonitor({'regex':'%s'})\"" % m
60
      monitors += " ]"
61
      lines.append(monitors)
62
63
    # All else
64
    else: lines.append(l)
65
  fp.close()
66
67
  # Store data
68
  fp = open(new_cnf_path, 'w')
69
  for l in lines:
70
    fp.write(l + '\n')
71
  fp.close()
72
except Exception, e:
73
  print 'ERROR: failed to update configuration [e=%s]' % e
74
  sys.exit(1)
75
76
# Done
77
print 'Configuration successfully updated'