• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Zope / 3956162881

pending completion
3956162881

push

github

Michael Howitz
Update to deprecation warning free releases.

4401 of 7036 branches covered (62.55%)

Branch coverage included in aggregate %.

27161 of 31488 relevant lines covered (86.26%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.94
/src/Testing/ZopeTestCase/ZopeLite.py
1
##############################################################################
2
#
3
# Copyright (c) 2005 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE.
11
#
12
##############################################################################
13
"""Lightweight Zope startup
1✔
14

15
Fast Zope startup is achieved by not installing (m)any
16
products. If your tests require a product you must
17
install it yourself using installProduct().
18

19
Typically used as in
20

21
  import ZopeLite as Zope2
22
  Zope2.installProduct('SomeProduct')
23
  app = Zope2.app()
24
"""
25

26
import os
1✔
27
import sys
1✔
28
import time
1✔
29

30
import App.ProductContext  # NOQA
1✔
31
import OFS.Application
1✔
32
import OFS.ObjectManager
1✔
33
import OFS.SimpleItem
1✔
34
import Products
1✔
35
import ZODB
1✔
36
import Zope2
1✔
37
import Zope2.Startup.run
1✔
38
# Allow test authors to install Zope products into the test environment. Note
39
# that installProduct() must be called at module level -- never from tests.
40
from OFS.Application import get_folder_permissions  # NOQA; NOQA
1✔
41
from OFS.Application import get_products
1✔
42
from OFS.Application import install_package
1✔
43
from OFS.Application import install_product
1✔
44
from OFS.Folder import Folder  # NOQA
1✔
45
from Testing.ZopeTestCase import layer
1✔
46
# ZODB sandbox factory
47
from ZODB.DemoStorage import DemoStorage  # NOQA
1✔
48

49

50
# Allow code to tell it is run by the test framework
51
os.environ['ZOPETESTCASE'] = '1'
1✔
52

53
# Always shut up
54
_quiet = True
1✔
55

56

57
def _print(msg):
1✔
58
    '''Writes 'msg' to stderr and flushes the stream.'''
59
    sys.stderr.write(msg)
×
60
    sys.stderr.flush()
×
61

62

63
def _write(msg):
1✔
64
    '''Writes 'msg' to stderr if not _quiet.'''
65
    if not _quiet:
1!
66
        _print(msg)
×
67

68

69
def _exec(cmd):
1✔
70
    '''Prints the time it takes to execute 'cmd'.'''
71
    if os.environ.get('X', None):
1!
72
        start = time.time()
×
73
        exec(cmd)
×
74
        _print('(%.3fs)' % (time.time() - start))
×
75

76

77
_write('Loading Zope, please stand by ')
1✔
78
_start = time.time()
1✔
79

80

81
def _configure_logging():
1✔
82
    # Initialize the logging module
83
    import logging
1✔
84
    root = logging.getLogger()
1✔
85
    if not root.handlers:
1!
86
        class NullHandler(logging.Handler):
×
87
            def emit(self, record):
×
88
                pass
×
89

90
        root.addHandler(NullHandler())
×
91
        logging.basicConfig()
×
92

93

94
def _configure_debug_mode():
1✔
95
    # Switch off debug mode
96
    import App.config
1✔
97
    config = App.config.getConfiguration()
1✔
98
    config.debug_mode = 0
1✔
99
    App.config.setConfiguration(config)
1✔
100

101

102
def _configure_client_cache():
1✔
103
    # Make sure we use a temporary client cache
104
    import App.config
1✔
105
    config = App.config.getConfiguration()
1✔
106
    config.zeo_client_name = None
1✔
107
    App.config.setConfiguration(config)
1✔
108

109

110
_configure_logging()
1✔
111
_configure_debug_mode()
1✔
112
_configure_client_cache()
1✔
113

114
_exec('import Zope2')
1✔
115
_exec('import ZODB')
1✔
116
_write('.')
1✔
117

118
_exec('import OFS.SimpleItem')
1✔
119
_exec('import OFS.ObjectManager')
1✔
120
_write('.')
1✔
121

122
_exec('import OFS.Application')
1✔
123
_write('.')
1✔
124

125
_patched = False
1✔
126

127

128
@layer.onsetup
1✔
129
def _apply_patches():
1✔
130
    # Do not patch a running Zope
131
    if Zope2._began_startup:
1!
132
        return
1✔
133

134
    # Avoid expensive product import
135
    def null_import_products():
×
136
        pass
×
137
    OFS.Application.import_products = null_import_products
×
138

139
    # Avoid expensive product installation
140
    def null_initialize(app):
×
141
        pass
×
142
    OFS.Application.initialize = null_initialize
×
143

144
    # Avoid loading any ZCML
145
    from Zope2.App import startup as zopeapp_startup
×
146

147
    def null_load_zcml():
×
148
        pass
×
149
    zopeapp_startup.load_zcml = null_load_zcml
×
150

151
    # Note that we applied the monkey patches
152
    global _patched
153
    _patched = True
×
154

155

156
_apply_patches()
1✔
157

158
_theApp = None
1✔
159

160

161
@layer.onsetup
1✔
162
def _startup():
1✔
163
    global _theApp
164
    _theApp = Zope2.app()
1✔
165

166

167
# Start ZopeLite
168
_startup()
1✔
169

170

171
_installedProducts = {}
1✔
172
_installedPackages = {}
1✔
173

174

175
def hasProduct(name):
1✔
176
    '''Checks if a product can be found along Products.__path__'''
177
    return name in [n[1] for n in get_products()]
×
178

179

180
@layer.onsetup
1✔
181
def installProduct(name, quiet=0):
1✔
182
    '''Installs a Zope product at layer setup time.'''
183
    quiet = 1  # Ignore argument
1✔
184
    _installProduct(name, quiet)
1✔
185

186

187
def _installProduct(name, quiet=0):
1✔
188
    '''Installs a Zope product.'''
189
    from AccessControl.class_init import InitializeClass
1✔
190
    start = time.time()
1✔
191
    meta_types = []
1✔
192
    if _patched and name not in _installedProducts:
1!
193
        for priority, product_name, index, product_dir in get_products():
×
194
            if product_name == name:
×
195
                if not quiet:
×
196
                    _print('Installing %s ... ' % product_name)
×
197
                install_product(_theApp, product_dir, product_name, meta_types,
×
198
                                get_folder_permissions())
199
                _installedProducts[product_name] = 1
×
200
                Products.meta_types = Products.meta_types + tuple(meta_types)
×
201
                InitializeClass(Folder)
×
202
                if not quiet:
×
203
                    _print('done (%.3fs)\n' % (time.time() - start))
×
204
                break
×
205
        else:
206
            if name != 'SomeProduct':   # Ignore the skeleton tests :-P
×
207
                if not quiet:
×
208
                    _print('Installing %s ... NOT FOUND\n' % name)
×
209

210

211
def hasPackage(name):
1✔
212
    '''Checks if a package has been registered with five:registerPackage.'''
213
    from OFS.metaconfigure import has_package
×
214
    return has_package(name)
×
215

216

217
def installPackage(name, quiet=0):
1✔
218
    '''Installs a registered Python package.'''
219
    quiet = 1  # Ignore argument
×
220
    _installPackage(name, quiet)
×
221

222

223
def _installPackage(name, quiet=0):
1✔
224
    '''Installs a registered Python package.'''
225
    from OFS.metaconfigure import get_packages_to_initialize
×
226
    start = time.time()
×
227
    if _patched and name not in _installedPackages:
×
228
        for module, init_func in get_packages_to_initialize():
×
229
            if module.__name__ == name:
×
230
                if not quiet:
×
231
                    _print('Installing %s ... ' % module.__name__)
×
232
                install_package(_theApp, module, init_func)
×
233
                _installedPackages[module.__name__] = 1
×
234
                if not quiet:
×
235
                    _print('done (%.3fs)\n' % (time.time() - start))
×
236
                break
×
237
        else:
238
            if not quiet:
×
239
                _print('Installing %s ... NOT FOUND\n' % name)
×
240

241

242
installProduct('OFSP', 1)
1✔
243

244
# So people can use ZopeLite.app()
245
app = Zope2.app
1✔
246
debug = Zope2.debug
1✔
247
DB = Zope2.DB
1✔
248
configure = Zope2.Startup.run.configure_wsgi
1✔
249

250

251
def startup():
1✔
252
    pass
×
253

254

255
Zope = Zope2
1✔
256
active = _patched
1✔
257

258

259
def sandbox(base=None):
1✔
260
    '''Returns a sandbox copy of the base ZODB.'''
261
    if base is None:
1!
262
        base = Zope2.DB
1✔
263
    storage = DemoStorage(base=base._storage)
1✔
264
    return ZODB.DB(storage)
1✔
265

266

267
_write(' done (%.3fs)\n' % (time.time() - _start))
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc