-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
package_map.cc
237 lines (197 loc) · 7.13 KB
/
package_map.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "drake/multibody/parsers/package_map.h"
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <utility>
#include <vector>
#include <spruce.hh>
#include <tinydir.h>
#include <tinyxml2.h>
#include "drake/common/drake_assert.h"
#include "drake/common/drake_throw.h"
#include "drake/common/text_logging.h"
using std::cerr;
using std::endl;
using std::getenv;
using std::istringstream;
using std::runtime_error;
using std::string;
using std::vector;
using tinyxml2::XMLDocument;
using tinyxml2::XMLElement;
namespace drake {
namespace parsers {
PackageMap::PackageMap() {}
void PackageMap::Add(const string& package_name, const string& package_path) {
DRAKE_DEMAND(map_.find(package_name) == map_.end());
if (!spruce::path(package_path).exists()) {
throw std::runtime_error(
"Could not add package://" + package_name + " to the search path "
"because directory " + package_path + " does not exist");
}
map_.insert(make_pair(package_name, package_path));
}
bool PackageMap::Contains(const string& package_name) const {
return map_.find(package_name) != map_.end();
}
int PackageMap::size() const {
return map_.size();
}
const string& PackageMap::GetPath(const string& package_name) const {
DRAKE_DEMAND(Contains(package_name));
return map_.at(package_name);
}
void PackageMap::PopulateFromFolder(const string& path) {
DRAKE_DEMAND(!path.empty());
CrawlForPackages(path);
}
void PackageMap::PopulateFromEnvironment(const string& environment_variable) {
DRAKE_DEMAND(!environment_variable.empty());
const char* path_char = getenv(environment_variable.c_str());
DRAKE_DEMAND(path_char);
string path = string(path_char);
CrawlForPackages(path);
}
namespace {
// Returns true if @p directory has a package.xml file.
bool HasPackageXmlFile(const string& directory) {
DRAKE_DEMAND(!directory.empty());
spruce::path spruce_path(directory);
spruce_path.append("package.xml");
return spruce_path.exists();
}
// Returns the parent directory of @p directory.
string GetParentDirectory(const string& directory) {
DRAKE_DEMAND(!directory.empty());
return spruce::path(directory).root();
}
// Parses the package.xml file specified by package_xml_file. Finds and returns
// the name of the package.
std::string GetPackageName(const string& package_xml_file) {
DRAKE_DEMAND(!package_xml_file.empty());
XMLDocument xml_doc;
xml_doc.LoadFile(package_xml_file.data());
if (xml_doc.ErrorID()) {
throw runtime_error("package_map.cc: GetPackageName(): "
"Failed to parse XML in file \"" + package_xml_file + "\".\n" +
xml_doc.ErrorName());
}
XMLElement* package_node = xml_doc.FirstChildElement("package");
if (!package_node) {
throw runtime_error("package_map.cc: GetPackageName(): "
"ERROR: XML file \"" + package_xml_file + "\" does not contain "
"element <package>.");
}
XMLElement* name_node = package_node->FirstChildElement("name");
if (!name_node) {
throw runtime_error("package_map.cc: GetPackageName(): "
"ERROR: <package> element does not contain element <name> "
"(XML file \"" + package_xml_file + "\").");
}
// Throws an exception if the name node does not have any children.
DRAKE_THROW_UNLESS(!name_node->NoChildren());
const string package_name = name_node->FirstChild()->Value();
DRAKE_THROW_UNLESS(package_name != "");
return package_name;
}
} // namespace
void PackageMap::AddPackageIfNew(const string& package_name,
const string& path) {
DRAKE_DEMAND(!package_name.empty());
DRAKE_DEMAND(!path.empty());
// Don't overwrite entries in the map.
if (!Contains(package_name)) {
Add(package_name, path);
} else {
drake::log()->warn("Package \"{}\" was found more than once in the "
"search space.", package_name);
}
}
namespace {
// This is the folder that PopulateUpstreamToDrake is documented to use.
const char* const kDrakeFolderName = "drake";
} // namespace
void PackageMap::PopulateUpstreamToDrakeHelper(const string& directory) {
DRAKE_DEMAND(!directory.empty());
// If there is a new package.xml file, then add it.
if (HasPackageXmlFile(directory)) {
spruce::path spruce_path(directory);
spruce_path.append("package.xml");
const string package_name = GetPackageName(spruce_path.getStr());
AddPackageIfNew(package_name, directory);
}
// If we've reached .../drake, then stop searching.
const vector<string> path_elements = spruce::path(directory).split();
DRAKE_DEMAND(!path_elements.empty());
const string& final_path_element = path_elements.back();
DRAKE_DEMAND(!final_path_element.empty());
if (final_path_element == kDrakeFolderName) {
return;
}
// Continue searching in our parent directory.
PopulateUpstreamToDrakeHelper(GetParentDirectory(directory));
}
void PackageMap::PopulateUpstreamToDrake(const string& model_file) {
DRAKE_DEMAND(!model_file.empty());
// Verify that the model_file names an URDF or SDF file.
spruce::path spruce_path(model_file);
std::string extension = spruce_path.extension();
std::transform(extension.begin(), extension.end(), extension.begin(),
::tolower);
DRAKE_DEMAND(extension == ".urdf" || extension == ".sdf");
// Bail out if the model file is not in .../drake.
const string path_sep("/");
const string required_substring = path_sep + kDrakeFolderName + path_sep;
if (model_file.find(required_substring) == string::npos) {
return;
}
// Search the directory containing the model_file and "upstream".
PopulateUpstreamToDrakeHelper(spruce_path.root());
}
void PackageMap::CrawlForPackages(const string& path) {
DRAKE_DEMAND(!path.empty());
string directory_path = path;
// Removes all trailing "/" characters if any exist.
while (directory_path.length() > 0 && *(directory_path.end() - 1) == '/') {
directory_path.erase(directory_path.end() - 1);
}
DRAKE_DEMAND(directory_path.length() > 0);
istringstream iss(directory_path);
const string target_filename("package.xml");
const char pathsep = ':';
string token;
while (getline(iss, token, pathsep)) {
tinydir_dir dir;
if (tinydir_open(&dir, token.c_str()) < 0) {
cerr << "Unable to open directory: " << token << endl;
continue;
}
while (dir.has_next) {
tinydir_file file;
tinydir_readfile(&dir, &file);
// Skips hidden directories (including "." and "..").
if (file.is_dir && (string(file.name) != "")
&& (file.name[0] != '.')) {
CrawlForPackages(file.path);
} else if (file.name == target_filename) {
const string package_name = GetPackageName(file.path);
spruce::path spruce_path(file.path);
const string package_path = spruce_path.root().append("/");
AddPackageIfNew(package_name, package_path);
}
tinydir_next(&dir);
}
tinydir_close(&dir);
}
}
std::ostream& operator<<(std::ostream& out, const PackageMap& package_map) {
out << "PackageMap:\n";
if (package_map.size() == 0)
out << " [EMPTY!]\n";
for (const auto& entry : package_map.map_) {
out << " - " << entry.first << ": " << entry.second << "\n";
}
return out;
}
} // namespace parsers
} // namespace drake