From de5ad4071ed3cf8bda2b615b45239f852712869c Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Sat, 21 Mar 2009 17:44:59 +0000 Subject: [PATCH] Send console trust prompt to stderr, not stdout Ask using stderr, because we may be writing XML to stdout. --- tests/testdownload.py | 76 ++++++++++++++++++----------------------- zeroinstall/injector/handler.py | 17 +++++---- 2 files changed, 43 insertions(+), 50 deletions(-) diff --git a/tests/testdownload.py b/tests/testdownload.py index 2aa87e3..e432725 100755 --- a/tests/testdownload.py +++ b/tests/testdownload.py @@ -1,9 +1,11 @@ #!/usr/bin/env python2.5 +from __future__ import with_statement from basetest import BaseTest import sys, tempfile, os from StringIO import StringIO import unittest, signal from logging import getLogger, WARN, ERROR +from contextlib import contextmanager sys.path.insert(0, '..') @@ -28,6 +30,18 @@ sys.modules['dbus.glib'] = my_dbus my_dbus.types = my_dbus sys.modules['dbus.types'] = my_dbus +@contextmanager +def output_suppressed(): + old_stdout = sys.stdout + old_stderr = sys.stderr + try: + sys.stdout = StringIO() + sys.stderr = StringIO() + yield + finally: + sys.stdout = old_stdout + sys.stderr = old_stderr + class Reply: def __init__(self, reply): self.reply = reply @@ -76,9 +90,7 @@ class TestDownload(BaseTest): self.child = None def testRejectKey(self): - old_out = sys.stdout - try: - sys.stdout = StringIO() + with output_suppressed(): self.child = server.handle_requests('Hello', '6FCF121BE2390E0B.gpg') policy = autopolicy.AutoPolicy('http://localhost:8000/Hello', download_only = False, handler = DummyHandler()) @@ -90,13 +102,9 @@ class TestDownload(BaseTest): except model.SafeException, ex: if "Not signed with a trusted key" not in str(ex): raise ex - finally: - sys.stdout = old_out def testRejectKeyXML(self): - old_out = sys.stdout - try: - sys.stdout = StringIO() + with output_suppressed(): self.child = server.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg') policy = autopolicy.AutoPolicy('http://example.com:8000/Hello.xml', download_only = False, handler = DummyHandler()) @@ -108,30 +116,26 @@ class TestDownload(BaseTest): except model.SafeException, ex: if "Not signed with a trusted key" not in str(ex): raise - finally: - sys.stdout = old_out def testImport(self): - old_out = sys.stdout - try: - from zeroinstall.injector import cli + from zeroinstall.injector import cli - rootLogger = getLogger() - rootLogger.disabled = True + rootLogger = getLogger() + rootLogger.disabled = True + try: try: - try: - cli.main(['--import', '-v', 'NO-SUCH-FILE']) - assert 0 - except model.SafeException, ex: - assert 'NO-SUCH-FILE' in str(ex) - finally: - rootLogger.disabled = False - rootLogger.setLevel(WARN) + cli.main(['--import', '-v', 'NO-SUCH-FILE']) + assert 0 + except model.SafeException, ex: + assert 'NO-SUCH-FILE' in str(ex) + finally: + rootLogger.disabled = False + rootLogger.setLevel(WARN) - hello = iface_cache.iface_cache.get_interface('http://localhost:8000/Hello') - self.assertEquals(0, len(hello.implementations)) + hello = iface_cache.iface_cache.get_interface('http://localhost:8000/Hello') + self.assertEquals(0, len(hello.implementations)) - sys.stdout = StringIO() + with output_suppressed(): self.child = server.handle_requests('6FCF121BE2390E0B.gpg') sys.stdin = Reply("Y\n") @@ -146,8 +150,6 @@ class TestDownload(BaseTest): # Shouldn't need to prompt the second time sys.stdin = None cli.main(['--import', 'Hello']) - finally: - sys.stdout = old_out def testSelections(self): from zeroinstall.injector.cli import _download_missing_selections @@ -155,9 +157,7 @@ class TestDownload(BaseTest): sels = selections.Selections(root) class Options: dry_run = False - old_out = sys.stdout - try: - sys.stdout = StringIO() + with output_suppressed(): self.child = server.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz') sys.stdin = Reply("Y\n") _download_missing_selections(Options(), sels) @@ -165,8 +165,6 @@ class TestDownload(BaseTest): assert os.path.exists(os.path.join(path, 'HelloWorld', 'main')) assert sels.download_missing(iface_cache.iface_cache, None) is None - finally: - sys.stdout = old_out def testSelectionsWithFeed(self): from zeroinstall.injector.cli import _download_missing_selections @@ -174,9 +172,7 @@ class TestDownload(BaseTest): sels = selections.Selections(root) class Options: dry_run = False - old_out = sys.stdout - try: - sys.stdout = StringIO() + with output_suppressed(): self.child = server.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz') sys.stdin = Reply("Y\n") @@ -191,13 +187,9 @@ class TestDownload(BaseTest): assert os.path.exists(os.path.join(path, 'HelloWorld', 'main')) assert sels.download_missing(iface_cache.iface_cache, None) is None - finally: - sys.stdout = old_out def testAcceptKey(self): - old_out = sys.stdout - try: - sys.stdout = StringIO() + with output_suppressed(): self.child = server.handle_requests('Hello', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz') policy = autopolicy.AutoPolicy('http://localhost:8000/Hello', download_only = False, handler = DummyHandler()) @@ -209,8 +201,6 @@ class TestDownload(BaseTest): except model.SafeException, ex: if "HelloWorld/Missing" not in str(ex): raise ex - finally: - sys.stdout = old_out def testRecipe(self): old_out = sys.stdout diff --git a/zeroinstall/injector/handler.py b/zeroinstall/injector/handler.py index 916a8c2..a0124ff 100644 --- a/zeroinstall/injector/handler.py +++ b/zeroinstall/injector/handler.py @@ -10,6 +10,7 @@ To do this, you supply a L{Handler} to the L{policy}. # Copyright (C) 2009, Thomas Leonard # See the README file for details, or visit http://0install.net. +import sys from logging import debug, warn from zeroinstall import NeedDownload, SafeException @@ -136,24 +137,26 @@ class Handler(object): domain = trust.domain_from_url(interface.uri) - print "\nInterface:", interface.uri - print "The interface is correctly signed with the following keys:" + # Ask on stderr, because we may be writing XML to stdout + print >>sys.stderr, "\nInterface:", interface.uri + print >>sys.stderr, "The interface is correctly signed with the following keys:" for x in valid_sigs: - print "-", x + print >>sys.stderr, "-", x if len(valid_sigs) == 1: - print "Do you want to trust this key to sign feeds from '%s'?" % domain + print >>sys.stderr, "Do you want to trust this key to sign feeds from '%s'?" % domain else: - print "Do you want to trust all of these keys to sign feeds from '%s'?" % domain + print >>sys.stderr, "Do you want to trust all of these keys to sign feeds from '%s'?" % domain while True: - i = raw_input("Trust [Y/N] ") + print >>sys.stderr, "Trust [Y/N] ", + i = raw_input() if not i: continue if i in 'Nn': raise NoTrustedKeys('Not signed with a trusted key') if i in 'Yy': break for key in valid_sigs: - print "Trusting", key.fingerprint, "for", domain + print >>sys.stderr, "Trusting", key.fingerprint, "for", domain trust.trust_db.trust_key(key.fingerprint, domain) trust.trust_db.notify() -- 2.11.4.GIT