~launchpad-pqm/launchpad/devel

10303.1.1 by Gary Poster
use newest version of zc.buildout
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
# This file is imported by parts/scripts/sitecustomize.py, as set up in our
5
# buildout.cfg (see the "initialization" key in the "[scripts]" section).
6
12536.2.4 by Robert Collins
Fix branch a bit.
7
from collections import defaultdict
11594.2.1 by Aaron Bentley
Fix use of itertools.groupby through security proxies.
8
import itertools
10303.1.1 by Gary Poster
use newest version of zc.buildout
9
import os
10981.1.1 by Maris Fogels
Silence DeprecationWarnings from pycrypto 2.0.1 using a new warnings hook in lp_sitecustomize.
10
import warnings
11136.3.1 by Diogo Matsubara
silence the root logger across the entire Launchpad project
11
import logging
10981.1.1 by Maris Fogels
Silence DeprecationWarnings from pycrypto 2.0.1 using a new warnings hook in lp_sitecustomize.
12
11892.6.4 by Julian Edwards
Fix sourcepackagerecipebuild's handleStatus
13
from twisted.internet.defer import (
14
    Deferred,
15
    DeferredList,
16
    )
11593.3.120 by Julian Edwards
re-add revno 11801 which was backed out in devel due to test failures resulting from a twisted bug
17
10981.1.1 by Maris Fogels
Silence DeprecationWarnings from pycrypto 2.0.1 using a new warnings hook in lp_sitecustomize.
18
from bzrlib.branch import Branch
11300.2.49 by Stuart Bishop
Tidy and improve logging system infrastructure for great justice.
19
from lp.services.log import loglevels
8758.2.57 by Stuart Bishop
Move LaunchpadLogger to a better location
20
from lp.services.log.logger import LaunchpadLogger
11300.2.44 by Stuart Bishop
Tidy custom loglevels into their own module, and initialize filtering and custom levels in lp_sitecustomize.py for great justice.
21
from lp.services.log.mappingfilter import MappingFilter
11136.3.3 by Diogo Matsubara
move NullHandler to a more central location where it can be imported from lp_sitecustomize.py and test_layers.py test.
22
from lp.services.log.nullhandler import NullHandler
10303.1.1 by Gary Poster
use newest version of zc.buildout
23
from lp.services.mime import customizeMimetypes
9590.1.94 by Michael Hudson
test passes, some zope security horror though
24
from zope.security import checker
10981.1.1 by Maris Fogels
Silence DeprecationWarnings from pycrypto 2.0.1 using a new warnings hook in lp_sitecustomize.
25
9590.1.95 by Michael Hudson
more comprehensive zope horror
26
11300.2.44 by Stuart Bishop
Tidy custom loglevels into their own module, and initialize filtering and custom levels in lp_sitecustomize.py for great justice.
27
def add_custom_loglevels():
28
    """Add out custom log levels to the Python logging package."""
11300.2.49 by Stuart Bishop
Tidy and improve logging system infrastructure for great justice.
29
30
    # This import installs custom ZODB loglevels, which we can then
31
    # override. BLATHER is between INFO and DEBUG, so we can leave it.
32
    # TRACE conflicts with DEBUG6, and since we are not using ZEO, we
33
    # just overwrite the level string by calling addLevelName.
34
    from ZODB.loglevels import BLATHER, TRACE
35
36
    # Confirm our above assumptions, and silence lint at the same time.
37
    assert BLATHER == 15
38
    assert TRACE == loglevels.DEBUG6
39
40
    logging.addLevelName(loglevels.DEBUG2, 'DEBUG2')
41
    logging.addLevelName(loglevels.DEBUG3, 'DEBUG3')
42
    logging.addLevelName(loglevels.DEBUG4, 'DEBUG4')
43
    logging.addLevelName(loglevels.DEBUG5, 'DEBUG5')
44
    logging.addLevelName(loglevels.DEBUG6, 'DEBUG6')
45
    logging.addLevelName(loglevels.DEBUG7, 'DEBUG7')
46
    logging.addLevelName(loglevels.DEBUG8, 'DEBUG8')
47
    logging.addLevelName(loglevels.DEBUG9, 'DEBUG9')
48
49
    # Install our customized Logger that provides easy access to our
50
    # custom loglevels.
8758.2.57 by Stuart Bishop
Move LaunchpadLogger to a better location
51
    logging.setLoggerClass(LaunchpadLogger)
