File: utils.py

package info (click to toggle)
zope-pas 1.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,084 kB
  • ctags: 1,205
  • sloc: python: 9,373; xml: 348; makefile: 31; sh: 2
file content (209 lines) | stat: -rw-r--r-- 6,049 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights
# Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this
# distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import os
import unittest
from types import TupleType, ListType

from Globals import package_home

def tuplize(value):
    if isinstance(value, TupleType):
        return value
    if isinstance(value, ListType):
        return tuple(value)
    return (value,)

try:
    from zope.interface import Interface
except ImportError:
    from Interface import Interface

try:
    from zope.interface import providedBy
except ImportError:
    def providedBy(obj):
        return tuplize(obj.__implements__)

try:
    from zope.interface import implementedBy
except ImportError:
    def implementedBy(klass):
        return tuplize(klass.__implements__)

try:
    from Products.Five.bridge import fromZ2Interface
except ImportError:
    def fromZ2Interface(i):
        # Raise ValueError to work around a cornerish case where
        # zope.interface is available but Five is not.
        raise ValueError, i

try:
    from zope import interface
except ImportError:
    def directlyProvides(obj, *interfaces):
        obj.__implements__ = tuple( interfaces )

    def classImplements(class_, *interfaces):
        class_.__implements__ = tuple( interfaces )

else:
    def directlyProvides(obj, *interfaces):
        # convert any Zope 2 interfaces to Zope 3 using fromZ2Interface
        normalized_interfaces = []
        for i in interfaces:
            try:
                i = fromZ2Interface(i)
            except ValueError: # already a Zope 3 interface
                pass
            normalized_interfaces.append(i)
        return interface.directlyProvides(obj, *normalized_interfaces)

    def classImplements(class_, *interfaces):
        # convert any Zope 2 interfaces to Zope 3 using fromZ2Interface
        normalized_interfaces = []
        for i in interfaces:
            try:
                i = fromZ2Interface(i)
            except ValueError: # already a Zope 3 interface
                pass
            normalized_interfaces.append(i)
        return interface.classImplements(class_, *normalized_interfaces)


product_dir = package_home( globals() )
product_prefix = os.path.join( os.path.split(product_dir)[:-1] )

_wwwdir = os.path.join( product_dir, 'www' )

#
#   Most of this module is shamelessly ripped off from Zope3.test
#
def remove_stale_bytecode( arg, dirname, names ):
    """
        Troll product, removing compiled turds whose source is now gone.
    """
    names = map( os.path.normcase, names )

    for name in names:

        if name.endswith( ".pyc" ) or name.endswith( ".pyo" ):

            srcname = name[:-1]

            if srcname not in names:

                fullname = os.path.join( dirname, name )

                if __debug__:
                    print "Removing stale bytecode file", fullname

                os.unlink( fullname )

class TestFileFinder:

    def __init__( self ):
        self.files = []

    def visit( self, prefix, dir, files ):
        """
            Visitor for os.path.walk:  accumulates filenamse of unittests.
        """
        #if dir[-5:] != "tests":
        #    return

        # ignore tests that aren't in packages
        if not "__init__.py" in files:

            if not files or files == ['CVS']:
                return

            if 0 and __debug__: # XXX: don't care!
                print "not a package", dir

            return

        for file in files:

            if file.startswith( prefix ) and file.endswith( ".py" ):
                path = os.path.join(dir, file)
                self.files.append(path)

def find_unit_test_files( from_dir=product_dir, test_prefix='test' ):
    """
        Walk the product, return a list of all unittest files.
    """
    finder = TestFileFinder()
    os.path.walk( from_dir, finder.visit, test_prefix )
    return finder.files

def module_name_from_path( path ):
    """
        Return the dotted module name matching the filesystem path.
    """
    assert path.endswith( '.py' )
    path = path[:-3]
    path = path[ len(product_prefix) + 1: ] # strip extraneous crap
    dirs = []
    while path:
        path, end = os.path.split( path )
        dirs.insert( 0, end )
    return ".".join( dirs )

def get_suite( file ):
    """
        Retrieve a TestSuite from 'file'.
    """
    module_name = module_name_from_path( file )
    loader = unittest.defaultTestLoader
    try:
        suite = loader.loadTestsFromName( '%s.test_suite' % module_name )
    except AttributeError:

        try:
            suite = loader.loadTestsFromName(  module_name )
        except ImportError, err:
            print "Error importing %s\n%s" % (module_name, err)
            raise
    return suite

def allTests( from_dir=product_dir, test_prefix='test' ):
    """
        Walk the product and build a unittest.TestSuite aggregating tests.
    """
    os.path.walk( from_dir, remove_stale_bytecode, None )
    test_files = find_unit_test_files( from_dir, test_prefix )
    test_files.sort()

    suite = unittest.TestSuite()

    for test_file in test_files:

        s = get_suite( test_file )
        if s is not None:
            suite.addTest( s )

    return suite

def createViewName(method_name, user_handle=None):
    """
        Centralized place for creating the "View Name" that identifies
        a ZCacheable record in a PASRAMCacheManager
    """
    if not user_handle:
        return method_name
    else:
        return '%s-%s' % (method_name, user_handle)