Fixed a bug where the long --suffix option was not working (although the

short option, '-s' was).

Added time-based format directives to the --suffix option, so that you
can do things like specify --suffix='%B%Y' to create archives named
after the current month and year.

Added some more tests to test_archivemail.py
This commit is contained in:
Paul Rodger 2002-04-27 06:08:45 +00:00
parent a9920b731d
commit bc41b68389
8 changed files with 339 additions and 213 deletions

View File

@ -1,4 +1,12 @@
Version 0.4.4 - 27 April 2002
* Fixed a bug where the long --suffix option was not working (although the
short option, '-s' was).
* Added time-based format directives to the --suffix option, so that you
can do things like specify --suffix='%B%Y' to create archives named
after the current month and year
* Added some more tests to test_archivemail.py
Version 0.4.3 - 26 April 2002 Version 0.4.3 - 26 April 2002
* Fixed a couple of bugs where I was using python version 2.2 syntax that * Fixed a couple of bugs where I was using python version 2.2 syntax that
was giving errors in python v2.0 and v2.1. was giving errors in python v2.0 and v2.1.

View File

@ -1,5 +1,5 @@
VERSION=0.4.3 VERSION=0.4.4
VERSION_TAG=v$(subst .,_,$(VERSION)) VERSION_TAG=v$(subst .,_,$(VERSION))
TARFILE=archivemail-$(VERSION).tar.gz TARFILE=archivemail-$(VERSION).tar.gz

2
README
View File

@ -30,7 +30,7 @@ module, although the zlib module comes with most python installations. If you
are compiling your own version of python < version 2.2, make sure you are compiling your own version of python < version 2.2, make sure you
uncomment the 'zlib' moduile in Modules/Setup in the python source directory. uncomment the 'zlib' moduile in Modules/Setup in the python source directory.
You can check to see if you version of python has the 'zlib' module by You can check to see if your version of python has the 'zlib' module by
trying this: trying this:
flare:~$ python flare:~$ python

5
TODO
View File

@ -1,5 +1,5 @@
Goals for next minor release (0.4.4): Goals for next minor release (0.4.5):
------------------------------------- -------------------------------------
* Think about the best way to specify the names of archives created with * Think about the best way to specify the names of archives created with
possibly an --archive-name option. possibly an --archive-name option.
@ -9,12 +9,15 @@ Goals for next minor release (0.4.4):
Goals for next major release (0.5.0): Goals for next major release (0.5.0):
------------------------------------- -------------------------------------
* Add a --size option to only archive messages greater than a certain byte
size.
* Lock any original .gz files * Lock any original .gz files
- is this necessary? - is this necessary?
* Check for symlink attacks for tempfiles (although we don't use /var/tmp) * Check for symlink attacks for tempfiles (although we don't use /var/tmp)
Longer Term goals: Longer Term goals:
------------------ ------------------
* Add an option to not cut threads.
* Add MMDF mailbox support * Add MMDF mailbox support
* Add Babyl mailbox support * Add Babyl mailbox support
* Add option to archive depending on mailbox size threshold * Add option to archive depending on mailbox size threshold

View File

@ -22,7 +22,7 @@ Website: http://archivemail.sourceforge.net/
""" """
# global administrivia # global administrivia
__version__ = "archivemail v0.4.3" __version__ = "archivemail v0.4.4"
__cvs_id__ = "$Id$" __cvs_id__ = "$Id$"
__copyright__ = """Copyright (C) 2002 Paul Rodger <paul@paulrodger.com> __copyright__ = """Copyright (C) 2002 Paul Rodger <paul@paulrodger.com>
This is free software; see the source for copying conditions. There is NO This is free software; see the source for copying conditions. There is NO
@ -165,7 +165,7 @@ class Options:
opts, args = getopt.getopt(args, '?D:Vd:hno:qs:uv', opts, args = getopt.getopt(args, '?D:Vd:hno:qs:uv',
["date=", "days=", "delete", "dry-run", "help", ["date=", "days=", "delete", "dry-run", "help",
"include-flagged", "no-compress", "output-dir=", "include-flagged", "no-compress", "output-dir=",
"preserve-unread", "quiet", "suffix", "verbose", "preserve-unread", "quiet", "suffix=", "verbose",
"version", "warn-duplicate"]) "version", "warn-duplicate"])
except getopt.error, msg: except getopt.error, msg:
user_error(msg) user_error(msg)
@ -858,7 +858,11 @@ def archive(mailbox_name):
set_signal_handlers() set_signal_handlers()
os.umask(077) # saves setting permissions on mailboxes/tempfiles os.umask(077) # saves setting permissions on mailboxes/tempfiles
final_archive_name = mailbox_name + options.archive_suffix # allow the user to embed time formats such as '%B' in the suffix string
parsed_suffix = time.strftime(options.archive_suffix,
time.localtime(time.time()))
final_archive_name = mailbox_name + parsed_suffix
if options.output_dir: if options.output_dir:
final_archive_name = os.path.join(options.output_dir, final_archive_name = os.path.join(options.output_dir,
os.path.basename(final_archive_name)) os.path.basename(final_archive_name))