11300.2.49 by Stuart Bishop
Tidy and improve logging system infrastructure for great justice.
52
9893.6.42 by Stuart Bishop
Override the root logger with an instance of LaunchpadLogger. Currently, code paths can fail when passed the root logger instead of a logger with our richer API.
53
    # Fix the root logger, replacing it with an instance of our
54
    # customized Logger. The original one is instantiated on import of
55
    # the logging module, so our override does not take effect without
56
    # this manual effort.
9893.6.45 by Stuart Bishop
Do more work replacing root logger, as bzrlib and zope have already created loggers on module import.
57
    old_root = logging.root
8758.2.57 by Stuart Bishop
Move LaunchpadLogger to a better location
58
    new_root = LaunchpadLogger('root', loglevels.WARNING)
9893.6.45 by Stuart Bishop
Do more work replacing root logger, as bzrlib and zope have already created loggers on module import.
59
60
    # Fix globals.
61
    logging.root = new_root
62
    logging.Logger.root = new_root
63
64
    # Fix manager.
65
    manager = logging.Logger.manager
66
    manager.root = new_root
67
68
    # Fix existing Logger instances.
69
    for logger in manager.loggerDict.values():
70
        if getattr(logger, 'parent', None) is old_root:
71
            logger.parent = new_root
9893.6.42 by Stuart Bishop
Override the root logger with an instance of LaunchpadLogger. Currently, code paths can fail when passed the root logger instead of a logger with our richer API.
72
11300.2.44 by Stuart Bishop
Tidy custom loglevels into their own module, and initialize filtering and custom levels in lp_sitecustomize.py for great justice.
73
11136.3.7 by Diogo Matsubara
change the function name to reflect what it's doing
74
def silence_bzr_logger():
11136.3.6 by Diogo Matsubara
fix docstring
75
    """Install the NullHandler on the bzr logger to silence logs."""
12144.1.6 by Jonathan Lange
Don't let bzr log messages propagate
76
    bzr_logger = logging.getLogger('bzr')
77
    bzr_logger.addHandler(NullHandler())
78
    bzr_logger.propagate = False
11136.3.1 by Diogo Matsubara
silence the root logger across the entire Launchpad project
79
11300.2.49 by Stuart Bishop
Tidy and improve logging system infrastructure for great justice.
80
11300.2.44 by Stuart Bishop
Tidy custom loglevels into their own module, and initialize filtering and custom levels in lp_sitecustomize.py for great justice.
81
def silence_zcml_logger():
82
    """Lower level of ZCML parsing DEBUG messages."""
83
    config_filter = MappingFilter(
84
        {logging.DEBUG: (7, 'DEBUG4')}, 'config')
85
    logging.getLogger('config').addFilter(config_filter)
86
11300.2.49 by Stuart Bishop
Tidy and improve logging system infrastructure for great justice.
87
11300.2.44 by Stuart Bishop
Tidy custom loglevels into their own module, and initialize filtering and custom levels in lp_sitecustomize.py for great justice.
88
def silence_transaction_logger():
89
    """Lower level of DEBUG messages from the transaction module."""
90
    # Transaction logging is too noisy. Lower its DEBUG messages
91
    # to DEBUG3. Transactions log to loggers named txn.<thread_id>,
92
    # so we need to register a null handler with a filter to ensure
93
    # the logging records get mutated before being propagated up
94
    # to higher level loggers.
95
    txn_handler = NullHandler()
96
    txn_filter = MappingFilter(
97
        {logging.DEBUG: (8, 'DEBUG3')}, 'txn')
98
    txn_handler.addFilter(txn_filter)
99
    logging.getLogger('txn').addHandler(txn_handler)
100
11300.2.49 by Stuart Bishop
Tidy and improve logging system infrastructure for great justice.
101
9590.1.95 by Michael Hudson
more comprehensive zope horror
102
def dont_wrap_class_and_subclasses(cls):
103
    checker.BasicTypes.update({cls: checker.NoProxy})
104
    for subcls in cls.__subclasses__():
105
        dont_wrap_class_and_subclasses(subcls)
10303.1.1 by Gary Poster
use newest version of zc.buildout
106
11300.2.49 by Stuart Bishop
Tidy and improve logging system infrastructure for great justice.
107
10981.1.1 by Maris Fogels
Silence DeprecationWarnings from pycrypto 2.0.1 using a new warnings hook in lp_sitecustomize.
108
def silence_warnings():
109
    """Silence warnings across the entire Launchpad project."""
