Skip to content

Commit

Permalink
add support for package format 3
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Nov 3, 2017
1 parent 44d3e89 commit 4f4697b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
24 changes: 22 additions & 2 deletions ament_package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def parse_package_string(data, *, filename=None):

from .exceptions import InvalidPackage
from .export import Export
from .group_dependency import GroupDependency
from .package import Package
from .person import Person
from .url import Url
Expand Down Expand Up @@ -123,7 +124,7 @@ def parse_package_string(data, *, filename=None):
"Unable to handle '%s' format version '%d', please update the " \
'manifest file to at least format version 2' % \
(filename, pkg.package_format)
assert pkg.package_format in [2], \
assert pkg.package_format in [2, 3], \
"Unable to handle '%s' format version '%d', please update " \
"'ament_package' (e.g. on Ubuntu/Debian use: sudo apt-get update && " \
'sudo apt-get install --only-upgrade python-ament-package)' % \
Expand All @@ -135,6 +136,8 @@ def parse_package_string(data, *, filename=None):
# version
version_node = _get_node(root, 'version')
pkg.version = _get_node_value(version_node)
pkg.version_compatibility = _get_node_attr(
version_node, 'compatibility', default=None)

# description
pkg.description = _get_node_value(
Expand Down Expand Up @@ -209,6 +212,11 @@ def parse_package_string(data, *, filename=None):
pkg.conflicts = _get_dependencies(root, 'conflict')
pkg.replaces = _get_dependencies(root, 'replace')

# group dependencies and membership
pkg.group_depends = [
GroupDependency(g) for g in _get_groups(root, 'group_depend')]
pkg.member_of_groups = _get_groups(root, 'member_of_group')

# exports
export_node = _get_optional_node(root, 'export')
if export_node is not None:
Expand Down Expand Up @@ -242,7 +250,7 @@ def parse_package_string(data, *, filename=None):
]
known = {
'name': [],
'version': [],
'version': ['compatibility'],
'description': [],
'maintainer': ['email'],
'license': [],
Expand All @@ -260,6 +268,11 @@ def parse_package_string(data, *, filename=None):
'replace': depend_attributes,
'export': [],
}
if pkg.package_format > 2:
known.update({
'group_depend': [],
'member_of_group': [],
})
nodes = [n for n in root.childNodes if n.nodeType == n.ELEMENT_NODE]
unknown_tags = {n.tagName for n in nodes if n.tagName not in known.keys()}
if unknown_tags:
Expand Down Expand Up @@ -359,3 +372,10 @@ def _get_dependencies(parent, tagname):
setattr(depend, attr, _get_node_attr(node, attr, default=None))
depends.append(depend)
return depends


def _get_groups(parent, tagname):
groups = []
for node in _get_nodes(parent, tagname):
groups.append(_get_node_value(node))
return groups
39 changes: 39 additions & 0 deletions ament_package/group_dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2014 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


class GroupDependency:
__slots__ = [
'name',
'members',
]

def __init__(self, name, members=None):
self.name = name
self.members = members

def __eq__(self, other):
if not isinstance(other, GroupDependency):
return False
return all(getattr(self, attr) == getattr(other, attr)
for attr in self.__slots__)

def __str__(self):
return self.name

def extract_group_members(self, packages):
self.members = set()
for pkg in packages:
if self.name in pkg.member_of_groups:
self.members.add(pkg.name)
16 changes: 15 additions & 1 deletion ament_package/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Package:
'package_format',
'name',
'version',
'version_compatibility',
'description',
'maintainers',
'licenses',
Expand All @@ -39,6 +40,8 @@ class Package:
'doc_depends',
'conflicts',
'replaces',
'group_depends',
'member_of_groups',
'exports',
'filename',
]
Expand Down Expand Up @@ -124,11 +127,17 @@ def validate(self):
errors.append("Package name '%s' does not follow naming "
'conventions' % self.name)

