Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidPackage has msg and package_path #1

Merged
merged 2 commits into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/catkin_pkg/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def get_build_type(self):
return 'catkin'
if len(build_type_exports) == 1:
return build_type_exports[0]
raise InvalidPackage('Only one <build_type> element is permitted.')
raise InvalidPackage('Only one <build_type> element is permitted.', self.filename)

def has_invalid_metapackage_dependencies(self):
"""
Expand Down Expand Up @@ -209,13 +209,6 @@ def validate(self, warnings=None):
:param warnings: Print warnings if None or return them in the given list
:raises InvalidPackage: in case validation fails
"""
try:
self._validate(warnings)
except InvalidPackage as e:
e.args = ('Invalid package manifest "{0}": {1}'.format(self.filename, e),)
raise

def _validate(self, warnings):
errors = []
new_warnings = []

Expand Down Expand Up @@ -256,7 +249,7 @@ def _validate(self, warnings):
try:
maintainer.validate()
except InvalidPackage as e:
errors.append(str(e))
errors.append(e.msg)
if not maintainer.email:
errors.append('Maintainers must have an email address')

Expand All @@ -270,7 +263,7 @@ def _validate(self, warnings):
try:
author.validate()
except InvalidPackage as e:
errors.append(str(e))
errors.append(e.msg)

dep_types = {
'build': self.build_depends,
Expand Down Expand Up @@ -309,7 +302,7 @@ def _validate(self, warnings):
warnings.append(warning)

if errors:
raise InvalidPackage('\n'.join(errors))
raise InvalidPackage('\n'.join(errors), self.filename)


class Dependency(object):
Expand Down Expand Up @@ -421,7 +414,14 @@ def parse_package_for_distutils(path=None):


class InvalidPackage(Exception):
pass
def __init__(self, msg, package_path=None):
self.msg = msg
self.package_path = package_path
Exception.__init__(self, self.msg)

def __str__(self):
result = '' if not self.package_path else 'Error(s) in package \'%s\':\n' % self.package_path
return result + Exception.__str__(self)


def package_exists_at(path):
Expand Down Expand Up @@ -503,22 +503,23 @@ def parse_package_string(data, filename=None, warnings=None):
try:
return _parse_package_string(data, filename=filename, warnings=warnings)
except InvalidPackage as e:
e.args = ('Invalid package manifest "{0}": {1}'.format(filename, e),)
if not e.package_path:
e.package_path = filename
raise


def _parse_package_string(data, filename=None, warnings=None):
try:
root = dom.parseString(data)
except Exception as ex:
raise InvalidPackage('The manifest contains invalid XML:\n%s' % ex)
raise InvalidPackage('The manifest contains invalid XML:\n%s' % ex, filename)

pkg = Package(filename)

# verify unique root node
nodes = _get_nodes(root, 'package')
if len(nodes) != 1:
raise InvalidPackage('The manifest must contain a single "package" root tag')
raise InvalidPackage('The manifest must contain a single "package" root tag', filename)
root = nodes[0]

# format attribute
Expand Down Expand Up @@ -671,7 +672,7 @@ def _parse_package_string(data, filename=None, warnings=None):
errors.append('The "%s" tag must not contain the following children: %s' % (node.tagName, ', '.join([n.tagName for n in subnodes])))

if errors:
raise InvalidPackage('Error(s):%s' % (''.join(['\n- %s' % e for e in errors])))
raise InvalidPackage('Error(s):%s' % (''.join(['\n- %s' % e for e in errors])), filename)

pkg.validate(warnings=warnings)

Expand Down
2 changes: 1 addition & 1 deletion src/catkin_pkg/python_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def generate_distutils_setup(package_xml_path=os.path.curdir, **kwargs):
for k, v in kwargs.items():
if k in data:
if v != data[k]:
raise InvalidPackage('The keyword argument "%s" does not match the information from package.xml: "%s" != "%s"' % (k, v, data[k]))
raise InvalidPackage('The keyword argument "%s" does not match the information from package.xml: "%s" != "%s"' % (k, v, data[k]), package_xml_path)
else:
data[k] = v

Expand Down
12 changes: 12 additions & 0 deletions test/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ def test_validate_package(self):
self.assertRaises(InvalidPackage, Package.validate, pack)
dep_type.remove(depend)

def test_invalid_package_exception(self):
try:
raise InvalidPackage('foo')
except InvalidPackage as e:
self.assertEqual('foo', str(e))
self.assertEqual(None, e.package_path)
try:
raise InvalidPackage('foo', package_path='\\bar')
except InvalidPackage as e:
self.assertEqual('Error(s) in package \'\\bar\':\nfoo', str(e))
self.assertEqual('\\bar', e.package_path)

def test_validate_person(self):
auth1 = Person('foo')
auth1.email = '[email protected]'
Expand Down