859d0aa566aa340fc9304d787e235d987ffb1dbe
[monitor.git] / config.py
1 #!/usr/bin/python
2 import pickle
3 import os
4 import getopt
5 import sys
6 import __main__
7 from optparse import OptionParser
8 from parser import parse_bool
9
10 debug=0
11 mail=0
12 bcc=0
13 email="soltesz@cs.utk.edu"
14 run=False
15 checkopt=False
16 squeeze=0
17 policysavedb=0
18
19 config_command = False
20
21 def updatemodule(module, cf):
22         module.__dict__.update(cf.__dict__)
23
24 class config:
25         debug=0
26         mail=0
27         bcc=0
28         email="soltesz@cs.utk.edu"
29         run=False
30         checkopt=False
31         squeeze=0
32         policysavedb=0
33         __file = ".config"
34
35         def __init__(self, parser=None):
36                 if os.path.exists(self.__file): # file exists, read that.
37                         f = open(self.__file, 'r')
38                         o = pickle.load(f)
39                         self.__dict__.update(o)
40                         f.close()
41
42                 if parser == None:
43                         self.parser = OptionParser()
44                 else:
45                         self.parser = parser
46
47                 self.parser.set_defaults(debug = self.debug,
48                                                                 mail = self.mail,
49                                                                 bcc  = self.bcc,
50                                                                 email = self.email,
51                                                                 run = self.run,
52                                                                 checkopt = False,
53                                                                 squeeze = self.squeeze,
54                                                                 policysavedb = self.policysavedb)
55
56                 self.parser.add_option("", "--debug", dest="debug",
57                                                   help="Enable debugging", 
58                                                   type="int",
59                                                   metavar="[0|1]",
60                                                   action="callback", 
61                                                   callback=parse_bool)
62                 self.parser.add_option("", "--mail", dest="mail",
63                                                   help="Enable sending email",
64                                                   type="int",
65                                                   metavar="[0|1]",
66                                                   action="callback", 
67                                                   callback=parse_bool)
68                 self.parser.add_option("", "--bcc", dest="bcc",
69                                                   help="Include BCC to user",
70                                                   type="int",
71                                                   metavar="[0|1]",
72                                                   action="callback", 
73                                                   callback=parse_bool)
74                 self.parser.add_option("", "--squeeze", dest="squeeze",
75                                                   help="Squeeze sites or not",
76                                                   type="int",
77                                                   metavar="[0|1]",
78                                                   action="callback", 
79                                                   callback=parse_bool)
80                 self.parser.add_option("", "--policysavedb", dest="policysavedb",
81                                                   help="Save the policy event database after a run",
82                                                   type="int",
83                                                   metavar="[0|1]",
84                                                   action="callback", 
85                                                   callback=parse_bool)
86                 self.parser.add_option("", "--checkopt", dest="checkopt", 
87                                                   action="store_true",
88                                                   help="print current options")
89                 self.parser.add_option("", "--run", dest="run", 
90                                                   action="store_true",
91                                                   help="Perform monitor or print configs")
92                 self.parser.add_option("", "--email", dest="email",
93                                                   help="Specify an email address to use for mail when "+\
94                                                                  "debug is enabled or for bcc when it is not")
95                 
96                 # config_command is needed to keep subsequent loads of config() from
97                 # trying to parse the arguments that have already been parsed by
98                 # the new main().
99                 if parser == None and config_command:
100                         print "calling parse_args"
101                         self.parse_args()
102
103         def parse_args(self):
104                 #print "self: %s" % self
105                 #import traceback
106                 #print traceback.print_stack()
107                 #print "Ccalling parse_args"
108                 (options, args) = self.parser.parse_args()
109                 #for o in options.__dict__:
110                 #       print "optin: %s == %s" % (o, options.__dict__[o])
111                 self.__dict__.update(options.__dict__)
112                 self.__dict__['args'] = args
113                 self.save(options)
114                 if options.checkopt:
115                         self.usage()
116                 #       print "\nAdd --run to actually perform the command"
117                         sys.exit(1)
118
119         def getListFromFile(self, file):
120                 f = open(file, 'r')
121                 list = []
122                 for line in f:
123                         line = line.strip()
124                         list += [line]
125                 return list
126
127         def print_values(self):
128                 exclude = ['parser']
129                 for key in self.__dict__.keys():
130                         if key not in exclude:
131                                 print "%20s == %s" % (key, self.__dict__[key])
132                 
133         def save(self, options=None):
134                 f = open(self.__file, 'w')
135                 if options == None:
136                         o = {'debug': self.debug, 
137                                  'mail': self.mail, 
138                                  'bcc': self.bcc, 
139                                  'email':self.email,
140                                  'squeeze':self.squeeze,
141                                  'policysavedb':self.policysavedb}
142                 else:
143                         o = options.__dict__
144
145                 pickle.dump(o, f)
146                 f.close()
147
148         def usage(self):
149                 self.print_values()
150                 self.parser.print_help()
151
152
153 def main():
154         """ Start threads, do some housekeeping, then daemonize. """
155         # Defaults
156         global config_command
157         config_command = True
158         config = __main__.config()
159
160         try:
161                 print "acalling parse_args"
162                 config.parse_args()
163                 
164         except Exception, err:
165                 print "Error: %s " %  err
166                 config.usage()
167                 sys.exit(1)
168
169         config.usage()
170
171
172 if __name__ == '__main__':
173         main()