]> scripts.mit.edu Git - wizard.git/blob - wizard/command/info.py
Refactor get*() to .() with property decorator.
[wizard.git] / wizard / command / info.py
1 import optparse
2 import sys
3 import subprocess
4
5 from wizard import deploy
6
7 def main(argv, baton):
8     options, args = parse_args(argv)
9     d = deploy.Deployment.fromDir(args[0])
10     d.log # force the log to be loaded, to pre-empt errors
11     with PipeToLess():
12         print_log(d, options)
13
14 def parse_args(argv):
15     usage = """usage: %prog info [ARGS] DIR
16
17 Prints information about an autoinstalled directory,
18 including its history and current version."""
19     parser = optparse.OptionParser(usage)
20     parser.add_option("--reverse", dest="reverse", action="store_true",
21             default=False, help="Print entries in chronological order (default is reverse)")
22     options, args = parser.parse_args(argv)
23     if len(args) > 1:
24         parser.error("too many arguments")
25     elif not args:
26         parser.error("must specify directory")
27     return options, args
28
29 def print_log(d, options):
30     if options.reverse:
31         log = reversed(d.log)
32     else:
33         log = d.log
34     for entry in log:
35         print "%s %s" % (entry.version.application.name, entry.version.version)
36         print "User: %s" % entry.user
37         print "Date: %s" % entry.datetime.strftime("%a %b %0d %H:%M:%S %Y %z")
38         print
39         info = "Unknown"
40         if isinstance(entry.source, deploy.TarballInstall):
41             info = "Installed with tarball at:\n%s" % \
42                 prettify(entry.source.location)
43         print indent(info, 4)
44         print
45
46 class PipeToLess(object):
47     def __enter__(self):
48         self.proc = subprocess.Popen("less", stdin=subprocess.PIPE)
49         self.old_stdout = sys.stdout
50         sys.stdout = self.proc.stdin
51     def __exit__(self, *args):
52         if self.proc:
53             self.proc.stdin.close()
54             self.proc.wait()
55             sys.stdout = self.old_stdout
56
57 def prettify(loc):
58     return loc.replace("/afs/athena.mit.edu/contrib/scripts", "~scripts")
59
60 def indent(text, indent):
61     # There should be a built-in
62     return "\n".join([" " * indent + x for x in text.split("\n")])