#include <iostream> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <string> #include <ctime> #include <fstream> #include <dirent.h> #include <unistd.h> #ifndef PROJECT_NAME #define PROJECT_NAME "UNDEF_PROJECT" #endif //terminal colors #define RESET "\033[0m" #define BLACK "\033[30m" /* Black */ #define RED "\033[31m" /* Red */ #define GREEN "\033[32m" /* Green */ #define YELLOW "\033[33m" /* Yellow */ #define BLUE "\033[34m" /* Blue */ #define MAGENTA "\033[35m" /* Magenta */ #define CYAN "\033[36m" /* Cyan */ #define WHITE "\033[37m" /* White */ #define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ #define BOLDRED "\033[1m\033[31m" /* Bold Red */ #define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ #define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ #define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ #define BOLDWHITE "\033[1m\033[37m" /* Bold White */ using namespace std; bool DirectoryExists( const char* pzPath ) { if ( pzPath == NULL) return false; DIR *pDir; bool bExists = false; pDir = opendir (pzPath); if (pDir != NULL) { bExists = true; (void) closedir (pDir); } return bExists; } string GetMachineID() { string id; ifstream machineIdFile; machineIdFile.open("/etc/machine-id"); if(machineIdFile.good()) { getline(machineIdFile, id); cout << BOLDGREEN << "This machine ID is: " << RESET << GREEN << id << RESET << endl; } else { id = "NULL"; cout << BOLDYELLOW << "Machine ID was not identified, " << RESET << YELLOW << "value \"NULL\" will be used instead" << RESET<< endl; } return id; } void ShowError(string cmd) { std::cout << BOLDRED << " --- ERROR: " << RESET << RED << "Command \"" << cmd << "\" returned bad value" << RESET << std::endl; std::cout << BOLDRED << "Backing data to git failed" << RESET << endl; std::cout << BOLDYELLOW << "Error details are in gitlog.txt:" << RESET << std::endl; system("cat gitlog.txt"); } void processGit(string filename) { //get time for backed-up file name time_t curr_time; tm * curr_tm; time(&curr_time); curr_tm = localtime(&curr_time); char nf[100]; string oldname = filename; oldname.erase(filename.find(".json"),5); strftime(nf, 100, "_%e-%m-%Y_%Hh%Mm%Ss", curr_tm); string newfile = nf; newfile = oldname + newfile + "__" + GetMachineID() + ".json"; //we must pull the submodule first to ensure all is up to date if (system("git --git-dir=./caenslowcontrol_json/.git pull > gitlog.txt") != 0) { ShowError("git --git-dir=./caenslowcontrol_json/.git pull > gitlog.txt"); return; } sleep(1); //check if project folder exists string path = PROJECT_NAME; path = "./caenslowcontrol_json/" + path; string cmd = "mkdir " + path; if(!DirectoryExists(path.c_str())) system(cmd.c_str()); //copy to project folder cmd = "cp " + filename + " " + path + "/" + newfile; if (system(cmd.c_str()) != 0) { ShowError(cmd); return; } //git add path = PROJECT_NAME; cmd = "git --git-dir=./caenslowcontrol_json/.git add " + path + "/" + newfile + " >> gitlog.txt"; if (system(cmd.c_str()) != 0) { ShowError(cmd); return; } //git commit string commit = "\"autopush of " + newfile + "\""; cmd = "git --git-dir=./caenslowcontrol_json/.git commit -m " + commit + " >> gitlog.txt"; if (system(cmd.c_str()) != 0) { ShowError(cmd); return; } //git push cmd = "git --git-dir=./caenslowcontrol_json/.git push >> gitlog.txt"; if (system(cmd.c_str()) != 0) { ShowError(cmd); return; } }