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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
2
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
3 """File access routines and classes with support for archives."""
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
4
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
5 from __future__ import division, with_statement
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
9
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
12
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
13 # In these two variables, use full stops no matter what os.extsep is;
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
14 # all full stops will be converted to os.extsep on the fly
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
15 archives = 'tests.tar', 'tests.zip', 'tests.tgz', 'tests.tar.gz', 'tests.tbz2', 'tests.tar.bz2'
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
16 formats = {}
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
17
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
20
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
21 if ABCMeta:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
22 __metaclass__ = ABCMeta
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
23
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
24 def __new__(cls, path):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
25 """
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
26 Create a new instance of the archive class corresponding
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
27 to the file name in the given path.
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
28 """
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
29 if cls is not Archive:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
30 return object.__new__(cls)
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
31 else:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
32 # Do this by hand rather than through os.path.splitext
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
33 # because we support multi-dotted file name extensions
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
34 ext = path.partition(os.path.extsep)[2]
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
35 while ext:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
36 if ext in formats:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
37 return formats[ext](path)
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
38 ext = ext.partition(os.path.extsep)[2]
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
39 raise LookupError("unsupported archive file name extension in file name '%s'" % filename)
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
40
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
41 @abstractmethod
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
42 def __init__(self, path): raise NotImplementedError
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
43
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
44 @abstractmethod
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
56 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
57 import tarfile
31
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 23
diff changeset
58 except ImportError:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 23
diff changeset
59 TarArchive = None
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 23
diff changeset
60 else:
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
63
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
84
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
89
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
106
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
110 return self
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
111
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
117 else:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
122
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
123 formats['tar'] = formats['tgz'] = formats['tar.gz'] = formats['tbz2'] = formats['tar.bz2'] = TarArchive
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
124
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
125 try:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
126 import zipfile
31
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 23
diff changeset
127 except ImportError:
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 23
diff changeset
128 ZipArchive = None
fe1463e7e24d Clean up try-except clauses
Oleg Oshmyan <chortos@inbox.lv>
parents: 23
diff changeset
129 else:
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
132
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
151
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
155 if os.path.isabs(target):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
156 # To my knowledge, this is as portable as it gets
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
163
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
181
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
185 return self
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
186
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
192
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
193 formats['zip'] = ZipArchive
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
194
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
195 # Remove unsupported archive formats and replace full stops
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
196 # with the platform-dependent file name extension separator
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
197 def issupported(filename, formats=formats):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
198 ext = filename.partition('.')[2]
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
199 while ext:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
200 if ext in formats: return True
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
201 ext = ext.partition('.')[2]
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
202 return False
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
203 archives = [filename.replace('.', os.path.extsep) for filename in filter(issupported, archives)]
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
204 formats = dict((item[0].replace('.', os.path.extsep), item[1]) for item in items(formats))
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
205
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
206 open_archives = {}
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
207
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
208 def open_archive(path):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
209 if path in open_archives:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
210 return open_archives[path]
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
211 else:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
212 open_archives[path] = archive = Archive(path)
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
213 return archive
16
f2279b7602d3 Initial 2.00 commit
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
214
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
215 class File(object):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
216 __slots__ = 'virtual_path', 'real_path', 'full_real_path', 'archive'
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
217
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
218 def __init__(self, virtpath, allow_root=False, msg='test data'):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
219 self.virtual_path = virtpath
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
220 self.archive = None
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
221 if not self.realize_path('', tuple(comp.replace('.', os.path.extsep) for comp in virtpath.split('/')), allow_root):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
222 raise IOError("%s file '%s' could not be found" % (msg, virtpath))
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
223
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
224 def realize_path(self, root, virtpath, allow_root=False, hastests=False):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
225 if root and not os.path.exists(root):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
226 return False
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
227 if len(virtpath) > 1:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
228 if self.realize_path(os.path.join(root, virtpath[0]), virtpath[1:], allow_root, hastests):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
229 return True
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
230 elif not hastests:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
231 if self.realize_path(os.path.join(root, 'tests'), virtpath, allow_root, True):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
232 return True
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
233 for archive in archives:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
234 path = os.path.join(root, archive)
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
235 if os.path.exists(path):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
236 if self.realize_path_archive(open_archive(path), '', virtpath, path):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
237 return True
23
c1f52b5d80d6 Compatibility and bug fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 21
diff changeset
238 if self.realize_path(root, virtpath[1:], allow_root, hastests):
21
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
239 return True
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
240 else:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
241 if not hastests:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
242 path = os.path.join(root, 'tests', virtpath[0])
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
243 if os.path.exists(path):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
244 self.full_real_path = self.real_path = path
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
245 return True
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
246 for archive in archives:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
247 path = os.path.join(root, archive)
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
248 if os.path.exists(path):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
249 if self.realize_path_archive(open_archive(path), '', virtpath, path):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
250 return True
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
251 if hastests or allow_root:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
252 path = os.path.join(root, virtpath[0])
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
253 if os.path.exists(path):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
254 self.full_real_path = self.real_path = path
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
255 return True
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
256 return False
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
259 if root and not archive.exists(root):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
262 if len(virtpath) > 1:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
263 if self.realize_path_archive(archive, path, virtpath[1:], archpath):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
264 return True
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
265 elif self.realize_path_archive(archive, root, virtpath[1:], archpath):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
266 return True
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
267 else:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
268 if archive.exists(path):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
269 self.archive = archive
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
270 self.real_path = path
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
271 self.full_real_path = os.path.join(archpath, *path.split('/'))
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
276 return False
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
277
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
278 def open(self):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
279 if self.archive:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
280 file = self.archive.open(self.real_path)
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
281 if hasattr(file, '__exit__'):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
282 return file
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
283 else:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
284 return contextlib.closing(file)
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
287
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
288 def copy(self, target):
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
289 if self.archive:
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
290 self.archive.extract(self.real_path, target)
ec6f1a132109 A pretty usable version
Oleg Oshmyan <chortos@inbox.lv>
parents: 16
diff changeset
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