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