[Piglit] [PATCH] summary: Moves parse_listfile to framework/core.py

Dylan Baker baker.dylan.c at gmail.com
Mon Jul 8 17:22:09 PDT 2013


This function was previously duplicated exactly in both
piglit-summary.py and piglit-summary-html.py. Moving this function to
core reduces code duplication.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/core.py      | 22 ++++++++++++++++++++++
 piglit-summary-html.py | 12 +-----------
 piglit-summary.py      |  9 +--------
 3 files changed, 24 insertions(+), 19 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index df6bece..5c0e864 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -25,6 +25,7 @@
 import errno
 import json
 import os
+import os.path as path
 import platform
 import re
 import stat
@@ -667,3 +668,24 @@ Test.ignoreErrors = map(re.compile, [
         "No memory leaks detected.",
         "libGL: Can't open configuration file.*",
 ])
+
+
+def parse_listfile(filename):
+    """
+    Parses a new-line seperated list in a text file and returns a python list
+    object. It will expand a tilde (~) when used to mean $HOME, as well as
+    shell variables.
+
+    ex file.txt:
+        ~/tests1
+        ~/tests2/main
+        /tmp/test3
+        $HOME/test4
+
+    returns:
+        ['/home/user/tests1', '/home/user/tests2/main', '/tmp/test3',
+         /home/user/test4']
+    """
+    with open(filename, 'r') as file:
+        return [path.expanduser(path.expandvars(i.rstrip('\n')))
+                for i in file.readlines()]
diff --git a/piglit-summary-html.py b/piglit-summary-html.py
index 276b187..cb16124 100755
--- a/piglit-summary-html.py
+++ b/piglit-summary-html.py
@@ -27,21 +27,11 @@ import shutil
 import os.path as path
 
 import framework.summary as summary
-from framework.core import checkDir
+from framework.core import checkDir, parse_listfile
 
 sys.path.append(path.dirname(path.realpath(sys.argv[0])))
 
 
-def parse_listfile(filename):
-    """
-    Read a list of newline seperated flies and return them as a python list.
-    strip the last newline character so the list doesn't have an extra ''
-    element at the end.
-    """
-    with open(filename, 'r') as file:
-        return [path.expanduser(i) for i in file.read().rstrip().split('\n')]
-
-
 def main():
     parser = argparse.ArgumentParser()
     parser.add_argument("-o", "--overwrite",
diff --git a/piglit-summary.py b/piglit-summary.py
index 99497ef..3f197cb 100755
--- a/piglit-summary.py
+++ b/piglit-summary.py
@@ -35,14 +35,7 @@ import sys
 
 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
 import framework.summary as summary
-
-
-def parse_listfile(filename):
-    """
-    Read a list of newline seperated file names and return them as a list
-    """
-    return open(filename, "r").read().rstrip().split('\n')
-
+from framework.core import parse_listfile
 
 
 def main():
-- 
1.8.3.1



More information about the Piglit mailing list