# HG changeset patch
# User Matt Harbison <matt_harbi...@yahoo.com>
# Date 1522466506 14400
#      Fri Mar 30 23:21:46 2018 -0400
# Node ID 6e77d0a68122a6513521c94c4b8256b7e6da6f93
# Parent  45aa79668d4ee38866b0829786da076ef59b0381
server: refactor 'daemon_postexec' instructions into a dictionary

diff --git a/mercurial/server.py b/mercurial/server.py
--- a/mercurial/server.py
+++ b/mercurial/server.py
@@ -30,26 +30,35 @@ def runservice(opts, parentfn=None, init
                runargs=None, appendpid=False):
     '''Run a command as a service.'''
 
+    postexecargs = {}
+
+    if opts['daemon_postexec']:
+        for inst in opts['daemon_postexec']:
+            if inst.startswith('unlink:'):
+                postexecargs['unlink'] = inst[7:]
+            elif inst.startswith('chdir:'):
+                postexecargs['chdir'] = inst[6:]
+            elif inst != 'none':
+                raise error.Abort(_('invalid value for --daemon-postexec: %s')
+                                  % inst)
+
     # When daemonized on Windows, redirect stdout/stderr to the lockfile (which
     # gets cleaned up after the child is up and running), so that the parent 
can
     # read and print the error if this child dies early.  See 594dd384803c.  On
     # other platforms, the child can write to the parent's stdio directly, 
until
     # it is redirected prior to runfn().
     if pycompat.iswindows and opts['daemon_postexec']:
-        for inst in opts['daemon_postexec']:
-            if inst.startswith('unlink:'):
-                lockpath = inst[7:]
-                if os.path.exists(lockpath):
-                    procutil.stdout.flush()
-                    procutil.stderr.flush()
+        if 'unlink' in postexecargs and os.path.exists(postexecargs['unlink']):
+            procutil.stdout.flush()
+            procutil.stderr.flush()
 
-                    fd = os.open(lockpath,
-                                 os.O_WRONLY | os.O_APPEND | os.O_BINARY)
-                    try:
-                        os.dup2(fd, 1)
-                        os.dup2(fd, 2)
-                    finally:
-                        os.close(fd)
+            fd = os.open(postexecargs['unlink'],
+                         os.O_WRONLY | os.O_APPEND | os.O_BINARY)
+            try:
+                os.dup2(fd, 1)
+                os.dup2(fd, 2)
+            finally:
+                os.close(fd)
 
     def writepid(pid):
         if opts['pid_file']:
@@ -103,21 +112,14 @@ def runservice(opts, parentfn=None, init
     if not opts['daemon']:
         writepid(procutil.getpid())
 
-    if opts['daemon_postexec']:
+    if postexecargs:
         try:
             os.setsid()
         except AttributeError:
             pass
 
-        lockpath = None
-        for inst in opts['daemon_postexec']:
-            if inst.startswith('unlink:'):
-                lockpath = inst[7:]
-            elif inst.startswith('chdir:'):
-                os.chdir(inst[6:])
-            elif inst != 'none':
-                raise error.Abort(_('invalid value for --daemon-postexec: %s')
-                                  % inst)
+        if 'chdir' in postexecargs:
+            os.chdir(postexecargs['chdir'])
         procutil.hidewindow()
         procutil.stdout.flush()
         procutil.stderr.flush()
@@ -137,8 +139,8 @@ def runservice(opts, parentfn=None, init
 
         # Only unlink after redirecting stdout/stderr, so Windows doesn't
         # complain about a sharing violation.
-        if lockpath:
-            os.unlink(lockpath)
+        if 'unlink' in postexecargs:
+            os.unlink(postexecargs['unlink'])
 
     if runfn:
         return runfn()
_______________________________________________
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Reply via email to