src/package.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "ros/package.h"
29 #include "rospack/rospack.h"
30 
31 #include <cstdio>
32 #include <iostream>
33 
34 #include <boost/algorithm/string/split.hpp>
35 #include <boost/algorithm/string/join.hpp>
36 #include <boost/algorithm/string/classification.hpp>
37 #include <boost/thread/mutex.hpp>
38 
39 namespace ros
40 {
41 namespace package
42 {
43 
44 // Mutex used to lock calls into librospack, which is not thread-safe.
45 static boost::mutex librospack_mutex;
46 
47 std::string command(const std::string& _cmd)
48 {
49  boost::mutex::scoped_lock lock(librospack_mutex);
50  // static allows caching of results in between calls (in same process)
51  static rospack::ROSPack rp;
52  int ret;
53  try
54  {
55  ret = rp.run(_cmd);
56  if(ret == 0)
57  return rp.getOutput();
58  else {
59  if ( !rp.is_quiet() )
60  std::cerr << "ROSPack::run returned non-zero." << std::endl;
61  }
62  }
63  catch(std::runtime_error &e)
64  {
65  if ( !rp.is_quiet() )
66  std::cerr << "[rospack] " << e.what() << std::endl;
67  }
68  return std::string("");
69 }
70 
71 void command(const std::string& cmd, V_string& output)
72 {
73  std::string out_string = command(cmd);
74  V_string full_list;
75  boost::split(full_list, out_string, boost::is_any_of("\r\n"));
76 
77  // strip empties
78  V_string::iterator it = full_list.begin();
79  V_string::iterator end = full_list.end();
80  for (; it != end; ++it)
81  {
82  if (!it->empty())
83  {
84  output.push_back(*it);
85  }
86  }
87 }
88 
89 std::string getPath(const std::string& package_name)
90 {
91  std::string path = command("find " + package_name);
92 
93  // scrape any newlines out of it
94  for (size_t newline = path.find('\n'); newline != std::string::npos;
95  newline = path.find('\n'))
96  {
97  path.erase(newline, 1);
98  }
99 
100  return path;
101 }
102 
103 bool getAll(V_string& packages)
104 {
105  command("list-names", packages);
106 
107  return true;
108 }
109 
110 static void getPlugins(const std::string& package, const std::string& attribute, V_string& packages, V_string& plugins, bool force_recrawl)
111 {
112  if (force_recrawl)
113  {
114  command("profile");
115  }
116 
117  V_string lines;
118  command("plugins --attrib=" + attribute + " " + package, lines);
119 
120  V_string::iterator it = lines.begin();
121  V_string::iterator end = lines.end();
122  for (; it != end; ++it)
123  {
124  V_string tokens;
125  boost::split(tokens, *it, boost::is_any_of(" "));
126 
127  if (tokens.size() >= 2)
128  {
129  std::string package = tokens[0];
130  std::string rest = boost::join(V_string(tokens.begin() + 1, tokens.end()), " ");
131  packages.push_back(package);
132  plugins.push_back(rest);
133  }
134  }
135 }
136 
138  const std::string& name,
139  const std::string& attribute,
140  std::vector<std::pair<std::string, std::string> >& exports,
141  bool force_recrawl
142 )
143 {
144  V_string packages, plugins;
145  getPlugins(name, attribute, packages, plugins, force_recrawl);
146  // works on the assumption the previous call always return equal length package/plugin lists
147  for (std::size_t i = 0; i < packages.size(); ++i ) {
148  exports.push_back(std::pair<std::string, std::string>(packages[i], plugins[i]));
149  }
150 }
151 
152 void getPlugins(const std::string& package, const std::string& attribute, V_string& plugins, bool force_recrawl)
153 {
154  V_string packages;
155  getPlugins(package, attribute, packages, plugins, force_recrawl);
156 }
157 
158 void getPlugins(const std::string& package, const std::string& attribute, M_string& plugins, bool force_recrawl)
159 {
160  V_string packages, plugins_v;
161  getPlugins(package, attribute, packages, plugins_v, force_recrawl);
162  for (std::size_t i = 0 ; i < packages.size() ; ++i)
163  plugins[packages[i]] = plugins_v[i];
164 }
165 
166 } // namespace package
167 } // namespace ros
std::map< std::string, std::string > M_string
Definition: package.h:85
ROSLIB_DECL bool getAll(V_string &packages)
Gets a list of all packages. Returns false if it could not run the command.
std::vector< std::string > V_string
Definition: package.h:84
string package
static boost::mutex librospack_mutex
Definition: src/package.cpp:45
ROSLIB_DECL std::string command(const std::string &cmd)
Runs a rospack command of the form &#39;rospack <cmd>&#39;, returning the output as a single string...
Definition: src/package.cpp:47
Definition: package.h:79
ROSLIB_DECL std::string getPath(const std::string &package_name)
Returns the fully-qualified path to a package, or an empty string if the package is not found...
Definition: src/package.cpp:89
ROSLIB_DECL void getPlugins(const std::string &package, const std::string &attribute, V_string &plugins, bool force_recrawl=false)
Call the "rospack plugins" command, eg. "rospack plugins --attrib=<attribute> <package>". Returns a vector of strings which are export values.
int run(int argc, char **argv)
std::string getOutput()


roslib
Author(s): Ken Conley , Morgan Quigley , Josh Faust
autogenerated on Mon Feb 28 2022 23:28:10