Skip to content

Commit

Permalink
Find FreeCAD path when used from external Python
Browse files Browse the repository at this point in the history
  • Loading branch information
ianrrees committed Mar 18, 2017
1 parent 7872579 commit da4ea8d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
73 changes: 55 additions & 18 deletions src/App/Application.cpp
Expand Up @@ -2281,27 +2281,64 @@ std::string Application::FindHomePath(const char* sCall)

std::string Application::FindHomePath(const char* call)
{
uint32_t sz = 0;
char *buf;
if (Py_IsInitialized()) {
// If we're initialised from an external Python, then there should be
// a path in the interpreter's sys.path that gets to FreeCAD.so .

auto pySysPath(PySys_GetObject("path"));
if ( PyList_Check(pySysPath) ) {
// pySysPath should be a *PyList of strings - iterate through it
// backwards since the FreeCAD path was likely appended just before
// we were imported.
for (auto i( PyList_Size(pySysPath)-1 ); i >= 0 ; --i) {
auto pyPath( PyList_GetItem(pySysPath, i) );

#if PY_MAJOR_VERSION >= 3
if ( !PyUnicode_Check(pyPath) )
continue;
std::string path( PyUnicode_AsUTF8(pyPath) );
#else
if ( !PyString_Check(pyPath) )
continue;
std::string path( PyString_AsString(pyPath) );
#endif // #if/else PY_MAJOR_VERSION >= 3

// Try to find FreeCAD.so within this path
auto dylibPath( path + "/FreeCAD.so" );
if (access(dylibPath.c_str(), R_OK | X_OK) == 0) {

// ...and the "home" path is one level up from that.
return path + "/../";
}
}
}

_NSGetExecutablePath(NULL, &sz); //function only returns "sz" if first arg is to small to hold value
buf = new char[++sz];
std::cerr << "Couldn't determine path to FreeCAD.so via Python.\n";

if (_NSGetExecutablePath(buf, &sz) == 0) {
char resolved[PATH_MAX];
char* path = realpath(buf, resolved);
delete [] buf;

if (path) {
std::string Call(resolved), TempHomePath;
std::string::size_type pos = Call.find_last_of(PATHSEP);
TempHomePath.assign(Call,0,pos);
pos = TempHomePath.find_last_of(PATHSEP);
TempHomePath.assign(TempHomePath,0,pos+1);
return TempHomePath;
}
} else {
delete [] buf;
uint32_t sz = 0;
char *buf;

// function only returns "sz" if first arg is too small to hold value
_NSGetExecutablePath(NULL, &sz);
buf = new char[++sz];

if (_NSGetExecutablePath(buf, &sz) == 0) {
char resolved[PATH_MAX];
char* path = realpath(buf, resolved);
delete [] buf;

if (path) {
std::string Call(resolved), TempHomePath;
std::string::size_type pos = Call.find_last_of(PATHSEP);
TempHomePath.assign(Call,0,pos);
pos = TempHomePath.find_last_of(PATHSEP);
TempHomePath.assign(TempHomePath,0,pos+1);
return TempHomePath;
}
} else {
delete [] buf;
}
}

return call; // error
Expand Down
7 changes: 5 additions & 2 deletions src/App/Application.h
Expand Up @@ -336,9 +336,12 @@ class AppExport Application
static void ParseOptions(int argc, char ** argv);
/// checks if the environment is allreight
//static void CheckEnv(void);
// search for the home path
/// Search for the FreeCAD home path based on argv[0] or Python interpreter
/*!
* There are multiple implementations of this method per-OS
*/
static std::string FindHomePath(const char* sCall);
/// print the help massage
/// Print the help message
static void PrintInitHelp(void);
/// figure out some things
static void ExtractUserPath();
Expand Down

0 comments on commit da4ea8d

Please sign in to comment.