Skip to content

Add a flag --skip-cached to skip already-downloaded models and collections #461

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

Open
wants to merge 3 commits into
base: gz-fuel-tools10
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions src/cmd/cmdfuel.rb.in
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ SUBCOMMANDS = {
" -t [--type] arg Limit what resource type (i.e. model, world) \n"\
" to download from a collection. All resources \n"\
" will be downloaded if unspecified. Ignored \n"\
" if not downloading collection. \n"+
" if not downloading collection. \n"\
" -s [--skip-cached] Skip downloading resources already in cache. \n"+
COMMON_OPTIONS,

'edit' =>
Expand Down Expand Up @@ -228,7 +229,8 @@ class Cmd
'onlymodels' => '0',
'onlyworlds' => '0',
'defaults' => false,
'console' => false
'console' => false,
'skip_cached' => 'false'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'skip_cached' => 'false'
'skip_cached' => false

}

usage = COMMANDS[args[0]]
Expand Down Expand Up @@ -302,6 +304,9 @@ class Cmd
opts.on('--console', 'Output to console') do
options['console'] = true
end
opts.on('-s', '--skip-cached', 'Skip downloading resources already in cache') do
options['skip_cached'] = 'true'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
options['skip_cached'] = 'true'
options['skip_cached'] = true

end
end # opt_parser do

opt_parser.parse!(args)
Expand Down Expand Up @@ -434,9 +439,9 @@ class Cmd
exit(-1)
end
when 'download'
Importer.extern 'int downloadUrl(const char *, const char *, const char *, const char *, unsigned int)'
Importer.extern 'int downloadUrl(const char *, const char *, const char *, const char *, unsigned int, const char *)'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Importer.extern 'int downloadUrl(const char *, const char *, const char *, const char *, unsigned int, const char *)'
Importer.extern 'int downloadUrl(const char *, const char *, const char *, const char *, unsigned int, bool)'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ahcorde
I followed the changes you suggested and updated both gz.cc and gz.hh to accept a bool parameter. However, when I tried to compile and run the code, I encountered the following issue:

Library error: Problem running [download]() from /root/gz_ws/install/lib/libgz-fuel_tools10.so.10.0.1

I need to determine whether this issue came from the Ruby script or the C++ implementation. Previously, when reading the file, I noticed that char was used to represent boolean values, so I followed the same approach. However, now I plan to recheck the full code to understand exactly where the issue is coming from.

if not Importer.downloadUrl(options['url'], options['config'],
options['header'], options['type'], options['jobs_int'])
options['header'], options['type'], options['jobs_int'], options['skip_cached'])
exit(-1)
end
when 'edit'
Expand Down
1 change: 1 addition & 0 deletions src/cmd/fuel.bash_completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ GZ_DOWNLOAD_COMPLETION_LIST="
-j --jobs
-t --type
-u --url
-s --skip-cached
--force-version
--versions
"
Expand Down
56 changes: 53 additions & 3 deletions src/gz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ extern "C" GZ_FUEL_TOOLS_VISIBLE int listWorlds(const char *_url,

//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int downloadUrl(const char *_url,
const char *_configFile, const char *_header, const char *_type, int _jobs)
const char *_configFile, const char *_header, const char *_type, int _jobs, const char *_skipCached)
{
// Add signal handler for SIGTERM and SIGINT. Ctrl-C doesn't work without this
// handler.
Expand Down Expand Up @@ -499,9 +499,24 @@ extern "C" GZ_FUEL_TOOLS_VISIBLE int downloadUrl(const char *_url,
gz::fuel_tools::WorldIdentifier world;
gz::fuel_tools::CollectionIdentifier collection;

// Check if we should skip cached resources
std::string skipCachedStr{_skipCached ? _skipCached : "false"};
std::transform(skipCachedStr.begin(), skipCachedStr.end(),
skipCachedStr.begin(), ::tolower);
bool skipCached = skipCachedStr == "true";

// Model?
if (client.ParseModelUrl(url, model))
{
// If skipCached is enabled, check if the model is already in the cache
if (skipCached)
{
std::string cachedPath;
if (client.CachedModel(url, cachedPath))
std::cout << "Model '" << model.Name() << "' found in cache at: " << cachedPath << ". Skipping download." << std::endl;
return true;
}

// Download
if (gz::common::Console::Verbosity() >= 3)
{
Expand Down Expand Up @@ -538,6 +553,13 @@ extern "C" GZ_FUEL_TOOLS_VISIBLE int downloadUrl(const char *_url,
// World?
else if (client.ParseWorldUrl(url, world))
{
if (skipCached)
{
std::string cachedPath;
if (client.CachedWorld(url, cachedPath))
std::cout << "World '" << world.Name() << "' found in cache at: " << cachedPath << ". Skipping download." << std::endl;
return true;
}
// Download
if (gz::common::Console::Verbosity() >= 3)
{
Expand Down Expand Up @@ -598,7 +620,21 @@ extern "C" GZ_FUEL_TOOLS_VISIBLE int downloadUrl(const char *_url,
auto modelsIter = client.Models(collection);
for (; modelsIter; ++modelsIter)
{
modelIds.push_back(modelsIter->Identification());
// modelIds.push_back(modelsIter->Identification());
auto modelId = modelsIter->Identification();
if (skipCached)
{
std::string cachedPath;
if (client.CachedModel(modelId, cachedPath))
{
if (gz::common::Console::Verbosity() >= 3)
{
gzmsg << "Skipping cached model: " << modelId.Name() << std::endl;
}
continue;
}
}
modelIds.push_back(modelId);
}
gzmsg << "Found " << modelIds.size() << " models in collection ["
<< collection.Name() << "]" << std::endl;
Expand All @@ -610,7 +646,21 @@ extern "C" GZ_FUEL_TOOLS_VISIBLE int downloadUrl(const char *_url,
auto worldIter = client.Worlds(collection);
for (; worldIter; ++worldIter)
{
worldIds.push_back(worldIter);
// worldIds.push_back(worldIter);
if (skipCached)
{
std::string cachedPath;
gz::common::URI worldUri = worldIter->Url();
if (client.CachedWorld(worldUri, cachedPath))
{
if (gz::common::Console::Verbosity() >= 3)
{
gzmsg << "Skipping cached world: " << worldIter->Name() << std::endl;
}
continue;
}
}
worldIds.push_back(*worldIter);
}
gzmsg << "Found " << worldIds.size() << " worlds in collection ["
<< collection.Name() << "]" << std::endl;
Expand Down
3 changes: 2 additions & 1 deletion src/gz.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern "C" GZ_FUEL_TOOLS_VISIBLE int listModels(
/// \param[in] _owner Optional owner name
/// \param[in] _raw 'true' for machine readable output.
/// \param[in] _configFile Path to a YAML configuration file.
/// \param[in] _skipCached "true" to skip resources already in cache.
/// \return 1 if successful, 0 if not.
extern "C" GZ_FUEL_TOOLS_VISIBLE int listWorlds(
const char *_url = nullptr, const char *_owner = "",
Expand All @@ -61,7 +62,7 @@ extern "C" GZ_FUEL_TOOLS_VISIBLE int listWorlds(
/// \return 1 if successful, 0 if not.
extern "C" GZ_FUEL_TOOLS_VISIBLE int downloadUrl(
const char *_url = nullptr, const char *_configFile = nullptr,
const char *_header = nullptr, const char *_type = nullptr, int _jobs = 1);
const char *_header = nullptr, const char *_type = nullptr, int _jobs = 1, const char *_skipCached = "false");

/// \brief External hook to execute 'gz fuel upload -m path' from the command
/// line.
Expand Down