Mercurial > ~astiob > upreckon > hgweb
annotate upreckon/files.py @ 195:c2490e39fd70
Revamped the implementation of files.Archive subclasses
They now normalize and sanitize all paths and provide a listdir method.
TarArchive also ignores all files that are not regular files or directories.
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Sun, 14 Aug 2011 01:02:10 +0300 |
parents | 8c30a2c8a09e |
children | 67088c1765b4 |
rev | line source |
---|---|
194
8c30a2c8a09e
Updated copyright years in files.py
Oleg Oshmyan <chortos@inbox.lv>
parents:
193
diff
changeset
|
1 # Copyright (c) 2010-2011 Chortos-2 <chortos@inbox.lv> |
16 | 2 |
21 | 3 """File access routines and classes with support for archives.""" |
4 | |
5 from __future__ import division, with_statement | |
6 | |
146
d5b6708c1955
Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents:
133
diff
changeset
|
7 from .compat import * |
193
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
8 import contextlib, itertools, os, posixpath, re, shutil, sys |
21 | 9 |
10 # You don't need to know about anything else. | |
193
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
11 __all__ = 'File', 'regexp' |
21 | 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): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
19 __slots__ = () |
21 | 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 | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
46 |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
47 @abstractmethod |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
48 def open(self, name): raise NotImplementedError |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
49 |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
50 @abstractmethod |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
51 def exists(self, name): raise NotImplementedError |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
52 |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
53 @abstractmethod |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
54 def listdir(self, name): raise NotImplementedError |
16 | 55 |
21 | 56 try: |
57 import tarfile | |
31 | 58 except ImportError: |
59 TarArchive = None | |
60 else: | |
21 | 61 class TarArchive(Archive): |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
62 __slots__ = '_tarfile', '_files', '_dirs', '_names' |
21 | 63 |
64 def __init__(self, path): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
65 self._tarfile = tarfile.open(path) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
66 files, dirs = {}, set() |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
67 for member in self._tarfile.getmembers(): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
68 cutname = posixpath.normpath(member.name).lstrip('/') |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
69 while cutname.startswith('../'): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
70 cutname = cutname[3:] |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
71 if cutname in ('.', '..'): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
72 continue |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
73 if member.isfile(): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
74 files[cutname] = member |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
75 cutname = posixpath.dirname(cutname) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
76 elif not member.isdir(): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
77 continue |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
78 while cutname: |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
79 dirs.add(cutname) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
80 cutname = posixpath.dirname(cutname) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
81 self._files = files |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
82 self._dirs = frozenset(dirs) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
83 self._names = self._dirs | frozenset(files) |
21 | 84 |
85 def extract(self, name, target): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
86 member = self._files[posixpath.normpath(name)] |
21 | 87 member.name = target |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
88 self._tarfile.extract(member) |
21 | 89 |
90 def open(self, name): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
91 name = posixpath.normpath(name) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
92 return self._tarfile.extractfile(self._files[name]) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
93 |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
94 def exists(self, name): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
95 return posixpath.normpath(name) in self._names |
21 | 96 |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
97 def listdir(self, name): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
98 normname = posixpath.normpath(name) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
99 if normname not in self._dirs: |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
100 raise KeyError('No such directory: %r' % name) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
101 normname += '/' |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
102 len_normname = len(normname) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
103 return [fname for fname in self._names |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
104 if fname.startswith(normname) and |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
105 fname.find('/', len_normname) == -1] |
21 | 106 |
107 def __enter__(self): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
108 if hasattr(self._tarfile, '__enter__'): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
109 self._tarfile.__enter__() |
21 | 110 return self |
111 | |
112 def __exit__(self, exc_type, exc_value, traceback): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
113 if hasattr(self._tarfile, '__exit__'): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
114 return self._tarfile.__exit__(exc_type, exc_value, traceback) |
21 | 115 elif exc_type is None: |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
116 self._tarfile.close() |
21 | 117 else: |
118 # This code was shamelessly copied from tarfile.py of Python 2.7 | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
119 if not self._tarfile._extfileobj: |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
120 self._tarfile.fileobj.close() |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
121 self._tarfile.closed = True |
21 | 122 |
123 formats['tar'] = formats['tgz'] = formats['tar.gz'] = formats['tbz2'] = formats['tar.bz2'] = TarArchive | |
124 | |
125 try: | |
126 import zipfile | |
31 | 127 except ImportError: |
128 ZipArchive = None | |
129 else: | |
21 | 130 class ZipArchive(Archive): |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
131 __slots__ = '_zipfile', '_files', '_dirs', '_names' |
21 | 132 |
133 def __init__(self, path): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
134 self._zipfile = zipfile.ZipFile(path) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
135 files, dirs = {}, set() |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
136 for member in self._zipfile.infolist(): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
137 cutname = posixpath.normpath(member.filename).lstrip('/') |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
138 while cutname.startswith('../'): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
139 cutname = cutname[3:] |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
140 if cutname in ('.', '..'): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
141 continue |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
142 if not member.filename.endswith('/'): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
143 files[cutname] = member |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
144 cutname = posixpath.dirname(cutname) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
145 while cutname: |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
146 dirs.add(cutname) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
147 cutname = posixpath.dirname(cutname) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
148 self._files = files |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
149 self._dirs = frozenset(dirs) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
150 self._names = self._dirs | frozenset(files) |
21 | 151 |
152 def extract(self, name, target): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
153 member = self._files[posixpath.normpath(name)] |
98
62a96d51bf94
Fixed ZipArchive.extract with relative paths on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
154 # FIXME: 2.5 lacks ZipFile.extract |
21 | 155 if os.path.isabs(target): |
156 # To my knowledge, this is as portable as it gets | |
157 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
|
158 member.filename = os.path.relpath(target, path) |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
159 self._zipfile.extract(member, path) |
21 | 160 else: |
98
62a96d51bf94
Fixed ZipArchive.extract with relative paths on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents:
91
diff
changeset
|
161 member.filename = os.path.relpath(target) |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
162 self._zipfile.extract(member) |
21 | 163 |
164 def open(self, name): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
165 name = posixpath.normpath(name) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
166 # FIXME: 2.5 lacks ZipFile.open |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
167 return self._zipfile.open(self._files[name]) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
168 |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
169 def exists(self, name): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
170 return posixpath.normpath(name) in self._names |
21 | 171 |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
172 def listdir(self, name): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
173 normname = posixpath.normpath(name) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
174 if normname not in self._dirs: |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
175 raise KeyError('No such directory: %r' % name) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
176 normname += '/' |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
177 len_normname = len(normname) |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
178 return [fname for fname in self._names |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
179 if fname.startswith(normname) and |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
180 fname.find('/', len_normname) == -1] |
21 | 181 |
182 def __enter__(self): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
183 if hasattr(self._zipfile, '__enter__'): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
184 self._zipfile.__enter__() |
21 | 185 return self |
186 | |
187 def __exit__(self, exc_type, exc_value, traceback): | |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
188 if hasattr(self._zipfile, '__exit__'): |
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
189 return self._zipfile.__exit__(exc_type, exc_value, traceback) |
21 | 190 else: |
195
c2490e39fd70
Revamped the implementation of files.Archive subclasses
Oleg Oshmyan <chortos@inbox.lv>
parents:
194
diff
changeset
|
191 return self._zipfile.close() |
21 | 192 |
193 formats['zip'] = ZipArchive | |
194 | |
195 # Remove unsupported archive formats and replace full stops | |
196 # with the platform-dependent file name extension separator | |
197 def issupported(filename, formats=formats): | |
198 ext = filename.partition('.')[2] | |
199 while ext: | |
200 if ext in formats: return True | |
201 ext = ext.partition('.')[2] | |
202 return False | |
203 archives = [filename.replace('.', os.path.extsep) for filename in filter(issupported, archives)] | |
204 formats = dict((item[0].replace('.', os.path.extsep), item[1]) for item in items(formats)) | |
205 | |
206 open_archives = {} | |
207 | |
208 def open_archive(path): | |
209 if path in open_archives: | |
210 return open_archives[path] | |
211 else: | |
212 open_archives[path] = archive = Archive(path) | |
213 return archive | |
16 | 214 |
21 | 215 class File(object): |
216 __slots__ = 'virtual_path', 'real_path', 'full_real_path', 'archive' | |
217 | |
218 def __init__(self, virtpath, allow_root=False, msg='test data'): | |
219 self.virtual_path = virtpath | |
220 self.archive = None | |
221 if not self.realize_path('', tuple(comp.replace('.', os.path.extsep) for comp in virtpath.split('/')), allow_root): | |
222 raise IOError("%s file '%s' could not be found" % (msg, virtpath)) | |
223 | |
224 def realize_path(self, root, virtpath, allow_root=False, hastests=False): | |
225 if root and not os.path.exists(root): | |
226 return False | |
227 if len(virtpath) > 1: | |
228 if self.realize_path(os.path.join(root, virtpath[0]), virtpath[1:], allow_root, hastests): | |
229 return True | |
230 elif not hastests: | |
231 if self.realize_path(os.path.join(root, 'tests'), virtpath, allow_root, True): | |
232 return True | |
233 for archive in archives: | |
234 path = os.path.join(root, archive) | |
235 if os.path.exists(path): | |
236 if self.realize_path_archive(open_archive(path), '', virtpath, path): | |
237 return True | |
23 | 238 if self.realize_path(root, virtpath[1:], allow_root, hastests): |
21 | 239 return True |
240 else: | |
241 if not hastests: | |
242 path = os.path.join(root, 'tests', virtpath[0]) | |
243 if os.path.exists(path): | |
244 self.full_real_path = self.real_path = path | |
245 return True | |
246 for archive in archives: | |
247 path = os.path.join(root, archive) | |
248 if os.path.exists(path): | |
249 if self.realize_path_archive(open_archive(path), '', virtpath, path): | |
250 return True | |
251 if hastests or allow_root: | |
252 path = os.path.join(root, virtpath[0]) | |
253 if os.path.exists(path): | |
254 self.full_real_path = self.real_path = path | |
255 return True | |
256 return False | |
257 | |
178
0d657576b1ac
tests directories are now searched for within archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
174
diff
changeset
|
258 def realize_path_archive(self, archive, root, virtpath, archpath, hastests=False): |
21 | 259 if root and not archive.exists(root): |
260 return False | |
178
0d657576b1ac
tests directories are now searched for within archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
174
diff
changeset
|
261 path = posixpath.join(root, virtpath[0]) |
21 | 262 if len(virtpath) > 1: |
263 if self.realize_path_archive(archive, path, virtpath[1:], archpath): | |
264 return True | |
265 elif self.realize_path_archive(archive, root, virtpath[1:], archpath): | |
266 return True | |
267 else: | |
268 if archive.exists(path): | |
269 self.archive = archive | |
270 self.real_path = path | |
271 self.full_real_path = os.path.join(archpath, *path.split('/')) | |
272 return True | |
178
0d657576b1ac
tests directories are now searched for within archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
174
diff
changeset
|
273 if not hastests: |
0d657576b1ac
tests directories are now searched for within archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
174
diff
changeset
|
274 if self.realize_path_archive(archive, posixpath.join(root, 'tests'), virtpath, archpath, True): |
0d657576b1ac
tests directories are now searched for within archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
174
diff
changeset
|
275 return True |
21 | 276 return False |
277 | |
278 def open(self): | |
279 if self.archive: | |
280 file = self.archive.open(self.real_path) | |
281 if hasattr(file, '__exit__'): | |
282 return file | |
283 else: | |
284 return contextlib.closing(file) | |
285 else: | |
174
e0b2fbd7ebe0
Improved built-in output validator; added conf. var. binary
Oleg Oshmyan <chortos@inbox.lv>
parents:
155
diff
changeset
|
286 return open(self.real_path, 'rb') |
21 | 287 |
288 def copy(self, target): | |
289 if self.archive: | |
290 self.archive.extract(self.real_path, target) | |
291 else: | |
193
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
292 shutil.copy(self.real_path, target) |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
293 |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
294 def regexp(pattern, hastests=False, droplast=False): |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
295 # FIXME: add test archives |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
296 if not pattern: |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
297 yield os.curdir, '' |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
298 return |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
299 dirname, basename = posixpath.split(pattern) |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
300 if hastests: |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
301 dirnames = regexp(dirname, True) |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
302 else: |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
303 dirnames = itertools.chain(regexp(dirname), regexp(posixpath.join(dirname, 'tests'), True, True)) |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
304 reobj = re.compile(pattern + '$', re.UNICODE) |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
305 for dirname, vdirname in dirnames: |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
306 try: |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
307 names = os.listdir(dirname) |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
308 except OSError: |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
309 continue |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
310 for name in names: |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
311 path = os.path.join(dirname, name) |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
312 vpath = posixpath.join(vdirname, name) |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
313 if re.match(reobj, vpath): |
a76cdc26ba9d
Added conf. var. match and match='regexp' for non-archives
Oleg Oshmyan <chortos@inbox.lv>
parents:
178
diff
changeset
|
314 yield path, vpath if not droplast else vdirname |