X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/3c391e8d97efac20e9b9f52b5446bfac1b4239c2..6554c6378a6e801b4fe47c50688cdae1d627bc18:/wizard/command/__init__.py diff --git a/wizard/command/__init__.py b/wizard/command/__init__.py index b483fe0..5d77be3 100644 --- a/wizard/command/__init__.py +++ b/wizard/command/__init__.py @@ -5,11 +5,14 @@ import sys import optparse import errno import pwd +import shutil +import cStringIO import wizard from wizard import util logging_setup = False +debug = True # This will get overwritten with the real value early on def boolish(val): """ @@ -30,36 +33,40 @@ def boolish(val): return False return bool(val) -def makeLogger(options, numeric_args): +def setup_logger(options, numeric_args): global logging_setup if logging_setup: return logging.getLogger() logger = logging.getLogger() + logger.handlers = [] # under certain cases, a spurious stream handler is set. We don't know why logger.setLevel(logging.INFO) stderr = logging.StreamHandler(sys.stderr) stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) - if not options.quiet: logger.addHandler(stderr) - else: logger.addHandler(NullLogHandler()) # prevent default + if not options.quiet: + logger.addHandler(stderr) + else: + logger.addHandler(NullLogHandler()) # prevent default if options.log_file: - file = logging.FileHandler(options.log_file) - logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%H:%M:%S") - file.setFormatter(logformatter) - logger.addHandler(file) + setup_file_logger(options.log_file, options.debug) if options.debug: logger.setLevel(logging.DEBUG) else: stderr.setLevel(logging.WARNING) if options.verbose: stderr.setLevel(logging.INFO) - if options.log_file: - file.setLevel(logging.INFO) - def our_excepthook(type, value, tb): - logging.error("".join(traceback.format_exception(type,value,tb))) - sys.exit(1) - sys.excepthook = our_excepthook logging_setup = True return logger -def makeBaseArgs(options, **grab): +def setup_file_logger(log_file, debug): + logger = logging.getLogger() + file = logging.FileHandler(log_file) + logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%Y-%m-%d %H:%M") + file.setFormatter(logformatter) + logger.addHandler(file) + if not debug: + file.setLevel(logging.INFO) + return file + +def make_base_args(options, **grab): """Takes parsed options, and breaks them back into a command line string that we can pass into a subcommand""" args = [] @@ -82,40 +89,34 @@ def security_check_homedir(location): This protects against malicious mountpoints, and is roughly equivalent to the suexec checks. """ - uid = util.get_dir_uid(location) - real = os.path.realpath(location) try: + uid = util.get_dir_uid(location) + real = os.path.realpath(location) if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"): - logging.error("Security check failed, owner of deployment and" - "owner of home directory mismatch for %s" % d.location) + logging.error("Security check failed, owner of deployment and " + "owner of home directory mismatch for %s" % location) return False except KeyError: - logging.error("Security check failed, could not look up" + logging.error("Security check failed, could not look up " "owner of %s (uid %d)" % (location, uid)) return False + except OSError as e: + logging.error("OSError: %s" % str(e)) + return False return True -def calculate_log_name(log_dir, i, dir): +def calculate_log_name(log_dir, i): """ - Calculates a log entry given a log directory, numeric identifier, and + Calculates a log entry given a numeric identifier, and directory under operation. """ - return os.path.join(log_dir, "%04d" % i + dir.replace('/', '-') + ".log") + return os.path.join(log_dir, "%04d.log" % i) -def open_logs(log_dir, log_names=('warnings', 'errors')): +def create_logdir(log_dir): """ - Opens a number of log files for auxiliary reporting. You can override what - log files to generate using ``log_names``, which corresponds to the tuple - of log files you will receive, i.e. the default returns a tuple - ``(warnings.log file object, errors.log file object)``. - - .. note:: - - The log directory is chmod'ed 777 after creation, to enable - de-priviledged processes to create files. + Creates a log directory and chmods it 777 to enable de-priviledged + processes to create files. """ - # must not be on AFS, since subprocesses won't be - # able to write to the logfiles do the to the AFS patch. try: os.mkdir(log_dir) except OSError as e: @@ -126,7 +127,6 @@ def open_logs(log_dir, log_names=('warnings', 'errors')): # os.mkdir(log_dir) # if fails, be fatal # # XXX: update last symlink os.chmod(log_dir, 0o777) - return (open(os.path.join(os.path.join(log_dir, "%s.log" % x)), "a") for x in log_names) class NullLogHandler(logging.Handler): """Log handler that doesn't do anything""" @@ -135,11 +135,18 @@ class NullLogHandler(logging.Handler): class WizardOptionParser(optparse.OptionParser): """Configures some default user-level options""" + store_help = False def __init__(self, *args, **kwargs): kwargs["add_help_option"] = False + if "store_help" in kwargs: + self.store_help = kwargs["store_help"] + del kwargs["store_help"] optparse.OptionParser.__init__(self, *args, **kwargs) - def parse_all(self, argv): - self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP) + def parse_all(self, *args, **kwargs): + if self.store_help: + self.add_option("-h", "--help", action="store_true", default=False, dest="help", help=optparse.SUPPRESS_HELP) + else: + self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP) group = optparse.OptionGroup(self, "Common Options") group.add_option("-v", "--verbose", dest="verbose", action="store_true", default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output. Envvar is WIZARD_VERBOSE") @@ -150,8 +157,13 @@ class WizardOptionParser(optparse.OptionParser): group.add_option("--log-file", dest="log_file", metavar="FILE", default=None, help="Logs verbose output to file") self.add_option_group(group) - options, numeric_args = self.parse_args(argv) - makeLogger(options, numeric_args) + options, numeric_args = self.parse_args(*args, **kwargs) + setup_logger(options, numeric_args) + debug = options.debug + # we're going to process the global --log-dir/--seen dependency here + if hasattr(options, "seen") and hasattr(options, "log_dir"): + if not options.seen and options.log_dir: + options.seen = os.path.join(options.log_dir, "seen.txt") return options, numeric_args class OptionBaton(object):