import optparse import sys import subprocess from wizard import deploy, log def main(argv, baton): options, args = parse_args(argv) d = deploy.Deployment(args[0]) d.log # force the log to be loaded, to pre-empt errors with PipeToLess(): print_log(d, options) def parse_args(argv): usage = """usage: %prog info [ARGS] DIR Prints information about an autoinstalled directory, including its history and current version.""" parser = optparse.OptionParser(usage) parser.add_option("--reverse", dest="reverse", action="store_true", default=False, help="Print entries in chronological order (default is reverse)") options, args = parser.parse_args(argv) if len(args) > 1: parser.error("too many arguments") elif not args: parser.error("must specify directory") return options, args def print_log(d, options): if options.reverse: dlog = reversed(d.log) else: dlog = d.log for entry in dlog: print "%s %s" % (entry.version.application.name, entry.version.version) print "User: %s" % entry.user print "Date: %s" % entry.datetime.strftime("%a %b %0d %H:%M:%S %Y %z") print info = "Unknown" if isinstance(entry.source, log.TarballInstall): info = "Installed with tarball at:\n%s" % \ prettify(entry.source.location) print indent(info, 4) print class PipeToLess(object): def __enter__(self): self.proc = subprocess.Popen("less", stdin=subprocess.PIPE) self.old_stdout = sys.stdout sys.stdout = self.proc.stdin def __exit__(self, *args): if self.proc: self.proc.stdin.close() self.proc.wait() sys.stdout = self.old_stdout def prettify(loc): return loc.replace("/afs/athena.mit.edu/contrib/scripts", "~scripts") def indent(text, indent): # There should be a built-in return "\n".join([" " * indent + x for x in text.split("\n")])