Elements  5.12.0
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 <array>
20 #include <cstdlib>
21 #include <string>
22 #include <vector>
23 #include <utility>
24 #include <algorithm>
25 
27 #include "ElementsKernel/System.h"
28 
30 
31 namespace ElementsServices {
32 namespace DataSync {
33 
34 using std::string;
35 
36 path confFilePath(path filename) {
37  return Elements::getConfigurationPath(filename);
38 }
39 
40 bool checkCall(string command) {
41  const int status = system(command.c_str());
42  return status == 0;
43 }
44 
46  string command) {
47  string out, err;
49  std::shared_ptr<FILE> cmdpipe(popen(command.c_str(), "r"), pclose);
50  if (not cmdpipe) {
51  throw std::runtime_error(string("Unable to run command: ") + command);
52  }
53  if (fgets(buffer.data(), BUFSIZ, cmdpipe.get()) != NULL) {
54  out += buffer.data();
55  }
56  // @TODO get standard error
57  return std::make_pair(out, err);
58 }
59 
60 bool localDirExists(path localDir) {
61  return boost::filesystem::is_directory(localDir);
62 }
63 
64 void createLocalDirOf(path localFile) {
65  if (not localFile.has_parent_path()) {
66  return;
67  }
68  const path dir = localFile.parent_path();
69  if (not localDirExists(dir)) {
70  boost::filesystem::create_directories(dir);
71  }
72 }
73 
74 string environmentVariable(string name) {
75  return Elements::System::getEnv(name); // Already returns "" if not found
76 }
77 
79  const string codeenPrefix("WORKSPACE");
80  const string prefixEnvVariable(codeenPrefix);
81  return path(environmentVariable(prefixEnvVariable));
82 }
83 
84 string lower(string text) {
85  string uncased(text);
86  std::transform(text.begin(), text.end(), uncased.begin(), ::tolower);
87  return uncased;
88 }
89 
91  string input,
92  std::vector<string> substrings) {
93  string::size_type offset(0);
94  for (auto substr : substrings) {
95  offset = input.find(substr, offset);
96  if (offset == string::npos) {
97  return false;
98  }
99  }
100  return true;
101 }
102 
103 } // namespace DataSync
104 } // namespace ElementsServices
provide functions to retrieve configuration files
This file is intended to iron out all the differences between systems (currently Linux and MacOSX)
T begin(T... args)
T c_str(T... args)
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::Path::Item path
Definition: DataSyncUtils.h:39
ELEMENTS_API std::string lower(std::string text)
ELEMENTS_API path confFilePath(path filename)
ELEMENTS_API bool localDirExists(path localDir)
ELEMENTS_API bool containsInThisOrder(std::string input, std::vector< std::string > substrings)
ELEMENTS_API void createLocalDirOf(path localFile)
ELEMENTS_API bool checkCall(std::string command)
ELEMENTS_API path localWorkspacePrefix()
ELEMENTS_API std::pair< std::string, std::string > runCommandAndCaptureOutErr(std::string command)
ELEMENTS_API std::string getEnv(const std::string &var)
get a particular environment variable
Definition: System.cpp:331
ELEMENTS_API Path::Item getConfigurationPath(const T &file_name, bool raise_exception=true)
T transform(T... args)