| 1 | # $Id$ |
|---|
| 2 | # |
|---|
| 3 | ##COPYRIGHT## |
|---|
| 4 | |
|---|
| 5 | __revision__ = '$Revision: 1.61 $' |
|---|
| 6 | |
|---|
| 7 | """ |
|---|
| 8 | Options used by commandline programs |
|---|
| 9 | """ |
|---|
| 10 | import os |
|---|
| 11 | import sys |
|---|
| 12 | import optparse |
|---|
| 13 | import socket |
|---|
| 14 | import logging |
|---|
| 15 | |
|---|
| 16 | import debug |
|---|
| 17 | |
|---|
| 18 | from twisted.python import log as tlog |
|---|
| 19 | |
|---|
| 20 | log = logging.getLogger('modipy') |
|---|
| 21 | |
|---|
| 22 | class 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 | |
|---|
| 72 | class 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 |
|---|