[GRASS-SVN] r67900 - grass/branches/releasebranch_7_0/lib/python/pygrass

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Feb 20 02:45:35 PST 2016


Author: martinl
Date: 2016-02-20 02:45:35 -0800 (Sat, 20 Feb 2016)
New Revision: 67900

Modified:
   grass/branches/releasebranch_7_0/lib/python/pygrass/utils.py
Log:
libpython: sync get_lib_path() and set_path() with trunk


Modified: grass/branches/releasebranch_7_0/lib/python/pygrass/utils.py
===================================================================
--- grass/branches/releasebranch_7_0/lib/python/pygrass/utils.py	2016-02-20 10:40:10 UTC (rev 67899)
+++ grass/branches/releasebranch_7_0/lib/python/pygrass/utils.py	2016-02-20 10:45:35 UTC (rev 67900)
@@ -265,32 +265,31 @@
 
 def get_lib_path(modname, libname=None):
     """Return the path of the libname contained in the module.
-
-    >>> get_lib_path(modname='r.modis', libname='libmodis')
     """
-    from os.path import isdir, join
+    from os.path import isdir, join, sep
     from os import getenv
 
     if isdir(join(getenv('GISBASE'), 'etc', modname)):
         path = join(os.getenv('GISBASE'), 'etc', modname)
     elif getenv('GRASS_ADDON_BASE') and libname and \
             isdir(join(getenv('GRASS_ADDON_BASE'), 'etc', modname, libname)):
-        path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname, libname)
+        path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname)
     elif getenv('GRASS_ADDON_BASE') and \
             isdir(join(getenv('GRASS_ADDON_BASE'), 'etc', modname)):
         path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname)
     elif getenv('GRASS_ADDON_BASE') and \
             isdir(join(getenv('GRASS_ADDON_BASE'), modname, modname)):
         path = join(os.getenv('GRASS_ADDON_BASE'), modname, modname)
-    elif libname and isdir(join('..', libname)): # used by g.extension compilation process
-        path = join('..', libname)
-    elif isdir(join('..', 'etc', modname)):      # used by g.extension compilation process
-        path = join('..', 'etc', modname)
-    elif isdir(join('etc', modname)):            # used by g.extension compilation process
-        path = join('etc', modname)
     else:
-        path = None
-
+        # used by g.extension compilation process
+        cwd = os.getcwd()
+        idx = cwd.find(modname)
+        if idx < 0:
+            return None
+        path = '{cwd}{sep}etc{sep}{modname}'.format(cwd=cwd[:idx+len(modname)],
+                                                    sep=sep,
+                                                    modname=modname)
+    
     return path
 
 
@@ -316,20 +315,20 @@
 
         grass_prompt> tree ../../../r.green
         ../../../r.green
-        ├── ...
-        ├── libgreen
-        │   ├── pyfile1.py
-        │   └── pyfile2.py
-        └── r.green.hydro
-           ├── Makefile
-           ├── libhydro
-           │   ├── pyfile1.py
-           │   └── pyfile2.py
-           ├── r.green.hydro.*
-           └── r.green.hydro.financial
-               ├── Makefile
-               ├── ...
-               └── r.green.hydro.financial.py
+        |-- ...
+        |-- libgreen
+        |   |-- pyfile1.py
+        |   +-- pyfile2.py
+        +-- r.green.hydro
+           |-- Makefile
+           |-- libhydro
+           |   |-- pyfile1.py
+           |   +-- pyfile2.py
+           |-- r.green.hydro.*
+           +-- r.green.hydro.financial
+               |-- Makefile
+               |-- ...
+               +-- r.green.hydro.financial.py
 
         21 directories, 125 files
 
@@ -363,13 +362,13 @@
         sys.path.append(os.path.abspath(path))
     else:
         # running from GRASS GIS session
-        from grass.pygrass.utils import get_lib_path
         path = get_lib_path(modulename, dirname)
         if path is None:
             pathname = os.path.join(modulename, dirname) if dirname else modulename
             raise ImportError("Not able to find the path '%s' directory "
                               "(current dir '%s')." % (pathname, os.getcwd()))
-        sys.path.append(path)
+        
+        sys.path.insert(0, path)
 
 
 def split_in_chunk(iterable, length=10):



More information about the grass-commit mailing list