[Piglit] [PATCH] piglit (executable): Make PIGLIT_SOURCE_DIR detection more robust

Dylan Baker baker.dylan.c at gmail.com
Thu Jun 12 11:51:15 PDT 2014

The initial implementation I committed was a bit hackish and I knew it
at the time, but it seemed to work for all the cases that I tested.
However, there were corner cases. The biggest of these cases is problems
running out of tree with piglit built in tree.
ex: src/piglit/piglit run ...

This patch makes the handling more robust by explicitly searching for
three distinct cases:
1) piglit is run from the source dir, built in the source dir
2) piglit is run from outside the source dir, built in the source dir
3) piglit has been installed out of tree

I have tested all three of these cases, and case 3 if piglit is
installed as piglit.py.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
 piglit | 45 +++++++++++++++++++++++++++++++++------------
 1 file changed, 33 insertions(+), 12 deletions(-)

diff --git a/piglit b/piglit
index bf53876..ea73268 100755
--- a/piglit
+++ b/piglit
@@ -36,18 +36,39 @@ import os.path as path
 import sys
 import argparse
-# If running in the source directory there will be a HACKING file, don't
-# muck with things, if not we need to screw with the python path
-if not path.exists('HACKING'):
-    _binpath, _bin = path.split(__file__)
-    _binname, _binext = path.splitext(_bin)
-    if _binext == '.py':
-        # Drop .py extension (used on Windows)
-        _bin = _binname
-    _libdir = path.abspath(path.join(_binpath, '..', 'lib', _bin))
-    sys.path.append(_libdir)
-    if 'PIGLIT_SOURCE_DIR' not in os.environ:
-        os.environ['PIGLIT_SOURCE_DIR'] = _libdir
+# Setting PIGLIT_SOURCE_DIR (and by extension sys.path) is actually pretty
+# complicated, since there are three seperate uses we need to detect:
+# 1) piglit is being run in the source directory, built in tree
+# 2) piglit is being run from the source directory outside of it, built in tree
+# 3) piglit has been built out of tree and installed, and is being run in or
+#    out of the install directory
+# It is critical that this block be run before importing anything from
+# framework (as there is no gaurantee that framework will be in python's path
+# before this blck is run)
+# Case 1
+if path.exists('framework/exectest.py'):
+    os.environ['PIGLIT_SOURCE_DIR'] = path.abspath(path.curdir)
+    dirpath = path.dirname(path.abspath(__file__))
+    # Case 2
+    if path.exists(path.join(dirpath, 'framework/exectest.py')):
+        os.environ['PIGLIT_SOURCE_DIR'] = dirpath
+        sys.path.append(dirpath)
+    # Case 3
+    else:
+        # In the case of windows piglit is called
+        # piglit${the_date_of_install}.py, and the .py needs to be thrown away
+        piglit = path.splitext(path.basename(__file__))[0]
+        # In the install case we have another problem, one could have multiple
+        # piglits installed as piglit${the_date_of_install}, and we need to
+        # detect that.
+        install_path = path.abspath(path.join(dirpath, '..', 'lib', piglit))
+        os.environ['PIGLIT_SOURCE_DIR'] = install_path
+        sys.path.append(install_path)
 import framework.programs.run as run
 import framework.programs.summary as summary

More information about the Piglit mailing list