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
=== modified file 'ubuntu_sso/logger.py'
--- ubuntu_sso/logger.py 2012-01-20 19:18:29 +0000
+++ ubuntu_sso/logger.py 2012-02-06 20:19:19 +0000
@@ -67,7 +67,7 @@
67 logger.propagate = False67 logger.propagate = False
68 logger.setLevel(LOG_LEVEL)68 logger.setLevel(LOG_LEVEL)
69 logger.addHandler(handler)69 logger.addHandler(handler)
70 if os.environ.get('DEBUG'):70 if os.environ.get('U1_DEBUG'):
71 debug_handler = logging.StreamHandler(sys.stderr)71 debug_handler = logging.StreamHandler(sys.stderr)
72 debug_handler.setFormatter(logging.Formatter(fmt=FMT))72 debug_handler.setFormatter(logging.Formatter(fmt=FMT))
73 logger.addHandler(debug_handler)73 logger.addHandler(debug_handler)
7474
=== modified file 'ubuntu_sso/main/windows.py'
--- ubuntu_sso/main/windows.py 2012-01-24 22:26:45 +0000
+++ ubuntu_sso/main/windows.py 2012-02-06 20:19:19 +0000
@@ -31,7 +31,7 @@
31from _winreg import HKEY_LOCAL_MACHINE, OpenKey, QueryValueEx31from _winreg import HKEY_LOCAL_MACHINE, OpenKey, QueryValueEx
32# pylint: enable=F040132# pylint: enable=F0401
3333
34from twisted.internet import defer, reactor34from twisted.internet import defer
35from twisted.internet.task import LoopingCall35from twisted.internet.task import LoopingCall
36from twisted.internet.threads import deferToThread36from twisted.internet.threads import deferToThread
37from twisted.python.failure import Failure37from twisted.python.failure import Failure
@@ -404,21 +404,27 @@
404404
405405
406timeout_func = add_timeout406timeout_func = add_timeout
407shutdown_func = reactor.stop
408start_setup = lambda *a, **kw: None407start_setup = lambda *a, **kw: None
409408
410# the reactor does have run and stop methods409# the reactor does have run and stop methods
411# pylint: disable=E1101410# pylint: disable=E1101
412411
413412
413def shutdown_func():
414 """Stop the reactor."""
415 from twisted.internet import reactor
416 reactor.stop()
417
418
414def finish_setup(result, loop):419def finish_setup(result, loop):
415 """Stop the reactor if a failure ocurred."""420 """Stop the reactor if a failure ocurred."""
416 if isinstance(result, Failure):421 if isinstance(result, Failure):
417 reactor.stop()422 shutdown_func()
418423
419424
420def main():425def main():
421 """Run the specific mainloop."""426 """Run the specific mainloop."""
427 from twisted.internet import reactor
422 reactor.run()428 reactor.run()
423429
424# pylint: enable=E1101430# pylint: enable=E1101
425431
=== modified file 'ubuntu_sso/utils/__init__.py'
--- ubuntu_sso/utils/__init__.py 2011-12-15 20:52:32 +0000
+++ ubuntu_sso/utils/__init__.py 2012-02-06 20:19:19 +0000
@@ -24,7 +24,6 @@
2424
25from oauth import oauth25from oauth import oauth
26from urlparse import urlparse26from urlparse import urlparse
27from twisted.web import http
2827
29from ubuntu_sso.logger import setup_logging28from ubuntu_sso.logger import setup_logging
30logger = setup_logging("ubuntu_sso.utils")29logger = setup_logging("ubuntu_sso.utils")
@@ -58,6 +57,8 @@
58 request = RequestHead(self.SERVER_URL, headers=headers)57 request = RequestHead(self.SERVER_URL, headers=headers)
59 response = urllib2.urlopen(request)58 response = urllib2.urlopen(request)
60 date_string = response.info()["Date"]59 date_string = response.info()["Date"]
60 # delay import, otherwise a default reactor gets installed
61 from twisted.web import http
61 timestamp = http.stringToDatetime(date_string)62 timestamp = http.stringToDatetime(date_string)
62 return timestamp63 return timestamp
6364
@@ -76,6 +77,9 @@
76 logger.debug("Error while verifying the server time skew: %r",77 logger.debug("Error while verifying the server time skew: %r",
77 server_error)78 server_error)
78 self.next_check = local_time + self.ERROR_INTERVAL79 self.next_check = local_time + self.ERROR_INTERVAL
80
81 # delay import, otherwise a default reactor gets installed
82 from twisted.web import http
79 logger.debug("Using corrected timestamp: %r",83 logger.debug("Using corrected timestamp: %r",
80 http.datetimeToString(local_time + self.skew))84 http.datetimeToString(local_time + self.skew))
81 return int(local_time + self.skew)85 return int(local_time + self.skew)
8286
=== modified file 'ubuntu_sso/utils/ipc.py'
--- ubuntu_sso/utils/ipc.py 2012-01-10 18:14:09 +0000
+++ ubuntu_sso/utils/ipc.py 2012-02-06 20:19:19 +0000
@@ -1,6 +1,6 @@
1# -*- coding: utf-8 -*-1# -*- coding: utf-8 -*-
2#2#
3# Copyright 2011 Canonical Ltd.3# Copyright 2011-2012 Canonical Ltd.
4#4#
5# This program is free software: you can redistribute it and/or modify it5# This program is free software: you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 3, as published6# under the terms of the GNU General Public License version 3, as published
@@ -19,7 +19,7 @@
19from functools import wraps, partial19from functools import wraps, partial
20from collections import defaultdict20from collections import defaultdict
2121
22from twisted.internet import defer, reactor22from twisted.internet import defer
23from twisted.spread.pb import (23from twisted.spread.pb import (
24 DeadReferenceError,24 DeadReferenceError,
25 NoSuchMethod,25 NoSuchMethod,
@@ -41,31 +41,37 @@
41logger = setup_logging("ubuntu_sso.utils.ipc")41logger = setup_logging("ubuntu_sso.utils.ipc")
4242
4343
44# pylint: disable=E110144# pylint: disable=E1103
4545
46@defer.inlineCallbacks46@defer.inlineCallbacks
47def server_listen(server_factory, service_name, activation_cmdline, port):47def server_listen(server_factory, service_name, activation_cmdline, port,
48 reactor=None):
48 """Connect the IPC server factory."""49 """Connect the IPC server factory."""
49 config = ActivationConfig(service_name, activation_cmdline, port)50 config = ActivationConfig(service_name, activation_cmdline, port)
50 ai = ActivationInstance(config)51 ai = ActivationInstance(config)
51 port = yield ai.get_port()52 port = yield ai.get_port()
5253
54 if reactor is None:
55 from twisted.internet import reactor
53 connector = reactor.listenTCP(port, server_factory, interface=LOCALHOST)56 connector = reactor.listenTCP(port, server_factory, interface=LOCALHOST)
54 defer.returnValue(connector)57 defer.returnValue(connector)
5558
5659
57@defer.inlineCallbacks60@defer.inlineCallbacks
58def client_connect(client_factory, service_name, activation_cmdline, port):61def client_connect(client_factory, service_name, activation_cmdline, port,
62 reactor=None):
59 """Connect the IPC client factory."""63 """Connect the IPC client factory."""
60 config = ActivationConfig(service_name, activation_cmdline, port)64 config = ActivationConfig(service_name, activation_cmdline, port)
61 ac = ActivationClient(config)65 ac = ActivationClient(config)
62 port = yield ac.get_active_port()66 port = yield ac.get_active_port()
6367
68 if reactor is None:
69 from twisted.internet import reactor
64 connector = reactor.connectTCP(LOCALHOST, port, client_factory)70 connector = reactor.connectTCP(LOCALHOST, port, client_factory)
65 defer.returnValue(connector)71 defer.returnValue(connector)
6672
6773
68# pylint: enable=E110174# pylint: enable=E1103
6975
70# ============================== service helpers ==============================76# ============================== service helpers ==============================
7177
7278
=== modified file 'ubuntu_sso/utils/tcpactivation.py'
--- ubuntu_sso/utils/tcpactivation.py 2011-07-15 17:43:18 +0000
+++ ubuntu_sso/utils/tcpactivation.py 2012-02-06 20:19:19 +0000
@@ -1,8 +1,6 @@
1# -*- coding: utf-8 -*-1# -*- coding: utf-8 -*-
2
3# Author: Alejandro J. Cura <alecu@canonical.com>
4#2#
5# Copyright 2011 Canonical Ltd.3# Copyright 2011-2012 Canonical Ltd.
6#4#
7# This program is free software: you can redistribute it and/or modify it5# This program is free software: you can redistribute it and/or modify it
8# under the terms of the GNU General Public License version 3, as published6# under the terms of the GNU General Public License version 3, as published
97
=== modified file 'ubuntu_sso/utils/tests/test_ipc.py'
--- ubuntu_sso/utils/tests/test_ipc.py 2012-01-05 20:17:53 +0000
+++ ubuntu_sso/utils/tests/test_ipc.py 2012-02-06 20:19:19 +0000
@@ -290,7 +290,6 @@
290 def setUp(self):290 def setUp(self):
291 yield super(ListenConnectTestCase, self).setUp()291 yield super(ListenConnectTestCase, self).setUp()
292 self.fake_reactor = FakeReactor()292 self.fake_reactor = FakeReactor()
293 self.patch(ipc, "reactor", self.fake_reactor)
294293
295 @defer.inlineCallbacks294 @defer.inlineCallbacks
296 def test_server_listen(self):295 def test_server_listen(self):
@@ -299,7 +298,8 @@
299298
300 fake_factory = object()299 fake_factory = object()
301 yield ipc.server_listen(fake_factory, TEST_SERVICE,300 yield ipc.server_listen(fake_factory, TEST_SERVICE,
302 TEST_CMDLINE, TEST_PORT)301 TEST_CMDLINE, TEST_PORT,
302 reactor=self.fake_reactor)
303303
304 self.assertEqual(len(self.fake_reactor.connections), 1)304 self.assertEqual(len(self.fake_reactor.connections), 1)
305 conn = self.fake_reactor.connections[0]305 conn = self.fake_reactor.connections[0]
@@ -314,7 +314,8 @@
314314
315 fake_factory = object()315 fake_factory = object()
316 yield ipc.client_connect(fake_factory, TEST_SERVICE,316 yield ipc.client_connect(fake_factory, TEST_SERVICE,
317 TEST_CMDLINE, TEST_PORT)317 TEST_CMDLINE, TEST_PORT,
318 reactor=self.fake_reactor)
318319
319 self.assertEqual(len(self.fake_reactor.connections), 1)320 self.assertEqual(len(self.fake_reactor.connections), 1)
320 conn = self.fake_reactor.connections[0]321 conn = self.fake_reactor.connections[0]
321322
=== modified file 'ubuntu_sso/utils/webclient/timestamp.py'
--- ubuntu_sso/utils/webclient/timestamp.py 2012-01-28 00:00:11 +0000
+++ ubuntu_sso/utils/webclient/timestamp.py 2012-02-06 20:19:19 +0000
@@ -18,7 +18,6 @@
18import time18import time
1919
20from twisted.internet import defer20from twisted.internet import defer
21from twisted.web import http
2221
23from ubuntu_sso.logger import setup_logging22from ubuntu_sso.logger import setup_logging
2423
@@ -54,6 +53,8 @@
54 def get_server_time(self):53 def get_server_time(self):
55 """Get the time at the server."""54 """Get the time at the server."""
56 date_string = yield self.get_server_date_header(self.SERVER_IRI)55 date_string = yield self.get_server_date_header(self.SERVER_IRI)
56 # delay import, otherwise a default reactor gets installed
57 from twisted.web import http
57 timestamp = http.stringToDatetime(date_string)58 timestamp = http.stringToDatetime(date_string)
58 defer.returnValue(timestamp)59 defer.returnValue(timestamp)
5960
@@ -72,6 +73,8 @@
72 except Exception, e:73 except Exception, e:
73 logger.debug("Error while verifying server time skew: %r", e)74 logger.debug("Error while verifying server time skew: %r", e)
74 self.next_check = local_time + self.ERROR_INTERVAL75 self.next_check = local_time + self.ERROR_INTERVAL
76 # delay import, otherwise a default reactor gets installed
77 from twisted.web import http
75 logger.debug("Using corrected timestamp: %r",78 logger.debug("Using corrected timestamp: %r",
76 http.datetimeToString(local_time + self.skew))79 http.datetimeToString(local_time + self.skew))
77 defer.returnValue(int(local_time + self.skew))80 defer.returnValue(int(local_time + self.skew))
7881
=== modified file 'ubuntu_sso/utils/webclient/txweb.py'
--- ubuntu_sso/utils/webclient/txweb.py 2012-01-17 20:39:09 +0000
+++ ubuntu_sso/utils/webclient/txweb.py 2012-02-06 20:19:19 +0000
@@ -19,8 +19,7 @@
1919
20from StringIO import StringIO20from StringIO import StringIO
2121
22from twisted.internet import defer, protocol, reactor22from twisted.internet import defer, protocol
23from twisted.web import client, http, http_headers, iweb
24from zope.interface import implements23from zope.interface import implements
2524
26from ubuntu_sso.utils.webclient.common import (25from ubuntu_sso.utils.webclient.common import (
@@ -52,6 +51,10 @@
5251
53class StringProducer(object):52class StringProducer(object):
54 """Simple implementation of IBodyProducer."""53 """Simple implementation of IBodyProducer."""
54
55 # delay import, otherwise a default reactor gets installed
56 from twisted.web import iweb
57
55 implements(iweb.IBodyProducer)58 implements(iweb.IBodyProducer)
5659
57 def __init__(self, body):60 def __init__(self, body):
@@ -75,6 +78,13 @@
75class WebClient(BaseWebClient):78class WebClient(BaseWebClient):
76 """A simple web client that does not support proxies, yet."""79 """A simple web client that does not support proxies, yet."""
7780
81 # delay import, otherwise a default reactor gets installed
82 from twisted.internet import reactor
83 from twisted.web import client, http, http_headers
84
85 # Undefined variable 'http_headers', 'client', 'reactor', 'http'
86 # pylint: disable=E0602
87
78 @defer.inlineCallbacks88 @defer.inlineCallbacks
79 def request(self, iri, method="GET", extra_headers=None,89 def request(self, iri, method="GET", extra_headers=None,
80 oauth_credentials=None, post_content=None):90 oauth_credentials=None, post_content=None):

Subscribers

People subscribed via source and target branches