00001
00002
00003
00004 #include <cerrno>
00005 #include <cstdlib>
00006 #include <exception>
00007 #include <iostream>
00008 #include <string>
00009
00010 #include <sys/wait.h>
00011 #include <unistd.h>
00012
00013 #include "boost/filesystem/convenience.hpp"
00014 #include "boost/filesystem/path.hpp"
00015
00016 #include "FWCore/Utilities/interface/EDMException.h"
00017 #include "FWCore/Utilities/interface/Exception.h"
00018 #include "FWCore/Utilities/interface/TestHelper.h"
00019
00020 namespace bf=boost::filesystem;
00021
00022
00023 extern char** environ;
00024
00025 int run_script(const std::string& shell, const std::string& script)
00026 {
00027 pid_t pid;
00028 int status=0;
00029
00030 if ((pid=fork())<0)
00031 {
00032 std::cerr << "fork failed, to run " << script << std::endl;;
00033 return -1;
00034 }
00035
00036 if (pid==0)
00037 {
00038 execlp(shell.c_str(), "sh", "-c", script.c_str(), 0);
00039 std::cerr <<"child failed becuase '"<<strerror(errno)<<"'\n";
00040 _exit(127);
00041 }
00042 else
00043 {
00044 while(waitpid(pid,&status,0)<0)
00045 {
00046 if (errno!=EINTR)
00047 {
00048 std::cerr <<"child process failed "<<strerror(errno)<<"\n";
00049 status=-1;
00050 break;
00051 } else {
00052 if( WIFSIGNALED(status) ) {
00053 std::cerr << "child existed because of a signal "<<WTERMSIG(status)<<"\n";
00054 }
00055 }
00056 }
00057 if( WIFSIGNALED(status) ) {
00058 std::cerr << "child existed because of a signal "<<WTERMSIG(status)<<"\n";
00059 }
00060 if(WIFEXITED(status)) {
00061 }
00062
00063 }
00064 return status;
00065 }
00066
00067
00068
00069 int do_work(int argc, char* argv[])
00070 {
00071 bf::path currentPath(bf::initial_path().string(), bf::no_check);
00072
00073 if (argc<4)
00074 {
00075 std::cout << "Usage: " << argv[0] << " shell subdir script1 script2 ... scriptN\n\n"
00076 << "where shell is the path+shell (e.g., /bin/bash) intended to run the scripts\n"
00077 << "and subdir is the subsystem/package/subdir in which the scripts are found\n"
00078 << "(e.g., FWCore/Utilities/test)\n"
00079 << std::endl;
00080
00081 std::cout << "Current directory is: " << currentPath.native_directory_string() << '\n';
00082 std::cout << "Current environment:\n";
00083 std::cout << "---------------------\n";
00084 for (int i = 0; environ[i] != 0; ++i) std::cout << environ[i] << '\n';
00085 std::cout << "---------------------\n";
00086 std::cout << "Executable name: " << argv[0] << '\n';
00087 return -1;
00088 }
00089
00090 for (int i = 0; i < argc; ++i)
00091 {
00092 std::cout << "argument " << i << ": " << argv[i] << '\n';
00093 }
00094
00095
00096 std::string shell(argv[1]);
00097 std::cerr << "shell is: " << shell << '\n';
00098
00099 std::cout << "Current directory is: " << currentPath.native_directory_string() << '\n';
00100
00101
00102 const char* topdir = getenv("SCRAMRT_LOCALRT");
00103 if (!topdir) topdir = getenv("LOCALRT");
00104
00105 const char* arch = getenv("SCRAM_ARCH");
00106
00107 if ( !arch )
00108 {
00109
00110 bf::path exepath(argv[0], bf::no_check);
00111 std::string maybe_arch = exepath.branch_path().leaf();
00112 if (setenv("SCRAM_ARCH", maybe_arch.c_str(), 1) != 0)
00113 {
00114 std::cerr << "SCRAM_ARCH not set and attempt to set it failed\n";
00115 return -1;
00116 }
00117 }
00118
00119 int rc=0;
00120
00121 if (!topdir)
00122 {
00123 std::cout << "Neither SCRAMRT_LOCALRT nor LOCALRT is not defined" << std::endl;;
00124 return -1;
00125 }
00126
00127
00128 std::string testdir(topdir); testdir += "/src/"; testdir += argv[2];
00129 std::string tmpdir(topdir); tmpdir+="/tmp/"; tmpdir+=arch;
00130 std::string testbin(topdir); testbin+="/test/"; testbin+=arch;
00131
00132 std::cout << "topdir is: " << topdir << '\n';
00133 std::cout << "testdir is: " << testdir << '\n';
00134 std::cout << "tmpdir is: " << tmpdir << '\n';
00135 std::cout << "testbin is: " << testbin << '\n';
00136
00137
00138 if (setenv("LOCAL_TEST_DIR",testdir.c_str(),1)!=0)
00139 {
00140 std::cerr << "Could not set LOCAL_TEST_DIR to " << testdir << std::endl;;
00141 return -1;
00142 }
00143 if (setenv("LOCAL_TMP_DIR",tmpdir.c_str(),1)!=0)
00144 {
00145 std::cerr << "Could not set LOCAL_TMP_DIR to " << tmpdir << std::endl;;
00146 return -1;
00147 }
00148 if (setenv("LOCAL_TOP_DIR",topdir,1)!=0)
00149 {
00150 std::cerr << "Could not set LOCAL_TOP_DIR to " << topdir << std::endl;;
00151 return -1;
00152 }
00153 if (setenv("LOCAL_TEST_BIN",testbin.c_str(),1)!=0)
00154 {
00155 std::cerr << "Could not set LOCAL_TEST_BIN to " << testbin << std::endl;;
00156 return -1;
00157 }
00158
00159 testdir+="/";
00160
00161 for(int i=3; i<argc && rc==0; ++i)
00162 {
00163 std::string scriptname(testdir);
00164 scriptname += argv[i];
00165 std::cout << "Running script: " << scriptname << std::endl;
00166 rc = run_script(shell, scriptname);
00167 }
00168
00169 std::cout << "status = " << rc << std::endl;;
00170 return rc == 0 ? 0 : -1;
00171 }
00172
00173 int ptomaine(int argc, char* argv[])
00174 {
00175 int rc = 1;
00176 try
00177 {
00178 rc = do_work(argc, argv);
00179 }
00180 catch ( edm::Exception& x )
00181 {
00182 std::cerr << "Caught an edm::Exception in "
00183 << argv[0] << '\n'
00184 << x;
00185 }
00186 catch ( cms::Exception& x )
00187 {
00188 std::cerr << "Caught a cms::Exception in "
00189 << argv[0] << '\n'
00190 << x;
00191 }
00192 catch ( std::exception& x )
00193 {
00194 std::cerr << "Caught a std::exception in "
00195 << argv[0] << '\n'
00196 << x.what();
00197 }
00198 catch (...)
00199 {
00200 std::cerr << "Caught an unknown exception in "
00201 << argv[0];
00202 }
00203 return rc;
00204 }