paula.testing: tests pass, no app dep, currently no zcml
[paula.git] / paula.testing / src / paula / testing / utils.py
blobeaa6b7b0e976c99210f81a491eb5d86450f216bd
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)
74 >>> l2 = recursedir(pkgpath(pkg), cond=ispackagedir)
75 >>> l2 = filter(lambda x: not x.endswith('.swp'), l2)
76 >>> len(l2)
78 """
79 files=[]
80 ls = os.listdir(path)
81 for item in ls:
82 fullpath = os.path.join(path, item)
83 if os.path.isdir(fullpath) and cond(fullpath):
84 files += recursedir(fullpath,cond,filefilter)
85 continue
86 if filefilter(fullpath):
87 files.append(fullpath)
88 return files
91 def saneimport(name):
92 mod = __import__(name)
93 components = name.split('.')
94 for x in components[1:]:
95 mod = getattr(mod, x)
96 return mod