瀏覽代碼

init: remove global variables from user, tmpdir and check GUI functions

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@65267 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 10 年之前
父節點
當前提交
85544cb775
共有 1 個文件被更改,包括 38 次插入16 次删除
  1. 38 16
      lib/init/grass.py

+ 38 - 16
lib/init/grass.py

@@ -194,11 +194,30 @@ def Popen(cmd, **kwargs):
 
 
 
 
 def gpath(*args):
 def gpath(*args):
+    """Costruct path to file or directory in GRASS GIS installation
+
+    Can be called only after gisbase was set.
+    """
     return os.path.join(gisbase, *args)
     return os.path.join(gisbase, *args)
 
 
 
 
+# for wxpath
+_WXPYTHON_BASE = None
+
+
 def wxpath(*args):
 def wxpath(*args):
-    return os.path.join(wxpython_base, *args)
+    """Costruct path to file or directory in GRASS wxGUI
+
+    Can be called only after gisbase was set.
+
+    This function does not check if the directories exist or if GUI works
+    this must be done by the caller if needed.
+    """
+    global _WXPYTHON_BASE
+    if not _WXPYTHON_BASE:
+        # this can be called only after gisbase was set
+        _WXPYTHON_BASE = gpath("gui", "wxpython")
+    return os.path.join(_WXPYTHON_BASE, *args)
 
 
 
 
 help_text = r"""GRASS GIS %s
 help_text = r"""GRASS GIS %s
@@ -274,8 +293,13 @@ def help_message():
     sys.stderr.write(s)
     sys.stderr.write(s)
 
 
 
 
-def create_tmp():
-    ## use $TMPDIR if it exists, then $TEMP, otherwise /tmp
+def create_tmp(user, gis_lock):
+    """Create temporary directory
+
+    :param user: user name to be used in the directory name
+    :param gis_lock: session lock filename to be used in the directory name
+    """
+    # use $TMPDIR if it exists, then $TEMP, otherwise /tmp
     tmp = os.getenv('TMPDIR')
     tmp = os.getenv('TMPDIR')
     if not tmp:
     if not tmp:
         tmp = os.getenv('TEMP')
         tmp = os.getenv('TEMP')
@@ -283,7 +307,7 @@ def create_tmp():
         tmp = os.getenv('TMP')
         tmp = os.getenv('TMP')
     if not tmp:
     if not tmp:
         tmp = tempfile.gettempdir()
         tmp = tempfile.gettempdir()
-    
+
     if tmp:
     if tmp:
         tmpdir = os.path.join(tmp, "grass7-%(user)s-%(lock)s" % {'user': user,
         tmpdir = os.path.join(tmp, "grass7-%(user)s-%(lock)s" % {'user': user,
                                                              'lock': gis_lock})
                                                              'lock': gis_lock})
@@ -291,7 +315,7 @@ def create_tmp():
             os.mkdir(tmpdir, 0700)
             os.mkdir(tmpdir, 0700)
         except:
         except:
             tmp = None
             tmp = None
-    
+
     if not tmp:
     if not tmp:
         for ttmp in ("/tmp", "/var/tmp", "/usr/tmp"):
         for ttmp in ("/tmp", "/var/tmp", "/usr/tmp"):
             tmp = ttmp
             tmp = ttmp
@@ -303,13 +327,14 @@ def create_tmp():
                 tmp = None
                 tmp = None
             if tmp:
             if tmp:
                 break
                 break
-    
+
     if not tmp:
     if not tmp:
         fatal(_("Unable to create temporary directory <grass7-%(user)s-"
         fatal(_("Unable to create temporary directory <grass7-%(user)s-"
                 "%(lock)s>! Exiting.") % {'user': user, 'lock': gis_lock})
                 "%(lock)s>! Exiting.") % {'user': user, 'lock': gis_lock})
-    
+
+    # promoting the variable even if it was not defined before
     os.environ['TMPDIR'] = tmpdir
     os.environ['TMPDIR'] = tmpdir
-    
+
     return tmpdir
     return tmpdir
 
 
 
 
@@ -563,7 +588,6 @@ MAPSET: <UNKNOWN>
 
 
 
 
 def check_gui(expected_gui):
 def check_gui(expected_gui):
-    global wxpython_base
     grass_gui = expected_gui
     grass_gui = expected_gui
     # Check if we are running X windows by checking the DISPLAY variable
     # Check if we are running X windows by checking the DISPLAY variable
     if os.getenv('DISPLAY') or windows or macosx:
     if os.getenv('DISPLAY') or windows or macosx:
@@ -576,10 +600,7 @@ def check_gui(expected_gui):
             p.stdin.write("variable=True")
             p.stdin.write("variable=True")
             p.stdin.close()
             p.stdin.close()
             p.wait()
             p.wait()
-            if p.returncode == 0:
-                # Set the wxpython base directory
-                wxpython_base = gpath("gui", "wxpython")
-            else:
+            if p.returncode != 0:
                 # Python was not found - switch to text interface mode
                 # Python was not found - switch to text interface mode
                 warning(_("The python command does not work as expected!\n"
                 warning(_("The python command does not work as expected!\n"
                           "Please check your GRASS_PYTHON environment variable.\n"
                           "Please check your GRASS_PYTHON environment variable.\n"
@@ -1274,7 +1295,7 @@ def print_params():
 
 
 
 
 def get_username():
 def get_username():
-    global user
+    """Get name of the current user"""
     if windows:
     if windows:
         user = os.getenv('USERNAME')
         user = os.getenv('USERNAME')
         if not user:
         if not user:
@@ -1293,6 +1314,7 @@ def get_username():
                 pass
                 pass
         if not user:
         if not user:
             user = "user_%d" % os.getuid()
             user = "user_%d" % os.getuid()
+    return user
 
 
 
 
 def parse_cmdline(argv):
 def parse_cmdline(argv):
@@ -1419,7 +1441,7 @@ if exit_grass and not create_new:
     fatal(_("Flag -e requires also flag -c"))
     fatal(_("Flag -e requires also flag -c"))
 
 
 # Set the username
 # Set the username
-get_username()
+user = get_username()
 
 
 # Set language
 # Set language
 # This has to be called before any _() function call!
 # This has to be called before any _() function call!
@@ -1428,7 +1450,7 @@ get_username()
 set_language()
 set_language()
 
 
 # Create the temporary directory and session grassrc file
 # Create the temporary directory and session grassrc file
-tmpdir = create_tmp()
+tmpdir = create_tmp(user, gis_lock)
 atexit.register(cleanup, tmpdir)
 atexit.register(cleanup, tmpdir)
 
 
 # Create the session grassrc file
 # Create the session grassrc file