[Piglit] [PATCH 4/4] Python: Remove code duplication
Dylan Baker
baker.dylan.c at gmail.com
Fri Aug 16 09:09:38 PDT 2013
This patch removes the Result class from Summary, and the
loadTestResults function from core. It does this by integrating the
functionality into TestrunResult.
Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
framework/core.py | 62 ++++++++++++++++++++++++-------------------------
framework/summary.py | 34 +++------------------------
piglit-summary-junit.py | 2 +-
3 files changed, 34 insertions(+), 64 deletions(-)
diff --git a/framework/core.py b/framework/core.py
index 680f3ab..2714bbf 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -261,7 +261,7 @@ class GroupResult(dict):
class TestrunResult:
- def __init__(self):
+ def __init__(self, file):
self.serialized_keys = ['options',
'name',
'tests',
@@ -275,6 +275,32 @@ class TestrunResult:
self.time_elapsed = None
self.tests = {}
+ self._load(file)
+ self._parseFile()
+
+ def _load(self, result):
+ """
+ Private: Load a result file, and store that file object as an
+ attribute.
+ """
+ # First assume that result is a main.zip file, if that fails assume
+ # that it is a main file. If that fails, try the same file order, but
+ # looking in a directory
+ try:
+ try:
+ zip = zipfile.ZipFile(result, 'r')
+ self.file = zip.open('main', 'r')
+ except zipfile.BadZipfile:
+ with open(result, 'r') as file:
+ self.file = file
+ except IOError:
+ try:
+ zip = zipfile.ZipFile(os.path.join(result, 'main.zip'), 'r')
+ self.file = zip.open('main', 'r')
+ except zipfile.BadZipfile:
+ with open(os.path.join(result, 'main'), 'r') as file:
+ self.file = file
+
def __repairFile(self, file):
'''
Reapair JSON file if necessary
@@ -343,13 +369,13 @@ class TestrunResult:
raw_dict = dict([(k, self.__dict__[k]) for k in keys])
json.dump(raw_dict, file, indent=JSONWriter.INDENT)
- def parseFile(self, file):
+ def _parseFile(self):
# Attempt to open the json file normally, if it fails then attempt to
# repair it.
try:
- raw_dict = json.load(file)
+ raw_dict = json.load(self.file)
except ValueError:
- raw_dict = json.load(self.__repairFile(file))
+ raw_dict = json.load(self.__repairFile(self.file))
# Check that only expected keys were unserialized.
for key in raw_dict:
@@ -598,34 +624,6 @@ def loadTestProfile(filename):
return ns['profile']
-def loadTestResults(relativepath):
- """
- Takes a result file as an argument, opens it, and creates a TestrunResult
- object out of it, which it returns
- """
- path = os.path.realpath(relativepath)
- testrun = TestrunResult()
-
- try:
- try:
- zip = zipfile.ZipFile(os.path.join(path, 'main.zip'), 'r')
- file = zip.open('main', 'r')
- except zipfile.BadZipfile:
- with open(os.path.join(path, 'main')) as f:
- file = f
- except IOError:
- try:
- zip = zipfile.ZipFile(path, 'r')
- file = zip.open('main', 'r')
- except zipfile.BadZipfile:
- with open(path) as f:
- file = f
-
- testrun.parseFile(file)
- assert(testrun.name)
- return testrun
-
-
# Error messages to be ignored
Test.ignoreErrors = map(re.compile,
["couldn't open libtxc_dxtn.so",
diff --git a/framework/summary.py b/framework/summary.py
index 49b53b9..8e4871a 100644
--- a/framework/summary.py
+++ b/framework/summary.py
@@ -22,7 +22,6 @@
import os
import os.path as path
import string
-import zipfile
from itertools import izip_longest
from shutil import copy
from json import loads
@@ -35,33 +34,6 @@ __all__ = [
]
-class Result(core.TestrunResult):
- """
- Object that opens, reads, and stores the data in a resultfile.
- """
- def __init__(self, resultfile):
- # Run the init from TestrunResult
- core.TestrunResult.__init__(self)
-
- # Attempt to load the json file, first looke for a zipped version, and
- # if that doesn't exist look for an unziped version. Assume first that
- # a directory was prvided, and then that a file was provided
- try:
- try:
- zip = zipfile.ZipFile(resultfile)
- result = self.parseFile(zip.open('main', 'r'))
- except zipfile.BadZipfile:
- with open(resultfile, 'r') as file:
- result = self.parseFile(file)
- except IOError:
- try:
- zip = zipfile.ZipFile(path.join(resultfile, 'main.zip'))
- result = self.parseFile(zip.open('main', 'r'))
- except IOError:
- with open(path.join(resultfile, 'main'), 'r') as file:
- result = self.parseFile(file)
-
-
class HTMLIndex(list):
"""
Builds HTML output to be passed to the index mako template, which will be
@@ -387,9 +359,9 @@ class Summary:
return counts, status
- # Create a Result object for each piglit result and append it to the
- # results list
- self.results = [Result(i) for i in resultfiles]
+ # Create a TestrunResult object for each piglit result and append it to
+ # the results list
+ self.results = [core.TestrunResult(i) for i in resultfiles]
self.status = {}
self.fractions = {}
diff --git a/piglit-summary-junit.py b/piglit-summary-junit.py
index d9e4b9c..247de9b 100755
--- a/piglit-summary-junit.py
+++ b/piglit-summary-junit.py
@@ -267,7 +267,7 @@ class Writer:
self.path = []
def write(self, arg):
- results = [core.loadTestResults(arg)]
+ results = [core.TestrunResult(arg)]
summary = Summary(results)
self.report.start()
--
1.8.1.5
More information about the Piglit
mailing list