110
    # pycrypto-2.0.1 on Python2.6:
111
    #   DeprecationWarning: the sha module is deprecated; use the hashlib
112
    #   module instead
113
    warnings.filterwarnings(
114
        "ignore",
115
        category=DeprecationWarning,
116
        module="Crypto")
12398.2.16 by Jonathan Lange
Respond to review comments
117
    # Filter all deprecation warnings for Zope 3.6, which emanate from
12398.2.9 by Jonathan Lange
Better docstring.
118
    # the zope package.
119
    filter_pattern = '.*(Zope 3.6|provide.*global site manager).*'
120
    warnings.filterwarnings(
121
        'ignore', filter_pattern, category=DeprecationWarning)
122
    # XXX wgrant 2010-03-30 bug=551510:
123
    # Also filter apt_pkg warnings, since Lucid's python-apt has a new API.
124
    warnings.filterwarnings(
125
        'ignore', '.*apt_pkg.*', category=DeprecationWarning)
10981.1.1 by Maris Fogels
Silence DeprecationWarnings from pycrypto 2.0.1 using a new warnings hook in lp_sitecustomize.
126
11300.2.49 by Stuart Bishop
Tidy and improve logging system infrastructure for great justice.
127
9893.6.28 by Stuart Bishop
Reset logging between tests to Launchpad default state, not Z3 default state
128
def customize_logger():
129
    """Customize the logging system.
130
131
    This function is also invoked by the test infrastructure to reset
132
    logging between tests.
133
    """
134
    silence_bzr_logger()
135
    silence_zcml_logger()
136
    silence_transaction_logger()
137
138
11692.4.1 by Gary Poster
Allow .lpconfig to be honored if initial configuration is "development"
139
def main(instance_name):
140
    # This is called by our custom buildout-generated sitecustomize.py
141
    # in parts/scripts/sitecustomize.py. The instance name is sent to
142
    # buildout from the Makefile, and then inserted into
143
    # sitecustomize.py.  See buildout.cfg in the "initialization" value
144
    # of the [scripts] section for the code that goes into this custom
145
    # sitecustomize.py.  We do all actual initialization here, in a more
146
    # visible place.
147
    if instance_name and instance_name != 'development':
148
        # See bug 656213 for why we do this carefully.
149
        os.environ.setdefault('LPCONFIG', instance_name)
10303.1.1 by Gary Poster
use newest version of zc.buildout
150
    os.environ['STORM_CEXTENSIONS'] = '1'
11300.2.44 by Stuart Bishop
Tidy custom loglevels into their own module, and initialize filtering and custom levels in lp_sitecustomize.py for great justice.
151
    add_custom_loglevels()
10303.1.1 by Gary Poster
use newest version of zc.buildout
152
    customizeMimetypes()
9590.1.95 by Michael Hudson
more comprehensive zope horror
153
    dont_wrap_class_and_subclasses(Branch)
12536.2.4 by Robert Collins
Fix branch a bit.
154
    checker.BasicTypes.update({defaultdict: checker.NoProxy})
11593.3.120 by Julian Edwards
re-add revno 11801 which was backed out in devel due to test failures resulting from a twisted bug
155
    checker.BasicTypes.update({Deferred: checker.NoProxy})
11892.6.4 by Julian Edwards
Fix sourcepackagerecipebuild's handleStatus
156
    checker.BasicTypes.update({DeferredList: checker.NoProxy})
11594.2.1 by Aaron Bentley
Fix use of itertools.groupby through security proxies.
157
    checker.BasicTypes[itertools.groupby] = checker._iteratorChecker
11594.2.2 by Aaron Bentley
Handle itertools._grouper as well.
158
    # The itertools._grouper type is not exposed by name, so we must get it
159
    # through actually using itertools.groupby.
160
    grouper = type(list(itertools.groupby([0]))[0][1])
161
    checker.BasicTypes[grouper] = checker._iteratorChecker
10981.1.1 by Maris Fogels
Silence DeprecationWarnings from pycrypto 2.0.1 using a new warnings hook in lp_sitecustomize.
162
    silence_warnings()
9893.6.28 by Stuart Bishop
Reset logging between tests to Launchpad default state, not Z3 default state
163
    customize_logger()