Skip to content

Commit

Permalink
7 ➡️8 (main) (#240)
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Peters <[email protected]>
Signed-off-by: Louise Poubel <[email protected]>
  • Loading branch information
chapulina authored Mar 28, 2022
2 parents e4caf26 + 03a13be commit ff89488
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ jobs:
codecov-enabled: true
cppcheck-enabled: true
cpplint-enabled: true
jammy-ci:
runs-on: ubuntu-latest
name: Ubuntu Jammy CI
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Compile and test
id: ci
uses: ignition-tooling/action-ignition-ci@jammy
21 changes: 21 additions & 0 deletions include/ignition/fuel_tools/Interface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,26 @@ namespace ignition
/// \return Path to the downloaded asset. Empty on error.
IGNITION_FUEL_TOOLS_VISIBLE std::string fetchResourceWithClient(
const std::string &_uri, ignition::fuel_tools::FuelClient &_client);

/// \brief Get the SDF file path for a model or world based on a directory
/// containing a Fuel model or world. Here is a typical use case:
///
/// 1. Fetch a Fuel resource:
/// `std::string resourcePath = fetchResource("https://...")`
/// 2. Get the SDF file for the resource:
/// `std::string resourceSdf = sdfFromPath(resourcePath)`
/// 3. Parse the SDF file using libsdformat.
///
/// This function will determine the SDF file according to the following:
/// 1. Check for a metadata.pbtxt file, and return the SDF file specified
/// within the metadata.pbtxt file.
/// 2. Check for a model.config file, and return the SDF file specified
/// withing the model.config file.
/// 3. Return the first file with an `.sdf` extension.
/// \param[in] _path Filesystem path to a Fuel model or world directory.
/// \return Full path to the model or world SDF file, or empty string if
/// the SDF file coult not be determined.
IGNITION_FUEL_TOOLS_VISIBLE std::string sdfFromPath(
const std::string &_path);
}
}
74 changes: 74 additions & 0 deletions src/Interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
*
*/

#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <google/protobuf/text_format.h>
#include <ignition/msgs/fuel_metadata.pb.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif

#include <ignition/msgs/Utility.hh>
#include "ignition/common/Console.hh"
#include "ignition/fuel_tools/Interface.hh"
#include "ignition/fuel_tools/WorldIdentifier.hh"
Expand Down Expand Up @@ -74,5 +84,69 @@ namespace ignition

return result;
}

//////////////////////////////////////////////
std::string sdfFromPath(const std::string &_path)
{
ignition::msgs::FuelMetadata meta;
std::string metadataPath =
ignition::common::joinPaths(_path, "metadata.pbtxt");
std::string modelConfigPath =
ignition::common::joinPaths(_path, "model.config");

bool foundMetadataPath = ignition::common::exists(metadataPath);
bool foundModelConfigPath = ignition::common::exists(modelConfigPath);

// Use the metadata.pbtxt or model.config first.
if (foundMetadataPath || foundModelConfigPath)
{
std::string modelPath =
(foundMetadataPath) ? metadataPath : modelConfigPath;

// Read the pbtxt or config file.
std::ifstream inputFile(modelPath);
std::string inputStr((std::istreambuf_iterator<char>(inputFile)),
std::istreambuf_iterator<char>());

if (foundMetadataPath)
{
// Parse the file into the fuel metadata message
google::protobuf::TextFormat::ParseFromString(inputStr, &meta);
}
else
{
if (!ignition::msgs::ConvertFuelMetadata(inputStr, meta))
return "";
}

if (meta.has_model())
return ignition::common::joinPaths(_path, meta.model().file());
else if (meta.has_world())
return ignition::common::joinPaths(_path, meta.world().file());
return "";
}

// Otherwise, use the first ".sdf" file found in the given path.
common::DirIter dirIter(_path);
common::DirIter end;
while (dirIter != end)
{
if (common::isFile(*dirIter))
{
std::string basename = ignition::common::basename(*dirIter);
// Just some safety checks.
if (!basename.empty() && basename.find(".sdf") != std::string::npos)
{
std::string extension = basename.substr(
basename.find_last_of(".") + 1);
if (extension == "sdf")
return *dirIter;
}
}
++dirIter;
}

return "";
}
}
}
25 changes: 23 additions & 2 deletions src/Interface_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ TEST(Interface, FetchResources)
// Download model
std::string path = fetchResourceWithClient(modelUrl.Str(), client);

std::string sdfPath = sdfFromPath(path);
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"fuel.ignitionrobotics.org", "chapulina", "models", "test box", "2",
"model.sdf"), sdfPath);

// Check it was downloaded to `2`
EXPECT_EQ(path, common::joinPaths(common::cwd(), "test_cache",
"fuel.ignitionrobotics.org", "chapulina", "models", "test box", "2"));
Expand Down Expand Up @@ -154,7 +159,14 @@ TEST(Interface, FetchResources)
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"fuel.ignitionrobotics.org", "openrobotics", "models", "bus", "1"),
cachedPath);
}
}

{
std::string sdfFile = sdfFromPath(cachedPath);
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"fuel.ignitionrobotics.org", "openrobotics", "models", "bus",
"1", "model.sdf"), sdfFile);
}

// Check file is cached
{
Expand All @@ -176,6 +188,8 @@ TEST(Interface, FetchResources)
Result res = client.CachedWorld(worldUrl, cachedPath);
EXPECT_FALSE(res) << "Cached Path: " << cachedPath;
EXPECT_EQ(ResultType::FETCH_ERROR, res.Type());
std::string sdfFile = sdfFromPath(cachedPath);
EXPECT_TRUE(sdfFile.empty());
}

// Download world
Expand Down Expand Up @@ -245,7 +259,14 @@ TEST(Interface, FetchResources)
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"fuel.ignitionrobotics.org", "chapulina", "worlds", "test world", "2"),
cachedPath);
}
}

{
std::string sdfFile = sdfFromPath(cachedPath);
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"fuel.ignitionrobotics.org", "chapulina", "worlds", "test world",
"2", "test.sdf"), sdfFile);
}

// Check file is cached
{
Expand Down

0 comments on commit ff89488

Please sign in to comment.