Skip to content

Commit

Permalink
relocatable wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
seanyen committed May 8, 2020
1 parent 904f2a1 commit a11153e
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions cmake/templates/python_win32_wrapper.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@
#ifdef _WIN32

#include <iostream>
#include <fstream>
#include <string>

#include <windows.h>

static const std::string SHEBANG = "#!";

static std::string dirnameOf(const std::string& fname)
{
size_t pos = fname.find_last_of("\\/");
Expand Down Expand Up @@ -87,20 +92,42 @@ int main(int argc, char* argv[]) try
fprintf(stderr, "[DEBUG] Python script name: %s\n", scriptName.c_str());
#endif

// use quoted string to ensure the correct path is used
return "\"" + scriptName + "\"";
return scriptName;
};

const auto GetPythonExecutable = []() -> std::string
const auto GetPythonExecutable = [](const std::string& exeFullPath) -> std::string
{
std::string pythonExecutable = "@PYTHON_EXECUTABLE@";
std::string scriptName;
scriptName += dirnameOf(exeFullPath);
scriptName += "\\@ARG_SCRIPT_NAME@";

std::ifstream infile(scriptName);

std::string firstLine;
if (std::getline(infile, firstLine) && firstLine.find(SHEBANG) == 0)
{
firstLine.erase(0, SHEBANG.size());
}

// Default to Python executable registered in runtime environment
std::string pythonExecutable = "python.exe";
if (!firstLine.empty() &&
(INVALID_FILE_ATTRIBUTES != GetFileAttributes(firstLine.c_str()))) // check file existence
{
pythonExecutable = firstLine;
}

#if defined(DEBUG)
fprintf(stderr, "[DEBUG] Python executable: %s\n", pythonExecutable.c_str());
#endif

return pythonExecutable;
};

const auto GetDoubleQuotedString = [](const std::string &input) -> std::string
{
// use quoted string to ensure the correct path is used
return "\"" + pythonExecutable + "\"";
return "\"" + input + "\"";
};

const auto ExecuteCommand = [](std::string command, bool printError = false) -> unsigned long
Expand Down Expand Up @@ -169,27 +196,9 @@ int main(int argc, char* argv[]) try
}
#endif

auto pythonExecutable = GetPythonExecutable();
try
{
// check if the Python executable specified at configure time could be found
ExecuteCommand(pythonExecutable + " -c \"import sys; sys.exit(0)\"");
}
catch (...)
{
if (::GetLastError() == ERROR_FILE_NOT_FOUND)
{
// Python executable cannot be found, fall back to use Python executable registered in runtime environment
pythonExecutable = "python";
}
else
{
throw;
}
}

const auto pythonExecutable = GetPythonExecutable(GetCurrentModuleName());
const auto pythonScript = FindPythonScript(GetCurrentModuleName());
std::string command = pythonExecutable + " " + pythonScript;
std::string command = GetDoubleQuotedString(pythonExecutable) + " " + GetDoubleQuotedString(pythonScript);
for (auto i = 1; i < argc; ++i)
{
command += " ";
Expand Down

0 comments on commit a11153e

Please sign in to comment.