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

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
import os
import fixtures
@ -39,6 +40,8 @@ class TestElementDeps(testtools.TestCase):
def setUp(self):
super(TestElementDeps, self).setUp()
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,
'foo',
@ -81,6 +84,8 @@ class TestElementDeps(testtools.TestCase):
self.assertRaises(SystemExit,
element_dependencies.expand_dependencies, ['fake'],
self.element_dir)
self.assertIn("Element 'fake' not found",
self.log_fixture.output)
def test_transitive_deps(self):
result = element_dependencies.expand_dependencies(
@ -128,12 +133,16 @@ class TestElementDeps(testtools.TestCase):
element_dependencies.expand_dependencies,
['provides_virtual'],
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):
self.assertRaises(SystemExit,
element_dependencies.expand_dependencies,
['circular1', 'operating-system'],
elements_dir=self.element_dir)
self.assertIn("provided by other included elements: operating-system",
self.log_fixture.output)
class TestElements(testtools.TestCase):