16
|
1 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv>
|
|
2
|
21
|
3 """File access routines and classes with support for archives."""
|
|
4
|
|
5 from __future__ import division, with_statement
|
|
6
|
|
7 try:
|
|
8 from compat import *
|
|
9 except ImportError:
|
|
10 import __main__
|
|
11 __main__.import_error(sys.exc_info()[1])
|
|
12
|
|
13 import contextlib, os, shutil, sys
|
|
14
|
|
15 # You don't need to know about anything else.
|
|
16 __all__ = 'File',
|
|
17
|
|
18 # In these two variables, use full stops no matter what os.extsep is;
|
|
19 # all full stops will be converted to os.extsep on the fly
|
|
20 archives = 'tests.tar', 'tests.zip', 'tests.tgz', 'tests.tar.gz', 'tests.tbz2', 'tests.tar.bz2'
|
|
21 formats = {}
|
|
22
|
|
23 class Archive(object):
|
|
24 __slots__ = 'file'
|
|
25
|
|
26 if ABCMeta:
|
|
27 __metaclass__ = ABCMeta
|
|
28
|
|
29 def __new__(cls, path):
|
|
30 """
|
|
31 Create a new instance of the archive class corresponding
|
|
32 to the file name in the given path.
|
|
33 """
|
|
34 if cls is not Archive:
|
|
35 return object.__new__(cls)
|
|
36 else:
|
|
37 # Do this by hand rather than through os.path.splitext
|
|
38 # because we support multi-dotted file name extensions
|
|
39 ext = path.partition(os.path.extsep)[2]
|
|
40 while ext:
|
|
41 if ext in formats:
|
|
42 return formats[ext](path)
|
|
43 ext = ext.partition(os.path.extsep)[2]
|
|
44 raise LookupError("unsupported archive file name extension in file name '%s'" % filename)
|
|
45
|
|
46 @abstractmethod
|
|
47 def __init__(self, path): raise NotImplementedError
|
|
48
|
|
49 @abstractmethod
|
|
50 def extract(self, name, target): raise NotImplementedError
|
|
51
|
|
52 def __del__(self):
|
|
53 del self.file
|
16
|
54
|
21
|
55 try:
|
|
56 import tarfile
|
31
|
57 except ImportError:
|
|
58 TarArchive = None
|
|
59 else:
|
21
|
60 class TarArchive(Archive):
|
|
61 __slots__ = '__namelist'
|
|
62
|
|
63 def __init__(self, path):
|
|
64 self.file = tarfile.open(path)
|
|
65
|
|
66 def extract(self, name, target):
|
|
67 member = self.file.getmember(name)
|
|
68 member.name = target
|
|
69 self.file.extract(member)
|
|
70
|
|
71 # TODO: somehow automagically emulate universal line break support
|
|
72 def open(self, name):
|
|
73 return self.file.extractfile(name)
|
|
74
|
|
75 def exists(self, queried_name):
|
|
76 if not hasattr(self, '__namelist'):
|
|
77 names = set()
|
|
78 for name in self.file.getnames():
|
|
79 cutname = name
|
|
80 while cutname:
|
|
81 names.add(cutname)
|
|
82 cutname = cutname.rpartition('/')[0]
|
|
83 self.__namelist = frozenset(names)
|
|
84 return queried_name in self.__namelist
|
|
85
|
|
86 def __enter__(self):
|
|
87 if hasattr(self.file, '__enter__'):
|
|
88 self.file.__enter__()
|
|
89 return self
|
|
90
|
|
91 def __exit__(self, exc_type, exc_value, traceback):
|
|
92 if hasattr(self.file, '__exit__'):
|
|
93 return self.file.__exit__(exc_type, exc_value, traceback)
|
|
94 elif exc_type is None:
|
|
95 self.file.close()
|
|
96 else:
|
|
97 # This code was shamelessly copied from tarfile.py of Python 2.7
|
|
98 if not self.file._extfileobj:
|
|
99 self.file.fileobj.close()
|
|
100 self.file.closed = True
|
|
101
|
|
102 formats['tar'] = formats['tgz'] = formats['tar.gz'] = formats['tbz2'] = formats['tar.bz2'] = TarArchive
|
|
103
|
|
104 try:
|
|
105 import zipfile
|
31
|
106 except ImportError:
|
|
107 ZipArchive = None
|
|
108 else:
|
21
|
109 class ZipArchive(Archive):
|
|
110 __slots__ = '__namelist'
|
|
111
|
|
112 def __init__(self, path):
|
|
113 self.file = zipfile.ZipFile(path)
|
|
114
|
|
115 def extract(self, name, target):
|
|
116 if os.path.isabs(target):
|
|
117 # To my knowledge, this is as portable as it gets
|
|
118 path = os.path.join(os.path.splitdrive(target)[0], os.path.sep)
|
|
119 else:
|
|
120 path = None
|
|
121
|
|
122 member = self.file.getinfo(name)
|
|
123 member.filename = os.path.relpath(target, path)
|
|
124 # FIXME: 2.5 lacks ZipFile.extract
|
|
125 self.file.extract(member, path)
|
|
126
|
|
127 def open(self, name):
|
|
128 return self.file.open(name, 'rU')
|
|
129
|
|
130 def exists(self, queried_name):
|
|
131 if not hasattr(self, '__namelist'):
|
|
132 names = set()
|
|
133 for name in self.file.namelist():
|
|
134 cutname = name
|
|
135 while cutname:
|
|
136 names.add(cutname)
|
|
137 cutname = cutname.rpartition('/')[0]
|
|
138 self.__namelist = frozenset(names)
|
|
139 return queried_name in self.__namelist
|
|
140
|
|
141 def __enter__(self):
|
|
142 if hasattr(self.file, '__enter__'):
|
|
143 self.file.__enter__()
|
|
144 return self
|
|
145
|
|
146 def __exit__(self, exc_type, exc_value, traceback):
|
|
147 if hasattr(self.file, '__exit__'):
|
|
148 return self.file.__exit__(exc_type, exc_value, traceback)
|
|
149 else:
|
|
150 return self.file.close()
|
|
151
|
|
152 formats['zip'] = ZipArchive
|
|
153
|
|
154 # Remove unsupported archive formats and replace full stops
|
|
155 # with the platform-dependent file name extension separator
|
|
156 def issupported(filename, formats=formats):
|
|
157 ext = filename.partition('.')[2]
|
|
158 while ext:
|
|
159 if ext in formats: return True
|
|
160 ext = ext.partition('.')[2]
|
|
161 return False
|
|
162 archives = [filename.replace('.', os.path.extsep) for filename in filter(issupported, archives)]
|
|
163 formats = dict((item[0].replace('.', os.path.extsep), item[1]) for item in items(formats))
|
|
164
|
|
165 open_archives = {}
|
|
166
|
|
167 def open_archive(path):
|
|
168 if path in open_archives:
|
|
169 return open_archives[path]
|
|
170 else:
|
|
171 open_archives[path] = archive = Archive(path)
|
|
172 return archive
|
16
|
173
|
21
|
174 class File(object):
|
|
175 __slots__ = 'virtual_path', 'real_path', 'full_real_path', 'archive'
|
|
176
|
|
177 def __init__(self, virtpath, allow_root=False, msg='test data'):
|
|
178 self.virtual_path = virtpath
|
|
179 self.archive = None
|
|
180 if not self.realize_path('', tuple(comp.replace('.', os.path.extsep) for comp in virtpath.split('/')), allow_root):
|
|
181 raise IOError("%s file '%s' could not be found" % (msg, virtpath))
|
|
182
|
|
183 def realize_path(self, root, virtpath, allow_root=False, hastests=False):
|
|
184 if root and not os.path.exists(root):
|
|
185 return False
|
|
186 if len(virtpath) > 1:
|
|
187 if self.realize_path(os.path.join(root, virtpath[0]), virtpath[1:], allow_root, hastests):
|
|
188 return True
|
|
189 elif not hastests:
|
|
190 if self.realize_path(os.path.join(root, 'tests'), virtpath, allow_root, True):
|
|
191 return True
|
|
192 for archive in archives:
|
|
193 path = os.path.join(root, archive)
|
|
194 if os.path.exists(path):
|
|
195 if self.realize_path_archive(open_archive(path), '', virtpath, path):
|
|
196 return True
|
23
|
197 if self.realize_path(root, virtpath[1:], allow_root, hastests):
|
21
|
198 return True
|
|
199 else:
|
|
200 if not hastests:
|
|
201 path = os.path.join(root, 'tests', virtpath[0])
|
|
202 if os.path.exists(path):
|
|
203 self.full_real_path = self.real_path = path
|
|
204 return True
|
|
205 for archive in archives:
|
|
206 path = os.path.join(root, archive)
|
|
207 if os.path.exists(path):
|
|
208 if self.realize_path_archive(open_archive(path), '', virtpath, path):
|
|
209 return True
|
|
210 if hastests or allow_root:
|
|
211 path = os.path.join(root, virtpath[0])
|
|
212 if os.path.exists(path):
|
|
213 self.full_real_path = self.real_path = path
|
|
214 return True
|
|
215 return False
|
|
216
|
|
217 def realize_path_archive(self, archive, root, virtpath, archpath):
|
|
218 if root and not archive.exists(root):
|
|
219 return False
|
|
220 if root: path = ''.join((root, '/', virtpath[0]))
|
|
221 else: path = virtpath[0]
|
|
222 if len(virtpath) > 1:
|
|
223 if self.realize_path_archive(archive, path, virtpath[1:], archpath):
|
|
224 return True
|
|
225 elif self.realize_path_archive(archive, root, virtpath[1:], archpath):
|
|
226 return True
|
|
227 else:
|
|
228 if archive.exists(path):
|
|
229 self.archive = archive
|
|
230 self.real_path = path
|
|
231 self.full_real_path = os.path.join(archpath, *path.split('/'))
|
|
232 return True
|
|
233 return False
|
|
234
|
|
235 def open(self):
|
|
236 if self.archive:
|
|
237 file = self.archive.open(self.real_path)
|
|
238 if hasattr(file, '__exit__'):
|
|
239 return file
|
|
240 else:
|
|
241 return contextlib.closing(file)
|
|
242 else:
|
54
|
243 return open(self.real_path, 'rU')
|
21
|
244
|
|
245 def copy(self, target):
|
|
246 if self.archive:
|
|
247 self.archive.extract(self.real_path, target)
|
|
248 else:
|
|
249 shutil.copy(self.real_path, target) |