comparison upreckon/files.py @ 193:a76cdc26ba9d

Added conf. var. match and match='regexp' for non-archives Specify match='regexp', and your tests and dummies will be treated as regular expressions describing test case identifiers. Every file that is in a suitable location and whose name matches {testcase,dummy}inname and the given regexp will be treated as a file with test case input data. You are free to use backreferences in the regexps, but group numbering starts at two rather than one. If you want test groups, you can get them magically created for you by putting a part of the test ID in a group in the regexp sense and specifying the tests variable as a pair consisting of the regexp itself and the number of this regexp group (remember group numbers start at two).
author Oleg Oshmyan <chortos@inbox.lv>
date Thu, 11 Aug 2011 23:20:52 +0300
parents 0d657576b1ac
children 8c30a2c8a09e
comparison
equal deleted inserted replaced
192:6e6b0ea63fa1 193:a76cdc26ba9d
3 """File access routines and classes with support for archives.""" 3 """File access routines and classes with support for archives."""
4 4
5 from __future__ import division, with_statement 5 from __future__ import division, with_statement
6 6
7 from .compat import * 7 from .compat import *
8 import contextlib, os, posixpath, shutil, sys 8 import contextlib, itertools, os, posixpath, re, shutil, sys
9 9
10 # You don't need to know about anything else. 10 # You don't need to know about anything else.
11 __all__ = 'File', 11 __all__ = 'File', 'regexp'
12 12
13 # In these two variables, use full stops no matter what os.extsep is; 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 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' 15 archives = 'tests.tar', 'tests.zip', 'tests.tgz', 'tests.tar.gz', 'tests.tbz2', 'tests.tar.bz2'
16 formats = {} 16 formats = {}
238 def copy(self, target): 238 def copy(self, target):
239 if self.archive: 239 if self.archive:
240 self.archive.extract(self.real_path, target) 240 self.archive.extract(self.real_path, target)
241 else: 241 else:
242 shutil.copy(self.real_path, target) 242 shutil.copy(self.real_path, target)
243
244 def regexp(pattern, hastests=False, droplast=False):
245 # FIXME: add test archives
246 if not pattern:
247 yield os.curdir, ''
248 return
249 dirname, basename = posixpath.split(pattern)
250 if hastests:
251 dirnames = regexp(dirname, True)
252 else:
253 dirnames = itertools.chain(regexp(dirname), regexp(posixpath.join(dirname, 'tests'), True, True))
254 reobj = re.compile(pattern + '$', re.UNICODE)
255 for dirname, vdirname in dirnames:
256 try:
257 names = os.listdir(dirname)
258 except OSError:
259 continue
260 for name in names:
261 path = os.path.join(dirname, name)
262 vpath = posixpath.join(vdirname, name)
263 if re.match(reobj, vpath):
264 yield path, vpath if not droplast else vdirname