mboxutils.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2013, Mahmoud Hashemi
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. #
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following
  13. # disclaimer in the documentation and/or other materials provided
  14. # with the distribution.
  15. #
  16. # * The names of the contributors may not be used to endorse or
  17. # promote products derived from this software without specific
  18. # prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. """Useful utilities for working with the `mbox`_-formatted
  32. mailboxes. Credit to Mark Williams for these.
  33. .. _mbox: https://en.wikipedia.org/wiki/Mbox
  34. """
  35. import mailbox
  36. import tempfile
  37. DEFAULT_MAXMEM = 4 * 1024 * 1024 # 4MB
  38. class mbox_readonlydir(mailbox.mbox):
  39. """A subclass of :class:`mailbox.mbox` suitable for use with mboxs
  40. insides a read-only mail directory, e.g., ``/var/mail``. Otherwise
  41. the API is exactly the same as the built-in mbox.
  42. Deletes messages via truncation, in the manner of `Heirloom mailx`_.
  43. Args:
  44. path (str): Path to the mbox file.
  45. factory (type): Message type (defaults to :class:`rfc822.Message`)
  46. create (bool): Create mailbox if it does not exist. (defaults
  47. to ``True``)
  48. maxmem (int): Specifies, in bytes, the largest sized mailbox
  49. to attempt to copy into memory. Larger mailboxes
  50. will be copied incrementally which is more
  51. hazardous. (defaults to 4MB)
  52. .. note::
  53. Because this truncates and rewrites parts of the mbox file,
  54. this class can corrupt your mailbox. Only use this if you know
  55. the built-in :class:`mailbox.mbox` does not work for your use
  56. case.
  57. .. _Heirloom mailx: http://heirloom.sourceforge.net/mailx.html
  58. """
  59. def __init__(self, path, factory=None, create=True, maxmem=1024 * 1024):
  60. mailbox.mbox.__init__(self, path, factory, create)
  61. self.maxmem = maxmem
  62. def flush(self):
  63. """Write any pending changes to disk. This is called on mailbox
  64. close and is usually not called explicitly.
  65. .. note::
  66. This deletes messages via truncation. Interruptions may
  67. corrupt your mailbox.
  68. """
  69. # Appending and basic assertions are the same as in mailbox.mbox.flush.
  70. if not self._pending:
  71. if self._pending_sync:
  72. # Messages have only been added, so syncing the file
  73. # is enough.
  74. mailbox._sync_flush(self._file)
  75. self._pending_sync = False
  76. return
  77. # In order to be writing anything out at all, self._toc must
  78. # already have been generated (and presumably has been modified
  79. # by adding or deleting an item).
  80. assert self._toc is not None
  81. # Check length of self._file; if it's changed, some other process
  82. # has modified the mailbox since we scanned it.
  83. self._file.seek(0, 2)
  84. cur_len = self._file.tell()
  85. if cur_len != self._file_length:
  86. raise mailbox.ExternalClashError('Size of mailbox file changed '
  87. '(expected %i, found %i)' %
  88. (self._file_length, cur_len))
  89. self._file.seek(0)
  90. # Truncation logic begins here. Mostly the same except we
  91. # can use tempfile because we're not doing rename(2).
  92. with tempfile.TemporaryFile() as new_file:
  93. new_toc = {}
  94. self._pre_mailbox_hook(new_file)
  95. for key in sorted(self._toc.keys()):
  96. start, stop = self._toc[key]
  97. self._file.seek(start)
  98. self._pre_message_hook(new_file)
  99. new_start = new_file.tell()
  100. while True:
  101. buffer = self._file.read(min(4096,
  102. stop - self._file.tell()))
  103. if buffer == '':
  104. break
  105. new_file.write(buffer)
  106. new_toc[key] = (new_start, new_file.tell())
  107. self._post_message_hook(new_file)
  108. self._file_length = new_file.tell()
  109. self._file.seek(0)
  110. new_file.seek(0)
  111. # Copy back our messages
  112. if self._file_length <= self.maxmem:
  113. self._file.write(new_file.read())
  114. else:
  115. while True:
  116. buffer = new_file.read(4096)
  117. if not buffer:
  118. break
  119. self._file.write(buffer)
  120. # Delete the rest.
  121. self._file.truncate()
  122. # Same wrap up.
  123. self._toc = new_toc
  124. self._pending = False
  125. self._pending_sync = False
  126. if self._locked:
  127. mailbox._lock_file(self._file, dotlock=False)