diff options
| -rw-r--r-- | Mailman/bin/show_mm_cfg.py | 12 | ||||
| -rw-r--r-- | bin/dumpdb | 204 |
2 files changed, 102 insertions, 114 deletions
diff --git a/Mailman/bin/show_mm_cfg.py b/Mailman/bin/show_mm_cfg.py index 3626e5b23..f9acb000c 100644 --- a/Mailman/bin/show_mm_cfg.py +++ b/Mailman/bin/show_mm_cfg.py @@ -17,6 +17,7 @@ import re
import sys
+import pprint
import optparse
from Mailman import mm_cfg
@@ -56,6 +57,7 @@ def main(): for pattern in args:
patterns.append(re.compile(pattern, flag))
+ pp = pprint.PrettyPrinter(indent=4)
names = mm_cfg.__dict__.keys()
names.sort()
for name in names:
@@ -70,7 +72,15 @@ def main(): break
if not hit:
continue
- print name, '=', mm_cfg.__dict__[name]
+ value = mm_cfg.__dict__[name]
+ if isinstance(value, str):
+ if re.search('\n', value):
+ print '%s = """%s"""' %(name, value)
+ else:
+ print "%s = '%s'" % (name, value)
+ else:
+ print '%s = ' % name,
+ pp.pprint(value)
diff --git a/bin/dumpdb b/bin/dumpdb index 0bde09b80..59170c6d4 100644 --- a/bin/dumpdb +++ b/bin/dumpdb @@ -1,6 +1,6 @@ #! @PYTHON@ # -# Copyright (C) 1998-2005 by the Free Software Foundation, Inc. +# Copyright (C) 1998-2006 by the Free Software Foundation, Inc. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -16,142 +16,120 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -"""Dump the contents of any Mailman `database' file. - -Usage: %(PROGRAM)s [options] filename - -Options: - - --marshal/-m - Assume the file contains a Python marshal, overridding any automatic - guessing. - - --pickle/-p - Assume the file contains a Python pickle, overridding any automatic - guessing. - - --noprint/-n - Don't attempt to pretty print the object. This is useful if there's - some problem with the object and you just want to get an unpickled - representation. Useful with `python -i bin/dumpdb <file>'. In that - case, the root of the tree will be left in a global called "msg". - - --help/-h - Print this help message and exit - -If the filename ends with `.db', then it is assumed that the file contains a -Python marshal. If the file ends with `.pck' then it is assumed to contain a -Python pickle. In either case, if you want to override the default assumption --- or if the file ends in neither suffix -- use the -p or -m flags. -""" - -import os import sys -import getopt import pprint -from cPickle import load -from types import StringType +import cPickle +import marshal +import optparse import paths -# Import this /after/ paths so that the sys.path is properly hacked -from email.Generator import Generator -from Mailman.i18n import _ -PROGRAM = sys.argv[0] -COMMASPACE = ', ' - -try: - True, False -except NameError: - True = 1 - False = 0 +from Mailman import mm_cfg +from Mailman.i18n import _ +__i18n_templates__ = True -def usage(code, msg=''): - if code: - fd = sys.stderr - else: - fd = sys.stdout - print >> fd, _(__doc__) % globals() - if msg: - print >> fd, msg - sys.exit(code) +COMMASPACE = ', ' +def parseargs(): + parser = optparse.OptionParser(version=mm_cfg.MAILMAN_VERSION, + usage=_("""\ +%%prog [options] filename - -def main(): - try: - opts, args = getopt.getopt(sys.argv[1:], 'mphn', - ['marshal', 'pickle', 'help', 'noprint']) - except getopt.error, msg: - usage(1, msg) +Dump the contents of any Mailman `database' file. +If the filename ends with `.db', then it is assumed that the file contains a +Python marshal. If the file ends with `.pck' then it is assumed to contain a +Python pickle. In either case, if you want to override the default assumption +-- or if the file ends in neither suffix -- use the -p or -m flags.""")) + parser.add_option('-m', '--marshal', + default=False, action='store_true', + help=_("""\ +Assume the file contains a Python marshal, +overridding any automatic guessing.""")) + parser.add_option('-p', '--pickle', + default=False, action='store_true', + help=_("""\ +Assume the file contains a Python pickle, +overridding any automatic guessing.""")) + parser.add_option('-n', '--noprint', + default=False, action='store_true', + help=_("""\ +Don't attempt to pretty print the object. This is useful if there's +some problem with the object and you just want to get an unpickled +representation. Useful with `python -i bin/dumpdb <file>'. In that +case, the root of the tree will be left in a global called "msg".""")) + opts, args = parser.parse_args() # Options. # None == guess, 0 == pickle, 1 == marshal - filetype = None - doprint = True - - for opt, arg in opts: - if opt in ('-h', '--help'): - usage(0) - elif opt in ('-p', '--pickle'): - filetype = 0 - elif opt in ('-m', '--marshal'): - filetype = 1 - elif opt in ('-n', '--noprint'): - doprint = False - + opts.filetype = None + if opts.pickle: + opts.filetype = 0 + if opts.marshal: + opts.filetype = 1 + opts.doprint = not opts.noprint if len(args) < 1: - usage(1, _('No filename given.')) + parser.print_help() + print >> sys.stderr, _('No filename given.') + sys.exit(1) elif len(args) > 1: + parser.print_help() pargs = COMMASPACE.join(args) - usage(1, _('Bad arguments: %(pargs)s')) + print >> sys.stderr, _('Bad arguments: $pargs') + sys.exit(1) else: - filename = args[0] - - if filetype is None: - if filename.endswith('.db'): - filetype = 1 - elif filename.endswith('.pck'): - filetype = 0 + opts.filename = args[0] + if opts.filetype is None: + if opts.filename.endswith('.db'): + opts.filetype = 1 + elif opts.filename.endswith('.pck'): + opts.filetype = 0 else: - usage(1, _('Please specify either -p or -m.')) + parser.print_help() + print >> sys.stderr, _('Please specify either -p or -m.') + sys.exit(1) + return parser, opts, args + + + +def main(): + parser, opts, args = parseargs() # Handle dbs pp = pprint.PrettyPrinter(indent=4) - if filetype == 1: + if opts.filetype == 1: # BAW: this probably doesn't work if there are mixed types of .db # files (i.e. some marshals, some bdbs). - d = DumperSwitchboard().read(filename) - if doprint: - pp.pprint(d) - return d + load = marshal.load + typename = 'marshal' else: - fp = open(filename) - m = [] - try: - cnt = 1 - if doprint: - print _('[----- start pickle file -----]') - while True: - try: - obj = load(fp) - except EOFError: - if doprint: - print _('[----- end pickle file -----]') - break - if doprint: - print _('<----- start object %(cnt)s ----->') - if isinstance(obj, StringType): - print obj - else: - pp.pprint(obj) - cnt += 1 - m.append(obj) - finally: - fp.close() - return m + load = cPickle.load + typename = 'pickle' + fp = open(opts.filename) + m = [] + try: + cnt = 1 + if opts.doprint: + print _('[----- start $typename file -----]') + while True: + try: + obj = load(fp) + except EOFError: + if opts.doprint: + print _('[----- end $typename file -----]') + break + if opts.doprint: + print _('<----- start object $cnt ----->') + if isinstance(obj, str): + print obj + else: + pp.pprint(obj) + cnt += 1 + m.append(obj) + finally: + fp.close() + return m |
