Mercurial > ~astiob > upreckon > hgweb
annotate files.py @ 137:f4361d557929
Fixed a few lines with wrong indentation in _unix (don't copy&paste code, kids)
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Tue, 24 May 2011 20:55:16 +0100 |
parents | a9d2aa6810c7 |
children |
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 | |
16 | 46 |
21 | 47 try: |
48 import tarfile | |
31 | 49 except ImportError: |
50 TarArchive = None | |
51 else: | |
21 | 52 class TarArchive(Archive): |
132
cdd0f970d112
Fixed several small bugs in the files module
Oleg Oshmyan <chortos@inbox.lv>
parents:
98
diff
changeset
|
53 __slots__ = '_namelist' |
21 | 54 |
55 def __init__(self, path): | |
56 self.file = tarfile.open(path) | |
57 | |
58 def extract(self, name, target): | |
59 member = self.file.getmember(name) | |
60 member.name = target | |
61 self.file.extract(member) | |
62 | |
63 # TODO: somehow automagically emulate universal line break support | |
64 def open(self, name): | |
65 return self.file.extractfile(name) | |
66 | |
67 def exists(self, queried_name): | |
132
cdd0f970d112
Fixed several small bugs in the files module
Oleg Oshmyan <chortos@inbox.lv>
parents:
98
diff
changeset
|
68 if not hasattr(self, '_namelist'): |
21 | 69 names = set() |
70 for name in self.file.getnames(): | |
71 cutname = name | |
72 while cutname: | |
73 names.add(cutname) | |
74 cutname = cutname.rpartition('/')[0] | |
132
cdd0f970d112
Fixed several small bugs in the files module
Oleg Oshmyan <chortos@inbox.lv>
parents:
98
diff
changeset
|
75 self._namelist = frozenset(names) |
cdd0f970d112
Fixed several small bugs in the files module
Oleg Oshmyan <chortos@inbox.lv>
parents:
98
diff
changeset
|
76 return queried_name in self._namelist |
21 | 77 |
78 def __enter__(self): | |
79 if hasattr(self.file, '__enter__'): | |
80 self.file.__enter__() | |
81 return self | |
82 | |
83 def __exit__(self, exc_type, exc_value, traceback): | |
84 if hasattr(self.file, '__exit__'): | |
85 return self.file.__exit__(exc_type, exc_value, traceback) | |
86 elif exc_type is None: | |
87 self.file.close() | |
88 else: | |
89 # This code was shamelessly copied from tarfile.py of Python 2.7 | |
90 if not self.file._extfileobj: | |
91 self.file.fileobj.close() | |
92 self.file.closed = True | |
93 | |
94 formats['tar'] = formats['tgz'] = formats['tar.gz'] = formats['tbz2'] = formats['tar.bz2'] = TarArchive | |
95 | |
96 try: | |
97 import zipfile | |
31 | 98 except ImportError: |
99 ZipArchive = None | |
100 else: | |
21 | 101 class ZipArchive(Archive): |
132
cdd0f970d112
Fixed several small bugs in the files module
Oleg Oshmyan <chortos@inbox.lv>
parents:
98
diff
changeset
|
102 __slots__ = '_namelist' |
21 | 103 |
104 def __init__(self, path): | |
105 self.file = zipfile.ZipFile(path) | |
106 | |
107 def extract(self, name, target): | |
98
62a96d51bf94
Fixed ZipArchive.extract with relative paths on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
108 member = self.file.getinfo(name) |
62a96d51bf94
Fixed ZipArchive.extract with relative paths on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
109 # FIXME: 2.5 lacks ZipFile.extract |
21 | 110 if os.path.isabs(target): |
111 # To my knowledge, this is as portable as it gets | |
112 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
|
113 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
|
114 self.file.extract(member, path) |
21 | 115 else: |
98
62a96d51bf94
Fixed ZipArchive.extract with relative paths on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
116 member.filename = os.path.relpath(target) |
62a96d51bf94
Fixed ZipArchive.extract with relative paths on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
117 self.file.extract(member) |
21 | 118 |
119 def open(self, name): | |
120 return self.file.open(name, 'rU') | |
121 | |
122 def exists(self, queried_name): | |
132
cdd0f970d112
Fixed several small bugs in the files module
Oleg Oshmyan <chortos@inbox.lv>
parents:
98
diff
changeset
|
123 if not hasattr(self, '_namelist'): |
21 | 124 names = set() |
125 for name in self.file.namelist(): | |
126 cutname = name | |
127 while cutname: | |
128 names.add(cutname) | |
129 cutname = cutname.rpartition('/')[0] | |
132
cdd0f970d112
Fixed several small bugs in the files module
Oleg Oshmyan <chortos@inbox.lv>
parents:
98
diff
changeset
|
130 self._namelist = frozenset(names) |
cdd0f970d112
Fixed several small bugs in the files module
Oleg Oshmyan <chortos@inbox.lv>
parents:
98
diff
changeset
|
131 return queried_name in self._namelist |
21 | 132 |
133 def __enter__(self): | |
134 if hasattr(self.file, '__enter__'): | |
135 self.file.__enter__() | |
136 return self | |
137 | |
138 def __exit__(self, exc_type, exc_value, traceback): | |
139 if hasattr(self.file, '__exit__'): | |
140 return self.file.__exit__(exc_type, exc_value, traceback) | |
141 else: | |
142 return self.file.close() | |
143 | |
144 formats['zip'] = ZipArchive | |
145 | |
146 # Remove unsupported archive formats and replace full stops | |
147 # with the platform-dependent file name extension separator | |
148 def issupported(filename, formats=formats): | |
149 ext = filename.partition('.')[2] | |
150 while ext: | |
151 if ext in formats: return True | |
152 ext = ext.partition('.')[2] | |
153 return False | |
154 archives = [filename.replace('.', os.path.extsep) for filename in filter(issupported, archives)] | |
155 formats = dict((item[0].replace('.', os.path.extsep), item[1]) for item in items(formats)) | |
156 | |
157 open_archives = {} | |
158 | |
159 def open_archive(path): | |
160 if path in open_archives: | |
161 return open_archives[path] | |
162 else: | |
163 open_archives[path] = archive = Archive(path) | |
164 return archive | |
16 | 165 |
21 | 166 class File(object): |
167 __slots__ = 'virtual_path', 'real_path', 'full_real_path', 'archive' | |
168 | |
169 def __init__(self, virtpath, allow_root=False, msg='test data'): | |
170 self.virtual_path = virtpath | |
171 self.archive = None | |
172 if not self.realize_path('', tuple(comp.replace('.', os.path.extsep) for comp in virtpath.split('/')), allow_root): | |
173 raise IOError("%s file '%s' could not be found" % (msg, virtpath)) | |
174 | |
175 def realize_path(self, root, virtpath, allow_root=False, hastests=False): | |
176 if root and not os.path.exists(root): | |
177 return False | |
178 if len(virtpath) > 1: | |
179 if self.realize_path(os.path.join(root, virtpath[0]), virtpath[1:], allow_root, hastests): | |
180 return True | |
181 elif not hastests: | |
182 if self.realize_path(os.path.join(root, 'tests'), virtpath, allow_root, True): | |
183 return True | |
184 for archive in archives: | |
185 path = os.path.join(root, archive) | |
186 if os.path.exists(path): | |
187 if self.realize_path_archive(open_archive(path), '', virtpath, path): | |
188 return True | |
23 | 189 if self.realize_path(root, virtpath[1:], allow_root, hastests): |
21 | 190 return True |
191 else: | |
192 if not hastests: | |
193 path = os.path.join(root, 'tests', virtpath[0]) | |
194 if os.path.exists(path): | |
195 self.full_real_path = self.real_path = path | |
196 return True | |
197 for archive in archives: | |
198 path = os.path.join(root, archive) | |
199 if os.path.exists(path): | |
200 if self.realize_path_archive(open_archive(path), '', virtpath, path): | |
201 return True | |
202 if hastests or allow_root: | |
203 path = os.path.join(root, virtpath[0]) | |
204 if os.path.exists(path): | |
205 self.full_real_path = self.real_path = path | |
206 return True | |
207 return False | |
208 | |
209 def realize_path_archive(self, archive, root, virtpath, archpath): | |
210 if root and not archive.exists(root): | |
211 return False | |
212 if root: path = ''.join((root, '/', virtpath[0])) | |
213 else: path = virtpath[0] | |
214 if len(virtpath) > 1: | |
215 if self.realize_path_archive(archive, path, virtpath[1:], archpath): | |
216 return True | |
217 elif self.realize_path_archive(archive, root, virtpath[1:], archpath): | |
218 return True | |
219 else: | |
220 if archive.exists(path): | |
221 self.archive = archive | |
222 self.real_path = path | |
223 self.full_real_path = os.path.join(archpath, *path.split('/')) | |
224 return True | |
225 return False | |
226 | |
227 def open(self): | |
228 if self.archive: | |
229 file = self.archive.open(self.real_path) | |
230 if hasattr(file, '__exit__'): | |
231 return file | |
232 else: | |
233 return contextlib.closing(file) | |
234 else: | |
54 | 235 return open(self.real_path, 'rU') |
21 | 236 |
237 def copy(self, target): | |
238 if self.archive: | |
239 self.archive.extract(self.real_path, target) | |
240 else: | |
241 shutil.copy(self.real_path, target) |