View File

@ -125,11 +125,66 @@ read.
<Option>-s <Replaceable/NAME/, --suffix=<Replaceable/NAME/</Option> <Option>-s <Replaceable/NAME/, --suffix=<Replaceable/NAME/</Option>
</Term> </Term>
<ListItem><Para> <ListItem><Para>
Use the suffix <Replaceable/NAME/ to create the filename used for archive Use the suffix <Replaceable/NAME/ to create the filename used for archives.
mailboxes. The default is <filename>_archive</filename>. For example, if you The default is <filename>_archive</filename>. For example, if you
run <Command/archivemail/ on a mailbox called run <Command/archivemail/ on a mailbox called
<filename>exsouthrock</filename>, any archive mailbox file will be created <filename>exsouthrock</filename>, the archive will be created
with the filename <filename>exsouthrock_archive.gz</filename>. with the filename <filename>exsouthrock_archive.gz</filename>.
</Para>
<Para>
<Replaceable/NAME/ is run through the &python; <application/time.strftime()/
function, which means that you can specify any of the following special
directives in <Replaceable/NAME/ to make archives named after the current
date:
<itemizedlist>
<listitem><para><option>%a</option>
Locale's abbreviated weekday name.</para></listitem>
<listitem><para><option>%A</option>
Locale's full weekday name.</para></listitem>
<listitem><para><option>%b</option>
Locale's abbreviated month name.</para></listitem>
<listitem><para><option>%B</option>
Locale's full month name.</para></listitem>
<listitem><para><option>%c</option>
Locale's appropriate date and time representation.</para></listitem>
<listitem><para><option>%d</option>
Day of the month as a decimal number [01,31].</para></listitem>
<listitem><para><option>%H</option>
Hour (24-hour clock) as a decimal number [00,23].</para></listitem>
<listitem><para><option>%I</option>
Hour (12-hour clock) as a decimal number [01,12].</para></listitem>
<listitem><para><option>%j</option>
Day of the year as a decimal number [001,366].</para></listitem>
<listitem><para><option>%m</option>
Month as a decimal number [01,12].</para></listitem>
<listitem><para><option>%M</option>
Minute as a decimal number [00,59].</para></listitem>
<listitem><para><option>%p</option>
Locale's equivalent of either AM or PM.</para></listitem>
<listitem><para><option>%S</option>
Second as a decimal number [00,61]. (1)</para></listitem>
<listitem><para><option>%U</option>
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.</para></listitem>
<listitem><para><option>%w</option>
Weekday as a decimal number [0(Sunday),6].</para></listitem>
<listitem><para><option>%W</option>
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.</para></listitem>
<listitem><para><option>%x</option>
Locale's appropriate date representation.</para></listitem>
<listitem><para><option>%X</option>
Locale's appropriate time representation.</para></listitem>
<listitem><para><option>%y</option>
Year without century as a decimal number [00,99].</para></listitem>
<listitem><para><option>%Y</option>
Year with century as a decimal number.</para></listitem>
<listitem><para><option>%Z</option>
Time zone name (or by no characters if no time zone exists).</para></listitem>
<listitem><para><option>%%</option>
A literal "%" character.</para></listitem>
</itemizedlist>
</Para></ListItem> </Para></ListItem>
</VarListEntry> </VarListEntry>
@ -296,6 +351,16 @@ are older than 180 days to a compressed mailbox called
</screen> </screen>
</Para> </Para>
<Para>
To archive all messages in the mailbox <filename>debian-user</filename> that
are older than 180 days to a compressed mailbox called
<filename>debian-user_April_2002.gz</filename> (where the current month and
year is April, 2002) in the current directory:
<screen>
<prompt>bash$ </prompt><userinput>archivemail --suffix '_%B_%Y' debian-user</userinput>
</screen>
</Para>
<Para> <Para>
To archive all messages in the mailbox <filename>cm-melb</filename> that To archive all messages in the mailbox <filename>cm-melb</filename> that
are older than the first of January 2002 to a compressed mailbox called are older than the first of January 2002 to a compressed mailbox called

