12 class Error(wizard.Error):
13 """Base error class for all command errors"""
16 class PermissionsError(Error):
17 def __init__(self, dir):
22 ERROR: You don't have permissions to access this directory.
23 Do you have tokens for AFS with your root instance, and
24 is your root instance on scripts-security-upd?
26 You can check by running the commands 'klist' and
27 'blanche scripts-security-upd'.
30 class NoSuchDirectoryError(Error):
31 def __init__(self, dir):
36 ERROR: No such directory... check your typing
41 Parse the contents of an environment variable as a boolean.
42 This recognizes more values as ``False`` than :func:`bool` would.
53 except (ValueError, TypeError):
54 if val == "No" or val == "no" or val == "false" or val == "False":
62 if e.errno == errno.EACCES:
63 raise PermissionsError(dir)
64 elif e.errno == errno.ENOENT:
65 raise NoSuchDirectoryError(dir)
68 def makeLogger(options, numeric_args):
70 if logging_setup: return logging.getLogger()
71 logger = logging.getLogger()
72 logger.setLevel(logging.INFO)
73 stderr = logging.StreamHandler(sys.stderr)
74 stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
75 if not options.quiet: logger.addHandler(stderr)
76 else: logger.addHandler(NullLogHandler()) # prevent default
78 file = logging.FileHandler(options.log_file)
79 logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%H:%M:%S")
80 file.setFormatter(logformatter)
81 logger.addHandler(file)
83 logger.setLevel(logging.DEBUG)
85 stderr.setLevel(logging.WARNING)
86 if options.verbose or hasattr(options, "dry_run"):
87 stderr.setLevel(logging.INFO)
89 file.setLevel(logging.INFO)
90 def our_excepthook(type, value, tb):
91 logging.error("".join(traceback.format_exception(type,value,tb)))
93 sys.excepthook = our_excepthook
97 def makeBaseArgs(options, **grab):
98 """Takes parsed options, and breaks them back into a command
99 line string that we can pass into a subcommand"""
101 grab["debug"] = "--debug"
102 grab["verbose"] = "--verbose"
103 grab["quiet"] = "--quiet"
104 #grab["log_db"] = "--log-db"
105 for k,flag in grab.items():
106 value = getattr(options, k)
107 if not value: continue
109 if type(value) is not bool:
110 args.append(str(value))
113 class NullLogHandler(logging.Handler):
114 """Log handler that doesn't do anything"""
115 def emit(self, record):
118 class WizardOptionParser(optparse.OptionParser):
119 """Configures some default user-level options"""
120 def __init__(self, *args, **kwargs):
121 kwargs["add_help_option"] = False
122 optparse.OptionParser.__init__(self, *args, **kwargs)
123 def parse_all(self, argv):
124 self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP)
125 group = optparse.OptionGroup(self, "Common Options")
126 group.add_option("-v", "--verbose", dest="verbose", action="store_true",
127 default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output. Environment variable is WIZARD_VERBOSE")
128 group.add_option("--debug", dest="debug", action="store_true",
129 default=boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output. Environment variable is WIZARD_DEBUG")
130 group.add_option("-q", "--quiet", dest="quiet", action="store_true",
131 default=boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Environment variable is WIZARD_QUIET")
132 group.add_option("--log-file", dest="log_file", metavar="FILE",
133 default=None, help="Logs verbose output to file")
134 self.add_option_group(group)
135 options, numeric_args = self.parse_args(argv)
136 makeLogger(options, numeric_args)
137 return options, numeric_args
139 class OptionBaton(object):
140 """Command classes may define options that they sub-commands may
141 use. Since wizard --global-command subcommand is not a supported
142 mode of operation, these options have to be passed down the command
143 chain until a option parser is ready to take it; this baton is
144 what is passed down."""
147 def add(self, *args, **kwargs):
148 key = kwargs["dest"] # require this to be set
149 self.store[key] = optparse.make_option(*args, **kwargs)
150 def push(self, option_parser, *args):
151 """Hands off parameters to option parser"""
153 option_parser.add_option(self.store[key])