CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/FWCore/Utilities/src/TestHelper.cc

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