]> scripts.mit.edu Git - wizard.git/blob - lib/wizard/command/info.py
Convert migrate to logger, and misc refactoring.
[wizard.git] / lib / wizard / command / info.py
1 import optparse
2 import sys
3 import subprocess
4
5 import wizard.deploy as wd
6
7 def prettify(loc):
8     return loc.replace("/afs/athena.mit.edu/contrib/scripts", "~scripts")
9
10 def indent(text, indent):
11     # There should be a built-in
12     return "\n".join([" " * indent + x for x in text.split("\n")])
13
14 def info(argv, global_options):
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     deploy = wd.Deployment.fromDir(args[0])
28     deploy.getLog() # force the log to be loaded, to pre-empt errors
29     proc = False
30     # This is prime candidate for refactoring. This code pipes
31     # stdout to less, so that you get scrolling and stuff.
32     if sys.stdout.isatty():
33         proc = subprocess.Popen("less", stdin=subprocess.PIPE)
34         sys.stdout = proc.stdin
35     try:
36         if options.reverse: munge = lambda x: x
37         else: munge = reversed
38         for entry in munge(deploy.getLog()):
39             print "%s %s" % (entry.version.application.name, entry.version.version)
40             print "User: %s" % entry.user
41             print "Date: %s" % entry.datetime.strftime("%a %b %0d %H:%M:%S %Y %z")
42             print
43             info = "Unknown"
44             if isinstance(entry.source, wd.TarballInstall):
45                 info = "Installed with tarball at:\n%s" % \
46                     prettify(entry.source.location)
47             print indent(info, 4)
48             print
49     finally:
50         if proc:
51             proc.stdin.close()
52             proc.wait()