📄 scriptserver.cpp
字号:
Eval(s.str());}voidScriptServer::CreateVariable(const string &varName, const string &value){ // create a string with: "createVariable 'ns', 'varName', 'value'" stringstream s; s << "createVariable('" << varName << "', '" << value << "')"; Eval(s.str());}boolScriptServer::ParseVarName(const string& varName, string& nameSpace, string& name){ stringstream ss(varName); string current; vector<string> tokens; // segment varName while(! ss.eof()) { getline(ss, current,'.'); if (current.size()) { tokens.push_back(current); } } if (tokens.size() != 2) { return false; } nameSpace = tokens[0]; name = tokens[1]; return ( (nameSpace.size() >= 1) && (nameSpace[0] >= 'A') && (nameSpace[0] <= 'Z') && (name.size() >= 1) && (name[0] >= 'A') && (name[0] <= 'Z') );}boolScriptServer::ExistsVariable(const string &varName){ return (! GetVariable(varName).IsNil());}GCValueScriptServer::GetVariable(const string &varName){ string nameSpace; string name; if (! ParseVarName(varName,nameSpace,name)) { return GCValue(); } GCValue v; if (nameSpace != "") { // get namespace class GCValue ns = rb_const_get(rb_cObject, rb_intern(nameSpace.c_str())); if (! ns.IsNil()) { // get member variable of namespace object ID var = rb_intern(name.c_str()); int error; RbArguments arg(ns.Get(), var, 0, 0); v = rb_protect( RbFuncallWrap, reinterpret_cast<VALUE>(&arg), &error ); if (error) { GetLog()->Debug() << "(ScriptServer) Ruby ERROR: " << RbGetError() << "\n"; v = Qnil; } } } else { v = rb_const_get(rb_cObject, rb_intern(name.c_str())); } return v;}boolScriptServer::GetVariable(const string &varName, int &value){ return GetVariable(varName).GetInt(value);}boolScriptServer::GetVariable(const std::string &varName, float &value){ return GetVariable(varName).GetFloat(value);}boolScriptServer::GetVariable(const string &varName, bool &value){ return GetVariable(varName).GetBool(value);}boolScriptServer::GetVariable(const string &varName, string &value){ return GetVariable(varName).GetString(value);}boost::shared_ptr<CoreContext>ScriptServer::GetContext() const{ return gMyPrivateContext;}boolScriptServer::ConstructInternal(){ if (! Leaf::ConstructInternal()) { return false; } gMyPrivateContext = GetCore()->CreateContext(); return true;}voidScriptServer::SetInitRelPathPrefix(const std::string &relPathPrefix){ mRelPathPrefix = relPathPrefix;}ScriptServer::ERunScriptErrorTypeScriptServer::RunInitScriptInternal(const string &sourceDir, const string &name, bool copy, const string& destDir){ // run the init script in the sourceDir string sourcePath = sourceDir + "/" + name; GetLog()->Debug() << "(ScriptServer) Running " << sourcePath << "... "; shared_ptr<salt::StdFile> file(new(salt::StdFile)); if (! file->Open(sourcePath.c_str())) { GetLog()->Debug() << "failed (script not found)" << endl; return eNotFound; } else if (! Run(file)) { GetLog()->Debug() << "failed (error in script" << endl; return eError; } else { GetLog()->Debug() << "ok" << endl; } // copy it to the destDir if (! copy) { return eOK; } string destPath = destDir + "/" + name; GetLog()->Normal() << "Copying " << sourcePath << " to " << destPath << endl; stringstream s; s << "cp " << sourcePath << " " << destPath; system(s.str().c_str()); return eOK;}boolScriptServer::GetDotDirName(string& dotDir){ if (mDotName == "") { GetLog()->Warning() << "(ScriptServer) WARNING: Dot directory name unset.\n"; return false; } char* home = getenv("HOME"); if (!home) { GetLog()->Warning() << "(ScriptServer) WARNING: $HOME is unset.\n"; return false; } dotDir = string(home) + "/" + mDotName; return true;}boolScriptServer::CreateDotDir(const string& dotDir){ char cwd[PATH_MAX+1]; if (getcwd(cwd,sizeof(cwd)) == NULL) { GetLog()->Error() << "(ScriptServer) ERROR: Cannot get current directory\n"; return false; } if (chdir(dotDir.c_str()) == 0) { // dot dir exists; change back to original directory chdir(cwd); return true; } // dot dir is not existent, try to create it if (mkdir(dotDir.c_str(),0777) != 0) { GetLog()->Error() << "(ScriptServer) ERROR: Cannot create directory '" << dotDir << "'\n"; return false; } GetLog()->Normal() << "(ScriptServer) Created Directory '" << dotDir << "'\n"; return true;}boolScriptServer::RunInitScript(const string &fileName, const string &relPath, EInitScriptType type){ string dotDir; bool validDotDir = (type == IS_USERLOCAL) && GetDotDirName(dotDir) && CreateDotDir(dotDir); // some macro magic (not at all) string pkgdatadir = PREFIX "/share/" PACKAGE_NAME; //std::cout << "dotDir = " << dotDir << std::endl; //std::cout << "pkgdatadir = " << pkgdatadir << std::endl; //std::cout << "filename = " << fileName << std::endl; ERunScriptErrorType result; if (validDotDir) { result = RunInitScriptInternal(dotDir, fileName, false); } if (result == eOK) { GetLog()->Debug() << "(ScriptServer) : Ran init script '" << dotDir << "/" << fileName << "'\n"; return true; } if (result == eNotFound) { GetLog()->Debug() << "(ScriptServer) : Did not find init script '" << dotDir << "/" << fileName << "'\n"; } else if (result == eError) { GetLog()->Error() << "(ScriptServer) ERROR: Found error in init script '" << dotDir << "/" << fileName << "'\n"; return false; } // result = RunInitScriptInternal(pkgdatadir, fileName, validDotDir, dotDir); if (result == eOK) { GetLog()->Debug() << "(ScriptServer) : Ran init script '" << pkgdatadir << "/" << fileName << "'\n"; return true; } if (result == eNotFound) { GetLog()->Debug() << "(ScriptServer) : Did not find init script '" << pkgdatadir << "/" << fileName << "'\n"; } else if (result == eError) { GetLog()->Error() << "(ScriptServer) ERROR: Found error in init script '" << pkgdatadir << "/" << fileName << "'\n"; } result = RunInitScriptInternal(mRelPathPrefix+relPath, fileName, validDotDir, dotDir); if (result == eNotFound) { GetLog()->Error() << "(ScriptServer) ERROR: Cannot locate init script '" << fileName << "'\n"; } else if (result == eError) { GetLog()->Error() << "(ScriptServer) ERROR: Found error in init script '" << mRelPathPrefix+relPath << "/" << fileName << "'\n"; } return (result == eOK);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -