From 25de3092d41c606dfa5b4b442e0813a7132aabf9 Mon Sep 17 00:00:00 2001 From: JJ Allaire Date: Thu, 19 May 2011 11:18:54 -0700 Subject: [PATCH] refactor REnvironmentPosix in preparation for detection changes --- src/cpp/core/r_util/REnvironmentPosix.cpp | 85 +++++++++++++++-------- 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/src/cpp/core/r_util/REnvironmentPosix.cpp b/src/cpp/core/r_util/REnvironmentPosix.cpp index 7a0776470e3..5fb90e20eb4 100644 --- a/src/cpp/core/r_util/REnvironmentPosix.cpp +++ b/src/cpp/core/r_util/REnvironmentPosix.cpp @@ -311,6 +311,52 @@ std::string resolveRPath(const FilePath& rHomePath, const std::string& path) return resolvedPath; } +bool detectRLocations(const FilePath& rScriptPath, + FilePath* pHomePath, + FilePath* pLibPath, + config_utils::Variables* pScriptVars, + std::string* pErrMsg) +{ + // scan R script for other locations and append them to our vars + Error error = config_utils::extractVariables(rScriptPath, pScriptVars); + if (error) + { + LOG_ERROR(error); + *pErrMsg = "Error reading R script (" + rScriptPath.absolutePath() + + "), " + error.summary(); + return false; + } + + // get r home path + std::string rHome, rLib; + if (!getRHomeAndLibPath(rScriptPath, *pScriptVars, &rHome, &rLib, pErrMsg)) + return false; + + // validate: error if we got no output + if (rHome.empty()) + { + *pErrMsg = "Unable to determine R home directory"; + LOG_ERROR(systemError(boost::system::errc::not_supported, + *pErrMsg, + ERROR_LOCATION)); + return false; + } + + // validate: error if `R RHOME` yields file that doesn't exist + *pHomePath = FilePath(rHome); + if (!pHomePath->exists()) + { + *pErrMsg = "R home path (" + rHome + ") not found"; + LOG_ERROR(pathNotFoundError(*pErrMsg, ERROR_LOCATION)); + return false; + } + + // get lib path + *pLibPath = FilePath(rLib); + + return true; +} + } // anonymous namespace @@ -325,6 +371,8 @@ bool detectREnvironment(const FilePath& ldPathsScript, pErrMsg); } + + bool detectREnvironment(const FilePath& whichRScript, const FilePath& ldPathsScript, const std::string& ldLibraryPath, @@ -371,40 +419,18 @@ bool detectREnvironment(const FilePath& whichRScript, return false; } - // scan R script for other locations and append them to our vars + // detect R locations + FilePath rHomePath, rLibPath; config_utils::Variables scriptVars; - Error error = config_utils::extractVariables(rScriptPath, &scriptVars); - if (error) + if (!detectRLocations(rScriptPath, + &rHomePath, + &rLibPath, + &scriptVars, + pErrMsg)) { - LOG_ERROR(error); - *pErrMsg = "Error reading R script (" + rScriptPath.absolutePath() + - "), " + error.summary(); return false; } - // get r home path - std::string rHome, rLib; - if (!getRHomeAndLibPath(rScriptPath, scriptVars, &rHome, &rLib, pErrMsg)) - return false; - - // validate: error if we got no output - if (rHome.empty()) - { - *pErrMsg = "Unable to determine R home directory"; - LOG_ERROR(systemError(boost::system::errc::not_supported, - *pErrMsg, - ERROR_LOCATION)); - return false; - } - - // validate: error if `R RHOME` yields file that doesn't exist - FilePath rHomePath(rHome); - if (!rHomePath.exists()) - { - *pErrMsg = "R home path (" + rHome + ") not found"; - LOG_ERROR(pathNotFoundError(*pErrMsg, ERROR_LOCATION)); - return false; - } // set R home path pVars->push_back(std::make_pair("R_HOME", rHomePath.absolutePath())); @@ -421,7 +447,6 @@ bool detectREnvironment(const FilePath& whichRScript, scriptVars["R_DOC_DIR"]))); // determine library path (existing + r lib dir + r extra lib dirs) - FilePath rLibPath(rLib); std::string libraryPath = core::system::getenv(kLibraryPathEnvVariable); if (!libraryPath.empty()) libraryPath.append(":");