Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
setup-resource: --linkDataset accepts 3 options (nolink, absolute, re…
Browse files Browse the repository at this point in the history
…lative), default is nolink (i.e. dataset copy), implicit value is absolute
  • Loading branch information
vaclavblazek committed Oct 23, 2020
1 parent 9e68f5c commit 6da1d5a
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions mapproxy/src/setup-resource/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ UTILITY_GENERATE_ENUM_CI(ResourceType,
((tin)("TIN"))
)

UTILITY_GENERATE_ENUM(DatasetLink,
((nolink))
((absolute))
((relative))
)

struct Config {
boost::optional<std::string> geoidGrid;
RasterFormat format;
Expand All @@ -104,7 +110,7 @@ class SetupResource : public service::Cmdline {
public:
SetupResource()
: service::Cmdline("mapproxy-setup-resource", BUILD_TARGET_VERSION)
, linkDataset_(false)
, linkDataset_(DatasetLink::nolink)
{}

private:
Expand All @@ -121,7 +127,7 @@ class SetupResource : public service::Cmdline {
std::string dataset_; // do not use fs::path since it needs quotes in case
// of spaces in filename

bool linkDataset_;
DatasetLink linkDataset_;

boost::optional<ResourceType> resourceType_;

Expand All @@ -145,8 +151,9 @@ void SetupResource::configuration(po::options_description &cmdline
, "Resource group ID. Deduced from filename if not provided.")
("dataset", po::value(&dataset_)->required()
, "Path to input raster dataset.")
("linkDataset", utility::implicit_value(&linkDataset_, true)
->default_value(false)
("linkDataset", utility::implicit_value
(&linkDataset_, DatasetLink::absolute)
->default_value(DatasetLink::nolink)
, "Link dataset instead of copying. NB: dataset must stay at given "
"path while resource is used.")
("resourceType", po::value<ResourceType>()
Expand Down Expand Up @@ -219,6 +226,14 @@ void SetupResource::configure(const po::variables_map &vars)
config_.bottomLod = vars["bottomLod"].as<vts::Lod>();
}

// absolutize destination paths
config_.mapproxyDataRoot = fs::absolute(config_.mapproxyDataRoot);
config_.mapproxyDefinitionDir
= fs::absolute(config_.mapproxyDefinitionDir);

// make dataset path absolute
dataset_ = fs::absolute(fs::path(dataset_)).string();

LOG(info3, log_)
<< "Config:"
<< "\nmapproxy.dataRoot = " << config_.mapproxyDataRoot
Expand All @@ -231,6 +246,8 @@ void SetupResource::configure(const po::variables_map &vars)
if (config_.bottomLod) { os << *config_.bottomLod; }
else { os << "none"; }
})
<< "\ndataset = " << dataset_
<< "\nlinkDataset = " << linkDataset_
<< "\n"
;
}
Expand Down Expand Up @@ -795,20 +812,28 @@ int SetupResource::run()
const auto mainDataset([&]()
{
if (!fs::exists(rootDir)) {
LOG(info4) << "Copying/symlinking dataset to destination.";

const auto tmpRootDir(utility::addExtension(rootDir, ".tmp"));

const auto tmpDatasetPath(tmpRootDir / baseDatasetPath);

fs::create_directories(tmpDatasetPath.parent_path());

if (linkDataset_) {
// symlink
fs::remove_all(tmpDatasetPath);
fs::create_symlink(dataset_, tmpDatasetPath);
fs::remove_all(tmpDatasetPath);

if (linkDataset_ != DatasetLink::nolink) {
LOG(info4) << "Symlinking dataset to destination.";
fs::path dst(dataset_);

if (linkDataset_ == DatasetLink::relative) {
// relativize already absolute path
dst = utility::lexically_relative(dst, rootDir);
}

// link
fs::create_symlink(dst, tmpDatasetPath);
} else {
// copy (overwrite)
LOG(info4) << "Copying dataset to destination.";
utility::copy_file(dataset_, tmpDatasetPath, true);
ds.copyFiles(datasetPath);
}
Expand Down

0 comments on commit 6da1d5a

Please sign in to comment.