Skip to content

Commit

Permalink
Issue #250: python-parser/utils: Check if the Python command exists
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKagstrom committed May 15, 2018
1 parent 5ad92d0 commit 2651b54
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/engines/python-engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,21 @@ class PythonEngine : public ScriptEngineBase

bool start(IEventListener &listener, const std::string &executable)
{
IConfiguration &conf = IConfiguration::getInstance();
std::string command = conf.keyAsString("python-command");

std::string kcov_python_pipe_path =
IOutputHandler::getInstance().getOutDirectory() + "kcov-python.pipe";
std::string kcov_python_path =
IOutputHandler::getInstance().getBaseDirectory() + "python-helper.py";

if (!executable_exists_in_path(command))
{
error("Cannot find Python parser '%s'", command.c_str());

return false;
}

if (write_file(python_helper_data.data(), python_helper_data.size(),
"%s", kcov_python_path.c_str()) < 0) {
error("Can't write python helper at %s", kcov_python_path.c_str());
Expand All @@ -85,12 +95,11 @@ class PythonEngine : public ScriptEngineBase
/* Launch the python helper */
m_child = fork();
if (m_child == 0) {
IConfiguration &conf = IConfiguration::getInstance();
const char **argv = conf.getArgv();
unsigned int argc = conf.getArgc();

std::string s = fmt("%s %s ",
conf.keyAsString("python-command").c_str(),
command.c_str(),
kcov_python_path.c_str());
for (unsigned int i = 0; i < argc; i++)
s += "'" + std::string(argv[i]) + "' ";
Expand Down
2 changes: 2 additions & 0 deletions src/include/utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ extern std::string dir_concat(const std::string &dir, const std::string &filenam

extern bool file_exists(const std::string &path);

extern bool executable_exists_in_path(const std::string &path);

extern uint64_t get_file_timestamp(const std::string &path);

extern int concat_files(const char *dst, const char *file_a, const char *file_b);
Expand Down
26 changes: 26 additions & 0 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,32 @@ bool file_exists(const std::string &path)
return out;
}

bool executable_exists_in_path(const std::string &executableName)
{
// Full path to it
if (file_exists(executableName))
{
return true;
}

std::string path = getenv("PATH");
std::vector<std::string> parts = split_string(path, ":");

for (std::vector<std::string>::iterator it = parts.begin();
it != parts.end();
++it)
{
std::string cur = fmt("%s/%s", it->c_str(), executableName.c_str());

if (file_exists(cur))
{
return true;
}
}

return false;
}

void mock_read_file(void *(*callback)(size_t *out_size, const char *path))
{
mocked_read_callback = callback;
Expand Down
14 changes: 14 additions & 0 deletions tests/tools/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ def runTest(self):

assert rv == noKcovRv

class python_can_set_illegal_parser(testbase.KcovTestCase):
def runTest(self):
self.setUp()
rv,o = self.do(testbase.kcov + " --python-parser=python7 " + testbase.outbase + "/kcov " + testbase.sources + "/tests/python/main 5")

assert o.find("Cannot find Python parser 'python7'") != -1

class python_can_set_legal_parser(testbase.KcovTestCase):
def runTest(self):
self.setUp()
rv,o = self.do(testbase.kcov + " --python-parser=python3 " + testbase.outbase + "/kcov " + testbase.sources + "/tests/python/main 5")

assert o.find("Cannot find Python parser 'python3'") == -1

class python_unittest(testbase.KcovTestCase):
def runTest(self):
self.setUp()
Expand Down

0 comments on commit 2651b54

Please sign in to comment.