mirror of
https://git.launchpad.net/~ubuntu-release/britney/+git/britney2-ubuntu
synced 2025-04-29 04:31:46 +00:00
Migrate hint parser to fully use logging
Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
parent
5e825043d3
commit
ac69b3b5c7
@ -282,7 +282,7 @@ class Britney(object):
|
|||||||
|
|
||||||
# parse the command line arguments
|
# parse the command line arguments
|
||||||
self.policies = []
|
self.policies = []
|
||||||
self._hint_parser = HintParser(self)
|
self._hint_parser = HintParser()
|
||||||
self.suite_info = {}
|
self.suite_info = {}
|
||||||
self.__parse_arguments()
|
self.__parse_arguments()
|
||||||
MigrationItem.set_architectures(self.options.architectures)
|
MigrationItem.set_architectures(self.options.architectures)
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
# GNU General Public License for more details.
|
# GNU General Public License for more details.
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from britney2.migrationitem import MigrationItem
|
from britney2.migrationitem import MigrationItem
|
||||||
@ -131,8 +133,9 @@ def single_hint_taking_list_of_packages(hints, who, hint_type, *args):
|
|||||||
|
|
||||||
class HintParser(object):
|
class HintParser(object):
|
||||||
|
|
||||||
def __init__(self, britney):
|
def __init__(self):
|
||||||
self._britney = britney
|
logger_name = ".".join((self.__class__.__module__, self.__class__.__name__))
|
||||||
|
self.logger = logging.getLogger(logger_name)
|
||||||
self.hints = HintCollection()
|
self.hints = HintCollection()
|
||||||
self._hint_table = {
|
self._hint_table = {
|
||||||
'remark': (0, lambda *x: None),
|
'remark': (0, lambda *x: None),
|
||||||
@ -215,24 +218,21 @@ class HintParser(object):
|
|||||||
if hint_name == 'finished':
|
if hint_name == 'finished':
|
||||||
break
|
break
|
||||||
if hint_name not in hint_table:
|
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
|
continue
|
||||||
if hint_name not in permitted_hints and 'ALL' not in permitted_hints:
|
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
|
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" % (
|
self.logger.info("Ignoring \"%s\" hint from %s found in %s (line %d): %s" % (
|
||||||
hint_name, who, filename, line_no, reason), type="I")
|
hint_name, who, filename, line_no, reason))
|
||||||
continue
|
continue
|
||||||
min_args, hint_parser_impl = hint_table[hint_name]
|
min_args, hint_parser_impl = hint_table[hint_name]
|
||||||
if len(l) - 1 < min_args:
|
if len(l) - 1 < min_args:
|
||||||
self.log("Malformed hint found in %s (line %d): Needs at least %d argument(s), got %d" % (
|
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), type="W")
|
filename, line_no, min_args, len(l) - 1))
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
hint_parser_impl(hints, who, *l)
|
hint_parser_impl(hints, who, *l)
|
||||||
except MalformedHintException as e:
|
except MalformedHintException as e:
|
||||||
self.log("Malformed hint found in %s (line %d): \"%s\"" % (
|
self.logger.warning("Malformed hint found in %s (line %d): \"%s\"" % (
|
||||||
filename, line_no, e.args[0]), type="W")
|
filename, line_no, e.args[0]))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def log(self, msg, type="I"):
|
|
||||||
self._britney.log(msg, type=type)
|
|
||||||
|
@ -1,18 +1,13 @@
|
|||||||
|
import logging
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from britney2.hints import HintParser, single_hint_taking_list_of_packages
|
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):
|
def new_hint_parser():
|
||||||
if logger is None:
|
return HintParser()
|
||||||
def empty_logger(x, type='I'):
|
|
||||||
pass
|
|
||||||
logger = empty_logger
|
|
||||||
fake_britney = MockObject(log=logger)
|
|
||||||
hint_parser = HintParser(fake_britney)
|
|
||||||
return hint_parser
|
|
||||||
|
|
||||||
|
|
||||||
def parse_should_not_call_this_function(*args, **kwargs):
|
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):
|
class HintParsing(unittest.TestCase):
|
||||||
|
|
||||||
def test_parse_invalid_hints(self):
|
def test_parse_invalid_hints(self):
|
||||||
hint_log = []
|
hint_parser = new_hint_parser()
|
||||||
hint_parser = new_hint_parser(lambda x, type='I': hint_log.append(x))
|
|
||||||
|
|
||||||
hint_parser.register_hint_type('min-10-arg', parse_should_not_call_this_function, min_args=10)
|
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)
|
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:
|
for test in tests:
|
||||||
hint_parser.parse_hints(TEST_HINTER, test['permissions'], 'test-parse-hint', [test['hint_text']])
|
with self.assertLogs() as cm:
|
||||||
assert len(hint_log) == 1
|
hint_parser.parse_hints(TEST_HINTER, test['permissions'], 'test-parse-hint', [test['hint_text']])
|
||||||
assert test['error_message_contains'] in hint_log[0]
|
|
||||||
|
assert len(cm.output) == 1
|
||||||
|
assert test['error_message_contains'] in cm.output[0]
|
||||||
assert hint_parser.hints.is_empty
|
assert hint_parser.hints.is_empty
|
||||||
hint_log.clear()
|
|
||||||
|
|
||||||
def test_alias(self):
|
def test_alias(self):
|
||||||
hint_parser = new_hint_parser()
|
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)
|
policy = policy_class(options, suite_info, *args)
|
||||||
fake_britney = MockObject(log=lambda x, y='I': None)
|
fake_britney = MockObject(log=lambda x, y='I': None)
|
||||||
hint_parser = HintParser(fake_britney)
|
hint_parser = HintParser()
|
||||||
policy.initialise(fake_britney)
|
policy.initialise(fake_britney)
|
||||||
policy.register_hints(hint_parser)
|
policy.register_hints(hint_parser)
|
||||||
hint_parser.parse_hints(TEST_HINTER, HINTS_ALL, 'test-%s' % test_name, hints)
|
hint_parser.parse_hints(TEST_HINTER, HINTS_ALL, 'test-%s' % test_name, hints)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user