~mailman-coders/mailman/2.1

987 by Mark Sapiro
Ooops! The previous rev copied a configured file by mistake. Fixed.
1
#! @PYTHON@
1 by
This commit was manufactured by cvs2svn to create branch
2
#
1779 by Mark Sapiro
Bump copyright dates.
3
# Copyright (C) 1998-2018 by the Free Software Foundation, Inc.
1 by
This commit was manufactured by cvs2svn to create branch
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
81 by bwarsaw
Backporting from the HEAD -- bin and cron scripts
9
#
1 by
This commit was manufactured by cvs2svn to create branch
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
81 by bwarsaw
Backporting from the HEAD -- bin and cron scripts
14
#
1 by
This commit was manufactured by cvs2svn to create branch
15
# You should have received a copy of the GNU General Public License
81 by bwarsaw
Backporting from the HEAD -- bin and cron scripts
16
# along with this program; if not, write to the Free Software
749 by tkikuchi
FSF office has moved to 51 Franklin Street.
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1 by
This commit was manufactured by cvs2svn to create branch
18
19
"""Dump the contents of any Mailman `database' file.
20
21
Usage: %(PROGRAM)s [options] filename
22
23
Options:
24
25
    --marshal/-m
26
        Assume the file contains a Python marshal, overridding any automatic
27
        guessing.
28
29
    --pickle/-p
30
        Assume the file contains a Python pickle, overridding any automatic
31
        guessing.
32
33
    --noprint/-n
34
        Don't attempt to pretty print the object.  This is useful if there's
35
        some problem with the object and you just want to get an unpickled
36
        representation.  Useful with `python -i bin/dumpdb <file>'.  In that
37
        case, the root of the tree will be left in a global called "msg".
38
39
    --help/-h
40
        Print this help message and exit
41
42
If the filename ends with `.db', then it is assumed that the file contains a
43
Python marshal.  If the file ends with `.pck' then it is assumed to contain a
44
Python pickle.  In either case, if you want to override the default assumption
45
-- or if the file ends in neither suffix -- use the -p or -m flags.
46
"""
47
48
import sys
49
import getopt
50
import pprint
986 by Mark Sapiro
Backported dumpdb changes from the 3.0 branch to allow dumping of marshals.
51
import cPickle
52
import marshal
325 by bwarsaw
main(): Extend pickle file dumping to print every object in the pickle file.
53
from types import StringType
1 by
This commit was manufactured by cvs2svn to create branch
54
55
import paths
56
# Import this /after/ paths so that the sys.path is properly hacked
1619.1.1 by Yasuhito FUTATSUKI at POEM
Importing locale patch for command line utils, from RHEL6 rpm source
57
from Mailman.i18n import C_
1 by
This commit was manufactured by cvs2svn to create branch
58
59
PROGRAM = sys.argv[0]
60
COMMASPACE = ', '
61
325 by bwarsaw
main(): Extend pickle file dumping to print every object in the pickle file.
62
try:
63
    True, False
64
except NameError:
65
    True = 1
66
    False = 0
67
1 by
This commit was manufactured by cvs2svn to create branch
68
69

70
def usage(code, msg=''):
71
    if code:
72
        fd = sys.stderr
73
    else:
74
        fd = sys.stdout
1619.1.1 by Yasuhito FUTATSUKI at POEM
Importing locale patch for command line utils, from RHEL6 rpm source
75
    print >> fd, C_(__doc__) % globals()
1 by
This commit was manufactured by cvs2svn to create branch
76
    if msg:
77
        print >> fd, msg
78
    sys.exit(code)
79
80
81

82
def main():
83
    try:
84
        opts, args = getopt.getopt(sys.argv[1:], 'mphn',
85
                                   ['marshal', 'pickle', 'help', 'noprint'])
86
    except getopt.error, msg:
87
        usage(1, msg)
88
89
    # Options.
90
    # None == guess, 0 == pickle, 1 == marshal
91
    filetype = None
325 by bwarsaw
main(): Extend pickle file dumping to print every object in the pickle file.
92
    doprint = True
1 by
This commit was manufactured by cvs2svn to create branch
93
94
    for opt, arg in opts:
95
        if opt in ('-h', '--help'):
96
            usage(0)
97
        elif opt in ('-p', '--pickle'):
98
            filetype = 0
99
        elif opt in ('-m', '--marshal'):
100
            filetype = 1
101
        elif opt in ('-n', '--noprint'):
325 by bwarsaw
main(): Extend pickle file dumping to print every object in the pickle file.
102
            doprint = False
1 by
This commit was manufactured by cvs2svn to create branch
103
104
    if len(args) < 1:
1619.1.1 by Yasuhito FUTATSUKI at POEM
Importing locale patch for command line utils, from RHEL6 rpm source
105
        usage(1, C_('No filename given.'))
1 by
This commit was manufactured by cvs2svn to create branch
106
    elif len(args) > 1:
107
        pargs = COMMASPACE.join(args)
1619.1.1 by Yasuhito FUTATSUKI at POEM
Importing locale patch for command line utils, from RHEL6 rpm source
108
        usage(1, C_('Bad arguments: %(pargs)s'))
1 by
This commit was manufactured by cvs2svn to create branch
109
    else:
110
        filename = args[0]
111
112
    if filetype is None:
113
        if filename.endswith('.db'):
114
            filetype = 1
115
        elif filename.endswith('.pck'):
116
            filetype = 0
117
        else:
1619.1.1 by Yasuhito FUTATSUKI at POEM
Importing locale patch for command line utils, from RHEL6 rpm source
118
            usage(1, C_('Please specify either -p or -m.'))
1 by
This commit was manufactured by cvs2svn to create branch
119
120
    # Handle dbs
121
    pp = pprint.PrettyPrinter(indent=4)
122
    if filetype == 1:
986 by Mark Sapiro
Backported dumpdb changes from the 3.0 branch to allow dumping of marshals.
123
        load = marshal.load
124
        typename = 'marshal'
125
    else:
126
        load = cPickle.load
127
        typename = 'pickle'
128
    fp = open(filename)
129
    m = []
130
    try:
131
        cnt = 1
1 by
This commit was manufactured by cvs2svn to create branch
132
        if doprint:
1619.1.1 by Yasuhito FUTATSUKI at POEM
Importing locale patch for command line utils, from RHEL6 rpm source
133
            print C_('[----- start %(typename)s file -----]')
986 by Mark Sapiro
Backported dumpdb changes from the 3.0 branch to allow dumping of marshals.
134
        while True:
135
            try:
136
                obj = load(fp)
137
            except EOFError:
138
                if doprint:
1619.1.1 by Yasuhito FUTATSUKI at POEM
Importing locale patch for command line utils, from RHEL6 rpm source
139
                    print C_('[----- end %(typename)s file -----]')
986 by Mark Sapiro
Backported dumpdb changes from the 3.0 branch to allow dumping of marshals.
140
                break
708 by bwarsaw
main(): Honor doprint even if we're not in filetype == 1.
141
            if doprint:
1619.1.1 by Yasuhito FUTATSUKI at POEM
Importing locale patch for command line utils, from RHEL6 rpm source
142
                print C_('<----- start object %(cnt)s ----->')
986 by Mark Sapiro
Backported dumpdb changes from the 3.0 branch to allow dumping of marshals.
143
                if isinstance(obj, StringType):
144
                    print obj
145
                else:
146
                    pp.pprint(obj)
147
            cnt += 1
148
            m.append(obj)
149
    finally:
150
        fp.close()
151
    return m
1 by
This commit was manufactured by cvs2svn to create branch
152
153
154

155
if __name__ == '__main__':
156
    msg = main()