11 from wizard import util
17 Parse the contents of an environment variable as a boolean.
18 This recognizes more values as ``False`` than :func:`bool` would.
29 except (ValueError, TypeError):
30 if val == "No" or val == "no" or val == "false" or val == "False":
34 def makeLogger(options, numeric_args):
36 if logging_setup: return logging.getLogger()
37 logger = logging.getLogger()
38 logger.handlers = [] # under certain cases, a spurious stream handler is set. We don't know why
39 logger.setLevel(logging.INFO)
40 stderr = logging.StreamHandler(sys.stderr)
41 stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
42 if not options.quiet: logger.addHandler(stderr)
43 else: logger.addHandler(NullLogHandler()) # prevent default
45 file = logging.FileHandler(options.log_file)
46 logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%Y-%m-%d %H:%M")
47 file.setFormatter(logformatter)
48 logger.addHandler(file)
50 logger.setLevel(logging.DEBUG)
52 stderr.setLevel(logging.WARNING)
54 stderr.setLevel(logging.INFO)
56 file.setLevel(logging.INFO)
57 def our_excepthook(type, value, tb):
58 logging.error("".join(traceback.format_exception(type,value,tb)))
60 sys.excepthook = our_excepthook
64 def makeBaseArgs(options, **grab):
65 """Takes parsed options, and breaks them back into a command
66 line string that we can pass into a subcommand"""
68 grab["debug"] = "--debug"
69 grab["verbose"] = "--verbose"
70 grab["quiet"] = "--quiet"
71 #grab["log_db"] = "--log-db"
72 for k,flag in grab.items():
73 value = getattr(options, k)
74 if not value: continue
76 if type(value) is not bool:
77 args.append(str(value))
80 def security_check_homedir(location):
82 Performs a check against a directory to determine if current
83 directory's owner has a home directory that is a parent directory.
84 This protects against malicious mountpoints, and is roughly equivalent
88 uid = util.get_dir_uid(location)
89 real = os.path.realpath(location)
90 if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"):
91 logging.error("Security check failed, owner of deployment and "
92 "owner of home directory mismatch for %s" % location)
95 logging.error("Security check failed, could not look up "
96 "owner of %s (uid %d)" % (location, uid))
99 logging.error("OSError: %s" % str(e))
103 def calculate_log_name(log_dir, i):
105 Calculates a log entry given a numeric identifier, and
106 directory under operation.
108 return os.path.join(log_dir, "%04d.log" % i)
110 def create_logdir(log_dir):
112 Creates a log directory and chmods it 777 to enable de-priviledged
113 processes to create files.
118 if e.errno != errno.EEXIST:
121 # log_dir = os.path.join(log_dir, str(int(time.time())))
122 # os.mkdir(log_dir) # if fails, be fatal
123 # # XXX: update last symlink
124 os.chmod(log_dir, 0o777)
126 class Report(object):
127 #: Set of indices that should be skipped
129 def __init__(self, names, fobjs, skip):
131 for name, fobj in zip(names, fobjs):
132 setattr(self, name, fobj)
134 def report_files(log_dir, names):
135 return [os.path.join(os.path.join(log_dir, "%s.txt" % x)) for x in names]
137 def read_reports(log_dir, names):
139 Reads a number of reports files. The return value is a :class:`Report`
140 object with attributes that are open file objects that correspond to ``names``.
142 return Report(names, [open(f, "r") for f in report_files(log_dir, names)], set())
144 def open_reports(log_dir, names=('warnings', 'errors'), redo=False, append_names=()):
146 Returns a :class:`Report` object configured appropriately for the
147 parameters passed. This object has attributes names + append_names which
148 contain file objects opened as "w". ``names`` report files are cleared unconditionally
149 when they are opened (i.e. are not preserved from run to run.) ``append_names``
150 report files are not cleared unless ``redo`` is True, and persist over
151 runs: assuming the convention that [0001] is the index of the deployment,
152 the ``skip`` attribute on the returned report object contains indexes that
157 rr = read_reports(log_dir, append_names)
158 def build_set(skip, fobj):
159 skip |= set(int(l[1:5]) for l in fobj.read().splitlines())
161 for name in append_names:
162 build_set(skip, getattr(rr, name))
164 names += append_names
166 files = report_files(log_dir, names)
167 append_files = report_files(log_dir, append_names)
169 old_reports = os.path.join(log_dir, "old-reports")
170 rundir = os.path.join(old_reports, "run")
171 if not os.path.exists(old_reports):
172 os.mkdir(old_reports)
174 util.safe_unlink(rundir)
176 if os.path.exists(f):
178 for f in append_files:
179 if os.path.exists(f):
180 shutil.copy(f, rundir)
181 return Report(names + append_names, [open(f, "w") for f in files] + [open(f, "a") for f in append_files], skip)
183 class NullLogHandler(logging.Handler):
184 """Log handler that doesn't do anything"""
185 def emit(self, record):
188 class WizardOptionParser(optparse.OptionParser):
189 """Configures some default user-level options"""
190 def __init__(self, *args, **kwargs):
191 kwargs["add_help_option"] = False
192 optparse.OptionParser.__init__(self, *args, **kwargs)
193 def parse_all(self, argv):
194 self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP)
195 group = optparse.OptionGroup(self, "Common Options")
196 group.add_option("-v", "--verbose", dest="verbose", action="store_true",
197 default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output. Envvar is WIZARD_VERBOSE")
198 group.add_option("--debug", dest="debug", action="store_true",
199 default=boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output. Envvar is WIZARD_DEBUG")
200 group.add_option("-q", "--quiet", dest="quiet", action="store_true",
201 default=boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Envvar is WIZARD_QUIET")
202 group.add_option("--log-file", dest="log_file", metavar="FILE",
203 default=None, help="Logs verbose output to file")
204 self.add_option_group(group)
205 options, numeric_args = self.parse_args(argv)
206 makeLogger(options, numeric_args)
207 # we're going to process the global --log-dir/--seen dependency here
208 if hasattr(options, "seen") and hasattr(options, "log_dir"):
209 if not options.seen and options.log_dir:
210 options.seen = os.path.join(options.log_dir, "seen.txt")
211 return options, numeric_args
213 class OptionBaton(object):
214 """Command classes may define options that they sub-commands may
215 use. Since wizard --global-command subcommand is not a supported
216 mode of operation, these options have to be passed down the command
217 chain until a option parser is ready to take it; this baton is
218 what is passed down."""
221 def add(self, *args, **kwargs):
222 key = kwargs["dest"] # require this to be set
223 self.store[key] = optparse.make_option(*args, **kwargs)
224 def push(self, option_parser, *args):
225 """Hands off parameters to option parser"""
227 option_parser.add_option(self.store[key])
229 class Error(wizard.Error):
230 """Base error class for all command errors"""