comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:77665d8e2254
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - Command line utilities
4
5 @copyright: 2000, 2001, 2002 by Jürgen Hermann <jh@web.de>
6 @license: GNU GPL, see COPYING for details.
7 """
8 __version__ = "$Revision: 1.6 $"[11:-2]
9
10 # Imports
11 import os, sys
12
13 # Globals
14 flag_quiet = 0
15 script_module = '__main__'
16
17
18 #############################################################################
19 ### Logging
20 #############################################################################
21
22 def fatal(msgtext, **kw):
23 """ Print error msg to stderr and exit.
24 """
25 sys.stderr.write("FATAL ERROR: " + msgtext + "\n")
26 if kw.get('usage', 0):
27 maindict = vars(sys.modules[script_module])
28 if maindict.has_key('usage'):
29 maindict['usage']()
30 sys.exit(1)
31
32
33 def log(msgtext):
34 """ Optionally print error msg to stderr.
35 """
36 if not flag_quiet:
37 sys.stderr.write(msgtext + "\n")
38
39
40 #############################################################################
41 ### Commandline Support
42 #############################################################################
43
44 class Script:
45
46 def __init__(self, script, usage):
47 import sys, time
48
49 self.script_module = sys.modules[script]
50
51 global _start_time
52 _start_time = time.clock()
53
54 import optparse
55 from MoinMoin import version
56
57 cmd = self.script_module.__name__.split('.')[-1].replace('_', '-')
58 ver = self.script_module.__version__
59 rev = "%s (%s %s [%s])" % (
60 ver, version.project, version.release, version.revision)
61 sys.argv[0] = cmd
62
63 self.parser = optparse.OptionParser(
64 usage=
65 "%(cmd)s %(usage)s\n"
66 "\n"
67 "%(cmd)s v%(ver)s, Copyright (c) 2002, 2003 by Jürgen Hermann <jh@web.de>"
68 % {'cmd': cmd, 'usage': usage, 'ver': ver},
69 version=rev)
70 self.parser.add_option(
71 "-q", "--quiet",
72 action="store_true", dest="quiet",
73 help="Be quiet (no informational messages)"
74 )
75
76
77 def run(self):
78 """ Run the main function of a command.
79 """
80 global flag_quiet
81
82 showtime = 1
83 try:
84 try:
85 self.options, self.args = self.parser.parse_args()
86 flag_quiet = self.options.quiet
87 self.mainloop()
88 except KeyboardInterrupt:
89 log("*** Interrupted by user!")
90 except SystemExit:
91 showtime = 0
92 raise
93 finally:
94 if showtime: self.logRuntime()
95
96
97 def logRuntime(self):
98 """ Print the total command run time.
99 """
100 import time
101 log("Needed %.3f secs." % (time.clock() - _start_time,))
102