From 7f07fe7fff83897ce7b225ac37935b5f6290a07d Mon Sep 17 00:00:00 2001
From: Romain Boman <r.boman@uliege.be>
Date: Mon, 6 Jun 2022 17:04:01 +0200
Subject: [PATCH] fix path problems

---
 fossils.py | 58 ++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 41 insertions(+), 17 deletions(-)

diff --git a/fossils.py b/fossils.py
index 83cf8be..68fd9d6 100644
--- a/fossils.py
+++ b/fossils.py
@@ -1,5 +1,7 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
+# Fossils:
+#   main script
 
 from PyQt5.QtCore import *
 from PyQt5.QtGui import *
@@ -116,48 +118,66 @@ def view_results():
     gmsh.fltk.run()
 
 
+def rm_folder_from_pypath(folder):
+    sys.path = [ p for p in sys.path if not folder in p]
+
+
 def add_folder2pypath(folder):
     if os.path.isdir(folder):
         print(f'{folder} added to pythonpath')
         sys.path.append(folder)
 
+
+def rm_folder_from_path(folder):
+    import platform
+    if 'Windows' in platform.uname():
+        path = [ p for p in os.environ['PATH'].split(';') if not folder in p]
+        os.environ['PATH'] = ';'.join(path)
+        # print(f'{folder} added to PATH')
+        # print(f"os.environ['PATH']={os.environ['PATH']}")    
+
+
 def add_folder2path(folder):
     if not os.path.isdir(folder):
         return
     import platform
     if 'Windows' in platform.uname():
         path = os.environ['PATH'].split(';')
-        path.insert(0, folder)
+        # path.insert(0, folder)
+        path.append(folder)
         os.environ['PATH'] = ';'.join(path)
         print(f'{folder} added to PATH')
-
+        # print(f"os.environ['PATH']={os.environ['PATH']}")
 
 def setup_pythonpath():
     """setup PYTHONPATH
     """
     # adds script folder to the pythonpath
-    thisdir = os.path.split(os.path.abspath(__file__))[0]
-    thisdir = os.path.normcase(thisdir)
-    add_folder2pypath(thisdir)
+    this_script_dir = os.path.split(os.path.abspath(__file__))[0]
+    this_script_dir = os.path.normcase(this_script_dir)
+    # add_folder2pypath(this_script_dir) # already there as first entry
 
     # add binary dir to PYTHONPATH
     pyexe = os.path.basename(sys.executable)
     print(f'pyexe = {pyexe}')
-    add_folder2pypath(os.path.join(thisdir, 'cxxfem',
+    add_folder2pypath(os.path.join(this_script_dir, 'cxxfem',
                     'build', 'bin'))  # gcc/mingw
-    add_folder2pypath(os.path.join(thisdir, 'cxxfem',
+    add_folder2pypath(os.path.join(this_script_dir, 'cxxfem',
                     'build', 'bin', 'Release'))  # msvc
 
-    # allows this script to be run without setting env 
-    add_folder2pypath(os.path.join(thisdir, 'lib',
+    # allows this script to be run without setting env
+    rm_folder_from_path('gmsh')
+    rm_folder_from_pypath('gmsh')
+    add_folder2pypath(os.path.join(this_script_dir, 'lib',
                     'gmsh-sdk', 'lib'))  # msvc 
-    add_folder2path(os.path.join(thisdir, 'lib',
-                    'gmsh-sdk', 'bin'))  # gmsh    
+    add_folder2path(os.path.join(this_script_dir, 'lib',
+                    'gmsh-sdk', 'bin'))  # gmsh
+    # print(f'sys.path={sys.path}')
+
 
 def create_workspace(workspace, testname):
-    """create workspace and chdir into it
+    """create workspace folder and chdir into it
     """
-    
     if workspace:
         # workspace is given:
         #   => workspace + testname
@@ -168,14 +188,14 @@ def create_workspace(workspace, testname):
     else:
         # workspace is not given:
         #   => current_folder + 'workspace' + testname
-        thisdir = os.path.normcase(os.getcwd())
+        current_dir = os.path.normcase(os.getcwd())
         # print(f'testname={testname}')
-        # print(f'thisdir={thisdir}')
-        common = os.path.commonprefix((testname, thisdir))
+        # print(f'current_dir={current_dir}')
+        common = os.path.commonprefix((testname, current_dir))
         # print(f'common={common}')
         resdir = testname[len(common)+1:].replace(os.sep, "_")
         resdir, ext = os.path.splitext(resdir)
-        wdir = os.path.join('workspace', resdir)
+        wdir = os.path.abspath(os.path.join('workspace', resdir))
     print('workspace =', wdir)
     # sys.exit()
     if not os.path.isdir(wdir):
@@ -187,6 +207,10 @@ if __name__ == "__main__":
     # add main program folder and binaries dir to the PYTHONPATH
     setup_pythonpath()
 
+    # preload some modules (vtk before gmsh)
+    import vtk
+    import gmsh
+
     # redirect C++ streams to Python
     import cxxfem
     redirect = cxxfem.StdOutErr2Py()
-- 
GitLab