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