Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 import os
00012 
00013 
00014 import rospkg
00015 import roslib.names
00016 
00017 from .catkin import package_index_from_package_path
00018 
00019 
00020 
00021 
00022 
00023 
00024 def find_resource_from_string(resource, rospack=None, extension=None):
00025     '''
00026       Convenience wrapper around roslib to find a resource (file) inside
00027       a package. This function passes off the work to find_resource
00028       once the input string is split.
00029 
00030       @param package : ros package
00031       @param resource : string resource identifier of the form package/filename
00032 
00033       @param extension : file name extension to look for/expect
00034       @type string
00035 
00036       @return full pathname to the resource
00037       @rtype str
00038 
00039       @raise rospkg.ResourceNotFound : raised if the resource is not found or has an inappropriate extension.
00040     '''
00041     if extension is not None:
00042         filename_extension = os.path.splitext(resource)[-1]
00043         if filename_extension == '':  
00044             resource += ".%s" % extension
00045         elif filename_extension != "." + extension and filename_extension != extension:
00046             raise rospkg.ResourceNotFound("resource with invalid filename extension specified [%s][%s]" % (resource, extension))
00047     package, filename = roslib.names.package_resource_name(resource)
00048     if not package:
00049         raise rospkg.ResourceNotFound("resource could not be split with a valid leading package name [%s]" % (resource))
00050     return find_resource(package, filename, rospack)
00051 
00052 
00053 def find_resource(package, filename, rospack=None):
00054     '''
00055       Convenience wrapper around roslib to find a resource (file) inside
00056       a package. It checks the output, and provides the appropriate
00057       error if there is one.
00058 
00059       @param package : ros package
00060       @param filename : some file inside the specified package
00061       @return str : absolute path to the file
00062 
00063       @raise rospkg.ResourceNotFound : raised if there is nothing found or multiple objects found.
00064     '''
00065     try:
00066         resolved = roslib.packages.find_resource(package, filename, rospack=rospack)
00067         if not resolved:
00068             raise rospkg.ResourceNotFound("cannot locate [%s] in package [%s]" % (filename, package))
00069         elif len(resolved) == 1:
00070             return resolved[0]
00071         elif len(resolved) > 1:
00072             raise rospkg.ResourceNotFound("multiple resources named [%s] in package [%s]:%s\nPlease specify full path instead" % (filename, package, ''.join(['\n- %s' % r for r in resolved])))
00073     except rospkg.ResourceNotFound:
00074         raise rospkg.ResourceNotFound("[%s] is not a package or launch file name [%s]" % (package, package + '/' + filename))
00075     return None
00076 
00077 
00078 def resource_index_from_package_exports(export_tag, package_paths=None, package_whitelist=None, package_blacklist=[]):
00079     '''
00080       Scans the package path looking for exports and grab the ones we are interested in.
00081 
00082       @param export_tag : export tagname
00083       @type str
00084 
00085       @return the dictionary of resource and its absolute path
00086       @type { resource_name : os.path }
00087     '''
00088     package_index = _get_package_index(package_paths)
00089     resources = {}
00090     invalid_resources = {}
00091     for package in package_index.values():
00092 
00093         if package_whitelist:
00094             if package.name not in package_whitelist:
00095                 continue
00096         elif package.name in package_blacklist:
00097             continue
00098         for export in package.exports:
00099             if export.tagname == export_tag:
00100                 filename_relative_path = export.content
00101                 resource_name = package.name + '/' + os.path.splitext(os.path.basename(filename_relative_path))[0]
00102                 resource_filename = os.path.join(os.path.dirname(package.filename), filename_relative_path)
00103                 if not os.path.isfile(resource_filename):
00104                     invalid_resources[resource_name] = resource_filename
00105                 else:
00106                     resources[resource_name] = (resource_filename, package)
00107     return (resources, invalid_resources)
00108 
00109 
00110 def _get_package_index(package_paths):
00111     
00112     
00113     ros_package_path = package_paths if package_paths else os.getenv('ROS_PACKAGE_PATH', '')
00114     ros_package_path = [x for x in ros_package_path.split(':') if x]
00115     package_index = package_index_from_package_path(ros_package_path)
00116     return package_index