10 from wizard import util
16 Parse the contents of an environment variable as a boolean.
17 This recognizes more values as ``False`` than :func:`bool` would.
28 except (ValueError, TypeError):
29 if val == "No" or val == "no" or val == "false" or val == "False":
33 def makeLogger(options, numeric_args):
35 if logging_setup: return logging.getLogger()
36 logger = logging.getLogger()
37 logger.setLevel(logging.INFO)
38 stderr = logging.StreamHandler(sys.stderr)
39 stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
40 if not options.quiet: logger.addHandler(stderr)
41 else: logger.addHandler(NullLogHandler()) # prevent default
43 file = logging.FileHandler(options.log_file)
44 logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%H:%M:%S")
45 file.setFormatter(logformatter)
46 logger.addHandler(file)
48 logger.setLevel(logging.DEBUG)
50 stderr.setLevel(logging.WARNING)
52 stderr.setLevel(logging.INFO)
54 file.setLevel(logging.INFO)
55 def our_excepthook(type, value, tb):
56 logging.error("".join(traceback.format_exception(type,value,tb)))
58 sys.excepthook = our_excepthook
62 def makeBaseArgs(options, **grab):
63 """Takes parsed options, and breaks them back into a command
64 line string that we can pass into a subcommand"""
66 grab["debug"] = "--debug"
67 grab["verbose"] = "--verbose"
68 grab["quiet"] = "--quiet"
69 #grab["log_db"] = "--log-db"
70 for k,flag in grab.items():
71 value = getattr(options, k)
72 if not value: continue
74 if type(value) is not bool:
75 args.append(str(value))
78 def security_check_homedir(location):
80 Performs a check against a directory to determine if current
81 directory's owner has a home directory that is a parent directory.
82 This protects against malicious mountpoints, and is roughly equivalent
85 uid = util.get_dir_uid(location)
86 real = os.path.realpath(location)
88 if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"):
89 logging.error("Security check failed, owner of deployment and"
90 "owner of home directory mismatch for %s" % d.location)
93 logging.error("Security check failed, could not look up"
94 "owner of %s (uid %d)" % (location, uid))
98 def calculate_log_name(log_dir, i, dir):
100 Calculates a log entry given a log directory, numeric identifier, and
101 directory under operation.
103 return os.path.join(log_dir, "%04d" % i + dir.replace('/', '-') + ".log")
105 def open_logs(log_dir, log_names=('warnings', 'errors')):
107 Opens a number of log files for auxiliary reporting. You can override what
108 log files to generate using ``log_names``, which corresponds to the tuple
109 of log files you will receive, i.e. the default returns a tuple
110 ``(warnings.log file object, errors.log file object)``.
114 The log directory is chmod'ed 777 after creation, to enable
115 de-priviledged processes to create files.
117 # must not be on AFS, since subprocesses won't be
118 # able to write to the logfiles do the to the AFS patch.
122 if e.errno != errno.EEXIST:
125 # log_dir = os.path.join(log_dir, str(int(time.time())))
126 # os.mkdir(log_dir) # if fails, be fatal
127 # # XXX: update last symlink
128 os.chmod(log_dir, 0o777)
129 return (open(os.path.join(os.path.join(log_dir, "%s.log" % x)), "a") for x in log_names)
131 class NullLogHandler(logging.Handler):
132 """Log handler that doesn't do anything"""
133 def emit(self, record):
136 class WizardOptionParser(optparse.OptionParser):
137 """Configures some default user-level options"""
138 def __init__(self, *args, **kwargs):
139 kwargs["add_help_option"] = False
140 optparse.OptionParser.__init__(self, *args, **kwargs)
141 def parse_all(self, argv):
142 self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP)
143 group = optparse.OptionGroup(self, "Common Options")
144 group.add_option("-v", "--verbose", dest="verbose", action="store_true",
145 default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output. Envvar is WIZARD_VERBOSE")
146 group.add_option("--debug", dest="debug", action="store_true",
147 default=boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output. Envvar is WIZARD_DEBUG")
148 group.add_option("-q", "--quiet", dest="quiet", action="store_true",
149 default=boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Envvar is WIZARD_QUIET")
150 group.add_option("--log-file", dest="log_file", metavar="FILE",
151 default=None, help="Logs verbose output to file")
152 self.add_option_group(group)
153 options, numeric_args = self.parse_args(argv)
154 makeLogger(options, numeric_args)
155 return options, numeric_args
157 class OptionBaton(object):
158 """Command classes may define options that they sub-commands may
159 use. Since wizard --global-command subcommand is not a supported
160 mode of operation, these options have to be passed down the command
161 chain until a option parser is ready to take it; this baton is
162 what is passed down."""
165 def add(self, *args, **kwargs):
166 key = kwargs["dest"] # require this to be set
167 self.store[key] = optparse.make_option(*args, **kwargs)
168 def push(self, option_parser, *args):
169 """Hands off parameters to option parser"""
171 option_parser.add_option(self.store[key])
173 class Error(wizard.Error):
174 """Base error class for all command errors"""