|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +############################################################################## |
| 4 | +# Imports |
| 5 | +############################################################################## |
| 6 | + |
| 7 | +import os |
| 8 | +import catkin_pkg.packages |
| 9 | +import catkin_pkg.topological_order |
| 10 | + |
| 11 | +############################################################################## |
| 12 | +# Methods |
| 13 | +############################################################################## |
| 14 | + |
| 15 | + |
| 16 | +def has_build_depend_on_message_generation(package): |
| 17 | + ''' |
| 18 | + Checks for a build dependency on message generation to determine if |
| 19 | + that package contains msgs/srvs. |
| 20 | +
|
| 21 | + @param package : typical catkin package object |
| 22 | + @type catkin_pkg.Package |
| 23 | +
|
| 24 | + @return True if it is a package that contains msgs/srvs |
| 25 | + @rtype Bool |
| 26 | + ''' |
| 27 | + return 'message_generation' in [d.name for d in package.build_depends] |
| 28 | + |
| 29 | + |
| 30 | +def index_message_package_dependencies_from_local_environment(package_name=None, package_paths=None): |
| 31 | + ''' |
| 32 | + Returns a topologically sorted list of message packages that can |
| 33 | + be used for sequencing builds of packages. |
| 34 | +
|
| 35 | + @param package_name : sort dependencies for this package only (defaults to all packages if None is given) |
| 36 | + @param package_paths : a python list of ros workspaces (defaults to ROS_PACKAGE_PATH if None is given) |
| 37 | + @return dict mapping relative path to a catkin_pkg.Package |
| 38 | + ''' |
| 39 | + if package_paths is None: |
| 40 | + package_paths = os.getenv('ROS_PACKAGE_PATH', '') |
| 41 | + package_paths = [x for x in package_paths.split(':') if x] |
| 42 | + all_packages = {} # mapping package name to (path, catkin_pkg.Package) tuple |
| 43 | + message_packages = {} |
| 44 | + # use reversed to write over any packages lower down in the overlay heirarchy |
| 45 | + # i.e. no duplicates! |
| 46 | + for path in reversed(package_paths): |
| 47 | + for package_path, package in catkin_pkg.packages.find_packages(path).items(): |
| 48 | + all_packages[package.name] = (package_path, package) |
| 49 | + if has_build_depend_on_message_generation(package): |
| 50 | + if package_name is not None: |
| 51 | + if package_name == package.name: |
| 52 | + message_packages[package.name] = (package_path, package) |
| 53 | + else: |
| 54 | + message_packages[package.name] = (package_path, package) |
| 55 | + # put into the correct form for sorting |
| 56 | + # The following returns: A list of tuples containing the relative path and a ``Package`` object, |
| 57 | + sorted_package_tuples = catkin_pkg.topological_order.topological_order_packages( |
| 58 | + packages=dict(message_packages.values()), |
| 59 | + whitelisted=None, |
| 60 | + blacklisted=None, |
| 61 | + underlay_packages=dict(all_packages.values())) |
| 62 | + # print("%s" % [p.name for (unused_relative_path, p) in sorted_package_tuples]) |
| 63 | + return sorted_package_tuples |
0 commit comments