Add Support For Configuring Addon Python Interpreter Version (#2591)

This commit is contained in:
Sam Lane 2020-04-05 09:57:28 +01:00 committed by GitHub
parent f01783238e
commit 60f9fefcd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 5 deletions

View File

@ -133,6 +133,9 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
else if (std::strncmp(argv[i], "--addon=", 8) == 0)
mSettings->addons.emplace_back(argv[i]+8);
else if (std::strncmp(argv[i],"--addon-python=", 15) == 0)
mSettings->addonPython.assign(argv[i]+15);
else if (std::strcmp(argv[i], "--clang") == 0)
mSettings->clang = true;
@ -973,6 +976,9 @@ void CmdLineParser::printHelp()
"Options:\n"
" --addon=<addon>\n"
" Execute addon. i.e. cert.\n"
" --addon-python=<python interpreter>\n"
" Use this python interpreter to run addons\n"
" If not present, defaults to \"python\"\n"
" --cppcheck-build-dir=<dir>\n"
" Analysis output directory. Useful for various data.\n"
" Some possible usages are; whole program analysis,\n"

View File

@ -67,6 +67,7 @@ namespace {
std::string name;
std::string scriptFile;
std::string args;
std::string python;
static std::string getFullPath(const std::string &fileName, const std::string &exename) {
if (Path::fileExists(fileName))
@ -131,16 +132,36 @@ namespace {
args += " " + v.get<std::string>();
}
if(obj.count("python")) {
// Python was defined in the config file
if (obj["python"].is<picojson::array>()){
return "Loading " + fileName +" failed. python must not be an array.";
}
python = obj["python"].get<std::string>();
}
else{
python = "";
}
return getAddonInfo(obj["script"].get<std::string>(), exename);
}
};
}
static std::string executeAddon(const AddonInfo &addonInfo, const std::string &dumpFile)
{
static std::string executeAddon(const AddonInfo &addonInfo,
const std::string &defaultPythonExe,
const std::string &dumpFile) {
std::string pythonExe = (addonInfo.python != "") ? addonInfo.python : defaultPythonExe;
if(pythonExe.find(" ") != std::string::npos){
// popen strips the first quote. Needs 2 sets to fully quote.
pythonExe = "\"\"" + pythonExe + "\"\"";
}
// Can python be executed?
{
const std::string cmd = "python --version 2>&1";
const std::string cmd = pythonExe + " --version 2>&1";
#ifdef _WIN32
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);
@ -157,7 +178,7 @@ static std::string executeAddon(const AddonInfo &addonInfo, const std::string &d
throw InternalError(nullptr, "Failed to execute '" + cmd + "' (" + result + ")");
}
const std::string cmd = "python \"" + addonInfo.scriptFile + "\" --cli" + addonInfo.args + " \"" + dumpFile + "\" 2>&1";
const std::string cmd = pythonExe + " \"" + addonInfo.scriptFile + "\" --cli" + addonInfo.args + " \"" + dumpFile + "\" 2>&1";
#ifdef _WIN32
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);
@ -778,7 +799,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
mExitCode = 1;
continue;
}
const std::string results = executeAddon(addonInfo, dumpFile);
const std::string results =
executeAddon(addonInfo, mSettings.addonPython, dumpFile);
std::istringstream istr(results);
std::string line;

View File

@ -30,6 +30,7 @@ const char Settings::SafeChecks::XmlExternalVariables[] = "external-variables";
Settings::Settings()
: mEnabled(0),
addonPython("python"),
checkConfiguration(false),
checkLibrary(false),
checkHeaders(true),

View File

@ -73,6 +73,9 @@ public:
std::list<std::string> addons;
/** @brief Path to the python interpreter to be used to run addons. */
std::string addonPython;
/** @brief Paths used as base for conversion to relative paths. */
std::vector<std::string> basePaths;