]> scripts.mit.edu Git - wizard.git/commitdiff
Factor out printing code to a simple logging class.
authorEdward Z. Yang <edwardzyang@thewritingpot.com>
Sun, 14 Jun 2009 00:39:32 +0000 (20:39 -0400)
committerEdward Z. Yang <edwardzyang@thewritingpot.com>
Sun, 14 Jun 2009 00:39:32 +0000 (20:39 -0400)
Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
bin/install-statistics

index 5bbc67d99a04745b72c748244426676731dac6eb..994baee9154cba33f57b60e7f3889dd882ce5114 100755 (executable)
@@ -101,6 +101,38 @@ class ApplicationVersion(object):
         return "    %-12s %3d  %s" \
             % (self.version, self.c, self.application._graph(self.c))
 
+class Printer(object):
+    def __init__(self, quiet, verbose):
+        self.i = 0
+        self.quiet = quiet
+        self.verbose = verbose
+        self.hanging = False
+    def tick(self):
+        self.i += 1
+        if not self.quiet and self.i % 10 == 0:
+            sys.stdout.write(".")
+            sys.stdout.flush()
+            self.hanging = True
+    def _hang(self):
+        if self.hanging:
+            self.hanging = False
+            print
+    def write(self, str = ""):
+        self._hang()
+        print str
+    def qwrite(self, str = ""):
+        if not self.quiet:
+            self._hang
+            print str
+    def tweet(self, str = ""):
+        if not self.quiet:
+            self._hang()
+            print str, # note comma
+    def chat(self, str = ""):
+        if self.verbose:
+            self._hang()
+            print str
+
 application_list = [
     "mediawiki", "wordpress", "joomla", "e107", "gallery2",
     "phpBB", "advancedbook", "phpical", "trac", "turbogears", "django",
@@ -116,7 +148,15 @@ def main():
 
 Scans all of the collected data from parallel-find.pl, and
 determines version histograms for our applications.  You may
-optionally pass application parameters to filter the installs."""
+optionally pass application parameters to filter the installs.
+
+Examples:
+    %prog
+        Basic usage
+    %prog mediawiki
+        Displays only MediaWiki statistics
+    %prog -v -q mediawiki-1.2.3
+        Displays all deployments of this version"""
     parser = optparse.OptionParser(usage)
     parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
             default=False, help="Print interesting directories")
@@ -140,15 +180,12 @@ optionally pass application parameters to filter the installs."""
     errors = 0
     unrecognized = 0
     processed = 0
+    printer = Printer(options.quiet, options.verbose)
     # I really don't like this boolean
     hanging = False # whether or not we last outputted a newline
-    if not options.quiet: print "Processing",
+    printer.tweet("Processing")
     for line in fi:
-        processed += 1
-        if not options.quiet and processed % 10 == 0:
-            sys.stdout.write(".")
-            sys.stdout.flush()
-            hanging = True
+        printer.tick()
         try:
             deploy = Deployment.parse(line)
         except DeploymentParseError:
@@ -158,11 +195,8 @@ optionally pass application parameters to filter the installs."""
             unrecognized += 1
             continue
         if deploy.application.name + "-" + str(deploy.version.version) in show:
-            if hanging:
-                hanging = False
-                print
-            print "%s-%s deployment at %s" \
-                % (deploy.application.name, deploy.version.version, deploy.location)
+            printer.write("%s-%s deployment at %s" \
+                % (deploy.application.name, deploy.version.version, deploy.location))
         elif deploy.application.name in show:
             pass
         else:
@@ -170,18 +204,14 @@ optionally pass application parameters to filter the installs."""
         deploy.count()
         if options.count_exists:
             r = deploy.count_exists(options.count_exists)
-            if r and options.verbose:
-                if hanging:
-                    hanging = False
-                    print
-                print "Found " + options.count_exists + " in " + deploy.location
-    if hanging: print
-    print
+            if r:
+                printer.chat("Found " + options.count_exists + " in " + deploy.location)
+    printer.write()
     for app in applications.values():
         if app.name not in show: continue
-        print app
-        print
-    print "With %d errors and %d unrecognized applications" % (errors, unrecognized)
+        printer.write(app)
+        printer.write()
+    printer.write("With %d errors and %d unrecognized applications" % (errors, unrecognized))
 
 if __name__ == "__main__":
     main()