Migrate hint parser to fully use logging

Signed-off-by: Niels Thykier <niels@thykier.net>
ubuntu/rebased
Niels Thykier 7 years ago
parent 5e825043d3
commit ac69b3b5c7

@ -282,7 +282,7 @@ class Britney(object):
# parse the command line arguments
self.policies = []
self._hint_parser = HintParser(self)
self._hint_parser = HintParser()
self.suite_info = {}
self.__parse_arguments()
MigrationItem.set_architectures(self.options.architectures)

@ -12,6 +12,8 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import logging
from itertools import chain
from britney2.migrationitem import MigrationItem
@ -131,8 +133,9 @@ def single_hint_taking_list_of_packages(hints, who, hint_type, *args):
class HintParser(object):
def __init__(self, britney):
self._britney = britney
def __init__(self):
logger_name = ".".join((self.__class__.__module__, self.__class__.__name__))
self.logger = logging.getLogger(logger_name)
self.hints = HintCollection()
self._hint_table = {
'remark': (0, lambda *x: None),
@ -215,24 +218,21 @@ class HintParser(object):
if hint_name == 'finished':
break
if hint_name not in hint_table:
self.log("Unknown hint found in %s (line %d): '%s'" % (filename, line_no, line), type="W")
self.logger.warning("Unknown hint found in %s (line %d): '%s'" % (filename, line_no, line))
continue
if hint_name not in permitted_hints and 'ALL' not in permitted_hints:
reason = 'The hint is not a part of the permitted hints for ' + who
self.log("Ignoring \"%s\" hint from %s found in %s (line %d): %s" % (
hint_name, who, filename, line_no, reason), type="I")
self.logger.info("Ignoring \"%s\" hint from %s found in %s (line %d): %s" % (
hint_name, who, filename, line_no, reason))
continue
min_args, hint_parser_impl = hint_table[hint_name]
if len(l) - 1 < min_args:
self.log("Malformed hint found in %s (line %d): Needs at least %d argument(s), got %d" % (
filename, line_no, min_args, len(l) - 1), type="W")
self.logger.warning("Malformed hint found in %s (line %d): Needs at least %d argument(s), got %d" % (
filename, line_no, min_args, len(l) - 1))
continue
try:
hint_parser_impl(hints, who, *l)
except MalformedHintException as e:
self.log("Malformed hint found in %s (line %d): \"%s\"" % (
filename, line_no, e.args[0]), type="W")
self.logger.warning("Malformed hint found in %s (line %d): \"%s\"" % (
filename, line_no, e.args[0]))
continue
def log(self, msg, type="I"):
self._britney.log(msg, type=type)

@ -1,18 +1,13 @@
import logging
import unittest
from britney2.hints import HintParser, single_hint_taking_list_of_packages
from . import MockObject, HINTS_ALL, TEST_HINTER
from . import HINTS_ALL, TEST_HINTER
def new_hint_parser(logger=None):
if logger is None:
def empty_logger(x, type='I'):
pass
logger = empty_logger
fake_britney = MockObject(log=logger)
hint_parser = HintParser(fake_britney)
return hint_parser
def new_hint_parser():
return HintParser()
def parse_should_not_call_this_function(*args, **kwargs):
@ -22,8 +17,7 @@ def parse_should_not_call_this_function(*args, **kwargs):
class HintParsing(unittest.TestCase):
def test_parse_invalid_hints(self):
hint_log = []
hint_parser = new_hint_parser(lambda x, type='I': hint_log.append(x))
hint_parser = new_hint_parser()
hint_parser.register_hint_type('min-10-arg', parse_should_not_call_this_function, min_args=10)
hint_parser.register_hint_type('simple-hint', parse_should_not_call_this_function)
@ -47,11 +41,12 @@ class HintParsing(unittest.TestCase):
]
for test in tests:
hint_parser.parse_hints(TEST_HINTER, test['permissions'], 'test-parse-hint', [test['hint_text']])
assert len(hint_log) == 1
assert test['error_message_contains'] in hint_log[0]
with self.assertLogs() as cm:
hint_parser.parse_hints(TEST_HINTER, test['permissions'], 'test-parse-hint', [test['hint_text']])
assert len(cm.output) == 1
assert test['error_message_contains'] in cm.output[0]
assert hint_parser.hints.is_empty
hint_log.clear()
def test_alias(self):
hint_parser = new_hint_parser()

@ -24,7 +24,7 @@ def initialize_policy(test_name, policy_class, *args, **kwargs):
}
policy = policy_class(options, suite_info, *args)
fake_britney = MockObject(log=lambda x, y='I': None)
hint_parser = HintParser(fake_britney)
hint_parser = HintParser()
policy.initialise(fake_britney)
policy.register_hints(hint_parser)
hint_parser.parse_hints(TEST_HINTER, HINTS_ALL, 'test-%s' % test_name, hints)

Loading…
Cancel
Save