]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/command/info.py
Move a bunch of summary items to full class commands.
[wizard.git] / wizard / command / info.py
index c5c335ce0bbdae74a4b60a2bcf32b20ac4995505..2fd47d267a79b3a803c01c4133842c551caae6be 100644 (file)
@@ -2,16 +2,16 @@ import optparse
 import sys
 import subprocess
 
-from wizard import deploy
+from wizard import deploy, log
 
-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")])
+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 main(argv, global_options):
+def parse_args(argv):
     usage = """usage: %prog info [ARGS] DIR
 
 Prints information about an autoinstalled directory,
@@ -24,29 +24,39 @@ including its history and current version."""
         parser.error("too many arguments")
     elif not args:
         parser.error("must specify directory")
-    d = deploy.Deployment.fromDir(args[0])
-    d.getLog() # force the log to be loaded, to pre-empt errors
-    proc = False
-    # This is prime candidate for refactoring. This code pipes
-    # stdout to less, so that you get scrolling and stuff.
-    if sys.stdout.isatty():
-        proc = subprocess.Popen("less", stdin=subprocess.PIPE)
-        sys.stdout = proc.stdin
-    try:
-        if options.reverse: munge = lambda x: x
-        else: munge = reversed
-        for entry in munge(d.getLog()):
-            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, deploy.TarballInstall):
-                info = "Installed with tarball at:\n%s" % \
-                    prettify(entry.source.location)
-            print indent(info, 4)
-            print
-    finally:
-        if proc:
-            proc.stdin.close()
-            proc.wait()
+    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")])