-
Notifications
You must be signed in to change notification settings - Fork 18
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
add support for package format 3 #63
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# 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. | ||
|
||
import pyparsing | ||
|
||
|
||
def evaluate_condition(condition, context): | ||
if condition is None: | ||
return True | ||
expr = _get_condition_expression() | ||
try: | ||
parse_results = expr.parseString(condition, parseAll=True) | ||
except pyparsing.ParseException as e: | ||
raise ValueError( | ||
"condition '%s' failed to parse: %s" % (condition, e)) | ||
return _evaluate(parse_results.asList()[0], context) | ||
|
||
|
||
_condition_expression = None | ||
|
||
|
||
def _get_condition_expression(): | ||
global _condition_expression | ||
if not _condition_expression: | ||
pp = pyparsing | ||
operator = pp.Regex('==|!=').setName('operator') | ||
identifier = pp.Word('$', pp.alphanums + '_', min=2) | ||
value = pp.Word(pp.alphanums + '_-') | ||
comparison_term = identifier | value | ||
condition = pp.Group(comparison_term + operator + comparison_term) | ||
_condition_expression = pp.operatorPrecedence( | ||
condition, [ | ||
('and', 2, pp.opAssoc.LEFT, ), | ||
('or', 2, pp.opAssoc.LEFT, ), | ||
]) | ||
return _condition_expression | ||
|
||
|
||
def _evaluate(parse_results, context): | ||
if not isinstance(parse_results, list): | ||
if parse_results.startswith('$'): | ||
# get variable from context | ||
return str(context.get(parse_results[1:], '')) | ||
# return literal value | ||
return parse_results | ||
|
||
# recursion | ||
assert len(parse_results) == 3 | ||
|
||
# handle logical operators | ||
if parse_results[1] == 'and': | ||
return _evaluate(parse_results[0], context) and \ | ||
_evaluate(parse_results[2], context) | ||
if parse_results[1] == 'or': | ||
return _evaluate(parse_results[0], context) or \ | ||
_evaluate(parse_results[2], context) | ||
|
||
# handle comparison operators | ||
assert parse_results[1] in ('==', '!=') | ||
if parse_results[1] == '==': | ||
return _evaluate(parse_results[0], context) == \ | ||
_evaluate(parse_results[2], context) | ||
if parse_results[1] == '!=': | ||
return _evaluate(parse_results[0], context) != \ | ||
_evaluate(parse_results[2], context) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2017 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. | ||
|
||
from ament_package.condition import evaluate_condition | ||
|
||
|
||
class GroupDependency: | ||
__slots__ = [ | ||
'name', | ||
'condition', | ||
'evaluated_condition', | ||
'members', | ||
] | ||
|
||
def __init__(self, name, condition=None, members=None): | ||
self.name = name | ||
self.condition = condition | ||
self.members = members | ||
self.evaluated_condition = None | ||
|
||
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 evaluate_condition(self, context): | ||
""" | ||
Evaluate the condition. | ||
|
||
The result is also stored in the member variable `evaluated_condition`. | ||
|
||
:param context: A dictionary with key value pairs to replace variables | ||
starting with $ in the condition. | ||
|
||
:returns: True if the condition evaluates to True, else False | ||
:raises: :exc:`ValueError` if the condition fails to parse | ||
""" | ||
self.evaluated_condition = evaluate_condition(self.condition, context) | ||
return self.evaluated_condition | ||
|
||
def extract_group_members(self, packages): | ||
self.members = set() | ||
for pkg in packages: | ||
if self.name in [g.name for g in pkg.member_of_groups]: | ||
self.members.add(pkg.name) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2017 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. | ||
|
||
from ament_package.condition import evaluate_condition | ||
|
||
|
||
class GroupMembership: | ||
__slots__ = [ | ||
'name', | ||
'condition', | ||
'evaluated_condition', | ||
] | ||
|
||
def __init__(self, name, condition=None): | ||
self.name = name | ||
self.condition = condition | ||
self.evaluated_condition = None | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, GroupMembership): | ||
return False | ||
return all(getattr(self, attr) == getattr(other, attr) | ||
for attr in self.__slots__) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
def evaluate_condition(self, context): | ||
""" | ||
Evaluate the condition. | ||
|
||
The result is also stored in the member variable `evaluated_condition`. | ||
|
||
:param context: A dictionary with key value pairs to replace variables | ||
starting with $ in the condition. | ||
|
||
:returns: True if the condition evaluates to True, else False | ||
:raises: :exc:`ValueError` if the condition fails to parse | ||
""" | ||
self.evaluated_condition = evaluate_condition(self.condition, context) | ||
return self.evaluated_condition |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ class Package: | |
'package_format', | ||
'name', | ||
'version', | ||
'version_compatibility', | ||
'description', | ||
'maintainers', | ||
'licenses', | ||
|
@@ -39,6 +40,8 @@ class Package: | |
'doc_depends', | ||
'conflicts', | ||
'replaces', | ||
'group_depends', | ||
'member_of_groups', | ||
'exports', | ||
'filename', | ||
] | ||
|
@@ -98,6 +101,32 @@ def get_build_type(self): | |
return build_type_exports[0] | ||
raise InvalidPackage('Only one <build_type> element is permitted.') | ||
|
||
def evaluate_conditions(self, context): | ||
""" | ||
Evaluate the conditions of all dependencies and memberships. | ||
|
||
:param context: A dictionary with key value pairs to replace variables | ||
starting with $ in the condition. | ||
|
||
:raises: :exc:`ValueError` if any condition fails to parse | ||
""" | ||
for attr in ( | ||
'build_depends', | ||
'buildtool_depends', | ||
'build_export_depends', | ||
'buildtool_export_depends', | ||
'exec_depends', | ||
'test_depends', | ||
'doc_depends', | ||
'conflicts', | ||
'replaces', | ||
'group_depends', | ||
'member_of_groups', | ||
): | ||
conditionals = getattr(self, attr) | ||
for conditional in conditionals: | ||
conditional.evaluate_condition(context) | ||
|
||
def validate(self): | ||
""" | ||
Ensure that all standards for packages are met. | ||
|
@@ -124,11 +153,17 @@ def validate(self): | |
errors.append("Package name '%s' does not follow naming " | ||
'conventions' % self.name) | ||
|
||
version_regexp = '^[0-9]+\.[0-9_]+\.[0-9_]+$' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be exposed as a constant for sharing with consuming code? So it doesn't necessarily need to be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was internal before this patch so I won't consider it part of this PR in order to keep the diff as small as possible. |
||
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') | ||
|
@@ -170,5 +205,13 @@ 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} & | ||
{g.name for g in 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have some kind of test for this function (
parse_package_string()
), and have it updated for the new features inpackage.xml
version 3. I guess those didn't make it over fromcatkin_pkg
.