~futatuki/mailman/2.1-forbid-subscription

« back to all changes in this revision

Viewing changes to cron/nightly_gzip

  • Committer:
  • Date: 2003-01-02 05:25:50 UTC
  • Revision ID: vcs-imports@canonical.com-20030102052550-qqbl1i96tzg3bach
This commit was manufactured by cvs2svn to create branch
'Release_2_1-maint'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! @PYTHON@
 
2
 
3
# Copyright (C) 1998,1999,2000,2001,2002 by the Free Software Foundation, Inc.
 
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 
 
17
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
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
 
59
 
 
60
 
 
61
 
 
62
program = sys.argv[0]
 
63
VERBOSE = 0
 
64
 
 
65
def usage(code, msg=''):
 
66
    if code:
 
67
        fd = sys.stderr
 
68
    else:
 
69
        fd = sys.stdout
 
70
    print >> fd, _(__doc__) % globals()
 
71
    if msg:
 
72
        print >> fd, msg
 
73
    sys.exit(code)
 
74
 
 
75
 
 
76
 
 
77
def compress(txtfile):
 
78
    if VERBOSE:
 
79
        print "gzip'ing:", txtfile
 
80
    infp = open(txtfile)
 
81
    outfp = gzip.open(txtfile+'.gz', 'wb', 6)
 
82
    outfp.write(infp.read())
 
83
    outfp.close()
 
84
    infp.close()
 
85
 
 
86
 
 
87
 
 
88
def main():
 
89
    global VERBOSE
 
90
    try:
 
91
        opts, args = getopt.getopt(sys.argv[1:], 'vh', ['verbose', 'help'])
 
92
    except getopt.error, msg:
 
93
        usage(1, msg)
 
94
 
 
95
    # defaults
 
96
    for opt, arg in opts:
 
97
        if opt in ('-h', '--help'):
 
98
            usage(0)
 
99
        elif opt in ('-v', '--verbose'):
 
100
            VERBOSE = 1
 
101
 
 
102
    # limit to the specified lists?
 
103
    if args:
 
104
        listnames = args
 
105
    else:
 
106
        listnames = Utils.list_names()
 
107
 
 
108
    # process all the specified lists
 
109
    for name in listnames:
 
110
        mlist = MailList.MailList(name, lock=0)
 
111
        if not mlist.archive:
 
112
            continue
 
113
        dir = mlist.archive_dir()
 
114
        try:
 
115
            allfiles = os.listdir(dir)
 
116
        except os.error:
 
117
            # has the list received any messages?  if not, last_post_time will
 
118
            # be zero, so it's not really a bogus archive dir.
 
119
            if mlist.last_post_time > 0:
 
120
                print 'List', name, 'has a bogus archive_directory:', dir
 
121
            continue
 
122
        if VERBOSE:
 
123
            print 'Processing list:', name
 
124
        files = []
 
125
        for f in allfiles:
 
126
            if f[-4:] <> '.txt':
 
127
                continue
 
128
            # stat both the .txt and .txt.gz files and append them only if 
 
129
            # the former is newer than the latter.
 
130
            txtfile = os.path.join(dir, f)
 
131
            gzpfile = txtfile + '.gz'
 
132
            txt_mtime = os.stat(txtfile)[ST_MTIME]
 
133
            try:
 
134
                gzp_mtime = os.stat(gzpfile)[ST_MTIME]
 
135
            except os.error:
 
136
                gzp_mtime = -1
 
137
            if txt_mtime > gzp_mtime:
 
138
                files.append(txtfile)
 
139
        for f in files:
 
140
            compress(f)
 
141
 
 
142
        
 
143
 
 
144
if __name__ == '__main__' and \
 
145
   gzip is not None and \
 
146
   mm_cfg.ARCHIVE_TO_MBOX in (1, 2) and \
 
147
   not mm_cfg.GZIP_ARCHIVE_TXT_FILES:
 
148
    # we're only going to run the nightly archiver if messages are archived to
 
149
    # the mbox, and the gzip file is not created on demand (i.e. for every
 
150
    # individual post).  This is the normal mode of operation.  Also, be sure
 
151
    # we can actually import the gzip module!
 
152
    omask = os.umask(002)
 
153
    try:
 
154
        main()
 
155
    finally:
 
156
        os.umask(omask)