Kaynağa Gözat

simplify GRASS exiting from GUI (see r66917:8)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@66920 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 9 yıl önce
ebeveyn
işleme
751608e0d0
4 değiştirilmiş dosya ile 27 ekleme ve 38 silme
  1. 5 7
      general/g.gui/main.c
  2. 10 7
      gui/wxpython/lmgr/frame.py
  3. 6 14
      gui/wxpython/wxgui.py
  4. 6 10
      lib/init/grass.py

+ 5 - 7
general/g.gui/main.c

@@ -29,7 +29,7 @@ int main(int argc, char *argv[])
     struct Option *type, *rc_file;
     struct Flag *update_ui, *fglaunch, *nolaunch;
     struct GModule *module;
-    const char *gui_type_env, *shell_pid;
+    const char *gui_type_env;
     char progname[GPATH_MAX];
     char *desc;
 
@@ -103,8 +103,6 @@ int main(int argc, char *argv[])
 	exit(EXIT_SUCCESS);
     }
 
-    shell_pid = G_getenv_nofatal("PID");
-    
     sprintf(progname, "%s/gui/wxpython/wxgui.py", G_gisbase());
     if (access(progname, F_OK) == -1)
         G_fatal_error(_("Your installation doesn't include GUI, exiting."));
@@ -113,22 +111,22 @@ int main(int argc, char *argv[])
         G_message(_("Launching <%s> GUI, please wait..."), type->answer);
         if (rc_file->answer) {
             G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
-                       "--workspace", rc_file->answer, "--pid", shell_pid, NULL);
+                       "--workspace", rc_file->answer, NULL);
         }
         else {
             G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
-                       "--pid", shell_pid, NULL);
+                       NULL);
         }
     }
     else {
         G_message(_("Launching <%s> GUI in the background, please wait..."), type->answer);
         if (rc_file->answer) {
             G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
-                       "--workspace", rc_file->answer, "--pid", shell_pid, SF_BACKGROUND, NULL);
+                       "--workspace", rc_file->answer, SF_BACKGROUND, NULL);
         }
         else {
             G_spawn_ex(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), progname,
-                       "--pid", shell_pid, SF_BACKGROUND, NULL);
+                       SF_BACKGROUND, NULL);
         }
         /* stop the impatient from starting it again
            before the splash screen comes up */

+ 10 - 7
gui/wxpython/lmgr/frame.py

@@ -84,10 +84,9 @@ class GMFrame(wx.Frame):
     commands, tree widget page for managing map layers.
     """
     def __init__(self, parent, id = wx.ID_ANY, title = None,
-                 workspace = None, shellPid = None,
+                 workspace = None,
                  size = globalvar.GM_WINDOW_SIZE, style = wx.DEFAULT_FRAME_STYLE, **kwargs):
         self.parent    = parent
-        self._shellPid = shellPid         # process id of shell running on the background
         if title:
             self.baseTitle = title
         else:
@@ -2260,12 +2259,16 @@ class GMFrame(wx.Frame):
     def OnCloseWindowExitGRASS(self, event):
         """Close wxGUI and exit GRASS shell."""
         self._closeWindow()
-        if self._shellPid:
-            Debug.msg(1, "Exiting shell with pid={}".format(self._shellPid))
-            import signal
-            os.kill(self._shellPid, signal.SIGTERM)
-        else:
+        try:
+            shellPid = int(grass.gisenv()['PID'])
+            print >> sys.stderr, grass.gisenv()
+        except:
             grass.warning(_("Unable to exit GRASS shell: unknown PID"))
+            return
+
+        Debug.msg(1, "Exiting shell with pid={}".format(shellPid))
+        import signal
+        os.kill(shellPid, signal.SIGTERM)
         
     def MsgNoLayerSelected(self):
         """Show dialog message 'No layer selected'"""

+ 6 - 14
gui/wxpython/wxgui.py

@@ -35,14 +35,12 @@ except ImportError:
 
 
 class GMApp(wx.App):
-    def __init__(self, workspace=None, shellPid=None):
+    def __init__(self, workspace=None):
         """ Main GUI class.
 
         :param workspace: path to the workspace file
-        :param shellPid: process id of shell running on background
         """
         self.workspaceFile = workspace
-        self._shellPid = shellPid
         
         # call parent class initializer
         wx.App.__init__(self, False)
@@ -80,7 +78,7 @@ class GMApp(wx.App):
         # create and show main frame
         from lmgr.frame import GMFrame
         mainframe = GMFrame(parent=None, id=wx.ID_ANY,
-                            workspace=self.workspaceFile, shellPid=self._shellPid)
+                            workspace=self.workspaceFile)
 
         mainframe.Show()
         self.SetTopWindow(mainframe)
@@ -99,7 +97,7 @@ def printHelp():
 
 def process_opt(opts, args):
     """ Process command-line arguments"""
-    workspaceFile = shellPid = None
+    workspaceFile = None
     for o, a in opts:
         if o in ("-h", "--help"):
             printHelp()
@@ -110,13 +108,7 @@ def process_opt(opts, args):
             else:
                 workspaceFile = args.pop(0)
 
-        elif o in ("-p", "--pid"):
-            if a != '':
-                shellPid = int(a)
-            else:
-                shellPid = int(args.pop(0))
-
-    return (workspaceFile, shellPid)
+    return workspaceFile
 
 
 def main(argv = None):
@@ -134,8 +126,8 @@ def main(argv = None):
         print >> sys.stderr, "for help use --help"
         printHelp()
     
-    workspaceFile, shellPid = process_opt(opts, args)
-    app = GMApp(workspaceFile, shellPid)
+    workspaceFile = process_opt(opts, args)
+    app = GMApp(workspaceFile)
     
     # suppress wxPython logs
     q = wx.LogNull()

+ 6 - 10
lib/init/grass.py

@@ -133,7 +133,7 @@ def clean_env(gisrc):
     env_curr = read_gisrc(gisrc)
     env_new = {}
     for k,v in env_curr.items():
-        if 'MONITOR' not in k:
+        if k not in ('MONITOR', 'PID'):
             env_new[k] = v
 
     write_gisrc(env_new, gisrc)
@@ -1338,7 +1338,7 @@ def run_batch_job(batch_job):
     return returncode
 
 
-def start_gui(grass_gui, shell_pid=None):
+def start_gui(grass_gui):
     """Start specified GUI
 
     :param grass_gui: GUI name (allowed values: 'wxpython')
@@ -1348,14 +1348,9 @@ def start_gui(grass_gui, shell_pid=None):
     
     # Check for gui interface
     if grass_gui == "wxpython":
-        cmd = [os.getenv('GRASS_PYTHON'), wxpath("wxgui.py")]
-        if shell_pid:
-            cmd.append('--pid')
-            cmd.append(str(shell_pid))
-        
-        Popen(cmd)
+        Popen([os.getenv('GRASS_PYTHON'), wxpath("wxgui.py")])
+
 
-    
 def clear_screen():
     """Clear terminal"""
     if windows:
@@ -1900,7 +1895,8 @@ def main():
             shell_process = default_startup(mapset_settings.full_mapset,
                                             mapset_settings.location)
 
-        start_gui(grass_gui, shell_process.pid)
+        # start GUI and register shell PID in rc file
+        start_gui(grass_gui)
         kv = read_gisrc(gisrc)
         kv['PID'] = str(shell_process.pid)
         write_gisrc(kv, gisrc)