def load_view(self, view_name, rosdep_db, verbose=False): """ Load view data into *rosdep_db*. If the view has already been loaded into *rosdep_db*, this method does nothing. If view has no rosdep data, it will be initialized with an empty data map. :raises: :exc:`InvalidData` if view rosdep.yaml is invalid :raises: :exc:`rospkg.ResourceNotFound` if view cannot be located :returns: ``True`` if view was loaded. ``False`` if view was already loaded. """ if rosdep_db.is_loaded(view_name): return if view_name not in self.get_loadable_views(): raise rospkg.ResourceNotFound(view_name) elif view_name == 'invalid': raise rospkg.ResourceNotFound('FOUND' + view_name + str(self.get_loadable_views())) if verbose: print('loading view [%s] with rospkg loader' % (view_name)) # chain into underlay if set if self._underlay_key: view_dependencies = [self._underlay_key] else: view_dependencies = [] # no rospkg view has actual data rosdep_db.set_view_data(view_name, {}, view_dependencies, '<nodata>')
def find_resource_from_string(resource, rospack=None, extension=None): ''' Convenience wrapper around roslib to find a resource (file) inside a package. This function passes off the work to find_resource once the input string is split. @param package : ros package @param resource : string resource identifier of the form package/filename @param extension : file name extension to look for/expect @type string @return full pathname to the resource @rtype str @raise rospkg.ResourceNotFound : raised if the resource is not found or has an inappropriate extension. ''' if extension is not None: filename_extension = os.path.splitext(resource)[-1] if filename_extension == '': # no ext given resource += ".%s" % extension elif filename_extension != "." + extension and filename_extension != extension: raise rospkg.ResourceNotFound("resource with invalid filename extension specified [%s][%s]" % (resource, extension)) package, filename = roslib.names.package_resource_name(resource) if not package: raise rospkg.ResourceNotFound("resource could not be split with a valid leading package name [%s]" % (resource)) return find_resource(package, filename, rospack)
def get_rosdeps(self, resource_name, implicit=True): """ Always raises as SourceListLoader defines no concrete resources with rosdeps. :raises: :exc:`rospkg.ResourceNotFound` """ raise rospkg.ResourceNotFound(resource_name)
def package_resource_name(name): """ Split a name into its package and resource name parts, e.g. - 'std_msgs/String -> std_msgs, String' - 'gopher_gazebo/gocart.xacro -> gopher_gazebo, gocart.xacro' - 'gopher_gazebo/urfd/gocart.xacro -> gopher_gazebo, urdf/gocart.xacro' This emulates what the original roslib.names.package_resource_name() function did, but also caters for the third example above where its a full relative path inside the package so as to disambiguate against multiple matches. @param name: package resource name, e.g. 'std_msgs/String' @type name: str @return: package name, resource name @rtype: str @raise rospkg.ResourceNotFound: if name is invalid (cannot split into two arguments) """ if roslib.names.PRN_SEPARATOR in name: val = tuple(name.split(roslib.names.PRN_SEPARATOR, 1)) if len(val) <= 1: raise rospkg.ResourceNotFound("invalid name [%s]" % name) else: return val else: return '', name
def get_rosdeps(self, resource_name, implicit=True): """ If *resource_name* is a stack, returns an empty list. :raises: :exc:`rospkg.ResourceNotFound` if *resource_name* cannot be found. """ if resource_name in self.get_catkin_paths(): pkg = catkin_pkg.package.parse_package(self.get_catkin_paths()[resource_name]) pkg.evaluate_conditions(os.environ) deps = pkg.build_depends + pkg.buildtool_depends + pkg.run_depends + pkg.test_depends + pkg.buildtool_export_depends return [d.name for d in deps if d.evaluated_condition] elif resource_name in self.get_loadable_resources(): rosdeps = set(self._rospack.get_rosdeps(resource_name, implicit=False)) if implicit: # This resource is a manifest.xml, but it might depend on things with a package.xml # Make sure they get a chance to evaluate conditions for dep in self._rospack.get_depends(resource_name): rosdeps = rosdeps.union(set(self.get_rosdeps(dep, implicit=True))) return list(rosdeps) elif resource_name in self._rosstack.list(): # stacks currently do not have rosdeps of their own, implicit or otherwise return [] else: raise rospkg.ResourceNotFound(resource_name)
def get_view_key(self, resource_name): """ Always raises as SourceListLoader defines no concrete resources with rosdeps. :returns: Name of view that *resource_name* is in, ``None`` if no associated view. :raises: :exc:`rospkg.ResourceNotFound` if *resource_name* cannot be found. """ raise rospkg.ResourceNotFound(resource_name)
def find_resource_pair_from_string(resource, rospack=None, extension=None): ''' Convenience wrapper around roslib to find a resource (file) inside a package. This function passes off the work to find_resource once the input string is split. Pass it a shared rospack (:class:`.rospkg.RosPack`) object to accelerate the crawling across the ROS_PACKAGE_PATH when you are calling this function for many resources consecutively. .. code-block:: python rospack = rospkg.RosPack() for ros_resource_name in ['rocon_interactions/pc.interactions', 'rocon_interactions/demo.interactions'] filename = find_resource_from_string(ros_resource_name, rospack) # do something :param str resource: ros resource name (in the form package/filename) :param rospack: a caching utility to help accelerate catkin filesystem lookups :type rospack: :class:`.rospkg.RosPack` :param str extension: file name extension to look for/expect :returns: (package, full pathname) to the resource :rtype: (str, str) :raises: :exc:`.rospkg.ResourceNotFound` raised if the resource is not found or has an inappropriate extension. ''' if extension is not None: filename_extension = os.path.splitext(resource)[-1] if filename_extension == '': # no ext given resource += ".%s" % extension elif filename_extension != "." + extension and filename_extension != extension: raise rospkg.ResourceNotFound( "resource with invalid filename extension specified [%s][%s]" % (resource, extension)) try: package, filename = package_resource_name(resource) except rospkg.ResourceNotFound: raise rospkg.ResourceNotFound( "resource with invalid ros name specified [%s][%s]" % (resource, extension)) if not package: raise rospkg.ResourceNotFound( "resource could not be split with a valid leading package name [%s]" % (resource)) return (package, find_resource(package, filename, rospack))
def get_view_key(self, resource_name): """ Map *resource_name* to a view key. In rospkg, this maps the DEFAULT_VIEW_KEY if *resource_name* exists. :raises: :exc:`rospkg.ResourceNotFound` """ if resource_name in self.get_loadable_resources(): return DEFAULT_VIEW_KEY else: raise rospkg.ResourceNotFound(resource_name)
def _read_service_profiles_from_cache(self): """ Load service profile from cached solution configuration. :returns: file path of loaded service profile :rtype: str :raises: :exc:`rospkg.ResourceNotFound` """ loaded_profile = {} concert_name = self._concert_name service_file_name = self._service_profile_file if not rocon_python_utils.ros.is_validation_file(service_file_name): raise rospkg.ResourceNotFound("can not find service file in cache [%s]" % service_file_name) else: self._profile_files.append([service_file_name, time.ctime(os.path.getmtime(service_file_name))]) with open(service_file_name) as f: loaded_profile = yaml.load(f) if 'parameters' in loaded_profile.keys(): loaded_profile['parameters_detail'] = [] parameters_yaml_file = os.path.join(get_service_profile_cache_home(concert_name, loaded_profile['name']), loaded_profile['parameters']) if not rocon_python_utils.ros.is_validation_file(parameters_yaml_file): raise rospkg.ResourceNotFound("can not find parameters file in cache [%s]" % parameters_yaml_file) self._profile_files.append([parameters_yaml_file, time.ctime(os.path.getmtime(parameters_yaml_file))]) with open(parameters_yaml_file) as f: parameters_yaml = yaml.load(f) loaded_profile['parameters_detail'] = parameters_yaml if 'interactions' in loaded_profile.keys(): loaded_profile['interactions_detail'] = [] interactions_yaml_file = os.path.join(get_service_profile_cache_home(concert_name, loaded_profile['name']), loaded_profile['interactions']) if not rocon_python_utils.ros.is_validation_file(interactions_yaml_file): raise rospkg.ResourceNotFound("can not find interactions file in cache [%s]" % interactions_yaml_file) self._profile_files.append([interactions_yaml_file, time.ctime(os.path.getmtime(interactions_yaml_file))]) with open(interactions_yaml_file) as f: interactions_yaml = yaml.load(f) loaded_profile['interactions_detail'] = interactions_yaml return loaded_profile
def find_resource(package, filename, rospack=None): ''' Convenience wrapper around roslib to find a resource (file) inside a package. It checks the output, and provides the appropriate error if there is one. @param package : ros package @param filename : some file inside the specified package @return str : absolute path to the file @raise rospkg.ResourceNotFound : raised if there is nothing found or multiple objects found. ''' try: resolved = roslib.packages.find_resource(package, filename, rospack=rospack) if not resolved: raise rospkg.ResourceNotFound("cannot locate [%s] in package [%s]" % (filename, package)) elif len(resolved) == 1: return resolved[0] elif len(resolved) > 1: 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]))) except rospkg.ResourceNotFound: raise rospkg.ResourceNotFound("[%s] is not a package or launch file name [%s]" % (package, package + '/' + filename)) return None
def find_resource(package, filename, rospack=None): ''' Convenience wrapper around roslib to find a resource (file) inside a package. It checks the output, and provides the appropriate error if there is one. :param str package: ros package :param str filename: some file inside the specified package :returns: absolute path to the file :rtype: str :raises: :exc:`.rospkg.ResourceNotFound` : raised if there is nothing found or multiple objects found. ''' # try an explicit lookup first by concatenating package and filename paths, it's faster and avoids ambiguities resolved_path = _find_resource_explicitly(package, filename) if resolved_path is not None: return resolved_path # must be a 'convenient' resource path. try: resolved = roslib.packages.find_resource(package, filename, rospack=rospack) if not resolved: raise rospkg.ResourceNotFound( "cannot locate [%s] in package [%s]" % (filename, package)) elif len(resolved) == 1: return resolved[0] elif len(resolved) > 1: 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]))) except rospkg.ResourceNotFound: raise rospkg.ResourceNotFound( "[%s] is not a package or launch file name [%s]" % (package, package + '/' + filename)) return None
def get_rosdeps(self, resource_name, implicit=True): """ If *resource_name* is a stack, returns an empty list. :raises: :exc:`rospkg.ResourceNotFound` if *resource_name* cannot be found. """ if resource_name in self.get_catkin_paths(): pkg = catkin_pkg.package.parse_package(self.get_catkin_paths()[resource_name]) deps = pkg.build_depends + pkg.buildtool_depends + pkg.run_depends + pkg.test_depends return [d.name for d in deps] elif resource_name in self.get_loadable_resources(): return self._rospack.get_rosdeps(resource_name, implicit=implicit) elif resource_name in self._rosstack.list(): # stacks currently do not have rosdeps of their own, implicit or otherwise return [] else: raise rospkg.ResourceNotFound(resource_name)
def _filename(self): ''' Try and find the service profile filename. Update the cache if necessary. :raises: :exc:`rospkg.ResourceNotFound` if the service profile yaml is not found ''' # 1st attempt - look in da cache try: if os.path.isfile(self._location_cache[self.resource_name]): return self._location_cache[self.resource_name] except KeyError: pass # 2nd attempt - do the hard yakka try: filename = rocon_python_utils.ros.find_resource_from_string(self.resource_name) self._location_cache[self.resource_name] = filename # update the cache return filename except rospkg.ResourceNotFound: raise rospkg.ResourceNotFound("could not find service profile [%s]" % self.resource_name)
def get_uri(path): """Get the path to the file. Raises: rospkg.ResourceNotFound """ mod_url = path if path.find('package://') == 0: mod_url = path[10:] pos = mod_url.find('/') if pos == -1: raise rospkg.ResourceNotFound( "Could not parse package:// format into raw URI path format for " + path) package = mod_url[0:pos] mod_url = mod_url[pos:] package_path = rospkg.RosPack().get_path(package) mod_url = package_path + mod_url elif path.find('file://') == 0: mod_url = path[7:] return mod_url
def get_source(self, view_name): matches = [x for x in self.sources if x.url == view_name] if matches: return matches[0] else: raise rospkg.ResourceNotFound(view_name)
def _package_args_handler(command, parser, options, args): if options.rosdep_all: if args: parser.error('cannot specify additional arguments with -a') else: # let the loader filter the -a. This will take out some # packages that are catkinized (for now). lookup = _get_default_RosdepLookup(options) loader = lookup.get_loader() args = loader.get_loadable_resources() not_found = [] elif not args: parser.error('no packages or stacks specified') # package or stack names as args. have to convert stack names to packages. # - overrides to enable testing packages = [] not_found = [] if options.from_paths: for path in args: if options.verbose: print("Using argument '{0}' as a path to search.".format(path)) if not os.path.exists(path): print("given path '{0}' does not exist".format(path)) return 1 path = os.path.abspath(path) if 'ROS_PACKAGE_PATH' not in os.environ: os.environ['ROS_PACKAGE_PATH'] = '{0}'.format(path) else: os.environ['ROS_PACKAGE_PATH'] = '{0}{1}{2}'.format( path, os.pathsep, os.environ['ROS_PACKAGE_PATH']) pkgs = find_catkin_packages_in(path, options.verbose) packages.extend(pkgs) # Make packages list unique packages = list(set(packages)) else: rospack = rospkg.RosPack() rosstack = rospkg.RosStack() val = rospkg.expand_to_packages(args, rospack, rosstack) packages = val[0] not_found = val[1] if not_found: raise rospkg.ResourceNotFound(not_found[0], rospack.get_ros_paths()) # Handle the --ignore-src option if command in ['install', 'check'] and options.ignore_src: if options.verbose: print('Searching ROS_PACKAGE_PATH for ' 'sources: ' + str(os.environ['ROS_PACKAGE_PATH'].split(':'))) ws_pkgs = get_workspace_packages() for path in os.environ['ROS_PACKAGE_PATH'].split(':'): path = os.path.abspath(path.strip()) if os.path.exists(path): pkgs = find_catkin_packages_in(path, options.verbose) ws_pkgs.extend(pkgs) elif options.verbose: print('Skipping non-existent path ' + path) set_workspace_packages(ws_pkgs) lookup = _get_default_RosdepLookup(options) # Handle the --skip-keys option by pretending that they are packages in the catkin workspace if command in ['install', 'check'] and options.skip_keys: if options.verbose: print('Skipping the specified rosdep keys:\n- ' + '\n- '.join(options.skip_keys)) lookup.skipped_keys = options.skip_keys if 0 and not packages: # disable, let individual handlers specify behavior # possible with empty stacks print('No packages in arguments, aborting') return return command_handlers[command](lookup, packages, options)