version_regexp = '^[0-9]+\.[0-9_]+\.[0-9_]+$'
if not self.version:
errors.append('Package version must not be empty')
elif not re.match('^[0-9]+\.[0-9_]+\.[0-9_]+$', self.version):
elif not re.match(version_regexp, self.version):
errors.append("Package version '%s' does not follow version "
'conventions' % self.version)
if self.version_compatibility:
if not re.match(version_regexp, self.version_compatibility):
errors.append(
"Package compatibility version '%s' does not follow "
'version conventions' % self.version_compatibility)

if not self.description:
errors.append('Package description must not be empty')
Expand Down Expand Up @@ -170,5 +179,10 @@ def validate(self):
"The package must not '%s_depend' on a package with "
'the same name as this package' % dep_type)

if {d.name for d in self.group_depends} & set(self.member_of_groups):
errors.append(
"The package must not 'group_depend' on a package which it "
'also declares to be a member of')

if errors:
raise InvalidPackage('\n'.join(errors))
26 changes: 23 additions & 3 deletions test/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ def get_maintainer(self):
maint.name = 'John Doe'
return maint

def get_group_dependency(self, name):
group = Mock()
group.name = name
return group

def test_init(self):
maint = self.get_maintainer()
pack = Package(name='foo',
version='0.0.0',
maintainers=[maint],
licenses=['BSD'])
self.assertEqual(None, pack.filename)
self.assertEqual('0.0.0', pack.version)
self.assertEqual(None, pack.version_compatibility)
self.assertEqual([], pack.urls)
self.assertEqual([], pack.authors)
self.assertEqual([maint], pack.maintainers)
Expand All @@ -50,6 +57,8 @@ def test_init(self):
self.assertEqual([], pack.conflicts)
self.assertEqual([], pack.replaces)
self.assertEqual([], pack.exports)
self.assertEqual([], pack.group_depends)
self.assertEqual([], pack.member_of_groups)
pack = Package(filename='foo',
name='bar',
version='0.0.0',
Expand Down Expand Up @@ -78,14 +87,16 @@ def test_init_kwargs_string(self):
pack = Package(filename='foo',
name='bar',
package_format='1',
version='0.0.0',
version='1.0.1',
version_compatibility='1.0.0',
description='pdesc',
licenses=['BSD'],
maintainers=[self.get_maintainer()])
self.assertEqual('foo', pack.filename)
self.assertEqual('bar', pack.name)
self.assertEqual('1', pack.package_format)
self.assertEqual('0.0.0', pack.version)
self.assertEqual('1.0.1', pack.version)
self.assertEqual('1.0.0', pack.version_compatibility)
self.assertEqual('pdesc', pack.description)

def test_init_kwargs_object(self):
Expand All @@ -99,7 +110,12 @@ def test_init_kwargs_object(self):
mconf = [Mock(), Mock()]
mrepl = [Mock(), Mock()]
mexp = [Mock(), Mock()]
pack = Package(name='bar',
mgroup = [
self.get_group_dependency('group1'),
self.get_group_dependency('group2')]
mmember = ['member1', 'member2']
pack = Package(package_format='3',
name='bar',
version='0.0.0',
maintainers=mmain,
licenses=mlis,
Expand All @@ -110,6 +126,8 @@ def test_init_kwargs_object(self):
test_depends=mtestdep,
conflicts=mconf,
replaces=mrepl,
group_depends=mgroup,
member_of_groups=mmember,
exports=mexp)
self.assertEqual(mmain, pack.maintainers)
self.assertEqual(mlis, pack.licenses)
Expand All @@ -121,6 +139,8 @@ def test_init_kwargs_object(self):
self.assertEqual(mconf, pack.conflicts)
self.assertEqual(mrepl, pack.replaces)
self.assertEqual(mexp, pack.exports)
self.assertEqual(mgroup, pack.group_depends)
self.assertEqual(mmember, pack.member_of_groups)

def test_validate_package(self):
maint = self.get_maintainer()
Expand Down

0 comments on commit 4f4697b

Please sign in to comment.