From 7cf0038d21ef8ecea2b3c726af5b4d29a0293464 Mon Sep 17 00:00:00 2001
From: Marco Biasini <marco.biasini@unibas.ch>
Date: Sun, 3 Jun 2012 11:52:51 +0200
Subject: [PATCH] make directory layout more pythonic

Instead of creating a lib/openstructure subdirectory containing
all ost modules, use lib/python$MAJOR.$MINOR/site-packages. This
follows more closely what other projects are doing. The main
motivation, however is to simplify deploment of tools built on
OpenStructure.
---
 cmake_support/OST.cmake  |  4 ++--
 deployment/macos/deps.py | 15 ++++++++-------
 doc/conf/conf.py         |  9 ++++-----
 modules/gui/src/gosty.cc | 13 ++++++++++---
 scripts/CMakeLists.txt   |  6 +++---
 scripts/init_cl.py       |  5 +++--
 scripts/ost.in           |  2 +-
 scripts/ost_config.in    |  2 +-
 8 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/cmake_support/OST.cmake b/cmake_support/OST.cmake
index 646adc2cf..53b0d8025 100644
--- a/cmake_support/OST.cmake
+++ b/cmake_support/OST.cmake
@@ -496,9 +496,9 @@ macro(pymod)
     return()
   endif()
   if (_ARG_OUTPUT_DIR)
-    set(PYMOD_DIR "openstructure/${_ARG_OUTPUT_DIR}")
+    set(PYMOD_DIR "python${PYTHON_VERSION}/site-packages/${_ARG_OUTPUT_DIR}")
   else()
-    set(PYMOD_DIR "openstructure/${_ARG_PREFIX}/${_ARG_NAME}")
+    set(PYMOD_DIR "python${PYTHON_VERSION}/site-packages/${_ARG_PREFIX}/${_ARG_NAME}")
   endif()
   set(_LIB_NAME ${_ARG_PREFIX}_${_ARG_NAME})
   set(PYMOD_STAGE_DIR "${LIB_STAGE_PATH}/${PYMOD_DIR}")
