diff MoinMoin/scripts/_util.py @ 0:77665d8e2254

tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0 (automatically generated log message) imported from: moin--main--1.5--base-0
author Thomas Waldmann <tw-public@gmx.de>
date Thu, 22 Sep 2005 15:09:50 +0000
parents
children 55ff4feb0f59
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/scripts/_util.py	Thu Sep 22 15:09:50 2005 +0000
@@ -0,0 +1,102 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - Command line utilities
+
+    @copyright: 2000, 2001, 2002 by Jürgen Hermann <jh@web.de>
+    @license: GNU GPL, see COPYING for details.
+"""
+__version__ = "$Revision: 1.6 $"[11:-2]
+
+# Imports
+import os, sys
+
+# Globals
+flag_quiet = 0
+script_module = '__main__'
+
+
+#############################################################################
+### Logging
+#############################################################################
+
+def fatal(msgtext, **kw):
+    """ Print error msg to stderr and exit.
+    """
+    sys.stderr.write("FATAL ERROR: " + msgtext + "\n")
+    if kw.get('usage', 0):
+        maindict = vars(sys.modules[script_module])
+        if maindict.has_key('usage'):
+            maindict['usage']()
+    sys.exit(1)
+
+
+def log(msgtext):
+    """ Optionally print error msg to stderr.
+    """
+    if not flag_quiet:
+        sys.stderr.write(msgtext + "\n")
+
+
+#############################################################################
+### Commandline Support
+#############################################################################
+
+class Script:
+
+    def __init__(self, script, usage):
+        import sys, time
+
+        self.script_module = sys.modules[script]
+
+        global _start_time
+        _start_time = time.clock()
+
+        import optparse
+        from MoinMoin import version
+
+        cmd = self.script_module.__name__.split('.')[-1].replace('_', '-')
+        ver = self.script_module.__version__
+        rev = "%s (%s %s [%s])" % (
+            ver, version.project, version.release, version.revision)
+        sys.argv[0] = cmd
+
+        self.parser = optparse.OptionParser(
+            usage=
+                "%(cmd)s %(usage)s\n"
+                "\n"
+                "%(cmd)s v%(ver)s, Copyright (c) 2002, 2003 by Jürgen Hermann <jh@web.de>"
+                % {'cmd': cmd, 'usage': usage, 'ver': ver},
+            version=rev)
+        self.parser.add_option(
+            "-q", "--quiet", 
+            action="store_true", dest="quiet",
+            help="Be quiet (no informational messages)"
+        )
+
+
+    def run(self):
+        """ Run the main function of a command.
+        """
+        global flag_quiet
+
+        showtime = 1
+        try:
+            try:
+                self.options, self.args = self.parser.parse_args()
+                flag_quiet = self.options.quiet
+                self.mainloop()
+            except KeyboardInterrupt:
+                log("*** Interrupted by user!")
+            except SystemExit:
+                showtime = 0
+                raise
+        finally:
+            if showtime: self.logRuntime()
+
+
+    def logRuntime(self):
+        """ Print the total command run time.
+        """
+        import time
+        log("Needed %.3f secs." % (time.clock() - _start_time,))
+