+ monitor.py -- modified the following three to use a record-based events,
[monitor.git] / config.py
1 #!/usr/bin/python
2 import pickle
3 import os
4 import getopt
5 import sys
6 import __main__
7
8 class config:
9         debug = True
10         mail = False
11         bcc  = True
12         email = "soltesz@cs.utk.edu"
13         userlist = None
14         cachert = True
15         cachenodes = True
16         cachesites = True
17         squeeze = False
18         policysavedb = True
19         __file = ".config"
20
21         def __init__(self):
22                  if os.path.exists(self.__file): # file exists, read that.
23                         f = open(self.__file, 'r')
24                         o = pickle.load(f)
25                         self.__dict__.update(o)
26                         f.close()
27
28         def getListFromFile(self, file):
29                 f = open(file, 'r')
30                 list = []
31                 for line in f:
32                         line = line.strip()
33                         list += [line]
34                 return list
35                 
36         def save(self):
37                 f = open(self.__file, 'w')
38                 o = {'debug': self.debug, 
39                          'mail': self.mail, 
40                          'bcc': self.bcc, 
41                          'email':self.email,
42                          'userlist': self.userlist,
43                          'cachert': self.cachert, 
44                          'cachenodes' : self.cachenodes, 
45                          'cachesites': self.cachesites,
46                          'squeeze':self.squeeze,
47                          'policysavedb':self.policysavedb}
48                 pickle.dump(o, f)
49                 f.close()
50
51
52 def usage():
53         config = __main__.config()
54      #   --cachesites=[0|1]      Cache Sites from PLC (current: %s)
55      #   --status                Print memory usage statistics and exit
56         print """
57 Settings:
58         --debug=[0|1]           Set debugging        (current: %s)
59         --mail=[0|1]            Send mail or not     (current: %s)
60         --bcc=[0|1]             Include bcc of user  (current: %s)
61         --email=[email]         Email to use above   (current: %s)
62         --userlist=[filename]   Use a list of nodes  (current: %s)
63         --cachert=[0|1]         Cache the RT db      (current: %s)
64         --cachenodes=[0|1]      Cache Nodes from PLC (current: %s)
65         --squeeze=[0|1]         Squeeze sites or not (current: %s)
66         --policysavedb=[0|1]    Save policy DBs      (current: %s)
67         -h, --help              This message
68 """.lstrip() % (config.debug, 
69                             config.mail, 
70                                 config.bcc, 
71                             config.email, 
72                                 config.userlist, 
73                                 config.cachert, 
74                                 config.cachenodes, 
75                                 config.squeeze, 
76                                 config.policysavedb)
77
78 def main():
79         """ Start threads, do some housekeeping, then daemonize. """
80         # Defaults
81         config = __main__.config()
82
83         try:
84                 longopts = [ "debug=", 
85                                         "mail=", 
86                                         "email=", 
87                                         "bcc=", 
88                                         "userlist=",
89                                         "cachert=", 
90                                         "cachesites=", 
91                                         "cachenodes=", 
92                                         "squeeze=", 
93                                         "policysavedb=", 
94                                         "status", 
95                                         "help"]
96                 (opts, argv) = getopt.getopt(sys.argv[1:], "h", longopts)
97         except getopt.GetoptError, err:
98                 print "Error: " + err.msg
99                 usage()
100                 sys.exit(1)
101
102         for (opt, optval) in opts:
103                 if opt in ["--debug"]:
104                         config.debug = bool(int(optval))
105                         print "Running in DEBUG mode. Copying DB & "
106                         print "caching correspondences. NO SQUEEZING."
107                 elif opt in ["--mail"]:
108                         config.mail = bool(int(optval))
109                         print "NO EMAILS SENT."
110                 elif opt in ["--email"]:
111                         config.email = optval
112                 elif opt in ["--bcc"]:
113                         config.bcc = bool(int(optval))
114                 elif opt in ["--userlist"]:
115                         if len(optval) == 0:
116                                 config.userlist = None
117                         else:
118                                 config.userlist = optval
119                 elif opt in ["--cachert"]:
120                         config.cachert = bool(int(optval))
121                 elif opt in ["--cachesites"]:
122                         config.cachesites = bool(int(optval))
123                 elif opt in ["--cachenodes"]:
124                         config.cachenodes = bool(int(optval))
125                 elif opt in ["--policysavedb"]:
126                         config.policysavedb = bool(int(optval))
127                 elif opt in ["--squeeze"]:
128                         config.squeeze = bool(int(optval))
129                 elif opt in ["--status"]:
130                         #print summary(names)
131                         sys.exit(0)
132                 else:
133                         usage()
134                         sys.exit(0)
135
136         config.save()
137         usage()
138
139
140 if __name__ == '__main__':
141         main()