Elements  6.2
A C++ base framework for the Euclid Software.
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 
33 namespace Elements {
34 inline namespace Services {
35 namespace DataSync {
36 
37 using std::string;
38 
39 path confFilePath(path filename) {
40  return Configuration::getPath(filename);
41 }
42 
43 bool 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 
64 bool localDirExists(path local_dir) {
65  return boost::filesystem::is_directory(local_dir);
66 }
67 
68 void 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 
78 string 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 
88 string lower(string text) {
89  string uncased(text);
90  std::transform(text.begin(), text.end(), uncased.begin(), ::tolower);
91  return uncased;
92 }
93 
94 bool 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
Elements::Services::DataSync::checkCall
ELEMENTS_API bool checkCall(const std::string &command)
Definition: DataSyncUtils.cpp:43
std::string
STL class.
std::shared_ptr
STL class.
std::pair
std::vector
STL class.
std::system
T system(T... args)
std::string::find
T find(T... args)
Elements::Services::DataSync::localDirExists
ELEMENTS_API bool localDirExists(path localDir)
Definition: DataSyncUtils.cpp:64
Elements::Services::DataSync::lower
ELEMENTS_API std::string lower(std::string text)
Definition: DataSyncUtils.cpp:88
Elements::Services::DataSync::runCommandAndCaptureOutErr
ELEMENTS_API std::pair< std::string, std::string > runCommandAndCaptureOutErr(std::string command)
Definition: DataSyncUtils.cpp:49
Elements::Services::DataSync::containsInThisOrder
ELEMENTS_API bool containsInThisOrder(std::string input, std::vector< std::string > substrings)
Definition: DataSyncUtils.cpp:94
Elements::Environment::get
std::string get(const std::string &index, const std::string &default_value="") const
Definition: Environment.cpp:240
std::string::c_str
T c_str(T... args)
std::array
STL class.
Elements::Services::DataSync::createLocalDirOf
ELEMENTS_API void createLocalDirOf(path localFile)
Definition: DataSyncUtils.cpp:68
std::runtime_error
STL class.
std::transform
T transform(T... args)
Elements::Services::DataSync::localWorkspacePrefix
ELEMENTS_API path localWorkspacePrefix()
Definition: DataSyncUtils.cpp:82
DataSyncUtils.h
std::string::begin
T begin(T... args)
Environment.h
Defines a class to handle the Environment.
Elements::Environment
Python dictionary-like Environment interface.
Definition: Environment.h:44
std::make_pair
T make_pair(T... args)
std::string::end
T end(T... args)
Elements::Kernel::Auxiliary::getPath
ELEMENTS_API Path::Item getPath(const T &file_name, bool raise_exception=true)
alias for the getAuxiliaryPath function
std::array::data
T data(T... args)
Elements::Services::DataSync::confFilePath
ELEMENTS_API path confFilePath(path filename)
Definition: DataSyncUtils.cpp:39
Configuration.h
provide functions to retrieve configuration files
Elements::Services::DataSync::environmentVariable
ELEMENTS_API std::string environmentVariable(std::string name)
Get the value of an environment variable.
Definition: DataSyncUtils.cpp:78
Elements
Definition: callBackExample.h:35
Elements::Services::DataSync::path
Path::Item path
importing the path item from ElementsKernel
Definition: DataSyncUtils.h:41