Convert element_dependencies to logging

Use standard logging module for output.  Add some basic testing of
error messages to the unit-tests.  Use the logging_config module to
setup the logging for interactive use.

Change-Id: Ia23722a7bd00aba336118edb155356a3b3ef6926
This commit is contained in:
Ian Wienand 2016-06-10 14:04:26 +10:00
parent 9dd267239d
commit 2b18513cad
2 changed files with 24 additions and 9 deletions

View File

@ -15,9 +15,14 @@
from __future__ import print_function from __future__ import print_function
import argparse import argparse
import collections import collections
import logging
import os import os
import sys import sys
import diskimage_builder.logging_config
logger = logging.getLogger(__name__)
def get_elements_dir(): def get_elements_dir():
if not os.environ.get('ELEMENTS_PATH'): if not os.environ.get('ELEMENTS_PATH'):
@ -42,8 +47,7 @@ def _get_set(element, fname, elements_dir=None):
else: else:
raise raise
sys.stderr.write("ERROR: Element '%s' not found in '%s'\n" % logger.error("Element '%s' not found in '%s'" % (element, elements_dir))
(element, elements_dir))
sys.exit(-1) sys.exit(-1)
@ -100,20 +104,22 @@ def expand_dependencies(user_elements, elements_dir=None):
final_elements.update(deps) final_elements.update(deps)
if "operating-system" not in provided: if "operating-system" not in provided:
sys.stderr.write( logger.error(
"ERROR: Please include an operating system element.\n") "Please include an operating system element.")
sys.exit(-1) sys.exit(-1)
conflicts = set(user_elements) & provided conflicts = set(user_elements) & provided
if conflicts: if conflicts:
sys.stderr.write("ERROR: Following elements were explicitly required " logger.error("Following elements were explicitly required "
"but are provided by other included elements: %s\n" % "but are provided by other included elements: %s" %
", ".join(conflicts)) ", ".join(conflicts))
sys.exit(-1) sys.exit(-1)
return final_elements - provided return final_elements - provided
def main(argv): def main(argv):
diskimage_builder.logging_config.setup()
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('elements', nargs='+', parser.add_argument('elements', nargs='+',
help='display dependencies of the given elements') help='display dependencies of the given elements')
@ -125,8 +131,8 @@ def main(argv):
args = parser.parse_args(argv[1:]) args = parser.parse_args(argv[1:])
if args.expand_dependencies: if args.expand_dependencies:
print("WARNING: expand-dependencies flag is deprecated, " logger.warning("expand-dependencies flag is deprecated, "
"and is now on by default.", file=sys.stderr) "and is now on by default.", file=sys.stderr)
print(' '.join(expand_dependencies(args.elements))) print(' '.join(expand_dependencies(args.elements)))
return 0 return 0

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import logging
import os import os
import fixtures import fixtures
@ -39,6 +40,8 @@ class TestElementDeps(testtools.TestCase):
def setUp(self): def setUp(self):
super(TestElementDeps, self).setUp() super(TestElementDeps, self).setUp()
self.element_dir = self.useFixture(fixtures.TempDir()).path self.element_dir = self.useFixture(fixtures.TempDir()).path
self.log_fixture = self.useFixture(
fixtures.FakeLogger(level=logging.DEBUG))
_populate_element(self.element_dir, 'requires-foo', ['foo']) _populate_element(self.element_dir, 'requires-foo', ['foo'])
_populate_element(self.element_dir, _populate_element(self.element_dir,
'foo', 'foo',
@ -81,6 +84,8 @@ class TestElementDeps(testtools.TestCase):
self.assertRaises(SystemExit, self.assertRaises(SystemExit,
element_dependencies.expand_dependencies, ['fake'], element_dependencies.expand_dependencies, ['fake'],
self.element_dir) self.element_dir)
self.assertIn("Element 'fake' not found",
self.log_fixture.output)
def test_transitive_deps(self): def test_transitive_deps(self):
result = element_dependencies.expand_dependencies( result = element_dependencies.expand_dependencies(
@ -128,12 +133,16 @@ class TestElementDeps(testtools.TestCase):
element_dependencies.expand_dependencies, element_dependencies.expand_dependencies,
['provides_virtual'], ['provides_virtual'],
elements_dir=self.element_dir) elements_dir=self.element_dir)
self.assertIn("Please include an operating system element",
self.log_fixture.output)
def test_duplicated_os_passed_as_element(self): def test_duplicated_os_passed_as_element(self):
self.assertRaises(SystemExit, self.assertRaises(SystemExit,
element_dependencies.expand_dependencies, element_dependencies.expand_dependencies,
['circular1', 'operating-system'], ['circular1', 'operating-system'],
elements_dir=self.element_dir) elements_dir=self.element_dir)
self.assertIn("provided by other included elements: operating-system",
self.log_fixture.output)
class TestElements(testtools.TestCase): class TestElements(testtools.TestCase):