Wrangle the Reporter to also be --quiet

This commit is contained in:
Peter Eckersley
2016-04-01 20:14:06 -07:00
parent 6e7cc4ee53
commit 9201f2691f
8 changed files with 30 additions and 28 deletions
+1 -1
View File
@@ -98,7 +98,7 @@ def report_new_account(acc, config):
recovery_msg = ("If you lose your account credentials, you can " recovery_msg = ("If you lose your account credentials, you can "
"recover through e-mails sent to {0}.".format( "recover through e-mails sent to {0}.".format(
", ".join(acc.regr.body.emails))) ", ".join(acc.regr.body.emails)))
reporter.add_message(recovery_msg, reporter.HIGH_PRIORITY) reporter.add_message(recovery_msg, reporter.MEDIUM_PRIORITY)
class AccountMemoryStorage(interfaces.AccountStorage): class AccountMemoryStorage(interfaces.AccountStorage):
+1 -1
View File
@@ -62,7 +62,7 @@ def renew_hook(config, domains, lineage_path):
os.environ["RENEWED_LINEAGE"] = lineage_path os.environ["RENEWED_LINEAGE"] = lineage_path
_run_hook(config.renew_hook) _run_hook(config.renew_hook)
else: else:
print("Dry run: skipping renewal hook command: {0}".format(config.renew_hook)) logger.warning("Dry run: skipping renewal hook command: %s", config.renew_hook)
def _run_hook(shell_cmd): def _run_hook(shell_cmd):
"""Run a hook command. """Run a hook command.
+1 -1
View File
@@ -687,7 +687,7 @@ def main(cli_args=sys.argv[1:]):
zope.component.provideUtility(displayer) zope.component.provideUtility(displayer)
# Reporter # Reporter
report = reporter.Reporter() report = reporter.Reporter(config)
zope.component.provideUtility(report) zope.component.provideUtility(report)
atexit.register(report.atexit_print_messages) atexit.register(report.atexit_print_messages)
+2 -4
View File
@@ -307,9 +307,6 @@ def _renew_describe_results(config, renew_successes, renew_failures,
def renew_all_lineages(config): def renew_all_lineages(config):
"""Examine each lineage; renew if due and report results""" """Examine each lineage; renew if due and report results"""
def _notify(msg):
zope.component.getUtility(interfaces.IDisplay).notification(msg, pause=False)
if config.domains != []: if config.domains != []:
raise errors.Error("Currently, the renew verb is only capable of " raise errors.Error("Currently, the renew verb is only capable of "
"renewing all installed certificates that are due " "renewing all installed certificates that are due "
@@ -324,7 +321,8 @@ def renew_all_lineages(config):
renew_skipped = [] renew_skipped = []
parse_failures = [] parse_failures = []
for renewal_file in renewal_conf_files(renewer_config): for renewal_file in renewal_conf_files(renewer_config):
_notify("Processing " + renewal_file) disp = zope.component.getUtility(interfaces.IDisplay)
disp.notification("Processing " + renewal_file, pause=False)
lineage_config = copy.deepcopy(config) lineage_config = copy.deepcopy(config)
# Note that this modifies config (to add back the configuration # Note that this modifies config (to add back the configuration
+8 -1
View File
@@ -35,8 +35,9 @@ class Reporter(object):
_msg_type = collections.namedtuple('ReporterMsg', 'priority text on_crash') _msg_type = collections.namedtuple('ReporterMsg', 'priority text on_crash')
def __init__(self): def __init__(self, config):
self.messages = queue.PriorityQueue() self.messages = queue.PriorityQueue()
self.config = config
def add_message(self, msg, priority, on_crash=True): def add_message(self, msg, priority, on_crash=True):
"""Adds msg to the list of messages to be printed. """Adds msg to the list of messages to be printed.
@@ -76,6 +77,7 @@ class Reporter(object):
if not self.messages.empty(): if not self.messages.empty():
no_exception = sys.exc_info()[0] is None no_exception = sys.exc_info()[0] is None
bold_on = sys.stdout.isatty() bold_on = sys.stdout.isatty()
if not self.config.quiet:
if bold_on: if bold_on:
print(le_util.ANSI_SGR_BOLD) print(le_util.ANSI_SGR_BOLD)
print('IMPORTANT NOTES:') print('IMPORTANT NOTES:')
@@ -86,6 +88,11 @@ class Reporter(object):
subsequent_indent=first_wrapper.subsequent_indent) subsequent_indent=first_wrapper.subsequent_indent)
while not self.messages.empty(): while not self.messages.empty():
msg = self.messages.get() msg = self.messages.get()
if self.config.quiet:
# In --quiet mode, we only print high priority messages that
# are flagged for crash cases
if not (msg.priority == self.HIGH_PRIORITY and msg.on_crash):
continue
if no_exception or msg.on_crash: if no_exception or msg.on_crash:
if bold_on and msg.priority > self.HIGH_PRIORITY: if bold_on and msg.priority > self.HIGH_PRIORITY:
sys.stdout.write(le_util.ANSI_SGR_RESET) sys.stdout.write(le_util.ANSI_SGR_RESET)
+1 -1
View File
@@ -373,7 +373,7 @@ class CLITest(unittest.TestCase): # pylint: disable=too-many-public-methods
try: try:
self._call(['--csr', CSR]) self._call(['--csr', CSR])
except errors.Error as e: except errors.Error as e:
assert "Please try the certonly" in e.message assert "Please try the certonly" in repr(e)
return return
assert False, "Expected supplying --csr to fail with default verb" assert False, "Expected supplying --csr to fail with default verb"
+6 -10
View File
@@ -3,7 +3,6 @@
import os import os
import unittest import unittest
import sys
import mock import mock
@@ -47,12 +46,14 @@ class HookTest(unittest.TestCase):
mockwhich.return_value = None mockwhich.return_value = None
self.assertEqual(hooks._prog("funky"), None) self.assertEqual(hooks._prog("funky"), None)
def _test_a_hook(self, config, hook_function, calls_expected): @mock.patch('letsencrypt.hooks.logger')
with mock.patch('letsencrypt.hooks.logger'): def _test_a_hook(self, config, hook_function, calls_expected, mock_logger):
mock_logger.warning = mock.MagicMock()
with mock.patch('letsencrypt.hooks._run_hook') as mock_run_hook: with mock.patch('letsencrypt.hooks._run_hook') as mock_run_hook:
hook_function(config) hook_function(config)
hook_function(config) hook_function(config)
self.assertEqual(mock_run_hook.call_count, calls_expected) self.assertEqual(mock_run_hook.call_count, calls_expected)
return mock_logger.warning
def test_pre_hook(self): def test_pre_hook(self):
config = mock.MagicMock(pre_hook="true") config = mock.MagicMock(pre_hook="true")
@@ -78,13 +79,8 @@ class HookTest(unittest.TestCase):
self.assertEqual(os.environ["RENEWED_LINEAGE"], "thing") self.assertEqual(os.environ["RENEWED_LINEAGE"], "thing")
config = mock.MagicMock(renew_hook="true", dry_run=True) config = mock.MagicMock(renew_hook="true", dry_run=True)
if sys.version_info < (2, 7): mock_warn = self._test_a_hook(config, rhook, 0)
# the print() function is not mockable in py26 self.assertEqual(mock_warn.call_count, 2)
self._test_a_hook(config, rhook, 0)
else:
with mock.patch("letsencrypt.hooks.print") as mock_print:
self._test_a_hook(config, rhook, 0)
self.assertEqual(mock_print.call_count, 2)
@mock.patch('letsencrypt.hooks.Popen') @mock.patch('letsencrypt.hooks.Popen')
def test_run_hook(self, mock_popen): def test_run_hook(self, mock_popen):
+2 -1
View File
@@ -1,4 +1,5 @@
"""Tests for letsencrypt.reporter.""" """Tests for letsencrypt.reporter."""
import mock
import sys import sys
import unittest import unittest
@@ -10,7 +11,7 @@ class ReporterTest(unittest.TestCase):
def setUp(self): def setUp(self):
from letsencrypt import reporter from letsencrypt import reporter
self.reporter = reporter.Reporter() self.reporter = reporter.Reporter(mock.MagicMock(quiet=False))
self.old_stdout = sys.stdout self.old_stdout = sys.stdout
sys.stdout = six.StringIO() sys.stdout = six.StringIO()