]> scripts.mit.edu Git - wizard.git/blob - wizard/command/__init__.py
Add success reporting.
[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 import shutil
9 import cStringIO
10
11 import wizard
12 from wizard import util
13
14 logging_setup = False
15 debug = True # This will get overwritten with the real value early on
16
17 def boolish(val):
18     """
19     Parse the contents of an environment variable as a boolean.
20     This recognizes more values as ``False`` than :func:`bool` would.
21
22         >>> boolish("0")
23         False
24         >>> boolish("no")
25         False
26         >>> boolish("1")
27         True
28     """
29     try:
30         return bool(int(val))
31     except (ValueError, TypeError):
32         if val == "No" or val == "no" or val == "false" or val == "False":
33             return False
34         return bool(val)
35
36 def setup_logger(options, numeric_args):
37     global logging_setup
38     if logging_setup: return logging.getLogger()
39     logger = logging.getLogger()
40     logger.handlers = [] # under certain cases, a spurious stream handler is set. We don't know why
41     logger.setLevel(logging.INFO)
42     stderr = logging.StreamHandler(sys.stderr)
43     stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
44     if not options.quiet:
45         logger.addHandler(stderr)
46     else:
47         logger.addHandler(NullLogHandler()) # prevent default
48     if options.log_file:
49         setup_file_logger(options.log_file, options.debug)
50     if options.debug:
51         logger.setLevel(logging.DEBUG)
52     else:
53         stderr.setLevel(logging.WARNING)
54         if options.verbose:
55             stderr.setLevel(logging.INFO)
56     logging_setup = True
57     return logger
58
59 def setup_file_logger(log_file, debug):
60     logger = logging.getLogger()
61     file = logging.FileHandler(log_file)
62     logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%Y-%m-%d %H:%M")
63     file.setFormatter(logformatter)
64     logger.addHandler(file)
65     if not debug:
66         file.setLevel(logging.INFO)
67     return file
68
69 def make_base_args(options, **grab):
70     """Takes parsed options, and breaks them back into a command
71     line string that we can pass into a subcommand"""
72     args = []
73     grab["debug"]   = "--debug"
74     grab["verbose"] = "--verbose"
75     grab["quiet"]   = "--quiet"
76     #grab["log_db"] = "--log-db"
77     for k,flag in grab.items():
78         value = getattr(options, k)
79         if not value: continue
80         args.append(flag)
81         if type(value) is not bool:
82             args.append(str(value))
83     return args
84
85 def security_check_homedir(location):
86     """
87     Performs a check against a directory to determine if current
88     directory's owner has a home directory that is a parent directory.
89     This protects against malicious mountpoints, and is roughly equivalent
90     to the suexec checks.
91     """
92     try:
93         uid = util.get_dir_uid(location)
94         real = os.path.realpath(location)
95         if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"):
96             logging.error("Security check failed, owner of deployment and "
97                     "owner of home directory mismatch for %s" % location)
98             return False
99     except KeyError:
100         logging.error("Security check failed, could not look up "
101                 "owner of %s (uid %d)" % (location, uid))
102         return False
103     except OSError as e:
104         logging.error("OSError: %s" % str(e))
105         return False
106     return True
107
108 def calculate_log_name(log_dir, i):
109     """
110     Calculates a log entry given a numeric identifier, and
111     directory under operation.
112     """
113     return os.path.join(log_dir, "%04d.log" % i)
114
115 def create_logdir(log_dir):
116     """
117     Creates a log directory and chmods it 777 to enable de-priviledged
118     processes to create files.
119     """
120     try:
121         os.mkdir(log_dir)
122     except OSError as e:
123         if e.errno != errno.EEXIST:
124             raise
125         #if create_subdirs:
126         #    log_dir = os.path.join(log_dir, str(int(time.time())))
127         #    os.mkdir(log_dir) # if fails, be fatal
128         #    # XXX: update last symlink
129     os.chmod(log_dir, 0o777)
130
131 class Report(object):
132     #: Set of indices that should be skipped
133     skip = None
134     #: Dict of append names to counts.  You should manually increment these as necessary
135     fails = None
136     #: Number of successes
137     successes = 0
138     #: Names of the files objects
139     names = None
140     def __init__(self, names, fobjs, skip, fails):
141         self.skip = skip
142         self.names = names
143         self.fails = fails
144         for name, fobj in zip(names, fobjs):
145             setattr(self, name, fobj)
146     def flush(self):
147         for n in self.names:
148             getattr(self, n).flush()
149
150 def report_files(log_dir, names):
151     return [os.path.join(os.path.join(log_dir, "%s.txt" % x)) for x in names]
152
153 def read_reports(log_dir, names):
154     """
155     Reads a number of reports files.  The return value is a :class:`Report`
156     object with attributes that are open file objects that correspond to ``names``.
157     """
158     return Report(names, [(os.path.exists(f) and open(f, "r") or cStringIO.StringIO()) for f in report_files(log_dir, names)], set(), {})
159
160 def open_reports(log_dir, names=('warnings', 'errors'), redo=False, append_names=()):
161     """
162     Returns a :class:`Report` object configured appropriately for the
163     parameters passed.  This object has attributes names + append_names which
164     contain file objects opened as "w".  ``names`` report files are cleared unconditionally
165     when they are opened (i.e. are not preserved from run to run.)  ``append_names``
166     report files are not cleared unless ``redo`` is True, and persist over
167     runs: assuming the convention that [0001] is the index of the deployment,
168     the ``skip`` attribute on the returned report object contains indexes that
169     should be skipped.
170     """
171     skip = set()
172     fails = {}
173     if not redo:
174         rr = read_reports(log_dir, append_names)
175         def build_set(skip, fails, name, fobj):
176             lines = fobj.read().strip().splitlines()
177             skip |= set(int(l[1:5]) for l in lines)
178             fails[name] = len(lines)
179             fobj.close()
180         for name in append_names:
181             build_set(skip, fails, name, getattr(rr, name))
182     else:
183         names += append_names
184         for name in append_names:
185             fails[name] = 0
186         append_names = ()
187     files = report_files(log_dir, names)
188     append_files = report_files(log_dir, append_names)
189     # backup old reports
190     old_reports = os.path.join(log_dir, "old-reports")
191     rundir = os.path.join(old_reports, "run")
192     if not os.path.exists(old_reports):
193         os.mkdir(old_reports)
194     else:
195         util.safe_unlink(rundir)
196     for f in files:
197         if os.path.exists(f):
198             os.rename(f, rundir)
199     for f in append_files:
200         if os.path.exists(f):
201             shutil.copy(f, rundir)
202     return Report(names + append_names, [open(f, "w") for f in files] + [open(f, "a") for f in append_files], skip, fails)
203
204 class NullLogHandler(logging.Handler):
205     """Log handler that doesn't do anything"""
206     def emit(self, record):
207         pass
208
209 class WizardOptionParser(optparse.OptionParser):
210     """Configures some default user-level options"""
211     store_help = False
212     def __init__(self, *args, **kwargs):
213         kwargs["add_help_option"] = False
214         if "store_help" in kwargs:
215             self.store_help = kwargs["store_help"]
216             del kwargs["store_help"]
217         optparse.OptionParser.__init__(self, *args, **kwargs)
218     def parse_all(self, *args, **kwargs):
219         if self.store_help:
220             self.add_option("-h", "--help", action="store_true", default=False, dest="help", help=optparse.SUPPRESS_HELP)
221         else:
222             self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP)
223         group = optparse.OptionGroup(self, "Common Options")
224         group.add_option("-v", "--verbose", dest="verbose", action="store_true",
225                 default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output.  Envvar is WIZARD_VERBOSE")
226         group.add_option("--debug", dest="debug", action="store_true",
227                 default=boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output.  Envvar is WIZARD_DEBUG")
228         group.add_option("-q", "--quiet", dest="quiet", action="store_true",
229                 default=boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Envvar is WIZARD_QUIET")
230         group.add_option("--log-file", dest="log_file", metavar="FILE",
231                 default=None, help="Logs verbose output to file")
232         self.add_option_group(group)
233         options, numeric_args = self.parse_args(*args, **kwargs)
234         setup_logger(options, numeric_args)
235         debug = options.debug
236         # we're going to process the global --log-dir/--seen dependency here
237         if hasattr(options, "seen") and hasattr(options, "log_dir"):
238             if not options.seen and options.log_dir:
239                 options.seen = os.path.join(options.log_dir, "seen.txt")
240         return options, numeric_args
241
242 class OptionBaton(object):
243     """Command classes may define options that they sub-commands may
244     use.  Since wizard --global-command subcommand is not a supported
245     mode of operation, these options have to be passed down the command
246     chain until a option parser is ready to take it; this baton is
247     what is passed down."""
248     def __init__(self):
249         self.store = {}
250     def add(self, *args, **kwargs):
251         key = kwargs["dest"] # require this to be set
252         self.store[key] = optparse.make_option(*args, **kwargs)
253     def push(self, option_parser, *args):
254         """Hands off parameters to option parser"""
255         for key in args:
256             option_parser.add_option(self.store[key])
257
258 class Error(wizard.Error):
259     """Base error class for all command errors"""
260     pass