paula.testing: test for finding txt files + minor bugfix
[paula.git] / paula.testing / src / paula / testing / utils.py
blob18c35050cbdc2000e67ff4ce4cc43373efa5a2e0
1 # Copyright (c) 2008 by Florian Friesdorf
3 # GNU Affero General Public License (AGPL)
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program. If not, see
17 # <http://www.gnu.org/licenses/>.
18 """
19 """
20 __author__ = "Florian Friesdorf <flo@chaoflow.net>"
21 __docformat__ = "plaintext"
23 import os
24 from paula.testing.interact import interact
26 def hasdoctests(file):
27 """Check whether a file has doctests
28 """
29 for line in open(file):
30 if line.lstrip().startswith('>>>'):
31 return True
34 def ispackagedir(path):
35 initfile = os.path.join(path, '__init__.py')
36 result = os.path.isfile(initfile)
37 return result
40 def pkgpath(pkg):
41 """Returns the path to a imported package
43 >>> from paula.testing.utils import saneimport
44 >>> from paula.testing.utils import pkgpath
45 >>> pkg = saneimport('paula.testing')
46 >>> pkgpath(pkg).split(os.sep)[-2:]
47 ['paula', 'testing']
48 """
49 path = pkg.__file__.replace('.pyc','').replace('.py','')
50 if not path.endswith('__init__'):
51 raise ValueError
52 path = path.replace(os.sep+'__init__', '')
53 if path.endswith(os.sep):
54 raise ValueError
55 return path
58 def recursedir(path, cond=lambda x: True, filefilter=lambda x: True):
59 """Recurses a directory structure and returns all contained files
61 Optionally a condition can be given that must be met in order to recurse
62 into a directory. The condition is a function that takes the directory as
63 argument and returns either True or False.
65 >>> from paula.testing.utils import saneimport
66 >>> from paula.testing.utils import recursedir
67 >>> from paula.testing.utils import pkgpath
68 >>> from paula.testing.utils import ispackagedir
69 >>> pkg = saneimport('paula.testing')
70 >>> l1 = recursedir(pkgpath(pkg))
71 >>> l1 = filter(lambda x: not x.endswith('.swp'), l1)
72 >>> len(l1)
75 >>> l2 = recursedir(pkgpath(pkg), cond=ispackagedir)
76 >>> l2 = filter(lambda x: not x.endswith('.swp'), l2)
77 >>> len(l2)
79 """
80 files=[]
81 ls = os.listdir(path)
82 for item in ls:
83 fullpath = os.path.join(path, item)
84 if os.path.isdir(fullpath):
85 if cond(fullpath):
86 files += recursedir(fullpath,cond,filefilter)
87 continue
88 if filefilter(fullpath):
89 files.append(fullpath)
90 return files
93 def saneimport(name):
94 mod = __import__(name)
95 components = name.split('.')
96 for x in components[1:]:
97 mod = getattr(mod, x)
98 return mod