def get_package_name(path): """ Get the name of the ROS package that contains *path*. This is determined by finding the nearest parent ``manifest.xml`` file. This routine may not traverse package setups that rely on internal symlinks within the package itself. :param path: filesystem path :return: Package name or ``None`` if package cannot be found, ``str`` """ #NOTE: the realpath is going to create issues with symlinks, most #likely. parent = os.path.dirname(os.path.realpath(path)) #walk up until we hit ros root or ros/pkg while not os.path.exists(os.path.join(path, MANIFEST_FILE)) and not os.path.exists(os.path.join(path, PACKAGE_FILE)) and parent != path: path = parent parent = os.path.dirname(path) # check termination condition if os.path.exists(os.path.join(path, MANIFEST_FILE)): return os.path.basename(os.path.abspath(path)) elif os.path.exists(os.path.join(path, PACKAGE_FILE)): root = ElementTree(None, os.path.join(path, PACKAGE_FILE)) return root.findtext('name') else: return None
def list_by_path(manifest_name, path, cache): """ List ROS stacks or packages within the specified path. The cache will be updated with the resource->path mappings. list_by_path() does NOT returned cached results -- it only updates the cache. :param manifest_name: MANIFEST_FILE or STACK_FILE, ``str`` :param path: path to list resources in, ``str`` :param cache: path cache to update. Maps resource name to directory path, ``{str: str}`` :returns: complete list of resources in ROS environment, ``[str]`` """ resources = [] path = os.path.abspath(path) basename = os.path.basename for d, dirs, files in os.walk(path, topdown=True, followlinks=True): if 'CATKIN_IGNORE' in files: del dirs[:] continue # leaf if PACKAGE_FILE in files: # parse package.xml and decide if it matches the search criteria root = ElementTree(None, os.path.join(d, PACKAGE_FILE)) is_metapackage = root.find('./export/metapackage') is not None if ( (manifest_name == STACK_FILE and is_metapackage) or (manifest_name == MANIFEST_FILE and not is_metapackage) or manifest_name == PACKAGE_FILE ): resource_name = root.findtext('name').strip(' \n\r\t') if resource_name not in resources: resources.append(resource_name) if cache is not None: cache[resource_name] = d del dirs[:] continue # leaf if manifest_name in files: resource_name = basename(d) if resource_name not in resources: resources.append(resource_name) if cache is not None: cache[resource_name] = d del dirs[:] continue # leaf elif MANIFEST_FILE in files or PACKAGE_FILE in files: # noop if manifest_name==MANIFEST_FILE, but a good # optimization for stacks. del dirs[:] continue # leaf elif 'rospack_nosubdirs' in files: del dirs[:] continue # leaf # remove hidden dirs (esp. .svn/.git) [dirs.remove(di) for di in dirs if di[0] == '.'] return resources
def list_by_path(manifest_name, path, cache): """ List ROS stacks or packages within the specified path. The cache will be updated with the resource->path mappings. list_by_path() does NOT returned cached results -- it only updates the cache. :param manifest_name: MANIFEST_FILE or STACK_FILE, ``str`` :param path: path to list resources in, ``str`` :param cache: path cache to update. Maps resource name to directory path, ``{str: str}`` :returns: complete list of resources in ROS environment, ``[str]`` """ resources = [] path = os.path.abspath(path) basename = os.path.basename for d, dirs, files in os.walk(path, topdown=True, followlinks=True): if 'CATKIN_IGNORE' in files: del dirs[:] continue # leaf if PACKAGE_FILE in files: # parse package.xml and decide if it matches the search criteria root = ElementTree(None, os.path.join(d, PACKAGE_FILE)) is_metapackage = root.find('./export/metapackage') is not None if ((manifest_name == STACK_FILE and is_metapackage) or (manifest_name == MANIFEST_FILE and not is_metapackage) or manifest_name == PACKAGE_FILE): resource_name = root.findtext('name').strip(' \n\r\t') if resource_name not in resources: resources.append(resource_name) if cache is not None: cache[resource_name] = d del dirs[:] continue # leaf if manifest_name in files: resource_name = basename(d) if resource_name not in resources: resources.append(resource_name) if cache is not None: cache[resource_name] = d del dirs[:] continue # leaf elif MANIFEST_FILE in files or PACKAGE_FILE in files: # noop if manifest_name==MANIFEST_FILE, but a good # optimization for stacks. del dirs[:] continue # leaf elif 'rospack_nosubdirs' in files: del dirs[:] continue # leaf # remove hidden dirs (esp. .svn/.git) [dirs.remove(di) for di in dirs if di[0] == '.'] return resources