~mailman-coders/mailman/2.1

1 by
This commit was manufactured by cvs2svn to create branch
1
#! @PYTHON@
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.
9
# 
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.
14
# 
15
# You should have received a copy of the GNU General Public License
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
"""Re-generate the Pipermail gzip'd archive flat files.
20
21
This script should be run nightly from cron.  When run from the command line,
22
the following usage is understood:
23
24
Usage: %(program)s [-v] [-h] [listnames]
25
26
Where:
27
    --verbose
28
    -v
29
        print each file as it's being gzip'd
30
31
    --help
32
    -h
33
        print this message and exit
34
35
    listnames
36
        Optionally, only compress the .txt files for the named lists.  Without 
37
        this, all archivable lists are processed.
38
39
"""
40
41
import sys
42
import os
43
import time
44
from stat import *
45
import getopt
46
47
try:
48
    import gzip
49
except ImportError:
50
    gzip = None
51
52
import paths
53
# mm_cfg must be imported before the other modules, due to the side-effect of
54
# it hacking sys.paths to include site-packages.  Without this, running this
55
# script from cron with python -S will fail.
56
from Mailman import mm_cfg
57
from Mailman import Utils
58
from Mailman import MailList
1449 by Mark Sapiro
Fixed a NameError exception in cron/nightly_gzip when it tries to print
59
from Mailman import i18n
1 by
This commit was manufactured by cvs2svn to create branch
60
61
62

63
program = sys.argv[0]
64
VERBOSE = 0
65
1449 by Mark Sapiro
Fixed a NameError exception in cron/nightly_gzip when it tries to print
66
_ = i18n._
67
i18n.set_language(mm_cfg.DEFAULT_SERVER_LANGUAGE)
68
1 by
This commit was manufactured by cvs2svn to create branch
69
def usage(code, msg=''):
70
    if code:
71
        fd = sys.stderr
72
    else:
73
        fd = sys.stdout
74
    print >> fd, _(__doc__) % globals()
75
    if msg:
76
        print >> fd, msg
77
    sys.exit(code)
78
79
80

81
def compress(txtfile):
82
    if VERBOSE:
83
        print "gzip'ing:", txtfile
84
    infp = open(txtfile)
85
    outfp = gzip.open(txtfile+'.gz', 'wb', 6)
86
    outfp.write(infp.read())
87
    outfp.close()
88
    infp.close()
89
90
91

92
def main():
93
    global VERBOSE
94
    try:
95
        opts, args = getopt.getopt(sys.argv[1:], 'vh', ['verbose', 'help'])
96
    except getopt.error, msg:
97
        usage(1, msg)
98
99
    # defaults
100
    for opt, arg in opts:
101
        if opt in ('-h', '--help'):
102
            usage(0)
103
        elif opt in ('-v', '--verbose'):
104
            VERBOSE = 1
105
106
    # limit to the specified lists?
107
    if args:
108
        listnames = args
109
    else:
110
        listnames = Utils.list_names()
111
112
    # process all the specified lists
113
    for name in listnames:
114
        mlist = MailList.MailList(name, lock=0)
115
        if not mlist.archive:
116
            continue
117
        dir = mlist.archive_dir()
118
        try:
119
            allfiles = os.listdir(dir)
120
        except os.error:
121
            # has the list received any messages?  if not, last_post_time will
122
            # be zero, so it's not really a bogus archive dir.
123
            if mlist.last_post_time > 0:
124
                print 'List', name, 'has a bogus archive_directory:', dir
125
            continue
126
        if VERBOSE:
127
            print 'Processing list:', name
128
        files = []
129
        for f in allfiles:
130
            if f[-4:] <> '.txt':
131
                continue
132
            # stat both the .txt and .txt.gz files and append them only if 
133
            # the former is newer than the latter.
134
            txtfile = os.path.join(dir, f)
135
            gzpfile = txtfile + '.gz'
136
            txt_mtime = os.stat(txtfile)[ST_MTIME]
137
            try:
138
                gzp_mtime = os.stat(gzpfile)[ST_MTIME]
139
            except os.error:
140
                gzp_mtime = -1
141
            if txt_mtime > gzp_mtime:
142
                files.append(txtfile)
143
        for f in files:
144
            compress(f)
145
146
        
147

148
if __name__ == '__main__' and \
149
   gzip is not None and \
150
   mm_cfg.ARCHIVE_TO_MBOX in (1, 2) and \
151
   not mm_cfg.GZIP_ARCHIVE_TXT_FILES:
152
    # we're only going to run the nightly archiver if messages are archived to
153
    # the mbox, and the gzip file is not created on demand (i.e. for every
154
    # individual post).  This is the normal mode of operation.  Also, be sure
155
    # we can actually import the gzip module!
156
    omask = os.umask(002)
157
    try:
158
        main()
159
    finally:
160
        os.umask(omask)