[GRASS-SVN] r53495 - in grass/trunk: gui/wxpython/core gui/wxpython/lmgr gui/wxpython/modules lib/init

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Oct 19 05:11:04 PDT 2012


Author: martinl
Date: 2012-10-19 05:11:03 -0700 (Fri, 19 Oct 2012)
New Revision: 53495

Modified:
   grass/trunk/gui/wxpython/core/globalvar.py
   grass/trunk/gui/wxpython/core/utils.py
   grass/trunk/gui/wxpython/lmgr/frame.py
   grass/trunk/gui/wxpython/modules/extensions.py
   grass/trunk/lib/init/grass.py
Log:
eliminate gis env ADDON_PATH, see #1696


Modified: grass/trunk/gui/wxpython/core/globalvar.py
===================================================================
--- grass/trunk/gui/wxpython/core/globalvar.py	2012-10-19 11:29:24 UTC (rev 53494)
+++ grass/trunk/gui/wxpython/core/globalvar.py	2012-10-19 12:11:03 UTC (rev 53495)
@@ -168,15 +168,20 @@
         if not os.path.exists(path) or not os.path.isdir(path):
             continue
         for fname in os.listdir(path):
+            if fname in ['docs', 'modules.xml']:
+                continue
             if grassScripts: # win32
                 name, ext = os.path.splitext(fname)
                 if name not in grassCmd:
-                    grassCmd.add(name)
-                    nCmd += 1
-                if ext in grassScripts.keys() and \
+                    if ext not in ['.exe', '.py']:
+                        continue
+                    if name not in grassCmd:
+                        grassCmd.add(name)
+                        nCmd += 1
+                if ext == '.py' and \
+                        ext in grassScripts.keys() and \
                         name not in grassScripts[ext]:
                     grassScripts[ext].append(name)
-                    nCmd += 1
             else:
                 if fname not in grassCmd:
                     grassCmd.add(fname)

Modified: grass/trunk/gui/wxpython/core/utils.py
===================================================================
--- grass/trunk/gui/wxpython/core/utils.py	2012-10-19 11:29:24 UTC (rev 53494)
+++ grass/trunk/gui/wxpython/core/utils.py	2012-10-19 12:11:03 UTC (rev 53495)
@@ -758,12 +758,85 @@
 
     # keep location of settings files rc and wx in sync with lib/init/grass.py
     if sys.platform == 'win32':
-        return os.path.join(os.getenv('APPDATA'), 'grass%d' % version)
+        return os.path.join(os.getenv('APPDATA'), 'GRASS%d' % version)
     
     return os.path.join(os.getenv('HOME'), '.grass%d' % version)
 
+def StoreEnvVariable(key, value, envFile = None):
+    """!Store environmental variable
 
+    @param key env key
+    @param value env value
+    @param envFile path to the environmental file (None for default location)
+    """
+    windows = sys.platform == 'win32'
+    if not envFile:
+        gVersion = grass.version()['version'].split('.', 1)[0]
+        if not windows:
+            envFile = os.path.join(os.getenv('HOME'), '.grass%s' % gVersion, 'bashrc')
+        else:
+            envFile = os.path.join(os.getenv('APPDATA'), 'GRASS%s' % gVersion, 'env.bat')
+    
+    # read env file
+    environ = dict()
+    if os.path.exists(envFile):
+        try:
+            fd = open(envFile)
+        except IOError, e:
+            sys.stderr.write(_("Unable to open file '%s'") % envFile)
+            return
+        for line in fd.readlines():
+            try:
+                key, value = line.split(' ', 1).strip().split('=', 1)
+            except:
+                sys.stderr.write(_("%s: unable to parse '%s'") % (envFile, line))
+                continue
+            if key in environ:
+                sys.stderr.write(_("Duplicated key: %s") % key)
+            environ[key] = value
+        
+        fd.close()
+    
+    # update environmental variables
+    environ[key] = value
+    
+    # write update env file
+    try:
+        fd = open(envFile, 'w')
+    except IOError, e:
+        sys.stderr.write(_("Unable to create file '%s'") % envFile)
+        return
+    if windows:
+        expCmd = 'set'
+    else:
+        expCmd = 'export'
+    
+    for key, value in environ.iteritems():
+        fd.write('%s %s=%s\n' % (expCmd, key, value))
+        
+    fd.close()
 
