Merge lp:~jml/launchpad/remove-unused-tracers into lp:launchpad

Proposed by Jonathan Lange
Status: Merged
Approved by: Robert Collins
Approved revision: no longer in the source branch.
Merged at revision: 11170
Proposed branch: lp:~jml/launchpad/remove-unused-tracers
Merge into: lp:launchpad
Diff against target: 256 lines (+0/-245)
2 files modified
lib/canonical/database/testing/tracers.py (+0/-80)
lib/canonical/launchpad/doc/storm-tracers.txt (+0/-165)
To merge this branch: bzr merge lp:~jml/launchpad/remove-unused-tracers
Reviewer Review Type Date Requested Status
Robert Collins (community) Approve
Review via email: mp+30214@code.launchpad.net

Description of the change

This patch removes some code that is apparently unused.

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

I don't know what they are for, but if they aren't useful, they shouldn't be here.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== removed directory 'lib/canonical/database/testing'
=== removed file 'lib/canonical/database/testing/__init__.py'
=== removed file 'lib/canonical/database/testing/tracers.py'
--- lib/canonical/database/testing/tracers.py 2009-06-25 05:30:52 +0000
+++ lib/canonical/database/testing/tracers.py 1970-01-01 00:00:00 +0000
@@ -1,80 +0,0 @@
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"""Storm tracers for debugging."""
5
6__metaclass__ = type
7__all__ = [
8 'BaseTracer',
9 'CountAllTracer',
10 'StderrDebugTracer',
11 ]
12
13
14import sys
15import storm.tracer
16
17
18class BaseTracer:
19 """Base class for all tracers."""
20
21 def __init__(self):
22 # A flag indicating whether tracing should be enabled or not.
23 self.trace = False
24
25 def install(self, only=False):
26 """Install this tracer.
27
28 :param only: When True, remove all existing tracers before adding this
29 one.
30 :type only: boolean
31 """
32 if only:
33 storm.tracer.remove_all_tracers()
34 storm.tracer.install_tracer(self)
35
36 def uninstall(self):
37 """Uninstall all tracers of this instance's type."""
38 storm.tracer.remove_tracer_type(type(self))
39
40 # The trace API
41 def connection_raw_execute(self, *args):
42 pass
43
44 def connection_raw_execute_error(self, *args):
45 pass
46
47 def set_statement_timeout(self, *args):
48 pass
49
50
51
52class CountAllTracer(BaseTracer):
53 """A counter of all SQL statements executed by Storm."""
54
55 def __init__(self):
56 super(CountAllTracer, self).__init__()
57 self.count = 0
58
59 def connection_raw_execute(self, *args):
60 if self.trace:
61 self.count += 1
62
63
64class StderrDebugTracer(BaseTracer):
65 """Print all executed SQL statements to a stream.
66
67 By default, print to the real stderr (e.g. not a possibly
68 doctest-redirected stderr).
69 """
70
71 def __init__(self, stream=None):
72 super(StderrDebugTracer, self).__init__()
73 if stream is None:
74 self.stream = sys.__stderr__
75 else:
76 self.stream = stream
77
78 def connection_raw_execute(self, connection, cursor, statement, params):
79 if self.trace:
80 print >> self.stream, statement
810
=== removed file 'lib/canonical/launchpad/doc/storm-tracers.txt'
--- lib/canonical/launchpad/doc/storm-tracers.txt 2009-04-17 10:32:16 +0000
+++ lib/canonical/launchpad/doc/storm-tracers.txt 1970-01-01 00:00:00 +0000
@@ -1,165 +0,0 @@
1= Storm tracers =
2
3The Storm ORM supports an interface for installing 'tracers', methods of which
4get called during certain parts of the query workflow. Launchpad itself
5provides a few helpers for tracing all SQL, or just those statements which
6succeed or fail. Demonstrating the entire Storm tracer API is outside of the
7scope of this document, but here you'll see Launchpad's convenience tracers.
8
9The base class for tracers provides the basic infrastructure for enabling,
10disabling, installing and uninstalling tracers.
11
12 # Save the global tracer state so that we can restore them after the test.
13 >>> import storm.tracer
14 >>> global_tracers = storm.tracer._tracers[:]
15
16 >>> from canonical.database.testing import tracers
17
18 >>> messages = []
19 >>> class WaveToTheCrowdTracer(tracers.BaseTracer):
20 ... def connection_raw_execute(self, *args):
21 ... if self.trace:
22 ... messages.append('hello world')
23
24 >>> tracer = WaveToTheCrowdTracer()
25 >>> tracer.install()
26
27Even though the tracer is installed, it won't get called until it's enabled.
28
29 >>> from lp.registry.interfaces.person import IPersonSet
30 >>> person_set = getUtility(IPersonSet)
31 >>> person = person_set.getByName('name12')
32 >>> len(messages)
33 0
34
35Once it's enabled, the tracer does its thing.
36
37 >>> tracer.trace = True
38 >>> person = person_set.getByName('name12')
39 >>> len(messages)
40 1
41
42Of course, disabling it, stops the tracer from doing its thing.
43
44 >>> tracer.trace = False
45 >>> person = person_set.getByName('name12')
46 >>> len(messages)
47 1
48
49Let's re-enable it...
50
51 >>> tracer.trace = True
52 >>> person = person_set.getByName('name12')
53 >>> len(messages)
54 2
55
56...and then uninstall it.
57
58 >>> tracer.uninstall()
59 >>> person = person_set.getByName('name12')
60 >>> len(messages)
61 2
62
63We can install more than one tracer and both will run.
64
65 >>> messages = []
66 >>> class WaveToTheCrowdTracer(tracers.BaseTracer):
67 ... def connection_raw_execute(self, *args):
68 ... messages.append('hello world')
69
70 >>> tracer_one = WaveToTheCrowdTracer()
71 >>> tracer_two = WaveToTheCrowdTracer()
72 >>> tracer_one.install()
73 >>> tracer_two.install()
74
75 >>> person = person_set.getByName('name12')
76 >>> len(messages)
77 2
78 >>> person = person_set.getByName('name12')
79 >>> len(messages)
80 4
81
82Because of the API presented by Storm, uninstalling a tracer actually
83uninstalls all tracers of its type.
84
85 >>> tracer_one.uninstall()
86 >>> person = person_set.getByName('name12')
87 >>> len(messages)
88 4
89
90When installing a tracer, we can request that all other tracers first be
91removed.
92
93 >>> messages = []
94 >>> tracer_one.install()
95 >>> tracer_two.install(only=True)
96 >>> person = person_set.getByName('name12')
97 >>> len(messages)
98 1
99 >>> person = person_set.getByName('name12')
100 >>> len(messages)
101 2
102
103 # Clear all the tracers for the next batch of tests.
104 >>> tracer_one.uninstall()
105
106
107== A counting tracer ==
108
109Launchpad provides a counting tracer which can be used to count the total
110number of SQL queries in a particular section of code.
111
112 >>> counter = tracers.CountAllTracer()
113 >>> counter.install()
114 >>> counter.trace = True
115
116 >>> person = person_set.getByName('name12')
117 >>> person = person_set.getByName('name12')
118 >>> counter.count
119 2
120
121 >>> counter.trace = False
122 >>> person = person_set.getByName('name12')
123 >>> person = person_set.getByName('name12')
124 >>> counter.count
125 2
126
127 # Clear all the tracers for the next batch of tests.
128 >>> counter.uninstall()
129
130
131== SQL debugging ==
132
133Launchpad also provides a tracer that can print SQL statements to a file
134stream. By default, it prints to the real stderr.
135
136 >>> import sys
137 >>> from cStringIO import StringIO
138
139 >>> old_real_stderr = sys.__stderr__
140 >>> sys.__stderr__ = out = StringIO()
141 >>> try:
142 ... debug = tracers.StderrDebugTracer()
143 ... debug.install()
144 ... debug.trace = True
145 ... person = person_set.getByName('name12')
146 ... finally:
147 ... sys.__stderr__ = old_real_stderr
148 >>> print out.getvalue()
149 SELECT Person...
150
151 # Clear all the tracers for the next batch of tests.
152 >>> debug.uninstall()
153
154You can also tell it to print to a specific stream.
155
156 >>> out = StringIO()
157 >>> debug = tracers.StderrDebugTracer(out)
158 >>> debug.install()
159 >>> debug.trace = True
160 >>> person = person_set.getByName('name12')
161 >>> print out.getvalue()
162 SELECT Person...
163
164 # Restore the global state of the tracers before this test was run.
165 >>> storm.tracer._tracers = global_tracers