From bc42717371fcb43aa25b929139e839185b20103d Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Wed, 31 Oct 2012 00:23:04 -0400 Subject: [PATCH] symlinks_install (OSX): update library search paths in shell scripts for easier python support --- doc/python.tex | 16 +++++++++++++--- src/mtsgui/mainwindow.cpp | 2 +- src/mtsgui/symlinks_auth.cpp | 6 +++++- src/mtsgui/symlinks_install.c | 26 ++++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/doc/python.tex b/doc/python.tex index 775c1c3e..27e4bad3 100644 --- a/doc/python.tex +++ b/doc/python.tex @@ -1,14 +1,21 @@ \section{Python integration} \label{sec:python} -A recent feature of Mitsuba is a simple Python interface to the renderer API. +A recent feature of Mitsuba is a Python interface to the renderer API. While the interface is still limited at this point, it can already be used for many useful purposes. To access the API, start your Python interpreter and enter \begin{python} import mitsuba \end{python} +\paragraph{Mac OS:} For this to work on MacOS X, you will first have to run the ``\emph{Apple Menu}$\to$\emph{Command-line access}'' menu item from within Mitsuba. +In the unlikely case that you run into shared library loading issues (this is +taken care of by default), you may have to set the \code{LD\_LIBRARY\_PATH} +environment variable before starting Python so that it points to where the +Mitsuba libraries are installed (e.g. the \code{Mitsuba.app/Contents/Frameworks} +directory). +\paragraph{Windows and Linux:} On Windows and \emph{non-packaged} Linux builds, you may have to explicitly specify the required extension search path before issuing the \code{import} command, e.g.: \begin{python} @@ -29,6 +36,9 @@ os.environ['PATH'] = 'path-to-mitsuba-directory' + os.pathsep + os.environ['PATH import mitsuba \end{python} +In rare cases when running on Linux, it may also be necessary to set the +\code{LD\_LIBRARY\_PATH} environment variable before starting Python so that it +points to where the Mitsuba core libraries are installed. For an overview of the currently exposed API subset, please refer to the following page: \url{http://www.mitsuba-renderer.org/api/group__libpython.html}. @@ -64,8 +74,8 @@ classes, function, or entire namespaces when running an interactive Python shell ... \end{shell} The docstrings list the currently exported functionality, as well as C++ and Python signatures, but they -don't document what these functions actually do. The web API documentation is the preferred source for -this information. +don't document what these functions actually do. The web API documentation is +the preferred source of this information. \subsection{Basics} Generally, the Python API tries to mimic the C++ API as closely as possible. diff --git a/src/mtsgui/mainwindow.cpp b/src/mtsgui/mainwindow.cpp index f983982c..716848c0 100644 --- a/src/mtsgui/mainwindow.cpp +++ b/src/mtsgui/mainwindow.cpp @@ -1593,7 +1593,7 @@ void MainWindow::on_actionStartServer_triggered() { void MainWindow::on_actionEnableCommandLine_triggered() { if (QMessageBox::question(this, tr("Enable command line access"), - tr("

If you proceed, Mitsuba will create symbolic links in /usr/bin and /Library/Python/{2.6,2.7}/site-packages, " + tr("

If you proceed, Mitsuba will create symbolic links in /usr/bin and /Library/Python/{2.6,2.7}/site-packages, as well as an entry in .bashrc, " "which enable command line and Python usage. Note that you will have to " "repeat this process every time the Mitsuba application is moved.

" "

Create links?

"), diff --git a/src/mtsgui/symlinks_auth.cpp b/src/mtsgui/symlinks_auth.cpp index 13f6fe37..a0bf914b 100644 --- a/src/mtsgui/symlinks_auth.cpp +++ b/src/mtsgui/symlinks_auth.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace mitsuba { extern std::string __mts_bundlepath(); @@ -30,7 +31,10 @@ bool create_symlinks() { } std::string bundlePath = mitsuba::__mts_bundlepath(); std::string path = bundlePath + "/Contents/MacOS/symlinks_install"; - char *args[] = { const_cast(bundlePath.c_str()), NULL }; + std::ostringstream oss; + oss << getuid(); + std::string uid = oss.str(); + char *args[] = { const_cast(bundlePath.c_str()), const_cast(uid.c_str()), NULL }; FILE *pipe = NULL; flags = kAuthorizationFlagDefaults; status = AuthorizationExecuteWithPrivileges(ref, const_cast(path.c_str()), flags, args, &pipe); diff --git a/src/mtsgui/symlinks_install.c b/src/mtsgui/symlinks_install.c index 6b9d49fb..058c8a7c 100644 --- a/src/mtsgui/symlinks_install.c +++ b/src/mtsgui/symlinks_install.c @@ -3,6 +3,7 @@ #include #include #include +#include void installPython(const char *basedir, const char *version) { char fname[MAXPATHLEN]; @@ -23,6 +24,22 @@ void installPython(const char *basedir, const char *version) { fclose(f); } +void appendShellConfig(const char *basedir, const char *target, const char *fmt, const char *dir) { + char fname[MAXPATHLEN]; + snprintf(fname, sizeof(fname), "%s/%s", basedir, target); + + if (access(fname, R_OK) < 0) + return; + + FILE *f = fopen(fname, "a"); + if (!f) + return; + + fprintf(f, fmt, dir); + + fclose(f); +} + void install(const char *basedir, const char *name) { char fname[MAXPATHLEN]; FILE *f; @@ -51,11 +68,11 @@ void install(const char *basedir, const char *name) { } int main(int argc, char **argv) { - if (argc != 2) { + if (argc != 3) { fprintf(stderr, "Incorrect number of arguments!\n"); return -1; } - + if (setuid(0) != 0) { fprintf(stderr, "setuid(): failed!\n"); return -1; @@ -68,6 +85,11 @@ int main(int argc, char **argv) { install(argv[1], "mtsimport"); installPython(argv[1], "2.6"); installPython(argv[1], "2.7"); + struct passwd *pw = getpwuid(atoi(argv[2])); + + appendShellConfig(pw->pw_dir, ".bashrc", "\nexport LD_LIBRARY_PATH=%s/Contents/Frameworks:$LD_LIBRARY_PATH\n", argv[1]); + appendShellConfig(pw->pw_dir, ".zshrc", "\nexport LD_LIBRARY_PATH=%s/Contents/Frameworks:$LD_LIBRARY_PATH\n", argv[1]); + appendShellConfig(pw->pw_dir, ".cshrc", "\nsetenv LD_LIBRARY_PATH %s/Contents/Frameworks:${LD_LIBRARY_PATH}\n", argv[1]); return 0; }