Merge lp:~nataliabidart/ubuntu-sso-client/delay-reactor-import into lp:ubuntu-sso-client

Proposed by Natalia Bidart
Status: Merged
Approved by: Natalia Bidart
Approved revision: 848
Merged at revision: 847
Proposed branch: lp:~nataliabidart/ubuntu-sso-client/delay-reactor-import
Merge into: lp:ubuntu-sso-client
Diff against target: 264 lines (+48/-20)
8 files modified
ubuntu_sso/logger.py (+1/-1)
ubuntu_sso/main/windows.py (+9/-3)
ubuntu_sso/utils/__init__.py (+5/-1)
ubuntu_sso/utils/ipc.py (+12/-6)
ubuntu_sso/utils/tcpactivation.py (+1/-3)
ubuntu_sso/utils/tests/test_ipc.py (+4/-3)
ubuntu_sso/utils/webclient/timestamp.py (+4/-1)
ubuntu_sso/utils/webclient/txweb.py (+12/-2)
To merge this branch: bzr merge lp:~nataliabidart/ubuntu-sso-client/delay-reactor-import
Reviewer Review Type Date Requested Status
dobey (community) Approve
Roberto Alsina (community) Approve
Review via email: mp+91712@code.launchpad.net

Commit message

 - Delay twisted.internet.reactor and twisted.web import to avoid
   ReactorAlreadyInstalledError (LP: #927788).

To post a comment you must log in.
Revision history for this message
Roberto Alsina (ralsina) wrote :

+1

review: Approve
Revision history for this message
dobey (dobey) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/logger.py'
2--- ubuntu_sso/logger.py 2012-01-20 19:18:29 +0000
3+++ ubuntu_sso/logger.py 2012-02-06 20:19:19 +0000
4@@ -67,7 +67,7 @@
5 logger.propagate = False
6 logger.setLevel(LOG_LEVEL)
7 logger.addHandler(handler)
8- if os.environ.get('DEBUG'):
9+ if os.environ.get('U1_DEBUG'):
10 debug_handler = logging.StreamHandler(sys.stderr)
11 debug_handler.setFormatter(logging.Formatter(fmt=FMT))
12 logger.addHandler(debug_handler)
13
14=== modified file 'ubuntu_sso/main/windows.py'
15--- ubuntu_sso/main/windows.py 2012-01-24 22:26:45 +0000
16+++ ubuntu_sso/main/windows.py 2012-02-06 20:19:19 +0000
17@@ -31,7 +31,7 @@
18 from _winreg import HKEY_LOCAL_MACHINE, OpenKey, QueryValueEx
19 # pylint: enable=F0401
20
21-from twisted.internet import defer, reactor
22+from twisted.internet import defer
23 from twisted.internet.task import LoopingCall
24 from twisted.internet.threads import deferToThread
25 from twisted.python.failure import Failure
26@@ -404,21 +404,27 @@
27
28
29 timeout_func = add_timeout
30-shutdown_func = reactor.stop
31 start_setup = lambda *a, **kw: None
32
33 # the reactor does have run and stop methods
34 # pylint: disable=E1101
35
36
37+def shutdown_func():
38+ """Stop the reactor."""
39+ from twisted.internet import reactor
40+ reactor.stop()
41+
42+
43 def finish_setup(result, loop):
44 """Stop the reactor if a failure ocurred."""
45 if isinstance(result, Failure):
46- reactor.stop()
47+ shutdown_func()
48
49
50 def main():
51 """Run the specific mainloop."""
52+ from twisted.internet import reactor
53 reactor.run()
54
55 # pylint: enable=E1101
56
57=== modified file 'ubuntu_sso/utils/__init__.py'
58--- ubuntu_sso/utils/__init__.py 2011-12-15 20:52:32 +0000
59+++ ubuntu_sso/utils/__init__.py 2012-02-06 20:19:19 +0000
60@@ -24,7 +24,6 @@
61
62 from oauth import oauth
63 from urlparse import urlparse
64-from twisted.web import http
65
66 from ubuntu_sso.logger import setup_logging
67 logger = setup_logging("ubuntu_sso.utils")
68@@ -58,6 +57,8 @@
69 request = RequestHead(self.SERVER_URL, headers=headers)
70 response = urllib2.urlopen(request)
71 date_string = response.info()["Date"]
72+ # delay import, otherwise a default reactor gets installed
73+ from twisted.web import http
74 timestamp = http.stringToDatetime(date_string)
75 return timestamp
76
77@@ -76,6 +77,9 @@
78 logger.debug("Error while verifying the server time skew: %r",
79 server_error)
80 self.next_check = local_time + self.ERROR_INTERVAL
81+
82+ # delay import, otherwise a default reactor gets installed
83+ from twisted.web import http
84 logger.debug("Using corrected timestamp: %r",
85 http.datetimeToString(local_time + self.skew))
86 return int(local_time + self.skew)
87
88=== modified file 'ubuntu_sso/utils/ipc.py'
89--- ubuntu_sso/utils/ipc.py 2012-01-10 18:14:09 +0000
90+++ ubuntu_sso/utils/ipc.py 2012-02-06 20:19:19 +0000
91@@ -1,6 +1,6 @@
92 # -*- coding: utf-8 -*-
93 #
94-# Copyright 2011 Canonical Ltd.
95+# Copyright 2011-2012 Canonical Ltd.
96 #
97 # This program is free software: you can redistribute it and/or modify it
98 # under the terms of the GNU General Public License version 3, as published
99@@ -19,7 +19,7 @@
100 from functools import wraps, partial
101 from collections import defaultdict
102
103-from twisted.internet import defer, reactor
104+from twisted.internet import defer
105 from twisted.spread.pb import (
106 DeadReferenceError,
107 NoSuchMethod,
108@@ -41,31 +41,37 @@
109 logger = setup_logging("ubuntu_sso.utils.ipc")
110
111
112-# pylint: disable=E1101
113+# pylint: disable=E1103
114
115 @defer.inlineCallbacks
116-def server_listen(server_factory, service_name, activation_cmdline, port):
117+def server_listen(server_factory, service_name, activation_cmdline, port,
118+ reactor=None):
119 """Connect the IPC server factory."""
120 config = ActivationConfig(service_name, activation_cmdline, port)
121 ai = ActivationInstance(config)
122 port = yield ai.get_port()
123
124+ if reactor is None:
125+ from twisted.internet import reactor
126 connector = reactor.listenTCP(port, server_factory, interface=LOCALHOST)
127 defer.returnValue(connector)
128
129
130 @defer.inlineCallbacks
131-def client_connect(client_factory, service_name, activation_cmdline, port):
132+def client_connect(client_factory, service_name, activation_cmdline, port,
133+ reactor=None):
134 """Connect the IPC client factory."""
135 config = ActivationConfig(service_name, activation_cmdline, port)
136 ac = ActivationClient(config)
137 port = yield ac.get_active_port()
138
139+ if reactor is None:
140+ from twisted.internet import reactor
141 connector = reactor.connectTCP(LOCALHOST, port, client_factory)
142 defer.returnValue(connector)
143
144
145-# pylint: enable=E1101
146+# pylint: enable=E1103
147
148 # ============================== service helpers ==============================
149
150
151=== modified file 'ubuntu_sso/utils/tcpactivation.py'
152--- ubuntu_sso/utils/tcpactivation.py 2011-07-15 17:43:18 +0000
153+++ ubuntu_sso/utils/tcpactivation.py 2012-02-06 20:19:19 +0000
154@@ -1,8 +1,6 @@
155 # -*- coding: utf-8 -*-
156-
157-# Author: Alejandro J. Cura <alecu@canonical.com>
158 #
159-# Copyright 2011 Canonical Ltd.
160+# Copyright 2011-2012 Canonical Ltd.
161 #
162 # This program is free software: you can redistribute it and/or modify it
163 # under the terms of the GNU General Public License version 3, as published
164
165=== modified file 'ubuntu_sso/utils/tests/test_ipc.py'
166--- ubuntu_sso/utils/tests/test_ipc.py 2012-01-05 20:17:53 +0000
167+++ ubuntu_sso/utils/tests/test_ipc.py 2012-02-06 20:19:19 +0000
168@@ -290,7 +290,6 @@
169 def setUp(self):
170 yield super(ListenConnectTestCase, self).setUp()
171 self.fake_reactor = FakeReactor()
172- self.patch(ipc, "reactor", self.fake_reactor)
173
174 @defer.inlineCallbacks
175 def test_server_listen(self):
176@@ -299,7 +298,8 @@
177
178 fake_factory = object()
179 yield ipc.server_listen(fake_factory, TEST_SERVICE,
180- TEST_CMDLINE, TEST_PORT)
181+ TEST_CMDLINE, TEST_PORT,
182+ reactor=self.fake_reactor)
183
184 self.assertEqual(len(self.fake_reactor.connections), 1)
185 conn = self.fake_reactor.connections[0]
186@@ -314,7 +314,8 @@
187
188 fake_factory = object()
189 yield ipc.client_connect(fake_factory, TEST_SERVICE,
190- TEST_CMDLINE, TEST_PORT)
191+ TEST_CMDLINE, TEST_PORT,
192+ reactor=self.fake_reactor)
193
194 self.assertEqual(len(self.fake_reactor.connections), 1)
195 conn = self.fake_reactor.connections[0]
196
197=== modified file 'ubuntu_sso/utils/webclient/timestamp.py'
198--- ubuntu_sso/utils/webclient/timestamp.py 2012-01-28 00:00:11 +0000
199+++ ubuntu_sso/utils/webclient/timestamp.py 2012-02-06 20:19:19 +0000
200@@ -18,7 +18,6 @@
201 import time
202
203 from twisted.internet import defer
204-from twisted.web import http
205
206 from ubuntu_sso.logger import setup_logging
207
208@@ -54,6 +53,8 @@
209 def get_server_time(self):
210 """Get the time at the server."""
211 date_string = yield self.get_server_date_header(self.SERVER_IRI)
212+ # delay import, otherwise a default reactor gets installed
213+ from twisted.web import http
214 timestamp = http.stringToDatetime(date_string)
215 defer.returnValue(timestamp)
216
217@@ -72,6 +73,8 @@
218 except Exception, e:
219 logger.debug("Error while verifying server time skew: %r", e)
220 self.next_check = local_time + self.ERROR_INTERVAL
221+ # delay import, otherwise a default reactor gets installed
222+ from twisted.web import http
223 logger.debug("Using corrected timestamp: %r",
224 http.datetimeToString(local_time + self.skew))
225 defer.returnValue(int(local_time + self.skew))
226
227=== modified file 'ubuntu_sso/utils/webclient/txweb.py'
228--- ubuntu_sso/utils/webclient/txweb.py 2012-01-17 20:39:09 +0000
229+++ ubuntu_sso/utils/webclient/txweb.py 2012-02-06 20:19:19 +0000
230@@ -19,8 +19,7 @@
231
232 from StringIO import StringIO
233
234-from twisted.internet import defer, protocol, reactor
235-from twisted.web import client, http, http_headers, iweb
236+from twisted.internet import defer, protocol
237 from zope.interface import implements
238
239 from ubuntu_sso.utils.webclient.common import (
240@@ -52,6 +51,10 @@
241
242 class StringProducer(object):
243 """Simple implementation of IBodyProducer."""
244+
245+ # delay import, otherwise a default reactor gets installed
246+ from twisted.web import iweb
247+
248 implements(iweb.IBodyProducer)
249
250 def __init__(self, body):
251@@ -75,6 +78,13 @@
252 class WebClient(BaseWebClient):
253 """A simple web client that does not support proxies, yet."""
254
255+ # delay import, otherwise a default reactor gets installed
256+ from twisted.internet import reactor
257+ from twisted.web import client, http, http_headers
258+
259+ # Undefined variable 'http_headers', 'client', 'reactor', 'http'
260+ # pylint: disable=E0602
261+
262 @defer.inlineCallbacks
263 def request(self, iri, method="GET", extra_headers=None,
264 oauth_credentials=None, post_content=None):

Subscribers

People subscribed via source and target branches