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