~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/bin/cleanarch.py

  • Committer: Barry Warsaw
  • Date: 2012-03-28 15:25:13 UTC
  • Revision ID: barry@list.org-20120328152513-obmb97i2mrtj69i1
Remove a few more obsolete scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2001-2012 by the Free Software Foundation, Inc.
2
 
#
3
 
# This file is part of GNU Mailman.
4
 
#
5
 
# GNU Mailman is free software: you can redistribute it and/or modify it under
6
 
# the terms of the GNU General Public License as published by the Free
7
 
# Software Foundation, either version 3 of the License, or (at your option)
8
 
# any later version.
9
 
#
10
 
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11
 
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
 
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13
 
# more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License along with
16
 
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.
17
 
 
18
 
"""Clean up an .mbox archive file."""
19
 
 
20
 
import re
21
 
import sys
22
 
import mailbox
23
 
import optparse
24
 
 
25
 
from mailman.core.i18n import _
26
 
from mailman.version import MAILMAN_VERSION
27
 
 
28
 
 
29
 
cre = re.compile(mailbox.UnixMailbox._fromlinepattern)
30
 
# From RFC 2822, a header field name must contain only characters from 33-126
31
 
# inclusive, excluding colon.  I.e. from oct 41 to oct 176 less oct 072.  Must
32
 
# use re.match() so that it's anchored at the beginning of the line.
33
 
fre = re.compile(r'[\041-\071\073-\176]+')
34
 
 
35
 
 
36
 
 
37
 
def parseargs():
38
 
    parser = optparse.OptionParser(version=MAILMAN_VERSION,
39
 
                                   usage=_("""\
40
 
%prog [options] < inputfile > outputfile
41
 
 
42
 
The archiver looks for Unix-From lines separating messages in an mbox archive
43
 
file.  For compatibility, it specifically looks for lines that start with
44
 
'From ' -- i.e. the letters capital-F, lowercase-r, o, m, space, ignoring
45
 
everything else on the line.
46
 
 
47
 
Normally, any lines that start 'From ' in the body of a message should be
48
 
escaped such that a > character is actually the first on a line.  It is
49
 
possible though that body lines are not actually escaped.  This script
50
 
attempts to fix these by doing a stricter test of the Unix-From lines.  Any
51
 
lines that start From ' but do not pass this stricter test are escaped with a
52
 
'>' character."""))
53
 
    parser.add_option('-q', '--quiet',
54
 
                      default=False, action='store_true', help=_("""\
55
 
Don't print changed line information to standard error."""))
56
 
    parser.add_option('-s', '--status',
57
 
                      default=-1, type='int', help=_("""\
58
 
Print a '#' character for every n lines processed.  With a number less than or
59
 
equal to zero, suppress the '#' characters."""))
60
 
    parser.add_option('-n', '--dry-run',
61
 
                      default=False, action='store_true', help=_("""\
62
 
Don't actually output anything."""))
63
 
    opts, args = parser.parser_args()
64
 
    if args:
65
 
        parser.print_error(_('Unexpected arguments'))
66
 
    return parser, opts, args
67
 
 
68
 
 
69
 
 
70
 
def escape_line(line, lineno, quiet, output):
71
 
    if output:
72
 
        sys.stdout.write('>' + line)
73
 
    if not quiet:
74
 
        print >> sys.stderr, _('Unix-From line changed: $lineno')
75
 
        print >> sys.stderr, line[:-1]
76
 
 
77
 
 
78
 
 
79
 
def main():
80
 
    parser, opts, args = parseargs()
81
 
 
82
 
    lineno = 0
83
 
    statuscnt = 0
84
 
    messages = 0
85
 
    prevline = None
86
 
    while True:
87
 
        lineno += 1
88
 
        line = sys.stdin.readline()
89
 
        if not line:
90
 
            break
91
 
        if line.startswith('From '):
92
 
            if cre.match(line):
93
 
                # This is a real Unix-From line.  But it could be a message
94
 
                # /about/ Unix-From lines, so as a second order test, make
95
 
                # sure there's at least one RFC 2822 header following
96
 
                nextline = sys.stdin.readline()
97
 
                lineno += 1
98
 
                if not nextline:
99
 
                    # It was the last line of the mbox, so it couldn't have
100
 
                    # been a Unix-From
101
 
                    escape_line(line, lineno, quiet, output)
102
 
                    break
103
 
                fieldname = nextline.split(':', 1)
104
 
                if len(fieldname) < 2 or not fre.match(nextline):
105
 
                    # The following line was not a header, so this wasn't a
106
 
                    # valid Unix-From
107
 
                    escape_line(line, lineno, quiet, output)
108
 
                    if output:
109
 
                        sys.stdout.write(nextline)
110
 
                else:
111
 
                    # It's a valid Unix-From line
112
 
                    messages += 1
113
 
                    if output:
114
 
                        # Before we spit out the From_ line, make sure the
115
 
                        # previous line was blank.
116
 
                        if prevline is not None and prevline != '\n':
117
 
                            sys.stdout.write('\n')
118
 
                        sys.stdout.write(line)
119
 
                        sys.stdout.write(nextline)
120
 
            else:
121
 
                # This is a bogus Unix-From line
122
 
                escape_line(line, lineno, quiet, output)
123
 
        elif output:
124
 
            # Any old line
125
 
            sys.stdout.write(line)
126
 
        if status > 0 and (lineno % status) == 0:
127
 
            sys.stderr.write('#')
128
 
            statuscnt += 1
129
 
            if statuscnt > 50:
130
 
                print >> sys.stderr
131
 
                statuscnt = 0
132
 
        prevline = line
133
 
    print >> sys.stderr, _('%(messages)d messages found')