Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PopulateUpstreamToDrake for checkout not named drake #7864

Merged
merged 1 commit into from
Jan 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions multibody/parsers/package_map.cc
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
#include <tinyxml2.h>

#include "drake/common/drake_assert.h"
#include "drake/common/drake_path.h"
#include "drake/common/drake_throw.h"
#include "drake/common/text_logging.h"

@@ -83,7 +84,7 @@ string GetParentDirectory(const string& directory) {

// 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) {
string GetPackageName(const string& package_xml_file) {
DRAKE_DEMAND(!package_xml_file.empty());
XMLDocument xml_doc;
xml_doc.LoadFile(package_xml_file.data());
@@ -129,16 +130,16 @@ void PackageMap::AddPackageIfNew(const string& 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) {
void PackageMap::PopulateUpstreamToDrakeHelper(
const string& directory,
const string& stop_at_directory) {
DRAKE_DEMAND(!directory.empty());

// If we've reached the top, then stop searching.
if (directory.length() <= stop_at_directory.length()) {
return;
}

// If there is a new package.xml file, then add it.
if (HasPackageXmlFile(directory)) {
spruce::path spruce_path(directory);
@@ -147,38 +148,36 @@ void PackageMap::PopulateUpstreamToDrakeHelper(const string& directory) {
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));
PopulateUpstreamToDrakeHelper(
GetParentDirectory(directory), stop_at_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();
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) {
const string model_dir = spruce_path.root();

// Bail out if the model file is not part of Drake.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
const string drake_path = GetDrakePath();
#pragma GCC diagnostic pop
auto iter = std::mismatch(drake_path.begin(), drake_path.end(),
model_dir.begin());
if (iter.first != drake_path.end()) {
// The drake_path was not a prefix of model_dir.
return;
}

// Search the directory containing the model_file and "upstream".
PopulateUpstreamToDrakeHelper(spruce_path.root());
PopulateUpstreamToDrakeHelper(spruce_path.root(), drake_path);
}

void PackageMap::CrawlForPackages(const string& path) {
6 changes: 5 additions & 1 deletion multibody/parsers/package_map.h
Original file line number Diff line number Diff line change
@@ -78,9 +78,13 @@ class PackageMap {
const std::string& path);

// Recursively searches up the directory path searching for package.xml files.
// The @p directory must be a child of @p stop_at_directory. Stops searching
// when the search directory is not longer than @p stop_at_directory.
// Adds the packages defined by these package.xml files to this PackageMap.
// This method is intended to be called by PopulateUpstreamToDrake().
void PopulateUpstreamToDrakeHelper(const std::string& directory);
void PopulateUpstreamToDrakeHelper(
const std::string& directory,
const std::string& stop_at_directory);

// The key is the name of a ROS package and the value is the package's
// directory.