PLearn 0.1
|
00001 00002 // -*- C++ -*- 00003 00004 // RunCommand.cc 00005 // 00006 // Copyright (C) 2003 Pascal Vincent 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: RunCommand.cc 10184 2009-05-07 14:55:53Z nouiz $ 00038 ******************************************************* */ 00039 00041 #include "RunCommand.h" 00042 #include <plearn/base/general.h> 00043 #include <plearn/io/fileutils.h> 00044 #include <plearn/base/plerror.h> 00045 #include <plearn/base/stringutils.h> 00046 #include <plearn/base/Object.h> 00047 #include <plearn/sys/Popen.h> 00048 00049 #include <plearn/io/PyPLearnScript.h> 00050 #include <plearn/io/openString.h> 00051 #include <plearn/io/openFile.h> 00052 #include <plearn/io/pl_log.h> 00053 00054 #include <algorithm> 00055 00056 namespace PLearn { 00057 using namespace std; 00058 00060 PLearnCommandRegistry RunCommand::reg_(new RunCommand); 00061 00063 void RunCommand::run(const vector<string>& args) 00064 { 00065 const vector<string>* the_args = &args; 00066 vector<string> args_augmented; 00067 PPath scriptfile = args[0]; 00068 if (!isfile(scriptfile)) { 00069 // There is no file with this exact name. Maybe there are parameters 00070 // appended to the name? 00071 string base; 00072 map<string, string> params; 00073 parseBaseAndParameters(scriptfile, base, params); 00074 if (!isfile(base)) 00075 PLERROR("Non-existent script file: %s\n",scriptfile.c_str()); 00076 // Add new arguments. 00077 args_augmented = args; 00078 map<string, string>::const_iterator it = params.begin(); 00079 for (; it != params.end(); it++) 00080 args_augmented.push_back(it->first + "=" + it->second); 00081 the_args = &args_augmented; 00082 scriptfile = base; 00083 } 00084 00085 string extension = scriptfile.extension(); 00086 string script; 00087 00088 PP<PyPLearnScript> pyplearn_script; 00089 PStream in; 00090 00091 if (extension == "pyplearn") 00092 { 00093 // Make a copy of args with the first argument (the name of the script) 00094 // removed, leaving the first argument to the script at index 0. 00095 vector<string> pyplearn_args(the_args->size()-1); 00096 copy(the_args->begin() + 1, the_args->end(), pyplearn_args.begin()); 00097 00098 pyplearn_script = PyPLearnScript::process(scriptfile, pyplearn_args); 00099 script = pyplearn_script->getScript(); 00100 00101 // When we call the pyplearn script with either 00102 // --help or --dump, everything will already have been done by 00103 // the time the PyPLearnScript is built. 00104 if ( script == "" ) 00105 return; 00106 00107 PL_Log::instance().enableNamedLogging(pyplearn_script->module_names); 00108 PL_Log::instance().verbosity(pyplearn_script->verbosity); 00109 00110 in = openString( script, PStream::plearn_ascii ); 00111 } 00112 else if(extension=="plearn") // perform plearn macro expansion 00113 { 00114 map<string, string> vars; 00115 // populate vars with the arguments passed on the command line 00116 for (unsigned int i=1; i<the_args->size(); i++) 00117 { 00118 string option = (*the_args)[i]; 00119 // Skip --foo command-lines options. 00120 if (option.size() < 2 || option.substr(0, 2) != "--") 00121 { 00122 pair<string, string> name_val = split_on_first(option, "="); 00123 vars[name_val.first] = name_val.second; 00124 } 00125 } 00126 00127 script = readFileAndMacroProcess(scriptfile, vars); 00128 in = openString( script, PStream::plearn_ascii ); 00129 } 00130 else if(extension=="psave") // do not perform plearn macro expansion 00131 { 00132 in = openFile(scriptfile, PStream::plearn_ascii); 00133 } 00134 else 00135 PLERROR("Invalid extension for script file. Must be one of .pyplearn .plearn .psave"); 00136 00137 while ( in.good() ) 00138 { 00139 PP<Object> o = readObject(in); 00140 o->run(); 00141 in.skipBlanksAndCommentsAndSeparators(); 00142 } 00143 00144 if ( pyplearn_script.isNotNull() ) 00145 pyplearn_script->close(); 00146 } 00147 00148 } // end of namespace PLearn 00149 00150 00151 /* 00152 Local Variables: 00153 mode:c++ 00154 c-basic-offset:4 00155 c-file-style:"stroustrup" 00156 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00157 indent-tabs-mode:nil 00158 fill-column:79 00159 End: 00160 */ 00161 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :