]> scripts.mit.edu Git - wizard.git/blob - wizard/command/__init__.py
Fix bugs with new reporting code.
[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 import pwd
8
9 import wizard
10 from wizard import util
11
12 logging_setup = False
13
14 def boolish(val):
15     """
16     Parse the contents of an environment variable as a boolean.
17     This recognizes more values as ``False`` than :func:`bool` would.
18
19         >>> boolish("0")
20         False
21         >>> boolish("no")
22         False
23         >>> boolish("1")
24         True
25     """
26     try:
27         return bool(int(val))
28     except (ValueError, TypeError):
29         if val == "No" or val == "no" or val == "false" or val == "False":
30             return False
31         return bool(val)
32
33 def makeLogger(options, numeric_args):
34     global logging_setup
35     if logging_setup: return logging.getLogger()
36     logger = logging.getLogger()
37     logger.handlers = [] # under certain cases, a spurious stream handler is set. We don't know why
38     logger.setLevel(logging.INFO)
39     stderr = logging.StreamHandler(sys.stderr)
40     stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
41     if not options.quiet: logger.addHandler(stderr)
42     else: logger.addHandler(NullLogHandler()) # prevent default
43     if options.log_file:
44         file = logging.FileHandler(options.log_file)
45         logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%Y-%m-%d %H:%M")
46         file.setFormatter(logformatter)
47         logger.addHandler(file)
48     if options.debug:
49         logger.setLevel(logging.DEBUG)
50     else:
51         stderr.setLevel(logging.WARNING)
52         if options.verbose:
53             stderr.setLevel(logging.INFO)
54         if options.log_file:
55             file.setLevel(logging.INFO)
56     def our_excepthook(type, value, tb):
57         logging.error("".join(traceback.format_exception(type,value,tb)))
58         sys.exit(1)
59     sys.excepthook = our_excepthook
60     logging_setup = True
61     return logger
62
63 def makeBaseArgs(options, **grab):
64     """Takes parsed options, and breaks them back into a command
65     line string that we can pass into a subcommand"""
66     args = []
67     grab["debug"]   = "--debug"
68     grab["verbose"] = "--verbose"
69     grab["quiet"]   = "--quiet"
70     #grab["log_db"] = "--log-db"
71     for k,flag in grab.items():
72         value = getattr(options, k)
73         if not value: continue
74         args.append(flag)
75         if type(value) is not bool:
76             args.append(str(value))
77     return args
78
79 def security_check_homedir(location):
80     """
81     Performs a check against a directory to determine if current
82     directory's owner has a home directory that is a parent directory.
83     This protects against malicious mountpoints, and is roughly equivalent
84     to the suexec checks.
85     """
86     try:
87         uid = util.get_dir_uid(location)
88         real = os.path.realpath(location)
89         if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"):
90             logging.error("Security check failed, owner of deployment and "
91                     "owner of home directory mismatch for %s" % location)
92             return False
93     except KeyError:
94         logging.error("Security check failed, could not look up "
95                 "owner of %s (uid %d)" % (location, uid))
96         return False
97     except OSError as e:
98         logging.error("OSError: %s" % str(e))
99         return False
100     return True
101
102 def calculate_log_name(log_dir, i):
103     """
104     Calculates a log entry given a numeric identifier, and
105     directory under operation.
106     """
107     return os.path.join(log_dir, "%04d.log" % i)
108
109 def create_logdir(log_dir):
110     """
111     Creates a log directory and chmods it 777 to enable de-priviledged
112     processes to create files.
113     """
114     try:
115         os.mkdir(log_dir)
116     except OSError as e:
117         if e.errno != errno.EEXIST:
118             raise
119         #if create_subdirs:
120         #    log_dir = os.path.join(log_dir, str(int(time.time())))
121         #    os.mkdir(log_dir) # if fails, be fatal
122         #    # XXX: update last symlink
123     os.chmod(log_dir, 0o777)
124
125 def open_reports(log_dir, names=('warnings', 'errors')):
126     """
127     Opens a number of reports files for auxiliary reporting.  You can override what
128     log files to generate using ``names``, which corresponds to the tuple
129     of report files you will receive, i.e. the default returns a tuple
130     ``(warnings.txt file object, errors.txt file object)``. Note that this will
131     delete any information that was previously in the file (but those logfiles
132     are backed up).
133     """
134     # must not be on AFS, since subprocesses won't be
135     # able to write to the logfiles do the to the AFS patch.
136     files = [os.path.join(os.path.join(log_dir, "%s.txt" % x)) for x in names]
137     old_reports = os.path.join(log_dir, "old-reports")
138     rundir = os.path.join(old_reports, "run")
139     if not os.path.exists(old_reports):
140         os.mkdir(old_reports)
141     else:
142         util.safe_unlink(rundir)
143     for f in files:
144         if os.path.exists(f):
145             os.rename(f, rundir)
146     return (open(f, "w") for f in files)
147
148 class NullLogHandler(logging.Handler):
149     """Log handler that doesn't do anything"""
150     def emit(self, record):
151         pass
152
153 class WizardOptionParser(optparse.OptionParser):
154     """Configures some default user-level options"""
155     def __init__(self, *args, **kwargs):
156         kwargs["add_help_option"] = False
157         optparse.OptionParser.__init__(self, *args, **kwargs)
158     def parse_all(self, argv):
159         self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP)
160         group = optparse.OptionGroup(self, "Common Options")
161         group.add_option("-v", "--verbose", dest="verbose", action="store_true",
162                 default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output.  Envvar is WIZARD_VERBOSE")
163         group.add_option("--debug", dest="debug", action="store_true",
164                 default=boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output.  Envvar is WIZARD_DEBUG")
165         group.add_option("-q", "--quiet", dest="quiet", action="store_true",
166                 default=boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Envvar is WIZARD_QUIET")
167         group.add_option("--log-file", dest="log_file", metavar="FILE",
168                 default=None, help="Logs verbose output to file")
169         self.add_option_group(group)
170         options, numeric_args = self.parse_args(argv)
171         makeLogger(options, numeric_args)
172         # we're going to process the global --log-dir/--seen dependency here
173         if hasattr(options, "seen") and hasattr(options, "log_dir"):
174             if not options.seen and options.log_dir:
175                 options.seen = os.path.join(options.log_dir, "seen.txt")
176         return options, numeric_args
177
178 class OptionBaton(object):
179     """Command classes may define options that they sub-commands may
180     use.  Since wizard --global-command subcommand is not a supported
181     mode of operation, these options have to be passed down the command
182     chain until a option parser is ready to take it; this baton is
183     what is passed down."""
184     def __init__(self):
185         self.store = {}
186     def add(self, *args, **kwargs):
187         key = kwargs["dest"] # require this to be set
188         self.store[key] = optparse.make_option(*args, **kwargs)
189     def push(self, option_parser, *args):
190         """Hands off parameters to option parser"""
191         for key in args:
192             option_parser.add_option(self.store[key])
193
194 class Error(wizard.Error):
195     """Base error class for all command errors"""
196     pass