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 "
"recover through e-mails sent to {0}.".format(
", ".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):
+1 -1
View File
@@ -62,7 +62,7 @@ def renew_hook(config, domains, lineage_path):
os.environ["RENEWED_LINEAGE"] = lineage_path
_run_hook(config.renew_hook)
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):
"""Run a hook command.
+1 -1
View File
@@ -687,7 +687,7 @@ def main(cli_args=sys.argv[1:]):
zope.component.provideUtility(displayer)
# Reporter
report = reporter.Reporter()
report = reporter.Reporter(config)
zope.component.provideUtility(report)
atexit.register(report.atexit_print_messages)
+3 -5
View File
@@ -288,7 +288,7 @@ def _renew_describe_results(config, renew_successes, renew_failures,
if parse_failures:
notify("\nAdditionally, the following renewal configuration files "
"were invalid: ")
"were invalid: ")
notify(parse_failures, "parsefail")
if config.dry_run:
@@ -307,9 +307,6 @@ def _renew_describe_results(config, renew_successes, renew_failures,
def renew_all_lineages(config):
"""Examine each lineage; renew if due and report results"""
def _notify(msg):
zope.component.getUtility(interfaces.IDisplay).notification(msg, pause=False)
if config.domains != []:
raise errors.Error("Currently, the renew verb is only capable of "
"renewing all installed certificates that are due "
@@ -324,7 +321,8 @@ def renew_all_lineages(config):
renew_skipped = []
parse_failures = []
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)
# Note that this modifies config (to add back the configuration
+11 -4
View File
@@ -35,8 +35,9 @@ class Reporter(object):
_msg_type = collections.namedtuple('ReporterMsg', 'priority text on_crash')
def __init__(self):
def __init__(self, config):
self.messages = queue.PriorityQueue()
self.config = config
def add_message(self, msg, priority, on_crash=True):
"""Adds msg to the list of messages to be printed.
@@ -76,9 +77,10 @@ class Reporter(object):
if not self.messages.empty():
no_exception = sys.exc_info()[0] is None
bold_on = sys.stdout.isatty()
if bold_on:
print(le_util.ANSI_SGR_BOLD)
print('IMPORTANT NOTES:')
if not self.config.quiet:
if bold_on:
print(le_util.ANSI_SGR_BOLD)
print('IMPORTANT NOTES:')
first_wrapper = textwrap.TextWrapper(
initial_indent=' - ', subsequent_indent=(' ' * 3))
next_wrapper = textwrap.TextWrapper(
@@ -86,6 +88,11 @@ class Reporter(object):
subsequent_indent=first_wrapper.subsequent_indent)
while not self.messages.empty():
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 bold_on and msg.priority > self.HIGH_PRIORITY:
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:
self._call(['--csr', CSR])
except errors.Error as e:
assert "Please try the certonly" in e.message
assert "Please try the certonly" in repr(e)
return
assert False, "Expected supplying --csr to fail with default verb"
+10 -14
View File
@@ -3,7 +3,6 @@
import os
import unittest
import sys
import mock
@@ -47,12 +46,14 @@ class HookTest(unittest.TestCase):
mockwhich.return_value = None
self.assertEqual(hooks._prog("funky"), None)
def _test_a_hook(self, config, hook_function, calls_expected):
with mock.patch('letsencrypt.hooks.logger'):
with mock.patch('letsencrypt.hooks._run_hook') as mock_run_hook:
hook_function(config)
hook_function(config)
self.assertEqual(mock_run_hook.call_count, calls_expected)
@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:
hook_function(config)
hook_function(config)
self.assertEqual(mock_run_hook.call_count, calls_expected)
return mock_logger.warning
def test_pre_hook(self):
config = mock.MagicMock(pre_hook="true")
@@ -78,13 +79,8 @@ class HookTest(unittest.TestCase):
self.assertEqual(os.environ["RENEWED_LINEAGE"], "thing")
config = mock.MagicMock(renew_hook="true", dry_run=True)
if sys.version_info < (2, 7):
# the print() function is not mockable in py26
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_warn = self._test_a_hook(config, rhook, 0)
self.assertEqual(mock_warn.call_count, 2)
@mock.patch('letsencrypt.hooks.Popen')
def test_run_hook(self, mock_popen):
+2 -1
View File
@@ -1,4 +1,5 @@
"""Tests for letsencrypt.reporter."""
import mock
import sys
import unittest
@@ -10,7 +11,7 @@ class ReporterTest(unittest.TestCase):
def setUp(self):
from letsencrypt import reporter
self.reporter = reporter.Reporter()
self.reporter = reporter.Reporter(mock.MagicMock(quiet=False))
self.old_stdout = sys.stdout
sys.stdout = six.StringIO()