Elements 6.3.1
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
DataSyncUtils.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2020 Euclid Science Ground Segment
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 3.0 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "ElementsServices/DataSync/DataSyncUtils.h" // for path, checkCall, confFilePath, containsInThisOrder, createLocalDirOf, environmentVariable, getWorkdirVariable, localDirExists, localWorkspacePrefix, lower, runCommandAndCaptureOutErr
20
21#include <algorithm> // for transform
22#include <array> // for array
23#include <cctype> // for tolower
24#include <cstdio> // for fgets, popen, BUFSIZ, pclose, FILE
25#include <cstdlib> // for system
26#include <memory> // for allocator, shared_ptr
27#include <stdexcept> // for runtime_error
28#include <string> // for string, operator+, basic_string, char_traits
29#include <utility> // for make_pair, pair
30#include <vector> // for vector
31
32#include <boost/filesystem/operations.hpp> // for create_directories, is_directory
33
34#include "ElementsKernel/Configuration.h" // for getPath
35#include "ElementsKernel/Environment.h" // for Environment
36
37namespace Elements {
38inline namespace Services {
39namespace DataSync {
40
41using std::string;
42
43const string DEFAULT_WORKDIR_VAR{"WORKSPACE"};
44
45const string WORKDIR_VAR_VAR{"DATASYNC_WORKDIR_VAR"};
46
48 return Configuration::getPath(filename);
49}
50
51bool checkCall(const string& command) {
52 string silent_command = command + " > /dev/null";
53 const int status = std::system(silent_command.c_str());
54 return status == 0;
55}
56
58 string out;
59 string err;
61 std::shared_ptr<FILE> cmdpipe(popen(command.c_str(), "r"), pclose);
62 if (not cmdpipe) {
63 throw std::runtime_error(string("Unable to run command: ") + command);
64 }
65 if (fgets(buffer.data(), BUFSIZ, cmdpipe.get()) != nullptr) {
66 out += buffer.data();
67 }
68 // @TODO get standard error
69 return std::make_pair(out, err);
70}
71
72bool localDirExists(path local_dir) {
73 return boost::filesystem::is_directory(local_dir);
74}
75
76void createLocalDirOf(path local_file) {
77 if (not local_file.has_parent_path()) {
78 return;
79 }
80 const path dir = local_file.parent_path();
81 if (not localDirExists(dir)) {
82 boost::filesystem::create_directories(dir);
83 }
84}
85
86string environmentVariable(string name) {
87 return Environment().get(name); // Already returns "" if not found
88}
89
91
92 string workdir_variable = DEFAULT_WORKDIR_VAR;
93 Environment current;
94
95 if (not current[WORKDIR_VAR_VAR].empty()) {
96 workdir_variable = current[WORKDIR_VAR_VAR];
97 }
98
99 return workdir_variable;
100}
101
103 const string workdir_variable = getWorkdirVariable();
104 const string codeen_prefix(workdir_variable);
105 const string prefix_env_variable(codeen_prefix);
106 return path(environmentVariable(prefix_env_variable));
107}
108
109string lower(string text) {
110 string uncased(text);
111 std::transform(text.begin(), text.end(), uncased.begin(), ::tolower);
112 return uncased;
113}
114
115bool containsInThisOrder(string input, std::vector<string> substrings) {
116 string::size_type offset(0);
117 for (auto substr : substrings) {
118 offset = input.find(substr, offset);
119 if (offset == string::npos) {
120 return false;
121 }
122 }
123 return true;
124}
125
126} // namespace DataSync
127} // namespace Services
128} // namespace Elements
provide functions to retrieve configuration files
Defines a class to handle the Environment.
T begin(T... args)
T c_str(T... args)
Python dictionary-like Environment interface.
Definition Environment.h:44
std::string get(const std::string &index, const std::string &default_value="") const
T data(T... args)
T end(T... args)
T find(T... args)
T get(T... args)
ELEMENTS_API std::string environmentVariable(std::string name)
Get the value of an environment variable.
ELEMENTS_API std::string getWorkdirVariable()
Get the datasync workdir variable.
ELEMENTS_API const std::string WORKDIR_VAR_VAR
Name of the variable containing the name of the workspace. It that variable is not set or empty,...
ELEMENTS_API const std::string DEFAULT_WORKDIR_VAR
Name of the default variable that contains the path to the work dir.
T make_pair(T... args)
ELEMENTS_API path confFilePath(path filename)
ELEMENTS_API path localWorkspacePrefix()
ELEMENTS_API std::pair< std::string, std::string > runCommandAndCaptureOutErr(std::string command)
ELEMENTS_API std::string lower(std::string text)
Path::Item path
importing the path item from ElementsKernel
ELEMENTS_API bool checkCall(const std::string &command)
ELEMENTS_API void createLocalDirOf(path localFile)
ELEMENTS_API bool containsInThisOrder(std::string input, std::vector< std::string > substrings)
ELEMENTS_API bool localDirExists(path localDir)
T system(T... args)
T transform(T... args)