Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: mailbox.py
"""Read/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes."""
[0] Fix | Delete
[1] Fix | Delete
# Notes for authors of new mailbox subclasses:
[2] Fix | Delete
#
[3] Fix | Delete
# Remember to fsync() changes to disk before closing a modified file
[4] Fix | Delete
# or returning from a flush() method. See functions _sync_flush() and
[5] Fix | Delete
# _sync_close().
[6] Fix | Delete
[7] Fix | Delete
import sys
[8] Fix | Delete
import os
[9] Fix | Delete
import time
[10] Fix | Delete
import calendar
[11] Fix | Delete
import socket
[12] Fix | Delete
import errno
[13] Fix | Delete
import copy
[14] Fix | Delete
import email
[15] Fix | Delete
import email.message
[16] Fix | Delete
import email.generator
[17] Fix | Delete
import StringIO
[18] Fix | Delete
try:
[19] Fix | Delete
if sys.platform == 'os2emx':
[20] Fix | Delete
# OS/2 EMX fcntl() not adequate
[21] Fix | Delete
raise ImportError
[22] Fix | Delete
import fcntl
[23] Fix | Delete
except ImportError:
[24] Fix | Delete
fcntl = None
[25] Fix | Delete
[26] Fix | Delete
import warnings
[27] Fix | Delete
with warnings.catch_warnings():
[28] Fix | Delete
if sys.py3kwarning:
[29] Fix | Delete
warnings.filterwarnings("ignore", ".*rfc822 has been removed",
[30] Fix | Delete
DeprecationWarning)
[31] Fix | Delete
import rfc822
[32] Fix | Delete
[33] Fix | Delete
__all__ = [ 'Mailbox', 'Maildir', 'mbox', 'MH', 'Babyl', 'MMDF',
[34] Fix | Delete
'Message', 'MaildirMessage', 'mboxMessage', 'MHMessage',
[35] Fix | Delete
'BabylMessage', 'MMDFMessage', 'UnixMailbox',
[36] Fix | Delete
'PortableUnixMailbox', 'MmdfMailbox', 'MHMailbox', 'BabylMailbox' ]
[37] Fix | Delete
[38] Fix | Delete
class Mailbox:
[39] Fix | Delete
"""A group of messages in a particular place."""
[40] Fix | Delete
[41] Fix | Delete
def __init__(self, path, factory=None, create=True):
[42] Fix | Delete
"""Initialize a Mailbox instance."""
[43] Fix | Delete
self._path = os.path.abspath(os.path.expanduser(path))
[44] Fix | Delete
self._factory = factory
[45] Fix | Delete
[46] Fix | Delete
def add(self, message):
[47] Fix | Delete
"""Add message and return assigned key."""
[48] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[49] Fix | Delete
[50] Fix | Delete
def remove(self, key):
[51] Fix | Delete
"""Remove the keyed message; raise KeyError if it doesn't exist."""
[52] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[53] Fix | Delete
[54] Fix | Delete
def __delitem__(self, key):
[55] Fix | Delete
self.remove(key)
[56] Fix | Delete
[57] Fix | Delete
def discard(self, key):
[58] Fix | Delete
"""If the keyed message exists, remove it."""
[59] Fix | Delete
try:
[60] Fix | Delete
self.remove(key)
[61] Fix | Delete
except KeyError:
[62] Fix | Delete
pass
[63] Fix | Delete
[64] Fix | Delete
def __setitem__(self, key, message):
[65] Fix | Delete
"""Replace the keyed message; raise KeyError if it doesn't exist."""
[66] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[67] Fix | Delete
[68] Fix | Delete
def get(self, key, default=None):
[69] Fix | Delete
"""Return the keyed message, or default if it doesn't exist."""
[70] Fix | Delete
try:
[71] Fix | Delete
return self.__getitem__(key)
[72] Fix | Delete
except KeyError:
[73] Fix | Delete
return default
[74] Fix | Delete
[75] Fix | Delete
def __getitem__(self, key):
[76] Fix | Delete
"""Return the keyed message; raise KeyError if it doesn't exist."""
[77] Fix | Delete
if not self._factory:
[78] Fix | Delete
return self.get_message(key)
[79] Fix | Delete
else:
[80] Fix | Delete
return self._factory(self.get_file(key))
[81] Fix | Delete
[82] Fix | Delete
def get_message(self, key):
[83] Fix | Delete
"""Return a Message representation or raise a KeyError."""
[84] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[85] Fix | Delete
[86] Fix | Delete
def get_string(self, key):
[87] Fix | Delete
"""Return a string representation or raise a KeyError."""
[88] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[89] Fix | Delete
[90] Fix | Delete
def get_file(self, key):
[91] Fix | Delete
"""Return a file-like representation or raise a KeyError."""
[92] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[93] Fix | Delete
[94] Fix | Delete
def iterkeys(self):
[95] Fix | Delete
"""Return an iterator over keys."""
[96] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[97] Fix | Delete
[98] Fix | Delete
def keys(self):
[99] Fix | Delete
"""Return a list of keys."""
[100] Fix | Delete
return list(self.iterkeys())
[101] Fix | Delete
[102] Fix | Delete
def itervalues(self):
[103] Fix | Delete
"""Return an iterator over all messages."""
[104] Fix | Delete
for key in self.iterkeys():
[105] Fix | Delete
try:
[106] Fix | Delete
value = self[key]
[107] Fix | Delete
except KeyError:
[108] Fix | Delete
continue
[109] Fix | Delete
yield value
[110] Fix | Delete
[111] Fix | Delete
def __iter__(self):
[112] Fix | Delete
return self.itervalues()
[113] Fix | Delete
[114] Fix | Delete
def values(self):
[115] Fix | Delete
"""Return a list of messages. Memory intensive."""
[116] Fix | Delete
return list(self.itervalues())
[117] Fix | Delete
[118] Fix | Delete
def iteritems(self):
[119] Fix | Delete
"""Return an iterator over (key, message) tuples."""
[120] Fix | Delete
for key in self.iterkeys():
[121] Fix | Delete
try:
[122] Fix | Delete
value = self[key]
[123] Fix | Delete
except KeyError:
[124] Fix | Delete
continue
[125] Fix | Delete
yield (key, value)
[126] Fix | Delete
[127] Fix | Delete
def items(self):
[128] Fix | Delete
"""Return a list of (key, message) tuples. Memory intensive."""
[129] Fix | Delete
return list(self.iteritems())
[130] Fix | Delete
[131] Fix | Delete
def has_key(self, key):
[132] Fix | Delete
"""Return True if the keyed message exists, False otherwise."""
[133] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[134] Fix | Delete
[135] Fix | Delete
def __contains__(self, key):
[136] Fix | Delete
return self.has_key(key)
[137] Fix | Delete
[138] Fix | Delete
def __len__(self):
[139] Fix | Delete
"""Return a count of messages in the mailbox."""
[140] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[141] Fix | Delete
[142] Fix | Delete
def clear(self):
[143] Fix | Delete
"""Delete all messages."""
[144] Fix | Delete
for key in self.iterkeys():
[145] Fix | Delete
self.discard(key)
[146] Fix | Delete
[147] Fix | Delete
def pop(self, key, default=None):
[148] Fix | Delete
"""Delete the keyed message and return it, or default."""
[149] Fix | Delete
try:
[150] Fix | Delete
result = self[key]
[151] Fix | Delete
except KeyError:
[152] Fix | Delete
return default
[153] Fix | Delete
self.discard(key)
[154] Fix | Delete
return result
[155] Fix | Delete
[156] Fix | Delete
def popitem(self):
[157] Fix | Delete
"""Delete an arbitrary (key, message) pair and return it."""
[158] Fix | Delete
for key in self.iterkeys():
[159] Fix | Delete
return (key, self.pop(key)) # This is only run once.
[160] Fix | Delete
else:
[161] Fix | Delete
raise KeyError('No messages in mailbox')
[162] Fix | Delete
[163] Fix | Delete
def update(self, arg=None):
[164] Fix | Delete
"""Change the messages that correspond to certain keys."""
[165] Fix | Delete
if hasattr(arg, 'iteritems'):
[166] Fix | Delete
source = arg.iteritems()
[167] Fix | Delete
elif hasattr(arg, 'items'):
[168] Fix | Delete
source = arg.items()
[169] Fix | Delete
else:
[170] Fix | Delete
source = arg
[171] Fix | Delete
bad_key = False
[172] Fix | Delete
for key, message in source:
[173] Fix | Delete
try:
[174] Fix | Delete
self[key] = message
[175] Fix | Delete
except KeyError:
[176] Fix | Delete
bad_key = True
[177] Fix | Delete
if bad_key:
[178] Fix | Delete
raise KeyError('No message with key(s)')
[179] Fix | Delete
[180] Fix | Delete
def flush(self):
[181] Fix | Delete
"""Write any pending changes to the disk."""
[182] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[183] Fix | Delete
[184] Fix | Delete
def lock(self):
[185] Fix | Delete
"""Lock the mailbox."""
[186] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[187] Fix | Delete
[188] Fix | Delete
def unlock(self):
[189] Fix | Delete
"""Unlock the mailbox if it is locked."""
[190] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[191] Fix | Delete
[192] Fix | Delete
def close(self):
[193] Fix | Delete
"""Flush and close the mailbox."""
[194] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[195] Fix | Delete
[196] Fix | Delete
# Whether each message must end in a newline
[197] Fix | Delete
_append_newline = False
[198] Fix | Delete
[199] Fix | Delete
def _dump_message(self, message, target, mangle_from_=False):
[200] Fix | Delete
# Most files are opened in binary mode to allow predictable seeking.
[201] Fix | Delete
# To get native line endings on disk, the user-friendly \n line endings
[202] Fix | Delete
# used in strings and by email.Message are translated here.
[203] Fix | Delete
"""Dump message contents to target file."""
[204] Fix | Delete
if isinstance(message, email.message.Message):
[205] Fix | Delete
buffer = StringIO.StringIO()
[206] Fix | Delete
gen = email.generator.Generator(buffer, mangle_from_, 0)
[207] Fix | Delete
gen.flatten(message)
[208] Fix | Delete
buffer.seek(0)
[209] Fix | Delete
data = buffer.read().replace('\n', os.linesep)
[210] Fix | Delete
target.write(data)
[211] Fix | Delete
if self._append_newline and not data.endswith(os.linesep):
[212] Fix | Delete
# Make sure the message ends with a newline
[213] Fix | Delete
target.write(os.linesep)
[214] Fix | Delete
elif isinstance(message, str):
[215] Fix | Delete
if mangle_from_:
[216] Fix | Delete
message = message.replace('\nFrom ', '\n>From ')
[217] Fix | Delete
message = message.replace('\n', os.linesep)
[218] Fix | Delete
target.write(message)
[219] Fix | Delete
if self._append_newline and not message.endswith(os.linesep):
[220] Fix | Delete
# Make sure the message ends with a newline
[221] Fix | Delete
target.write(os.linesep)
[222] Fix | Delete
elif hasattr(message, 'read'):
[223] Fix | Delete
lastline = None
[224] Fix | Delete
while True:
[225] Fix | Delete
line = message.readline()
[226] Fix | Delete
if line == '':
[227] Fix | Delete
break
[228] Fix | Delete
if mangle_from_ and line.startswith('From '):
[229] Fix | Delete
line = '>From ' + line[5:]
[230] Fix | Delete
line = line.replace('\n', os.linesep)
[231] Fix | Delete
target.write(line)
[232] Fix | Delete
lastline = line
[233] Fix | Delete
if self._append_newline and lastline and not lastline.endswith(os.linesep):
[234] Fix | Delete
# Make sure the message ends with a newline
[235] Fix | Delete
target.write(os.linesep)
[236] Fix | Delete
else:
[237] Fix | Delete
raise TypeError('Invalid message type: %s' % type(message))
[238] Fix | Delete
[239] Fix | Delete
[240] Fix | Delete
class Maildir(Mailbox):
[241] Fix | Delete
"""A qmail-style Maildir mailbox."""
[242] Fix | Delete
[243] Fix | Delete
colon = ':'
[244] Fix | Delete
[245] Fix | Delete
def __init__(self, dirname, factory=rfc822.Message, create=True):
[246] Fix | Delete
"""Initialize a Maildir instance."""
[247] Fix | Delete
Mailbox.__init__(self, dirname, factory, create)
[248] Fix | Delete
self._paths = {
[249] Fix | Delete
'tmp': os.path.join(self._path, 'tmp'),
[250] Fix | Delete
'new': os.path.join(self._path, 'new'),
[251] Fix | Delete
'cur': os.path.join(self._path, 'cur'),
[252] Fix | Delete
}
[253] Fix | Delete
if not os.path.exists(self._path):
[254] Fix | Delete
if create:
[255] Fix | Delete
os.mkdir(self._path, 0700)
[256] Fix | Delete
for path in self._paths.values():
[257] Fix | Delete
os.mkdir(path, 0o700)
[258] Fix | Delete
else:
[259] Fix | Delete
raise NoSuchMailboxError(self._path)
[260] Fix | Delete
self._toc = {}
[261] Fix | Delete
self._toc_mtimes = {'cur': 0, 'new': 0}
[262] Fix | Delete
self._last_read = 0 # Records last time we read cur/new
[263] Fix | Delete
self._skewfactor = 0.1 # Adjust if os/fs clocks are skewing
[264] Fix | Delete
[265] Fix | Delete
def add(self, message):
[266] Fix | Delete
"""Add message and return assigned key."""
[267] Fix | Delete
tmp_file = self._create_tmp()
[268] Fix | Delete
try:
[269] Fix | Delete
self._dump_message(message, tmp_file)
[270] Fix | Delete
except BaseException:
[271] Fix | Delete
tmp_file.close()
[272] Fix | Delete
os.remove(tmp_file.name)
[273] Fix | Delete
raise
[274] Fix | Delete
_sync_close(tmp_file)
[275] Fix | Delete
if isinstance(message, MaildirMessage):
[276] Fix | Delete
subdir = message.get_subdir()
[277] Fix | Delete
suffix = self.colon + message.get_info()
[278] Fix | Delete
if suffix == self.colon:
[279] Fix | Delete
suffix = ''
[280] Fix | Delete
else:
[281] Fix | Delete
subdir = 'new'
[282] Fix | Delete
suffix = ''
[283] Fix | Delete
uniq = os.path.basename(tmp_file.name).split(self.colon)[0]
[284] Fix | Delete
dest = os.path.join(self._path, subdir, uniq + suffix)
[285] Fix | Delete
if isinstance(message, MaildirMessage):
[286] Fix | Delete
os.utime(tmp_file.name,
[287] Fix | Delete
(os.path.getatime(tmp_file.name), message.get_date()))
[288] Fix | Delete
# No file modification should be done after the file is moved to its
[289] Fix | Delete
# final position in order to prevent race conditions with changes
[290] Fix | Delete
# from other programs
[291] Fix | Delete
try:
[292] Fix | Delete
if hasattr(os, 'link'):
[293] Fix | Delete
os.link(tmp_file.name, dest)
[294] Fix | Delete
os.remove(tmp_file.name)
[295] Fix | Delete
else:
[296] Fix | Delete
os.rename(tmp_file.name, dest)
[297] Fix | Delete
except OSError, e:
[298] Fix | Delete
os.remove(tmp_file.name)
[299] Fix | Delete
if e.errno == errno.EEXIST:
[300] Fix | Delete
raise ExternalClashError('Name clash with existing message: %s'
[301] Fix | Delete
% dest)
[302] Fix | Delete
else:
[303] Fix | Delete
raise
[304] Fix | Delete
return uniq
[305] Fix | Delete
[306] Fix | Delete
def remove(self, key):
[307] Fix | Delete
"""Remove the keyed message; raise KeyError if it doesn't exist."""
[308] Fix | Delete
os.remove(os.path.join(self._path, self._lookup(key)))
[309] Fix | Delete
[310] Fix | Delete
def discard(self, key):
[311] Fix | Delete
"""If the keyed message exists, remove it."""
[312] Fix | Delete
# This overrides an inapplicable implementation in the superclass.
[313] Fix | Delete
try:
[314] Fix | Delete
self.remove(key)
[315] Fix | Delete
except KeyError:
[316] Fix | Delete
pass
[317] Fix | Delete
except OSError, e:
[318] Fix | Delete
if e.errno != errno.ENOENT:
[319] Fix | Delete
raise
[320] Fix | Delete
[321] Fix | Delete
def __setitem__(self, key, message):
[322] Fix | Delete
"""Replace the keyed message; raise KeyError if it doesn't exist."""
[323] Fix | Delete
old_subpath = self._lookup(key)
[324] Fix | Delete
temp_key = self.add(message)
[325] Fix | Delete
temp_subpath = self._lookup(temp_key)
[326] Fix | Delete
if isinstance(message, MaildirMessage):
[327] Fix | Delete
# temp's subdir and suffix were specified by message.
[328] Fix | Delete
dominant_subpath = temp_subpath
[329] Fix | Delete
else:
[330] Fix | Delete
# temp's subdir and suffix were defaults from add().
[331] Fix | Delete
dominant_subpath = old_subpath
[332] Fix | Delete
subdir = os.path.dirname(dominant_subpath)
[333] Fix | Delete
if self.colon in dominant_subpath:
[334] Fix | Delete
suffix = self.colon + dominant_subpath.split(self.colon)[-1]
[335] Fix | Delete
else:
[336] Fix | Delete
suffix = ''
[337] Fix | Delete
self.discard(key)
[338] Fix | Delete
tmp_path = os.path.join(self._path, temp_subpath)
[339] Fix | Delete
new_path = os.path.join(self._path, subdir, key + suffix)
[340] Fix | Delete
if isinstance(message, MaildirMessage):
[341] Fix | Delete
os.utime(tmp_path,
[342] Fix | Delete
(os.path.getatime(tmp_path), message.get_date()))
[343] Fix | Delete
# No file modification should be done after the file is moved to its
[344] Fix | Delete
# final position in order to prevent race conditions with changes
[345] Fix | Delete
# from other programs
[346] Fix | Delete
os.rename(tmp_path, new_path)
[347] Fix | Delete
[348] Fix | Delete
def get_message(self, key):
[349] Fix | Delete
"""Return a Message representation or raise a KeyError."""
[350] Fix | Delete
subpath = self._lookup(key)
[351] Fix | Delete
f = open(os.path.join(self._path, subpath), 'r')
[352] Fix | Delete
try:
[353] Fix | Delete
if self._factory:
[354] Fix | Delete
msg = self._factory(f)
[355] Fix | Delete
else:
[356] Fix | Delete
msg = MaildirMessage(f)
[357] Fix | Delete
finally:
[358] Fix | Delete
f.close()
[359] Fix | Delete
subdir, name = os.path.split(subpath)
[360] Fix | Delete
msg.set_subdir(subdir)
[361] Fix | Delete
if self.colon in name:
[362] Fix | Delete
msg.set_info(name.split(self.colon)[-1])
[363] Fix | Delete
msg.set_date(os.path.getmtime(os.path.join(self._path, subpath)))
[364] Fix | Delete
return msg
[365] Fix | Delete
[366] Fix | Delete
def get_string(self, key):
[367] Fix | Delete
"""Return a string representation or raise a KeyError."""
[368] Fix | Delete
f = open(os.path.join(self._path, self._lookup(key)), 'r')
[369] Fix | Delete
try:
[370] Fix | Delete
return f.read()
[371] Fix | Delete
finally:
[372] Fix | Delete
f.close()
[373] Fix | Delete
[374] Fix | Delete
def get_file(self, key):
[375] Fix | Delete
"""Return a file-like representation or raise a KeyError."""
[376] Fix | Delete
f = open(os.path.join(self._path, self._lookup(key)), 'rb')
[377] Fix | Delete
return _ProxyFile(f)
[378] Fix | Delete
[379] Fix | Delete
def iterkeys(self):
[380] Fix | Delete
"""Return an iterator over keys."""
[381] Fix | Delete
self._refresh()
[382] Fix | Delete
for key in self._toc:
[383] Fix | Delete
try:
[384] Fix | Delete
self._lookup(key)
[385] Fix | Delete
except KeyError:
[386] Fix | Delete
continue
[387] Fix | Delete
yield key
[388] Fix | Delete
[389] Fix | Delete
def has_key(self, key):
[390] Fix | Delete
"""Return True if the keyed message exists, False otherwise."""
[391] Fix | Delete
self._refresh()
[392] Fix | Delete
return key in self._toc
[393] Fix | Delete
[394] Fix | Delete
def __len__(self):
[395] Fix | Delete
"""Return a count of messages in the mailbox."""
[396] Fix | Delete
self._refresh()
[397] Fix | Delete
return len(self._toc)
[398] Fix | Delete
[399] Fix | Delete
def flush(self):
[400] Fix | Delete
"""Write any pending changes to disk."""
[401] Fix | Delete
# Maildir changes are always written immediately, so there's nothing
[402] Fix | Delete
# to do.
[403] Fix | Delete
pass
[404] Fix | Delete
[405] Fix | Delete
def lock(self):
[406] Fix | Delete
"""Lock the mailbox."""
[407] Fix | Delete
return
[408] Fix | Delete
[409] Fix | Delete
def unlock(self):
[410] Fix | Delete
"""Unlock the mailbox if it is locked."""
[411] Fix | Delete
return
[412] Fix | Delete
[413] Fix | Delete
def close(self):
[414] Fix | Delete
"""Flush and close the mailbox."""
[415] Fix | Delete
return
[416] Fix | Delete
[417] Fix | Delete
def list_folders(self):
[418] Fix | Delete
"""Return a list of folder names."""
[419] Fix | Delete
result = []
[420] Fix | Delete
for entry in os.listdir(self._path):
[421] Fix | Delete
if len(entry) > 1 and entry[0] == '.' and \
[422] Fix | Delete
os.path.isdir(os.path.join(self._path, entry)):
[423] Fix | Delete
result.append(entry[1:])
[424] Fix | Delete
return result
[425] Fix | Delete
[426] Fix | Delete
def get_folder(self, folder):
[427] Fix | Delete
"""Return a Maildir instance for the named folder."""
[428] Fix | Delete
return Maildir(os.path.join(self._path, '.' + folder),
[429] Fix | Delete
factory=self._factory,
[430] Fix | Delete
create=False)
[431] Fix | Delete
[432] Fix | Delete
def add_folder(self, folder):
[433] Fix | Delete
"""Create a folder and return a Maildir instance representing it."""
[434] Fix | Delete
path = os.path.join(self._path, '.' + folder)
[435] Fix | Delete
result = Maildir(path, factory=self._factory)
[436] Fix | Delete
maildirfolder_path = os.path.join(path, 'maildirfolder')
[437] Fix | Delete
if not os.path.exists(maildirfolder_path):
[438] Fix | Delete
os.close(os.open(maildirfolder_path, os.O_CREAT | os.O_WRONLY,
[439] Fix | Delete
0666))
[440] Fix | Delete
return result
[441] Fix | Delete
[442] Fix | Delete
def remove_folder(self, folder):
[443] Fix | Delete
"""Delete the named folder, which must be empty."""
[444] Fix | Delete
path = os.path.join(self._path, '.' + folder)
[445] Fix | Delete
for entry in os.listdir(os.path.join(path, 'new')) + \
[446] Fix | Delete
os.listdir(os.path.join(path, 'cur')):
[447] Fix | Delete
if len(entry) < 1 or entry[0] != '.':
[448] Fix | Delete
raise NotEmptyError('Folder contains message(s): %s' % folder)
[449] Fix | Delete
for entry in os.listdir(path):
[450] Fix | Delete
if entry != 'new' and entry != 'cur' and entry != 'tmp' and \
[451] Fix | Delete
os.path.isdir(os.path.join(path, entry)):
[452] Fix | Delete
raise NotEmptyError("Folder contains subdirectory '%s': %s" %
[453] Fix | Delete
(folder, entry))
[454] Fix | Delete
for root, dirs, files in os.walk(path, topdown=False):
[455] Fix | Delete
for entry in files:
[456] Fix | Delete
os.remove(os.path.join(root, entry))
[457] Fix | Delete
for entry in dirs:
[458] Fix | Delete
os.rmdir(os.path.join(root, entry))
[459] Fix | Delete
os.rmdir(path)
[460] Fix | Delete
[461] Fix | Delete
def clean(self):
[462] Fix | Delete
"""Delete old files in "tmp"."""
[463] Fix | Delete
now = time.time()
[464] Fix | Delete
for entry in os.listdir(os.path.join(self._path, 'tmp')):
[465] Fix | Delete
path = os.path.join(self._path, 'tmp', entry)
[466] Fix | Delete
if now - os.path.getatime(path) > 129600: # 60 * 60 * 36
[467] Fix | Delete
os.remove(path)
[468] Fix | Delete
[469] Fix | Delete
_count = 1 # This is used to generate unique file names.
[470] Fix | Delete
[471] Fix | Delete
def _create_tmp(self):
[472] Fix | Delete
"""Create a file in the tmp subdirectory and open and return it."""
[473] Fix | Delete
now = time.time()
[474] Fix | Delete
hostname = socket.gethostname()
[475] Fix | Delete
if '/' in hostname:
[476] Fix | Delete
hostname = hostname.replace('/', r'\057')
[477] Fix | Delete
if ':' in hostname:
[478] Fix | Delete
hostname = hostname.replace(':', r'\072')
[479] Fix | Delete
uniq = "%s.M%sP%sQ%s.%s" % (int(now), int(now % 1 * 1e6), os.getpid(),
[480] Fix | Delete
Maildir._count, hostname)
[481] Fix | Delete
path = os.path.join(self._path, 'tmp', uniq)
[482] Fix | Delete
try:
[483] Fix | Delete
os.stat(path)
[484] Fix | Delete
except OSError, e:
[485] Fix | Delete
if e.errno == errno.ENOENT:
[486] Fix | Delete
Maildir._count += 1
[487] Fix | Delete
try:
[488] Fix | Delete
return _create_carefully(path)
[489] Fix | Delete
except OSError, e:
[490] Fix | Delete
if e.errno != errno.EEXIST:
[491] Fix | Delete
raise
[492] Fix | Delete
else:
[493] Fix | Delete
raise
[494] Fix | Delete
[495] Fix | Delete
# Fall through to here if stat succeeded or open raised EEXIST.
[496] Fix | Delete
raise ExternalClashError('Name clash prevented file creation: %s' %
[497] Fix | Delete
path)
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function