root/tags/v1.0/options.py

Revision 29, 3.2 KB (checked in by daedalus, 4 years ago)

* Manual bailout when in authoritarian mode now works correctly.
* Fixed up some of the log level and stats reporting.
* Added 'backout' mode, that will only run the backout portion of changes. More

useful for testing, but can also be used to undo provisioning by repurposing
the same set of templates used for provisioning.

Line 
1# $Id$
2#
3##COPYRIGHT##
4
5__revision__ = '$Revision: 1.61 $'
6
7"""
8Options used by commandline programs
9"""
10import os
11import sys
12import optparse
13import socket
14import logging
15
16import debug
17
18from twisted.python import log as tlog
19
20log = logging.getLogger('modipy')
21
22class BaseOptions(optparse.OptionParser):
23    """
24    Base options common to all programs.
25    """
26    def __init__(self, *args, **kwargs):
27        """
28        Initialise a base level options parser.
29        """
30        optparse.OptionParser.__init__(self, **kwargs)
31
32        help_license = 'Display the license agreement and exit.'
33        help_debug = "Set the output debug level to: debug, info, warn, error, or critical."
34#        help_logfile = "Log to specified file instead of default logfile"
35#        help_no_logfile = "Disable logging to logfile"
36
37       
38        self.add_option('', '--license',       dest='license', action='store_true', help=help_license)   
39        self.add_option('', '--debug',         dest='debug', type='choice', choices=('debug', 'info', 'warn', 'error', 'critical'), metavar='LEVEL', default='info', help=help_debug)
40#        self.add_option('', '--logfile',       dest='logfile', type='string', help=help_logfile)
41#        self.add_option('', '--no-logfile',       dest='no-logfile', action='store_true', default=False, help=help_no_logfile)
42
43        self.addOptions()
44
45    def addOptions(self):
46        """
47        Override this method in subclasses to add more options.
48        This enables multiple inheritence from the common base class.
49        """
50        pass
51       
52    def parseOptions(self, argv=sys.argv[1:]):
53        """
54        Emulate the twisted options parser API.
55        """
56        options, args = self.parse_args(argv)
57        self.options = options
58        self.args = args
59        self.postOptions()
60
61    def postOptions(self):
62        """
63        Perform post options parsing operations.
64        """
65        # Set standard logging level
66        log.setLevel(logging._levelNames.get(self.options.debug.upper(), logging.INFO))
67        if self.options.license:
68            print license.long
69            sys.exit(1)
70            pass
71
72class ChangeOptions(BaseOptions):
73    """
74    Options for change management.
75    """
76
77    def addOptions(self):
78        help_authoritarian = "Authoritarian mode. Confirm every change command."
79        help_configfile = "The configuration file to load"
80        help_loadonly = "Load the configuration file and exit. Used to test parsing."
81        help_backout = "Run the backout portion of the changes."
82
83        self.add_option('-a', '--authoritarian',  dest='authoritarian', action='store_true', default=False, help=help_authoritarian)       
84        self.add_option('-b', '--backout',  dest='backout', action='store_true', default=False, help=help_backout)       
85        self.add_option('-c', '--configfile', dest='configfile', type='string', help=help_configfile)
86        self.add_option('', '--loadonly', dest='loadonly', action='store_true', default=False, help=help_loadonly)
87
88    def check_values(self, options, args):
89
90        if not options.configfile:
91            self.error("Configuration file not specified.")
92            pass
93
94        return options, args
Note: See TracBrowser for help on using the browser.