+def SetAddOnPath(addonPath = None, key = 'PATH'):
+    """!Set default AddOn path
+
+    @addonPath path to addons (None for default)
+    @key env key - 'PATH' or 'BASE'
+    """
+    gVersion = grass.version()['version'].split('.', 1)[0]
+    # update env file
+    if not addonPath:
+        if sys.platform != 'win32':
+            addonPath = os.path.join(os.path.join(os.getenv('HOME'),
+                                                  '.grass%s' % gVersion,
+                                                  'addons'))
+        else:
+            addonPath = os.path.join(os.path.join(os.getenv('APPDATA'),
+                                                  'GRASS%s' % gVersion,
+                                                  'addons'))
+    
+    StoreEnvVariable('GRASS_ADDON_' + key, addonPath)
+    os.environ['GRASS_ADDON_' + key] = addonPath
+
 # From lib/gis/col_str.c, except purple which is mentioned
 # there but not given RGB values
 str2rgb = {'aqua': (100, 128, 255),
@@ -786,7 +859,6 @@
 for (s,r) in str2rgb.items():
     rgb2str[ r ] = s
 
-
 def color_resolve(color):
     if len(color) > 0 and color[0] in "0123456789":
         rgb = tuple(map(int, color.split(':')))

Modified: grass/trunk/gui/wxpython/lmgr/frame.py
===================================================================
--- grass/trunk/gui/wxpython/lmgr/frame.py	2012-10-19 11:29:24 UTC (rev 53494)
+++ grass/trunk/gui/wxpython/lmgr/frame.py	2012-10-19 12:11:03 UTC (rev 53495)
@@ -41,6 +41,7 @@
 
 from core.gcmd             import RunCommand, GError, GMessage
 from core.settings         import UserSettings, GetDisplayVectSettings
+from core.utils            import SetAddOnPath
 from gui_core.preferences  import MapsetAccess, PreferencesDialog, EVT_SETTINGS_CHANGED
 from lmgr.layertree        import LayerTree, LMIcons
 from lmgr.menudata         import ManagerData
@@ -708,8 +709,7 @@
                                    caption = _("Update Addons path?"),
                                    style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
             if dlg.ShowModal() == wx.ID_YES:
-                os.environ['GRASS_ADDON_PATH'] = os.pathsep.join(addonPath)
-                RunCommand('g.gisenv', set = 'ADDON_PATH=%s' % os.environ['GRASS_ADDON_PATH'])
+                SetAddOnPath(os.pathsep.join(addonPath), key = 'PATH')
         
         self.goutput.WriteCmdLog(_("Launching script '%s'...") % filename)
         self.goutput.RunCmd([filename], switchPage = True)

Modified: grass/trunk/gui/wxpython/modules/extensions.py
===================================================================
--- grass/trunk/gui/wxpython/modules/extensions.py	2012-10-19 11:29:24 UTC (rev 53494)
+++ grass/trunk/gui/wxpython/modules/extensions.py	2012-10-19 12:11:03 UTC (rev 53495)
@@ -32,6 +32,7 @@
 
 from core             import globalvar
 from core.gcmd        import GError, RunCommand
+from core.utils       import SetAddOnPath
 from gui_core.forms   import GUI
 from gui_core.widgets import ItemTree, GListCtrl
 from gui_core.ghelp   import SearchModuleWindow
@@ -225,10 +226,13 @@
         
     def OnDone(self, cmd, returncode):
         if returncode == 0:
+            if not os.getenv('GRASS_ADDON_BASE'):
+                SetAddOnPath(key = 'BASE')
+            
             globalvar.UpdateGRASSAddOnCommands()
             log = self.parent.GetLogWindow()
             log.GetPrompt().SetFilter(None)
-        
+
     def OnItemSelected(self, event):
         """!Item selected"""
         item = event.GetItem()

Modified: grass/trunk/lib/init/grass.py
===================================================================
--- grass/trunk/lib/init/grass.py	2012-10-19 11:29:24 UTC (rev 53494)
+++ grass/trunk/lib/init/grass.py	2012-10-19 12:11:03 UTC (rev 53495)
@@ -48,9 +48,11 @@
 if sys.platform == 'win32':
     grass_config_dirname = "GRASS7"
     grass_config_dir = os.path.join(os.getenv('APPDATA'), grass_config_dirname)
+    grass_env_file = os.path.join(grass_config_dir, 'env.bat')
 else:
     grass_config_dirname = ".grass7"
     grass_config_dir = os.path.join(os.getenv('HOME'), grass_config_dirname)
+    grass_env_file = os.path.join(grass_config_dir, 'bashrc')
 
 gisbase = os.path.normpath(gisbase)
 
@@ -638,17 +640,10 @@
     location = os.path.join(gisdbase, location_name, mapset)
 
 def set_env_from_gisrc():
+    ### TODO: eliminate all env-from-gisrc
     import locale
     kv = read_gisrc()
     
-    ### addons
-    if kv.get('ADDON_PATH'):
-        addon_path = kv.get('ADDON_PATH')
-        if os.getenv('GRASS_ADDON_PATH'):
-            os.environ['GRASS_ADDON_PATH'] += os.pathsep + addon_path
-        else:
-            os.environ['GRASS_ADDON_PATH'] = addon_path
-    
     ### language
     language = kv.get('LANG')
     if language:
@@ -900,9 +895,12 @@
     f.write("PS1='GRASS %s (%s):\w > '\n" % (grass_version, location_name))
     f.write("PROMPT_COMMAND=\"'%s'\"\n" % os.path.join(gisbase, 'etc', 'prompt.py'))
     
-    path = os.path.join(userhome, ".grass.bashrc")
+    # read environmental variables
+    path = os.path.join(userhome, ".grass.bashrc") # left for backward compatibility
     if os.access(path, os.R_OK):
         f.write(readfile(path) + '\n')
+    if os.access(grass_env_file, os.R_OK):
+        f.write(readfile(grass_env_file) + '\n')
     
     f.write("export PATH=\"%s\"\n" % os.getenv('PATH'))
     f.write("export HOME=\"%s\"\n" % userhome) # restore user home path



More information about the grass-commit mailing list