[PATCH] run-tests.py: fix handling of symlink to the right python

Mads Kiilerich mads at kiilerich.com
Sun Jan 6 19:14:54 CST 2013


# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1357521281 -3600
# Node ID 5d8832d8a97a178d3edc05986c3d55865d76c621
# Parent  f5842787a958b8af558bd8aa148f13be6df41992
run-tests.py: fix handling of symlink to the right python

Before: a symlink for python in BINDIR was sometimes created, but it was never
updated when a different Python was used and it was never removed. An invalid
python could thus be left around and used when testing with --local.

Now: the symlink is removed when wrong and created when necessary.

The mechanism for finding the right name (python or python.exe) also had to be
simplified and made more explicit.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -356,33 +356,35 @@ def cleanup(options):
 def usecorrectpython():
     # some tests run python interpreter. they must use same
     # interpreter we use or bad things will happen.
-    exedir, exename = os.path.split(sys.executable)
-    if exename in ('python', 'python.exe'):
-        path = findprogram(exename)
-        if os.path.dirname(path) == exedir:
-            return
-    else:
-        exename = 'python'
-        if sys.platform == 'win32':
-            exename = 'python.exe'
+    pyexename = sys.platform == 'win32' and 'python.exe' or 'python'
     if getattr(os, 'symlink', None):
         vlog("# Making python executable in test path a symlink to '%s'" %
              sys.executable)
-        mypython = os.path.join(BINDIR, exename)
+        mypython = os.path.join(BINDIR, pyexename)
         try:
-            os.symlink(sys.executable, mypython)
+            if os.readlink(mypython) == sys.executable:
+                return
+            os.unlink(mypython)
         except OSError, err:
-            # child processes may race, which is harmless
-            if err.errno != errno.EEXIST:
+            if err.errno != errno.ENOENT:
                 raise
+        if findprogram(pyexename) != sys.executable:
+            try:
+                os.symlink(sys.executable, mypython)
+            except OSError, err:
+                # child processes may race, which is harmless
+                if err.errno != errno.EEXIST:
+                    raise
     else:
-        vlog("# Modifying search path to find %s in '%s'" % (exename, exedir))
+        exedir, exename = os.path.split(sys.executable)
+        vlog("# Modifying search path to find %s as %s in '%s'" %
+             (exename, pyexename, exedir))
         path = os.environ['PATH'].split(os.pathsep)
         while exedir in path:
             path.remove(exedir)
         os.environ['PATH'] = os.pathsep.join([exedir] + path)
-        if not findprogram(exename):
-            print "WARNING: Cannot find %s in search path" % exename
+        if not findprogram(pyexename):
+            print "WARNING: Cannot find %s in search path" % pyexename
 
 def installhg(options):
     vlog("# Performing temporary installation of HG")


More information about the Mercurial-devel mailing list