Mercurial > ~astiob > upreckon > hgweb
diff 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 |
line wrap: on
line diff
--- a/upreckon/files.py Thu Aug 11 20:44:23 2011 +0300 +++ b/upreckon/files.py Thu Aug 11 23:20:52 2011 +0300 @@ -5,10 +5,10 @@ from __future__ import division, with_statement from .compat import * -import contextlib, os, posixpath, shutil, sys +import contextlib, itertools, os, posixpath, re, shutil, sys # You don't need to know about anything else. -__all__ = 'File', +__all__ = 'File', 'regexp' # In these two variables, use full stops no matter what os.extsep is; # all full stops will be converted to os.extsep on the fly @@ -239,4 +239,26 @@ if self.archive: self.archive.extract(self.real_path, target) else: - shutil.copy(self.real_path, target) \ No newline at end of file + shutil.copy(self.real_path, target) + +def regexp(pattern, hastests=False, droplast=False): + # FIXME: add test archives + if not pattern: + yield os.curdir, '' + return + dirname, basename = posixpath.split(pattern) + if hastests: + dirnames = regexp(dirname, True) + else: + dirnames = itertools.chain(regexp(dirname), regexp(posixpath.join(dirname, 'tests'), True, True)) + reobj = re.compile(pattern + '$', re.UNICODE) + for dirname, vdirname in dirnames: + try: + names = os.listdir(dirname) + except OSError: + continue + for name in names: + path = os.path.join(dirname, name) + vpath = posixpath.join(vdirname, name) + if re.match(reobj, vpath): + yield path, vpath if not droplast else vdirname