Elements 6.1.2
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 <algorithm>
20#include <array>
21#include <cstdio>
22#include <cstdlib>
23#include <memory>
24#include <string>
25#include <utility>
26#include <vector>
27
29#include "ElementsKernel/Environment.h" // For Environment
30
32
33namespace Elements {
34inline namespace Services {
35namespace DataSync {
36
37using std::string;
38
40 return Configuration::getPath(filename);
41}
42
43bool checkCall(const string& command) {
44 string silent_command = command + " > /dev/null";
45 const int status = std::system(silent_command.c_str());
46 return status == 0;
47}
48
50 string out;
51 string err;
53 std::shared_ptr<FILE> cmdpipe(popen(command.c_str(), "r"), pclose);
54 if (not cmdpipe) {
55 throw std::runtime_error(string("Unable to run command: ") + command);
56 }
57 if (fgets(buffer.data(), BUFSIZ, cmdpipe.get()) != nullptr) {
58 out += buffer.data();
59 }
60 // @TODO get standard error
61 return std::make_pair(out, err);
62}
63
64bool localDirExists(path local_dir) {
65 return boost::filesystem::is_directory(local_dir);
66}
67
68void createLocalDirOf(path local_file) {
69 if (not local_file.has_parent_path()) {
70 return;
71 }
72 const path dir = local_file.parent_path();
73 if (not localDirExists(dir)) {
74 boost::filesystem::create_directories(dir);
75 }
76}
77
78string environmentVariable(string name) {
79 return Environment().get(name); // Already returns "" if not found
80}
81
83 const string codeen_prefix("WORKSPACE");
84 const string prefix_env_variable(codeen_prefix);
85 return path(environmentVariable(prefix_env_variable));
86}
87
88string lower(string text) {
89 string uncased(text);
90 std::transform(text.begin(), text.end(), uncased.begin(), ::tolower);
91 return uncased;
92}
93
94bool containsInThisOrder(string input, std::vector<string> substrings) {
95 string::size_type offset(0);
96 for (auto substr : substrings) {
97 offset = input.find(substr, offset);
98 if (offset == string::npos) {
99 return false;
100 }
101 }
102 return true;
103}
104
105} // namespace DataSync
106} // namespace Services
107} // 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.
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
Definition: DataSyncUtils.h:41
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)