1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import dircache
19 import os
20 import qm
21 import sys
22 import tempfile
23
24
25
26
27
29 """A 'TemporaryDirectory' is a directory for temporary files.
30
31 Creating a new 'TemporaryDirectory results in the creation of a
32 new directory in the file system. The directory is automatically
33 removed from the file system when the 'TemporaryDirectory' is
34 destroyed."""
35
37 """Construct a new 'TemporaryDirectory."""
38
39 self.__directory = None
40
41 dir_path = tempfile.mktemp()
42 try:
43 os.mkdir(dir_path, 0700)
44 except:
45 exc_info = sys.exc_info()
46 raise qm.common.QMException, \
47 qm.error("temp dir error",
48 dir_path=dir_path,
49 exc_class=str(exc_info[0]),
50 exc_arg=str(exc_info[1]))
51
52 self.__directory = dir_path
53
54
56 """Returns the path to the temporary directory.
57
58 returns -- The path to the temporary directory."""
59
60 return self.__directory
61
62
66
67
69 """Remove the temporary directory.
70
71 Removes the temporary directory, and all files and directories
72 contained within it, from the file system."""
73
74 if self.__directory is not None:
75 self.__RemoveDirectory(self.__directory)
76 self.__directory = None
77
78
80 """Remove the directory 'path'.
81
82 Removes 'path', after first removing all of its contents."""
83
84
85 for entry in dircache.listdir(path):
86 entry_path = os.path.join(path, entry)
87 if os.path.isdir(entry_path):
88 self.__RemoveDirectory(entry_path)
89 else:
90 os.unlink(entry_path)
91
92 os.rmdir(path)
93