View File

@ -19,7 +19,7 @@ check_python_version() # define & run this early - 'distutils.core' is new
from distutils.core import setup from distutils.core import setup
setup(name="archivemail", setup(name="archivemail",
version="0.4.3", version="0.4.4",
description="archive and compress old email", description="archive and compress old email",
license="GNU GPL", license="GNU GPL",
url="http://archivemail.sourceforge.net/", url="http://archivemail.sourceforge.net/",

View File

@ -55,6 +55,7 @@ import copy
import fcntl import fcntl
import filecmp import filecmp
import os import os
import re
import shutil import shutil
import stat import stat
import tempfile import tempfile
@ -252,6 +253,10 @@ class TestOptionDefaults(unittest.TestCase):
"""we should not archive flagged messages by default""" """we should not archive flagged messages by default"""
self.assertEqual(archivemail.options.include_flagged, 0) self.assertEqual(archivemail.options.include_flagged, 0)
def testPreserveUnread(self):
"""we should not preserve unread messages by default"""
self.assertEqual(archivemail.options.preserve_unread, 0)
########## archivemail.is_older_than_days() unit testing ################# ########## archivemail.is_older_than_days() unit testing #################
class TestIsTooOld(unittest.TestCase): class TestIsTooOld(unittest.TestCase):
@ -349,22 +354,31 @@ class TestArchiveMbox(unittest.TestCase):
archivemail.options.quiet = 1 archivemail.options.quiet = 1
def testOld(self): def testOld(self):
"""archiving an old mailbox should create an exact archive""" """archiving an old mailbox"""
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181)) for execute in ("package", "system"):
self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE] self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181))
self.copy_name = tempfile.mktemp() self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE]
shutil.copyfile(self.mbox_name, self.copy_name) self.copy_name = tempfile.mktemp()
archivemail.archive(self.mbox_name) shutil.copyfile(self.mbox_name, self.copy_name)
assert(os.path.exists(self.mbox_name)) if execute == "package":
self.assertEqual(os.path.getsize(self.mbox_name), 0) archivemail.options.quiet = 1
new_mode = os.stat(self.mbox_name)[stat.ST_MODE] archivemail.archive(self.mbox_name)
self.assertEqual(self.mbox_mode, new_mode) elif execute == "system":
archive_name = self.mbox_name + "_archive.gz" run = "./archivemail.py --quiet %s" % self.mbox_name
assert(os.path.exists(archive_name)) self.assertEqual(os.system(run), 0)
self.assertEqual(os.system("gzip -d %s" % archive_name), 0) else:
archive_name = self.mbox_name + "_archive" sys.exit(1)
assert(os.path.exists(archive_name)) assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0)) self.assertEqual(os.path.getsize(self.mbox_name), 0)
new_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.assertEqual(self.mbox_mode, new_mode)
archive_name = self.mbox_name + "_archive.gz"
assert(os.path.exists(archive_name))
self.assertEqual(os.system("gzip -d %s" % archive_name), 0)
archive_name = self.mbox_name + "_archive"
assert(os.path.exists(archive_name))
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0))
self.tearDown()
def testOldFromInBody(self): def testOldFromInBody(self):
"""archiving an old mailbox with 'From ' in the body""" """archiving an old mailbox with 'From ' in the body"""
@ -384,28 +398,6 @@ This is after the ^From line"""
assert(os.path.exists(archive_name)) assert(os.path.exists(archive_name))
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0)) assert(filecmp.cmp(archive_name, self.copy_name, shallow=0))
def testOldSystem(self):
"""archiving an old mailbox via system should create an archive"""
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181))
self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name)
run = "./archivemail.py --quiet %s" % self.mbox_name
self.assertEqual(os.system(run), 0)
assert(os.path.exists(self.mbox_name))
self.assertEqual(os.path.getsize(self.mbox_name), 0)
new_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.assertEqual(self.mbox_mode, new_mode)
archive_name = self.mbox_name + "_archive.gz"
assert(os.path.exists(archive_name))
self.assertEqual(os.system("gzip -d %s" % archive_name), 0)
archive_name = self.mbox_name + "_archive"
assert(os.path.exists(archive_name))
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0))
def testDateSystem(self): def testDateSystem(self):
"""test that the --date option works as expected""" """test that the --date option works as expected"""
test_headers = ( test_headers = (
@ -459,117 +451,88 @@ This is after the ^From line"""
self.tearDown() self.tearDown()
def testMixed(self): def testMixed(self):
"""archiving a mixed mailbox should make a mixed-archive""" """archiving a mixed mailbox"""
self.new_mbox = make_mbox(messages=3, hours_old=(24 * 179)) for execute in ("package", "system"):
self.old_mbox = make_mbox(messages=3, hours_old=(24 * 181)) self.new_mbox = make_mbox(messages=3, hours_old=(24 * 179))
self.mbox_name = tempfile.mktemp() self.old_mbox = make_mbox(messages=3, hours_old=(24 * 181))
shutil.copyfile(self.new_mbox, self.mbox_name) self.mbox_name = tempfile.mktemp()
append_file(self.old_mbox, self.mbox_name) shutil.copyfile(self.new_mbox, self.mbox_name)
archivemail.archive(self.mbox_name) append_file(self.old_mbox, self.mbox_name)
assert(os.path.exists(self.mbox_name)) if execute == "package":
assert(filecmp.cmp(self.new_mbox, self.mbox_name, shallow=0)) archivemail.options.quiet = 1
archive_name = self.mbox_name + "_archive.gz" archivemail.archive(self.mbox_name)
assert(os.path.exists(archive_name)) elif execute == "system":
self.assertEqual(os.system("gzip -d %s" % archive_name), 0) run = "./archivemail.py --quiet %s" % self.mbox_name
archive_name = self.mbox_name + "_archive" self.assertEqual(os.system(run), 0)
assert(os.path.exists(archive_name)) else:
assert(filecmp.cmp(archive_name, self.old_mbox, shallow=0)) sys.exit(1)
assert(os.path.exists(self.mbox_name))
def testMixedSystem(self): assert(filecmp.cmp(self.new_mbox, self.mbox_name, shallow=0))
"""archiving a mixed mailbox via system should make a mixed-archive""" archive_name = self.mbox_name + "_archive.gz"
self.new_mbox = make_mbox(messages=3, hours_old=(24 * 179)) assert(os.path.exists(archive_name))
self.old_mbox = make_mbox(messages=3, hours_old=(24 * 181)) self.assertEqual(os.system("gzip -d %s" % archive_name), 0)
self.mbox_name = tempfile.mktemp() archive_name = self.mbox_name + "_archive"
shutil.copyfile(self.new_mbox, self.mbox_name) assert(os.path.exists(archive_name))
append_file(self.old_mbox, self.mbox_name) assert(filecmp.cmp(archive_name, self.old_mbox, shallow=0))
run = "./archivemail.py --quiet %s" % self.mbox_name self.tearDown()
self.assertEqual(os.system(run), 0)
assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(self.new_mbox, self.mbox_name, shallow=0))
archive_name = self.mbox_name + "_archive.gz"
assert(os.path.exists(archive_name))
self.assertEqual(os.system("gzip -d %s" % archive_name), 0)
archive_name = self.mbox_name + "_archive"
assert(os.path.exists(archive_name))
assert(filecmp.cmp(archive_name, self.old_mbox, shallow=0))
def testNew(self): def testNew(self):
"""archiving a new mailbox should not create an archive""" """archiving a new mailbox"""
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 179)) for execute in ("package", "system"):
self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE] self.mbox_name = make_mbox(messages=3, hours_old=(24 * 179))
self.copy_name = tempfile.mktemp() self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE]
shutil.copyfile(self.mbox_name, self.copy_name) self.copy_name = tempfile.mktemp()
archivemail.archive(self.mbox_name) shutil.copyfile(self.mbox_name, self.copy_name)
assert(os.path.exists(self.mbox_name)) if execute == "package":
assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0)) archivemail.options.quiet = 1
new_mode = os.stat(self.mbox_name)[stat.ST_MODE] archivemail.archive(self.mbox_name)
self.assertEqual(self.mbox_mode, new_mode) elif execute == "system":
archive_name = self.mbox_name + "_archive.gz" run = "./archivemail.py --quiet %s" % self.mbox_name
assert(not os.path.exists(archive_name)) self.assertEqual(os.system(run), 0)
else:
sys.exit(1)
assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0))
new_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.assertEqual(self.mbox_mode, new_mode)
archive_name = self.mbox_name + "_archive.gz"
assert(not os.path.exists(archive_name))
self.tearDown()
def testNewSystem(self):
"""archiving a new mailbox via system should not create an archive"""
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 179))
self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name)
run = "./archivemail.py --quiet %s" % self.mbox_name
self.assertEqual(os.system(run), 0)
archivemail.archive(self.mbox_name)
assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0))
new_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.assertEqual(self.mbox_mode, new_mode)
archive_name = self.mbox_name + "_archive.gz"
assert(not os.path.exists(archive_name))
def testOldExisting(self): def testOldExisting(self):
"""archiving an old mailbox with an existing archive""" """archiving an old mailbox with an existing archive"""
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181)) for execute in ("package", "system"):
self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE] self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181))
self.copy_name = tempfile.mktemp() self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE]
archive_name = self.mbox_name + "_archive" self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name) archive_name = self.mbox_name + "_archive"
shutil.copyfile(self.mbox_name, archive_name) # archive has 3 msgs shutil.copyfile(self.mbox_name, self.copy_name)
append_file(self.mbox_name, self.copy_name) # copy now has 6 msgs shutil.copyfile(self.mbox_name, archive_name) # archive has 3 msgs
self.assertEqual(os.system("gzip %s" % archive_name), 0) append_file(self.mbox_name, self.copy_name) # copy now has 6 msgs
archivemail.archive(self.mbox_name) self.assertEqual(os.system("gzip %s" % archive_name), 0)
assert(os.path.exists(self.mbox_name)) if execute == "package":
self.assertEqual(os.path.getsize(self.mbox_name), 0) archivemail.options.quiet = 1
new_mode = os.stat(self.mbox_name)[stat.ST_MODE] archivemail.archive(self.mbox_name)
self.assertEqual(self.mbox_mode, new_mode) elif execute == "system":
archive_name = self.mbox_name + "_archive.gz" run = "./archivemail.py --quiet %s" % self.mbox_name
assert(os.path.exists(archive_name)) self.assertEqual(os.system(run), 0)
self.assertEqual(os.system("gzip -d %s" % archive_name), 0) else:
archive_name = self.mbox_name + "_archive" sys.exit(1)
assert(os.path.exists(archive_name)) assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0)) self.assertEqual(os.path.getsize(self.mbox_name), 0)
new_mode = os.stat(self.mbox_name)[stat.ST_MODE]
def testOldExistingSystem(self): self.assertEqual(self.mbox_mode, new_mode)
"""archiving an old mailbox via system with an existing archive""" archive_name = self.mbox_name + "_archive.gz"
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181)) assert(os.path.exists(archive_name))
self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE] self.assertEqual(os.system("gzip -d %s" % archive_name), 0)
self.copy_name = tempfile.mktemp() archive_name = self.mbox_name + "_archive"
archive_name = self.mbox_name + "_archive" assert(os.path.exists(archive_name))
shutil.copyfile(self.mbox_name, self.copy_name) assert(filecmp.cmp(archive_name, self.copy_name, shallow=0))
shutil.copyfile(self.mbox_name, archive_name) # archive has 3 msgs self.tearDown()
append_file(self.mbox_name, self.copy_name) # copy now has 6 msgs
self.assertEqual(os.system("gzip %s" % archive_name), 0)
run = "./archivemail.py --quiet %s" % self.mbox_name
self.assertEqual(os.system(run), 0)
assert(os.path.exists(self.mbox_name))
self.assertEqual(os.path.getsize(self.mbox_name), 0)
new_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.assertEqual(self.mbox_mode, new_mode)
archive_name = self.mbox_name + "_archive.gz"
assert(os.path.exists(archive_name))
self.assertEqual(os.system("gzip -d %s" % archive_name), 0)
archive_name = self.mbox_name + "_archive"
assert(os.path.exists(archive_name))
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0))
def testOldWeirdHeaders(self): def testOldWeirdHeaders(self):
"""old mailboxes with weird headers should create an exact archive""" """archiving old mailboxes with weird headers"""
weird_headers = ( weird_headers = (
{ # we should archive even though date < epoch { # we should archive even though date < epoch
'From_' : 'sender@dummy.domain Sat Feb 09 02:35:07 1962', 'From_' : 'sender@dummy.domain Sat Feb 09 02:35:07 1962',
@ -694,7 +657,6 @@ class TestArchiveMboxPreserveStatus(unittest.TestCase):
headers={"Status":"RO"}) headers={"Status":"RO"})
self.copy_name = tempfile.mktemp() self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name) shutil.copyfile(self.mbox_name, self.copy_name)
archivemail.archive(self.mbox_name) archivemail.archive(self.mbox_name)
assert(os.path.exists(self.mbox_name)) assert(os.path.exists(self.mbox_name))
self.assertEqual(os.path.getsize(self.mbox_name), 0) self.assertEqual(os.path.getsize(self.mbox_name), 0)
@ -710,7 +672,6 @@ class TestArchiveMboxPreserveStatus(unittest.TestCase):
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181)) self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181))
self.copy_name = tempfile.mktemp() self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name) shutil.copyfile(self.mbox_name, self.copy_name)
archivemail.archive(self.mbox_name) archivemail.archive(self.mbox_name)
assert(os.path.exists(self.mbox_name)) assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0)) assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0))
@ -726,6 +687,50 @@ class TestArchiveMboxPreserveStatus(unittest.TestCase):
os.remove(name) os.remove(name)
class TestArchiveMboxSuffix(unittest.TestCase):
"""make sure the 'suffix' option works"""
def testSuffix(self):
"""archiving with specified --suffix arguments"""
for suffix in ("_static_", "_%B_%Y", "-%Y-%m-%d"):
for execute in ("system_long", "system_short", "package"):
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181))
self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name)
if execute == "system_long":
run = "./archivemail.py --quiet --suffix='%s' %s" % \
(suffix, self.mbox_name)
self.assertEqual(os.system(run), 0)
elif execute == "system_short":
run = "./archivemail.py --quiet -s'%s' %s" % \
(suffix, self.mbox_name)
self.assertEqual(os.system(run), 0)
elif execute == "package":
archivemail.options.archive_suffix = suffix
archivemail.options.quiet = 1
archivemail.archive(self.mbox_name)
else:
sys.exit(1)
assert(os.path.exists(self.mbox_name))
self.assertEqual(os.path.getsize(self.mbox_name), 0)
parsed_suffix = time.strftime(suffix, time.localtime(time.time()))
archive_name = self.mbox_name + parsed_suffix + ".gz"
assert(os.path.exists(archive_name))
self.assertEqual(os.system("gzip -d %s" % archive_name), 0)
archive_name = re.sub("\.gz$", "", archive_name)
assert(os.path.exists(archive_name))
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0))
os.remove(archive_name)
self.tearDown()
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.archive_suffix = "_archive"
archive = self.mbox_name + "_archive"
for name in (self.mbox_name, self.copy_name, archive, archive + ".gz"):
if os.path.exists(name):
os.remove(name)
class TestArchiveMboxFlagged(unittest.TestCase): class TestArchiveMboxFlagged(unittest.TestCase):
"""make sure the 'include_flagged' option works""" """make sure the 'include_flagged' option works"""
def setUp(self): def setUp(self):
@ -738,7 +743,6 @@ class TestArchiveMboxFlagged(unittest.TestCase):
headers={"X-Status":"F"}) headers={"X-Status":"F"})
self.copy_name = tempfile.mktemp() self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name) shutil.copyfile(self.mbox_name, self.copy_name)
archivemail.archive(self.mbox_name) archivemail.archive(self.mbox_name)
assert(os.path.exists(self.mbox_name)) assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0)) assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0))
@ -752,7 +756,6 @@ class TestArchiveMboxFlagged(unittest.TestCase):
headers={"X-Status":"F"}) headers={"X-Status":"F"})
self.copy_name = tempfile.mktemp() self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name) shutil.copyfile(self.mbox_name, self.copy_name)
archivemail.archive(self.mbox_name) archivemail.archive(self.mbox_name)
assert(os.path.exists(self.mbox_name)) assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0)) assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0))
@ -766,7 +769,6 @@ class TestArchiveMboxFlagged(unittest.TestCase):
headers={"X-Status":"F"}) headers={"X-Status":"F"})
self.copy_name = tempfile.mktemp() self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name) shutil.copyfile(self.mbox_name, self.copy_name)
archivemail.archive(self.mbox_name) archivemail.archive(self.mbox_name)
assert(os.path.exists(self.mbox_name)) assert(os.path.exists(self.mbox_name))
self.assertEqual(os.path.getsize(self.mbox_name), 0) self.assertEqual(os.path.getsize(self.mbox_name), 0)
@ -798,69 +800,113 @@ class TestArchiveMboxUncompressed(unittest.TestCase):
archivemail.options.no_compress = 1 archivemail.options.no_compress = 1
def testOld(self): def testOld(self):
"""archiving an old mailbox uncompressed should create an ok archive""" """archiving an old mailbox uncompressed"""
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181)) for execute in ("package", "system"):
self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE] self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181))
self.copy_name = tempfile.mktemp() self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE]
shutil.copyfile(self.mbox_name, self.copy_name) self.copy_name = tempfile.mktemp()
archivemail.archive(self.mbox_name) shutil.copyfile(self.mbox_name, self.copy_name)
assert(os.path.exists(self.mbox_name)) if execute == "package":
self.assertEqual(os.path.getsize(self.mbox_name), 0) archivemail.options.no_compress = 1
new_mode = os.stat(self.mbox_name)[stat.ST_MODE] archivemail.options.quiet = 1
self.assertEqual(self.mbox_mode, new_mode) archivemail.archive(self.mbox_name)
archive_name = self.mbox_name + "_archive" elif execute == "system":
assert(os.path.exists(archive_name)) run = "./archivemail.py --no-compress --quiet %s" % \
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0)) self.mbox_name
assert(not os.path.exists(archive_name + ".gz")) self.assertEqual(os.system(run), 0)
else:
sys.exit(1)
assert(os.path.exists(self.mbox_name))
self.assertEqual(os.path.getsize(self.mbox_name), 0)
new_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.assertEqual(self.mbox_mode, new_mode)
archive_name = self.mbox_name + "_archive"
assert(os.path.exists(archive_name))
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0))
assert(not os.path.exists(archive_name + ".gz"))
self.tearDown()
def testNew(self): def testNew(self):
"""archiving a new mailbox uncompressed should not create an archive""" """archiving a new mailbox uncompressed"""
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 179)) for execute in ("package", "system"):
self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE] self.mbox_name = make_mbox(messages=3, hours_old=(24 * 179))
self.copy_name = tempfile.mktemp() self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE]
shutil.copyfile(self.mbox_name, self.copy_name) self.copy_name = tempfile.mktemp()
archivemail.archive(self.mbox_name) shutil.copyfile(self.mbox_name, self.copy_name)
assert(os.path.exists(self.mbox_name)) if execute == "package":
assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0)) archivemail.options.no_compress = 1
new_mode = os.stat(self.mbox_name)[stat.ST_MODE] archivemail.options.quiet = 1
self.assertEqual(self.mbox_mode, new_mode) archivemail.archive(self.mbox_name)
archive_name = self.mbox_name + "_archive" elif execute == "system":
assert(not os.path.exists(archive_name)) run = "./archivemail.py --no-compress --quiet %s" % \
assert(not os.path.exists(archive_name + ".gz")) self.mbox_name
self.assertEqual(os.system(run), 0)
else:
sys.exit(1)
assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(self.mbox_name, self.copy_name, shallow=0))
new_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.assertEqual(self.mbox_mode, new_mode)
archive_name = self.mbox_name + "_archive"
assert(not os.path.exists(archive_name))
assert(not os.path.exists(archive_name + ".gz"))
self.tearDown()
def testMixed(self): def testMixed(self):
"""archiving a mixed mailbox uncompressed should create a mixed archive""" """archiving a mixed mailbox uncompressed"""
self.new_mbox = make_mbox(messages=3, hours_old=(24 * 179)) for execute in ("package", "system"):
self.old_mbox = make_mbox(messages=3, hours_old=(24 * 181)) self.new_mbox = make_mbox(messages=3, hours_old=(24 * 179))
self.mbox_name = tempfile.mktemp() self.old_mbox = make_mbox(messages=3, hours_old=(24 * 181))
shutil.copyfile(self.new_mbox, self.mbox_name) self.mbox_name = tempfile.mktemp()
append_file(self.old_mbox, self.mbox_name) shutil.copyfile(self.new_mbox, self.mbox_name)
archivemail.archive(self.mbox_name) append_file(self.old_mbox, self.mbox_name)
assert(os.path.exists(self.mbox_name)) if execute == "package":
assert(filecmp.cmp(self.new_mbox, self.mbox_name, shallow=0)) archivemail.options.no_compress = 1
archive_name = self.mbox_name + "_archive" archivemail.options.quiet = 1
assert(os.path.exists(archive_name)) archivemail.archive(self.mbox_name)
assert(filecmp.cmp(archive_name, self.old_mbox, shallow=0)) elif execute == "system":
assert(not os.path.exists(archive_name + ".gz")) run = "./archivemail.py --no-compress --quiet %s" % \
self.mbox_name
self.assertEqual(os.system(run), 0)
else:
sys.exit(1)
assert(os.path.exists(self.mbox_name))
assert(filecmp.cmp(self.new_mbox, self.mbox_name, shallow=0))
archive_name = self.mbox_name + "_archive"
assert(os.path.exists(archive_name))
assert(filecmp.cmp(archive_name, self.old_mbox, shallow=0))
assert(not os.path.exists(archive_name + ".gz"))
self.tearDown()
def testOldExists(self): def testOldExists(self):
"""archiving an old mailbox without compressing with an existing archive""" """archiving an old mailbox uncopressed with an existing archive"""
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181)) for execute in ("package", "system"):
self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE] self.mbox_name = make_mbox(messages=3, hours_old=(24 * 181))
self.copy_name = tempfile.mktemp() self.mbox_mode = os.stat(self.mbox_name)[stat.ST_MODE]
archive_name = self.mbox_name + "_archive" self.copy_name = tempfile.mktemp()
shutil.copyfile(self.mbox_name, self.copy_name) archive_name = self.mbox_name + "_archive"
shutil.copyfile(self.mbox_name, archive_name) # archive has 3 msgs shutil.copyfile(self.mbox_name, self.copy_name)
append_file(self.mbox_name, self.copy_name) # copy now has 6 msgs shutil.copyfile(self.mbox_name, archive_name) # archive has 3 msgs
archivemail.archive(self.mbox_name) append_file(self.mbox_name, self.copy_name) # copy now has 6 msgs
assert(os.path.exists(self.mbox_name)) if execute == "package":
self.assertEqual(os.path.getsize(self.mbox_name), 0) archivemail.options.no_compress = 1
new_mode = os.stat(self.mbox_name)[stat.ST_MODE] archivemail.options.quiet = 1
self.assertEqual(self.mbox_mode, new_mode) archivemail.archive(self.mbox_name)
archive_name = self.mbox_name + "_archive" elif execute == "system":
assert(os.path.exists(archive_name)) run = "./archivemail.py --no-compress --quiet %s" % \
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0)) self.mbox_name
assert(not os.path.exists(archive_name + ".gz")) self.assertEqual(os.system(run), 0)
else:
sys.exit(1)
assert(os.path.exists(self.mbox_name))
self.assertEqual(os.path.getsize(self.mbox_name), 0)
new_mode = os.stat(self.mbox_name)[stat.ST_MODE]
self.assertEqual(self.mbox_mode, new_mode)
archive_name = self.mbox_name + "_archive"
assert(os.path.exists(archive_name))
assert(filecmp.cmp(archive_name, self.copy_name, shallow=0))
assert(not os.path.exists(archive_name + ".gz"))
self.tearDown()
def tearDown(self): def tearDown(self):
archivemail.options.quiet = 0 archivemail.options.quiet = 0