]> scripts.mit.edu Git - wizard.git/blob - wizard/command/__init__.py
Fix exceptions, quiet subprocesses, change logging and order.
[wizard.git] / wizard / command / __init__.py
1 import logging
2 import traceback
3 import os
4 import sys
5 import optparse
6 import errno
7
8 import wizard
9
10 logging_setup = False
11
12 class Error(wizard.Error):
13     """Base error class for all command errors"""
14     pass
15
16 class PermissionsError(Error):
17     def __init__(self, dir):
18         self.dir = dir
19     def __str__(self):
20         return """
21
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?
25
26 You can check by running the commands 'klist' and
27 'blanche scripts-security-upd'.
28 """
29
30 class NoSuchDirectoryError(Error):
31     def __init__(self, dir):
32         self.dir = dir
33     def __str__(self):
34         return """
35
36 ERROR: No such directory... check your typing
37 """
38
39 def boolish(val):
40     """
41     Parse the contents of an environment variable as a boolean.
42     This recognizes more values as ``False`` than :func:`bool` would.
43
44         >>> boolish("0")
45         False
46         >>> boolish("no")
47         False
48         >>> boolish("1")
49         True
50     """
51     try:
52         return bool(int(val))
53     except (ValueError, TypeError):
54         if val == "No" or val == "no" or val == "false" or val == "False":
55             return False
56         return bool(val)
57
58 def chdir(dir):
59     try:
60         os.chdir(dir)
61     except OSError as e:
62         if e.errno == errno.EACCES:
63             raise PermissionsError(dir)
64         elif e.errno == errno.ENOENT:
65             raise NoSuchDirectoryError(dir)
66         else: raise e
67
68 def makeLogger(options, numeric_args):
69     global logging_setup
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
77     if options.log_file:
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)
82     if options.debug:
83         logger.setLevel(logging.DEBUG)
84     else:
85         stderr.setLevel(logging.WARNING)
86         if options.verbose or hasattr(options, "dry_run"):
87             stderr.setLevel(logging.INFO)
88         if options.log_file:
89             file.setLevel(logging.INFO)
90     def our_excepthook(type, value, tb):
91         logging.error("".join(traceback.format_exception(type,value,tb)))
92         sys.exit(1)
93     sys.excepthook = our_excepthook
94     logging_setup = True
95     return logger
96
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"""
100     args = []
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
108         args.append(flag)
109         if type(value) is not bool:
110             args.append(str(value))
111     return args
112
113 class NullLogHandler(logging.Handler):
114     """Log handler that doesn't do anything"""
115     def emit(self, record):
116         pass
117
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
138
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."""
145     def __init__(self):
146         self.store = {}
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"""
152         for key in args:
153             option_parser.add_option(self.store[key])