1 # Copyright (c) 2000 Autonomous Zone Industries
2 # Copyright (c) 2002-2007 Bryce "Zooko" Wilcox-O'Hearn
3 # This file is licensed under the
4 # GNU Lesser General Public License v2.1.
5 # See the file COPYING or visit http://www.gnu.org/ for details.
6 # Portions snarfed out of the Python standard library.
7 # The du part is due to Jim McCoy.
10 Futz with files like a pro.
13 import exceptions, os, stat, tempfile, time
15 from twisted.python import log
17 def rename(src, dst, tries=4, basedelay=0.1):
18 """ Here is a superkludge to workaround the fact that occasionally on
19 Windows some other process (e.g. an anti-virus scanner, a local search
20 engine, etc.) is looking at your file when you want to delete or move it,
21 and hence you can't. The horrible workaround is to sit and spin, trying
22 to delete it, for a short time and then give up.
24 With the default values of tries and basedelay this can block for less
27 @param tries: number of tries -- each time after the first we wait twice
28 as long as the previous wait
29 @param basedelay: how long to wait before the second try
31 for i in range(tries-1):
33 return os.rename(src, dst)
34 except EnvironmentError, le:
35 # XXX Tighten this to check if this is a permission denied error (possibly due to another Windows process having the file open and execute the superkludge only in this case.
36 log.msg("XXX KLUDGE Attempting to move file %s => %s; got %s; sleeping %s seconds" % (src, dst, le, basedelay,))
39 return os.rename(src, dst) # The last try.
41 def remove(f, tries=4, basedelay=0.1):
42 """ Here is a superkludge to workaround the fact that occasionally on
43 Windows some other process (e.g. an anti-virus scanner, a local search
44 engine, etc.) is looking at your file when you want to delete or move it,
45 and hence you can't. The horrible workaround is to sit and spin, trying
46 to delete it, for a short time and then give up.
48 With the default values of tries and basedelay this can block for less
51 @param tries: number of tries -- each time after the first we wait twice
52 as long as the previous wait
53 @param basedelay: how long to wait before the second try
56 os.chmod(f, stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
59 for i in range(tries-1):
62 except EnvironmentError, le:
63 # XXX Tighten this to check if this is a permission denied error (possibly due to another Windows process having the file open and execute the superkludge only in this case.
64 if not os.path.exists(f):
66 log.msg("XXX KLUDGE Attempting to remove file %s; got %s; sleeping %s seconds" % (f, le, basedelay,))
69 return os.remove(f) # The last try.
71 class NamedTemporaryDirectory:
73 This calls tempfile.mkdtemp(), stores the name of the dir in
74 self.name, and rmrf's the dir when it gets garbage collected or
77 def __init__(self, cleanup=True, *args, **kwargs):
78 """ If cleanup, then the directory will be rmrf'ed when the object is shutdown. """
79 self.cleanup = cleanup
80 self.name = tempfile.mkdtemp(*args, **kwargs)
83 return "<%s instance at %x %s>" % (self.__class__.__name__, id(self), self.name)
86 return self.__repr__()
96 if self.cleanup and hasattr(self, 'name'):
99 def make_dirs(dirname, mode=0777, strictmode=False):
101 A threadsafe and idempotent version of os.makedirs(). If the dir already
102 exists, do nothing and return without raising an exception. If this call
103 creates the dir, return without raising an exception. If there is an
104 error that prevents creation or if the directory gets deleted after
105 make_dirs() creates it and before make_dirs() checks that it exists, raise
108 @param strictmode if true, then make_dirs() will raise an exception if the
109 directory doesn't have the desired mode. For example, if the
110 directory already exists, and has a different mode than the one
111 specified by the mode parameter, then if strictmode is true,
112 make_dirs() will raise an exception, else it will ignore the
117 os.makedirs(dirname, mode)
121 if not os.path.isdir(dirname):
124 raise exceptions.IOError, "unknown error prevented creation of directory, or deleted the directory immediately after creation: %s" % dirname # careful not to construct an IOError with a 2-tuple, as that has a special meaning...
127 if hasattr(os, 'chmod'):
129 os.chmod(dirname, mode)
133 if strictmode and hasattr(os, 'stat'):
135 resmode = stat.S_IMODE(s.st_mode)
139 raise exceptions.IOError, "unknown error prevented setting correct mode of directory, or changed mode of the directory immediately after creation. dirname: %s, mode: %04o, resmode: %04o" % (dirname, mode, resmode,) # careful not to construct an IOError with a 2-tuple, as that has a special meaning...
143 A threadsafe and idempotent version of shutil.rmtree(). If the dir is
144 already gone, do nothing and return without raising an exception. If this
145 call removes the dir, return without raising an exception. If there is an
146 error that prevents deletion or if the directory gets created again after
147 rm_dir() deletes it and before rm_dir() checks that it is gone, raise an
152 os.chmod(dirname, stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
153 for f in os.listdir(dirname):
154 fullname = os.path.join(dirname, f)
155 if os.path.isdir(fullname):
160 except Exception, le:
161 # Ignore "No such file or directory"
162 if (not isinstance(le, OSError)) or le.args[0] != 2:
165 # Okay, now we've recursively removed everything, ignoring any "No
166 # such file or directory" errors, and collecting any other errors.
168 if os.path.exists(dirname):
172 raise OSError, "Failed to remove dir for unknown reason."
176 def remove_if_possible(f):
182 def open_or_create(fname, binarymode=True):
184 return open(fname, binarymode and "r+b" or "r+")
185 except EnvironmentError:
186 return open(fname, binarymode and "w+b" or "w+")
192 for root, dirs, files in os.walk(basedir):
194 fn = os.path.join(root, f)
195 size += os.path.getsize(fn)