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.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
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)
49 logger.setLevel(logging.DEBUG)
51 stderr.setLevel(logging.WARNING)
53 stderr.setLevel(logging.INFO)
55 file.setLevel(logging.INFO)
56 def our_excepthook(type, value, tb):
57 logging.error("".join(traceback.format_exception(type,value,tb)))
59 sys.excepthook = our_excepthook
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"""
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
75 if type(value) is not bool:
76 args.append(str(value))
79 def security_check_homedir(location):
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
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)
94 logging.error("Security check failed, could not look up "
95 "owner of %s (uid %d)" % (location, uid))
98 logging.error("OSError: %s" % str(e))
102 def calculate_log_name(log_dir, i):
104 Calculates a log entry given a numeric identifier, and
105 directory under operation.
107 return os.path.join(log_dir, "%04d.log" % i)
109 def create_logdir(log_dir):
111 Creates a log directory and chmods it 777 to enable de-priviledged
112 processes to create files.
117 if e.errno != errno.EEXIST:
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)
125 def open_reports(log_dir, names=('warnings', 'errors')):
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
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)
142 util.safe_unlink(rundir)
144 if os.path.exists(f):
146 return (open(f, "w") for f in files)
148 class NullLogHandler(logging.Handler):
149 """Log handler that doesn't do anything"""
150 def emit(self, record):
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
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."""
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"""
192 option_parser.add_option(self.store[key])
194 class Error(wizard.Error):
195 """Base error class for all command errors"""