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/>.
20 __author__
= "Florian Friesdorf <flo@chaoflow.net>"
21 __docformat__
= "plaintext"
24 from paula
.testing
.interact
import interact
26 def hasdoctests(file):
27 """Check whether a file has doctests
29 for line
in open(file):
30 if line
.lstrip().startswith('>>>'):
34 def ispackagedir(path
):
35 initfile
= os
.path
.join(path
, '__init__.py')
36 result
= os
.path
.isfile(initfile
)
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:]
49 path
= pkg
.__file
__.replace('.pyc','').replace('.py','')
50 if not path
.endswith('__init__'):
52 path
= path
.replace(os
.sep
+'__init__', '')
53 if path
.endswith(os
.sep
):
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)
75 >>> l2 = recursedir(pkgpath(pkg), cond=ispackagedir)
76 >>> l2 = filter(lambda x: not x.endswith('.swp'), l2)
83 fullpath
= os
.path
.join(path
, item
)
84 if os
.path
.isdir(fullpath
):
86 files
+= recursedir(fullpath
,cond
,filefilter
)
88 if filefilter(fullpath
):
89 files
.append(fullpath
)
94 mod
= __import__(name
)
95 components
= name
.split('.')
96 for x
in components
[1:]: