Package qm :: Module temporary_directory
[hide private]
[frames] | no frames]

Source Code for Module qm.temporary_directory

 1  ######################################################################## 
 2  # 
 3  # File:   temporary_directory.py 
 4  # Author: Mark Mitchell 
 5  # Date:   05/07/2003 
 6  # 
 7  # Contents: 
 8  #   TemporaryDirectory 
 9  # 
10  # Copyright (c) 2003 by CodeSourcery, LLC.  All rights reserved.  
11  # 
12  ######################################################################## 
13   
14  ######################################################################## 
15  # Imports 
16  ######################################################################## 
17   
18  import dircache 
19  import os 
20  import qm 
21  import sys 
22  import tempfile 
23   
24  ######################################################################## 
25  # Classes 
26  ######################################################################## 
27   
28 -class TemporaryDirectory:
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
36 - def __init__(self):
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
55 - def GetPath(self):
56 """Returns the path to the temporary directory. 57 58 returns -- The path to the temporary directory.""" 59 60 return self.__directory
61 62
63 - def __del__(self):
64 65 self.Remove()
66 67
68 - def Remove(self):
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
79 - def __RemoveDirectory(self, path):
80 """Remove the directory 'path'. 81 82 Removes 'path', after first removing all of its contents.""" 83 84 # Remove everything in the directory. 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 # Remove the directory itself. 92 os.rmdir(path)
93