Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Related Pages

SctRodDaqVersion.cpp

00001 #include "Sct/Version.h"
00002 #include "Sct/SctNames.h"
00003 #include <iostream>
00004 #include <fstream>
00005 #include <cstring>
00006 #include <sstream>
00007 #include <stdlib.h>
00008 using namespace Sct;
00009 using namespace std;
00010 
00011 
00012 void longVersion() {
00013     cout << "VersionString:" << VersionString << endl;
00014     cout << "MajorVersion: " << MajorVersion << " Minor Version: " << MinorVersion << " Modifier: " << VersionModifier << endl;
00015     cout << "Version: " << Version << endl;
00016     cout << "BuildSystem: " << BuildSystem << endl;
00017     cout << "BuildHost: " << BuildHost << endl;
00018     cout << "BuildDate: " << BuildDate << endl;
00019     cout << "RodDaq Version: " << RodDaqVersion << endl;
00020     cout << "CmtConfig: " << CmtConfig << endl;
00021     cout << "Dataflow Version: " << DataFlowVersion << endl;
00022     cout << "OnlineVersion : " << OnlineVersion << endl;
00023     cout << "RootVersion: " << RootVersion << endl;
00024     cout << "GccVersion: " << GccVersion << endl;    
00025 }
00026 
00027 void shortVersion() {
00028     cout << Version << VersionModifier << endl;
00029 }
00030 
00031 void check_scratch_exists(){
00032   bool ok=true;
00033   string cmd1 = string (" test -d ")+ SctNames::getTempDir();
00034   if (system(cmd1.c_str())!=0){
00035      cerr << "SctRodDaqVersion: ERROR Command failed : " << cmd1 << endl;
00036      ok=false;
00037   }
00038   string cmd2 = string (" test -w ")+ SctNames::getTempDir();
00039   if (system(cmd2.c_str())!=0){
00040      cerr << "SctRodDaqVersion: ERROR Command failed : " << cmd2 << endl;
00041      ok=false;
00042   }
00043   if(!ok){ 
00044     cerr << " .. when testing for scratch directory. " << endl; 
00045     cerr << "please check your environment variable: SCT_SCRATCH_DIR" << endl;
00046     exit(1);
00047   }
00048 }
00049 
00050 void check_equal_command(string cmd, string check_output, string label){
00051   istringstream iss(check_output);
00052   iss>>check_output;
00053   ostringstream oss;  oss << SctNames::getTempDir() << "/SctRodDaqVersion.XXXXXX";
00054   char tempfile_template[oss.str().size()];
00055   char* tempfile = tempfile_template;
00056   strcpy(tempfile, oss.str().c_str());
00057   mkstemp(tempfile);
00058 
00059   string fullcmd=cmd;  
00060   fullcmd += " > "; fullcmd += tempfile;
00061   if (system (fullcmd.c_str() ) !=0 ) {
00062     cerr << "Error executing command :" << cmd << endl;
00063     exit(1);
00064   }
00065 
00066   string output; ifstream f(tempfile); f>>output;
00067   system( (string("rm -f ")+tempfile).c_str() );
00068 
00069   if (output != check_output) {
00070     cerr << "ERROR: SctRodDaqVersion likely wrong version of external software \n The output of the command `" << cmd << "'" 
00071      << " was : `" << output << "'" << endl 
00072      << "... but I think I was built with " << label << " = `" << check_output<< "'" << endl;
00073     exit(1);
00074   }else{
00075     cout << "SctRodDaqVersion: Checked " << label << "\t=\t" << check_output << endl;
00076   }
00077 }
00078 
00079 void checkVersion(){
00080   check_scratch_exists();
00081   check_equal_command ( string ("echo $CMTCONFIG"), CmtConfig, "CmtConfig   ");
00082   check_equal_command ( string ("echo $CMTRELEASE"), OnlineVersion, "OnlineVersion");
00083   check_equal_command ( string ("echo $DF_INST_PATH | sed -e \"s@.*/\\\(DF-[0-9]\\+-[0-9]\\+-[0-9]\\+\\).*@\\1@\""), DataFlowVersion, "DataFlow");
00084   check_equal_command ( string ("root-config --version"), RootVersion, "RootVersion");
00085   check_equal_command ( string ("gcc -dumpversion"), GccVersion, "GccVersion   ");
00086   exit(0);
00087 }
00088 
00089 void helpMessage() {
00090     cout << "Prints out version information about the SctRodDaq system." << endl << endl;
00091     cout << "Usage: SctRodDaqVersion <options>" << endl;
00092     cout << "Possible options are: " << endl;
00093     cout << "\t-h, --help\t\tPrint this help message" << endl;
00094     cout << "\t-l, --long\t\tPrint out all version information" << endl;
00095     cout << "\t-s, --short\t\tPrint out short version information" << endl;
00096     cout << "\t-c, --check\t\tCheck versions" << endl;
00097     cout << endl << "If no option is given, the default is -s" << endl;
00098 }
00099 
00100 void handleShortArg(char arg) {
00101     switch (arg) {
00102     case 'h':
00103     helpMessage();
00104     break;
00105     case 'l':
00106     longVersion();
00107     break;
00108     case 's':
00109     shortVersion();
00110     break;
00111     case 'c':
00112     checkVersion();
00113     default:
00114     cout << "Argument -" << arg << " not recognized" << endl;
00115     }
00116 }
00117 
00118 void handleLongArg(const char* arg) {
00119     if (strcmp(arg, "--help") == 0) return handleShortArg('h');
00120     if (strcmp(arg, "--long") == 0) return handleShortArg('l');
00121     if (strcmp(arg, "--short") == 0) return handleShortArg('s');
00122     if (strcmp(arg, "--check") == 0) return handleShortArg('c');
00123     cout << "Argument " << arg << " not recognized" << endl;
00124     helpMessage();
00125 }
00126 
00127 void handleArg(const char* arg) {
00128     if (arg[0] != '-') {
00129     cout << "Argument " << arg << " not recognized" << endl;
00130     helpMessage();
00131     return;
00132     }
00133     if (strlen(arg) == 2) handleShortArg(arg[1]);
00134     else handleLongArg(arg);
00135 }
00136 
00137 int main(int argc, char** argv) {
00138     //Handle args
00139     switch (argc) {
00140     case 1:
00141     shortVersion();
00142     break;
00143     case 2:
00144     handleArg(argv[1]);
00145     break;
00146     default:
00147     helpMessage();
00148     }
00149     return 0;
00150 }

Generated on Fri Sep 16 18:01:58 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5