def test_get_packages_with_prefixes():
    set_ament_prefix_path(['prefix1', 'prefix2'])

    packages = get_packages_with_prefixes()
    assert 'foo' in packages, "Expected to find 'foo'"
    assert PurePath(packages['foo']).name == 'prefix1', "Expected to find 'foo' in 'prefix1'"
    assert 'bar' in packages, "Expected to find 'bar'"
    assert PurePath(packages['bar']).name == 'prefix1', "Expected to find 'bar' in 'prefix1'"
    assert 'baz' in packages, "Expected to find 'baz'"
    assert PurePath(packages['baz']).name == 'prefix2', "Expected to find 'baz' in 'prefix2'"
    os.environ['AMENT_PREFIX_PATH'] = '/path/does/not/exist'

    assert not get_packages_with_prefixes(), 'Expected to find no packages'
Example #2
0
 def _is_ros_package(self, pkg):
     if ROS1_AVAILABLE:
         if pkg in rospkg.RosPack().list():
             return True
     if ROS2_AVAILABLE:
         if pkg in \
                 ament_index_python.get_packages_with_prefixes().keys():
             return True
     return False
Example #3
0
    def _resolve_ros_package(self):
        pkgs = list()
        if ROS1_AVAILABLE:
            pkgs = pkgs + list(rospkg.RosPack().list())
        elif ROS2_AVAILABLE:
            pkgs = pkgs + list(
                ament_index_python.get_packages_with_prefixes().keys())

        for ros_pkg in pkgs:
            pkg_path = self._find_ros_package_resources_path(ros_pkg)
            if pkg_path in os.path.dirname(self.absolute_uri):
                self._ros_pkg = ros_pkg
                return
Example #4
0
 def _get_ros_package_name(self, filename):
     if ROS1_AVAILABLE:
         finder = rospkg.RosPack()
         if os.path.isfile(filename):
             for pkg_name in finder.list():
                 if finder.get_path(pkg_name) in filename:
                     return pkg_name
     else:
         pkgs = ament_index_python.get_packages_with_prefixes()
         if os.path.isfile(filename):
             for pkg_name in pkgs.keys():
                 if pkgs[pkg_name] in filename:
                     return pkg_name
     return None
def get_local_package_versions() -> dict:
    """
    Return local package name and versions.

    :return: dictionary of local package name and version
    """
    local_packages = {}
    package_name_prefixes = get_packages_with_prefixes()
    if package_name_prefixes:
        for name, prefix in package_name_prefixes.items():
            file_path = os.path.join(prefix, 'share', name)
            package_obj = parse_package(file_path)
            local_packages[
                name] = package_obj.version if package_obj.version else ''
    return local_packages
def load_gazebo_models():
    """Search for Gazebo models in the local `.gazebo/models` folder
    and in the ROS paths.

    > *Returns*

    `dict`: Information of all Gazebo models found
    """
    import os
    try:
        import rospkg
        ROS1_AVAILABLE = True
    except ImportError:
        ROS1_AVAILABLE = False

    try:
        import ament_index_python
        ROS2_AVAILABLE = True
    except ImportError:
        ROS2_AVAILABLE = False

    global GAZEBO_MODELS
    GAZEBO_MODELS = dict()

    ros_pkgs = list()
    if ROS1_AVAILABLE:
        ros_pkgs = ros_pkgs + list(rospkg.RosPack().list())
    if ROS2_AVAILABLE:
        ros_pkgs = ros_pkgs + list(
            ament_index_python.get_packages_with_prefixes().keys())
    if '/opt/ros/kinetic/share' not in rospkg.RosPack(
    ).ros_paths:  # TEMP TEMP TEMP
        # Load all models from catkin packages
        for ros_pkg in ros_pkgs:
            ros_path = None
            # print("line 110.")
            print(ros_pkg)
            if ROS1_AVAILABLE:
                try:
                    ros_path = rospkg.RosPack().get_path(ros_pkg)
                except rospkg.ResourceNotFound:
                    pass
            if ROS2_AVAILABLE and ros_path is None:
                try:
                    ros_path = \
                        ament_index_python.get_package_share_directory(
                            ros_pkg)
                except ament_index_python.PackageNotFoundError:
                    pass

            if ros_path:
                for folder in os.listdir(ros_path):
                    if not os.path.isdir(os.path.join(ros_path, folder)):
                        continue
                    models = get_gazebo_model_folders(
                        os.path.join(ros_path, folder))
                    # print("line 110.")
                    # print(os.path.join(ros_path, folder))
                    # print(models)
                    for tag in models:
                        models[tag]['ros_pkg'] = ros_pkg
                    GAZEBO_MODELS.update(models)

    # Load all models from ~/.gazebo/models
    home_folder = os.path.expanduser('~')
    gazebo_folder = os.path.join(home_folder, '.gazebo', 'models')
    if os.path.isdir(gazebo_folder):
        GAZEBO_MODELS.update(get_gazebo_model_folders(gazebo_folder))

    gazebo_folder = None
    for folder in os.listdir('/usr/share'):
        if 'gazebo-' in folder:
            gazebo_folder = os.path.join('/usr', 'share', folder, 'models')
            break

    if gazebo_folder is not None:
        if os.path.isdir(gazebo_folder):
            GAZEBO_MODELS.update(get_gazebo_model_folders(gazebo_folder))

    # Parse the GAZEBO_MODEL_PATH, if available
    if 'GAZEBO_MODEL_PATH' in os.environ:
        for folder in os.environ['GAZEBO_MODEL_PATH'].split(':'):
            if os.path.isdir(folder):
                GAZEBO_MODELS.update(get_gazebo_model_folders(folder))

    if len(CUSTOM_GAZEBO_RESOURCE_PATHS) > 0:
        for folder in CUSTOM_GAZEBO_RESOURCE_PATHS:
            GAZEBO_MODELS.update(get_gazebo_model_folders(folder))
    print("line 111.")
    return GAZEBO_MODELS
Example #7
0
def get_package_names():
    return get_packages_with_prefixes().keys()