[Piglit] [PATCH 3/6] framework.environment.py: Replace Environment.collectData static method

Dylan Baker baker.dylan.c at gmail.com
Wed Dec 18 13:58:18 PST 2013


First, static methods are ugly, and python's namespaces make them even
less interesting, since a toplevel function is like a static method but
without being part of a class. Second this static method was even worse
since it had nothing to do with the class it was attached.

This is replaced with a toplevel function in the environment module.
This also removed the need for checking what OS piglit is running on,
since it blindly attempts to run all of the possible commands that we
might want using a try/except block to skip the ones that are not
returned.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/environment.py | 50 +++++++++++++++++++++++++++---------------------
 piglit-run.py            |  2 +-
 2 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/framework/environment.py b/framework/environment.py
index d26c54a..50cbd43 100644
--- a/framework/environment.py
+++ b/framework/environment.py
@@ -23,10 +23,10 @@
 import os
 import re
 import subprocess
-import platform
 
 __all__ = ['Environment',
-           'TEST_BIN_DIR']
+           'TEST_BIN_DIR',
+           'get_environment']
 
 if 'PIGLIT_BUILD_DIR' in os.environ:
     TEST_BIN_DIR = os.path.join(os.environ['PIGLIT_BUILD_DIR'], 'bin')
@@ -81,24 +81,30 @@ class Environment:
             else:
                 yield (key, values)
 
-    def run(self, command):
+
+def get_environment():
+    """ Helper function that collects information about the test system
+
+    This uses popen to run a predefined set of binaries, returning their output
+    if they run successfully, or "Not Available" if they are not.
+
+    The commands are given as tuples in the form (<name to store>, <command to
+    run>); with <command to run> as a list-like object
+
+    """
+    commands = [("wglinfo", ['wglinfo']),
+                ("glxinfo", ['glxinfo']),
+                ("lspci", ['lspci'])]
+    results = {}
+
+    for name, command in commands:
         try:
-            p = subprocess.Popen(command,
-                                 stdout=subprocess.PIPE,
-                                 stderr=subprocess.PIPE,
-                                 universal_newlines=True)
-            (stdout, stderr) = p.communicate()
-        except:
-            return "Failed to run " + command
-        return stderr+stdout
-
-    def collectData(self):
-        result = {}
-        system = platform.system()
-        if (system == 'Windows' or system.find("CYGWIN_NT") == 0):
-            result['wglinfo'] = self.run('wglinfo')
-        else:
-            result['glxinfo'] = self.run('glxinfo')
-        if system == 'Linux':
-            result['lspci'] = self.run('lspci')
-        return result
+            results[name] = subprocess.check_output(command)
+        except OSError as e:
+            # This is the error raised if the binary isn't available
+            if e.errno == 2:
+                results[name] = "Not Available"
+            else:
+                raise
+
+    return results
diff --git a/piglit-run.py b/piglit-run.py
index 5a25376..d236d5d 100755
--- a/piglit-run.py
+++ b/piglit-run.py
@@ -120,7 +120,7 @@ def main():
     json_writer.close_dict()
 
     json_writer.write_dict_item('name', results.name)
-    for (key, value) in env.collectData().items():
+    for (key, value) in environment.get_environment().iteritems():
         json_writer.write_dict_item(key, value)
 
     profile = core.loadTestProfile(args.test_profile)
-- 
1.8.5.1



More information about the Piglit mailing list