diff --git a/deployment/macos/deps.py b/deployment/macos/deps.py
index 654744721..6c699b92e 100644
--- a/deployment/macos/deps.py
+++ b/deployment/macos/deps.py
@@ -27,7 +27,7 @@ def _deps_for_lib(lib, pool, recursive=True):
   return
 
 def collect_deps(stage_dir, components, binaries, libexec_binaries,
-                 site_packages, site_packages_dir):
+                 site_packages, site_packages_dir, libexec_path='openstructure'):
   """
   Collect the dependencies for the given components and returns a list of 
   frameworks/libraries that the component depends on.
@@ -50,7 +50,7 @@ def collect_deps(stage_dir, components, binaries, libexec_binaries,
     if bin_name not in pool:
       _deps_for_lib(bin_name, pool)
   for bin in libexec_binaries:
-    bin_name=os.path.abspath(os.path.join(stage_dir, 'libexec/openstructure',
+    bin_name=os.path.abspath(os.path.join(stage_dir, 'libexec', libexec_path,
                                           bin))
     if not os.path.exists(bin_name):
       print 'WARNING:', bin_name, 'does not exist'
@@ -239,7 +239,8 @@ def make_standalone(stage_dir, outdir, no_includes, force_no_rpath=False,
   # when running in non-gui mode, we are most likely missing the boost
   # python library. Let's add it to the list of dependencies by
   # inspecting "_ost_base.so".
-  _deps_for_lib(os.path.join(stage_dir, 'lib/openstructure/ost/_ost_base.so'),
+  pymod_dir='lib/python%d.%d/site-packages' % sys.version_info[0:2]
+  _deps_for_lib(os.path.join(stage_dir, pymod_dir, 'ost/_ost_base.so'),
                 deps, recursive=False)
   print 'copying dependencies'
   copy_deps(deps, outdir)
@@ -249,8 +250,8 @@ def make_standalone(stage_dir, outdir, no_includes, force_no_rpath=False,
   print 'copying binaries'
   copy_binaries(stage_dir, outdir, binaries, scripts, 'bin')
   print 'copying pymod'
-  shutil.copytree(os.path.join(stage_dir, 'lib/openstructure'), 
-                  os.path.join(outdir, 'lib/openstructure'))
+  shutil.copytree(os.path.join(stage_dir,pymod_dir),
+                  os.path.join(outdir, pymod_dir))
   copied_py_framework=False
   non_std_python=False
   if os.path.exists(os.path.join(outdir, 'lib/Python.framework')):
@@ -310,9 +311,9 @@ def make_standalone(stage_dir, outdir, no_includes, force_no_rpath=False,
   for sp in SITE_PACKAGES:
     src=get_python_module_path(sp)
     if os.path.isdir(src):
-      shutil.copytree(src, os.path.join(outdir, 'lib/openstructure', sp))
+      shutil.copytree(src, os.path.join(outdir, pymod_dir, sp))
     else:
-      shutil.copy(src, os.path.join(outdir, 'lib/openstructure', sp))
+      shutil.copy(src, os.path.join(outdir, pymod_dir, sp))
   print 'updating link commands of python shared objects'
   os.path.walk(os.path.join(outdir, 'lib'), 
                update_pymod_shared_objects, 
diff --git a/doc/conf/conf.py b/doc/conf/conf.py
index b2d284a53..e87782b4c 100644
--- a/doc/conf/conf.py
+++ b/doc/conf/conf.py
@@ -13,13 +13,12 @@
 
 import sys, os
 
-# If extensions (or modules to document with autodoc) are in another directory,
-# add these directories to sys.path here. If the directory is relative to the
-# documentation root, use os.path.abspath to make it absolute, like shown here.
+site_packs='python%d.%d/site-packages' % sys.version_info[0:2]
 sys.path.append(os.path.join(os.path.abspath('../..'), 
-                             'stage/lib/openstructure'))
+                             'stage/lib', site_packs))
 sys.path.append(os.path.join(os.path.abspath('../..'), 
-                             'stage/lib64/openstructure'))
+                             'stage/lib64', site_packs))
+print site_packs
 import ost
 # -- General configuration -----------------------------------------------------
 
diff --git a/modules/gui/src/gosty.cc b/modules/gui/src/gosty.cc
index 49750b9dc..670c454c1 100644
--- a/modules/gui/src/gosty.cc
+++ b/modules/gui/src/gosty.cc
@@ -108,13 +108,20 @@ String get_ost_root()
 
 void setup_python_search_path(const String& root, PythonInterpreter& pi)
 {
+  std::stringstream site_pkgs;
+  site_pkgs << "python" << PY_MAJOR_VERSION << "." << PY_MINOR_VERSION;
 #ifdef _MSC_VER
-  pi.AppendModulePath(QString::fromStdString(root+"\\lib\\openstructure"));
+  pi.AppendModulePath(QString::fromStdString(root+"\\lib\\"+site_pkgs.str()
+                                             +"\\site-packages"));
 #else  
 #  if (defined(__ppc64__) || defined(__x86_64__)) && !defined(__APPLE__)
-  pi.AppendModulePath(QString::fromStdString(root+"/lib64/openstructure"));
+  pi.AppendModulePath(QString::fromStdString(root+"/lib64/"+
+                                             site_pkgs.str()+
+                                             "site-packages"));
 #  else
-  pi.AppendModulePath(QString::fromStdString(root+"/lib/openstructure"));
+  pi.AppendModulePath(QString::fromStdString(root+"/lib/"+
+                                             site_pkgs.str()+
+                                             "/site-packages"));
 #  endif
 #endif
   pi.AppendModulePath(".");  
diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt
index 0b6b429a4..f9528d7e4 100644
--- a/scripts/CMakeLists.txt
+++ b/scripts/CMakeLists.txt
@@ -33,13 +33,13 @@ if (NOT ENABLE_STATIC)
   set(INIT_SCRIPTS 
     init_cl.py
   )
-
+  set(SPD "${LIB_DIR}/python${PYTHON_VERSION}/site-packages")
   if (ENABLE_GUI)
     list(APPEND INIT_SCRIPTS init.py)
   endif()
-  copy_if_different("./" "${STAGE_DIR}/${LIB_DIR}/openstructure" 
+  copy_if_different("./" "${STAGE_DIR}/${SPD}" 
                   "${INIT_SCRIPTS}" "python init scripts" ost_scripts)
 
-  install(FILES ${INIT_SCRIPTS} DESTINATION "${LIB_DIR}/openstructure")
+  install(FILES ${INIT_SCRIPTS} DESTINATION "${SPD}")
 
 endif()
diff --git a/scripts/init_cl.py b/scripts/init_cl.py
index 09b38ed97..d4a14df4c 100644
--- a/scripts/init_cl.py
+++ b/scripts/init_cl.py
@@ -26,10 +26,11 @@ parser.add_option("-v", "--verbosity_level", action="store", type="int", dest="v
 parser.disable_interspersed_args()
 (options, args) = parser.parse_args()
 
+_site_packs='python%d.%d/site-packages' % sys.version_info[0:2]
 if platform.machine()=='x86_64':
-  sys.path.insert(0, os.getenv('DNG_ROOT')+'/lib64/openstructure')
+  sys.path.insert(0, os.path.join(os.getenv('DNG_ROOT'), 'lib64', _site_packs))
 else:
-  sys.path.insert(0,os.getenv('DNG_ROOT')+'/lib/openstructure')
+  sys.path.insert(0,os.path.join(os.getenv('DNG_ROOT'), 'lib', _site_packs))
      
 from ost import *
 import ost
diff --git a/scripts/ost.in b/scripts/ost.in
index 2891d37c2..5eee1638b 100755
--- a/scripts/ost.in
+++ b/scripts/ost.in
@@ -31,7 +31,7 @@ BIN_DIR=`dirname "$SCRIPT_NAME"`
 
 source $BIN_DIR/../@LIBEXEC_PATH@/ost_config
 
-$pyexec $interactive "$DNG_ROOT/@LIBDIR@/openstructure/init_cl.py" $opts
+$pyexec $interactive "$DNG_ROOT/@LIBDIR@/python@PYTHON_VERSION@/site-packages/init_cl.py" $opts
 RC=$?
 exit $RC
 
diff --git a/scripts/ost_config.in b/scripts/ost_config.in
index faaae8e26..5894bdb12 100644
--- a/scripts/ost_config.in
+++ b/scripts/ost_config.in
@@ -24,7 +24,7 @@
 export DNG_ROOT=`cd "$BIN_DIR/..";pwd`
 export DNG_BINDIR="$DNG_ROOT/bin"
 export DNG_LIBDIR="$DNG_ROOT/@LIBDIR@"
-export DNG_INITDIR="$DNG_LIBDIR/openstructure"
+export DNG_INITDIR="$DNG_LIBDIR/python@PYTHON_VERSION@/site-packages"
 
 export PATH="$DNG_BINDIR:${PATH}"
 export DYLD_FRAMEWORK_PATH="$DNG_LIBDIR:${DYLD_FRAMEWORK_PATH}"
-- 
GitLab