[Piglit] [PATCH 9/9] framework/backends: Refactor base classes for better code sharing

Dylan Baker baker.dylan.c at gmail.com
Tue Sep 23 17:55:56 PDT 2014


This removes the FSyncMixin and replaces it with a FileBackend class.
This class provides more shared code between the JSONbackend and the
JunitBackend.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/backends/abstract.py | 59 ++++++++++++++++++++++++++----------------
 framework/backends/json_.py    | 12 ++-------
 framework/backends/junit.py    | 11 +++-----
 framework/programs/run.py      |  2 +-
 4 files changed, 43 insertions(+), 41 deletions(-)

diff --git a/framework/backends/abstract.py b/framework/backends/abstract.py
index 7665cb7..2850297 100644
--- a/framework/backends/abstract.py
+++ b/framework/backends/abstract.py
@@ -27,29 +27,7 @@ This module provides mixins and base classes for backend modules.
 
 import os
 import abc
-
-
-class FSyncMixin(object):
-    """ Mixin class that adds fsync support
-
-    This class provides an init method that sets self._file_sync from a keyword
-    arugment file_fsync, and then provides an _fsync() method that does an
-    fsync if self._file_sync is truthy
-
-    """
-    def __init__(self, file_fsync=False, **options):
-        self._file_sync = file_fsync
-
-    def _fsync(self, file_):
-        """ Sync the file to disk
-
-        If self._fsync is truthy this will sync self._file to disk
-
-        """
-        file_.flush()
-
-        if self._file_sync:
-            os.fsync(file_.fileno())
+import itertools
 
 
 class Backend(object):
@@ -131,3 +109,38 @@ class Backend(object):
         data -- a TestResult object representing the test data
 
         """
+
+
+class FileBackend(Backend):
+    """ A baseclass for file based backends
+
+    This class provides a few methods and setup required for a file based
+    backend.
+
+    Arguments:
+    dest -- a folder to store files in
+
+    Keyword Arguments:
+    file_start_count -- controls the counter for the test result files.
+                        Whatever this is set to will be the first name of the
+                        tests. It is important for resumes that this is not
+                        overlapping as the Inheriting classes assume they are
+                        not. Default: 0
+    file_sync -- If file_sync is truthy then the _fsync method will flush and
+                 sync files to disk, otherwise it will pass. Defualt: False
+
+    """
+    def __init__(self, dest, file_start_count=0, file_fsync=False, **kwargs):
+        self._dest = dest
+        self._counter = itertools.count(file_start_count)
+        self._file_sync = file_fsync
+
+    def _fsync(self, file_):
+        """ Sync the file to disk
+
+        If self._file_sync is truthy this will sync self._file to disk
+
+        """
+        file_.flush()
+        if self._file_sync:
+            os.fsync(file_.fileno())
diff --git a/framework/backends/json_.py b/framework/backends/json_.py
index 8b377d1..4632680 100644
--- a/framework/backends/json_.py
+++ b/framework/backends/json_.py
@@ -22,13 +22,12 @@
 
 import os
 import shutil
-import itertools
 try:
     import simplejson as json
 except ImportError:
     import json
 import framework.status as status
-from .abstract import Backend, FSyncMixin
+from .abstract import FileBackend
 
 __all__ = [
     'CURRENT_JSON_VERSION',
@@ -54,7 +53,7 @@ def piglit_encoder(obj):
     return obj
 
 
-class JSONBackend(FSyncMixin, Backend):
+class JSONBackend(FileBackend):
     """ Piglit's native JSON backend
 
     This writes out to piglit's native json backend. This class uses the python
@@ -69,13 +68,6 @@ class JSONBackend(FSyncMixin, Backend):
     """
     INDENT = 4
 
-    def __init__(self, dest, start_count=0, **options):
-        self._dest = dest
-        FSyncMixin.__init__(self, **options)
-
-        # A counter the ensures each test gets a unique name
-        self._counter = itertools.count(start_count)
-
     def initialize(self, metadata):
         """ Write boilerplate json code
 
diff --git a/framework/backends/junit.py b/framework/backends/junit.py
index ed21471..fe0f0c1 100644
--- a/framework/backends/junit.py
+++ b/framework/backends/junit.py
@@ -23,20 +23,19 @@
 import os
 import re
 import posixpath
-import itertools
 import shutil
 try:
     from lxml import etree
 except ImportError:
     import xml.etree.cElementTree as etree
-from .abstract import Backend, FSyncMixin
+from .abstract import FileBackend
 
 __all__ = [
     'JUnitBackend',
 ]
 
 
-class JUnitBackend(FSyncMixin, Backend):
+class JUnitBackend(FileBackend):
     """ Backend that produces ANT JUnit XML
 
     Based on the following schema:
@@ -45,11 +44,9 @@ class JUnitBackend(FSyncMixin, Backend):
     """
     _REPLACE = re.compile(r'[/\\]')
 
-    def __init__(self, dest, junit_test_suffix='', start_count=0, **options):
-        FSyncMixin.__init__(self, **options)
-        self._dest = dest
+    def __init__(self, dest, junit_test_suffix='', **options):
+        super(JUnitBackend, self).__init__(dest, **options)
         self._test_suffix = junit_test_suffix
-        self._counter = itertools.count(start_count)
 
     def initialize(self, metadata):
         """ Do nothing
diff --git a/framework/programs/run.py b/framework/programs/run.py
index e7ec65f..9d0ace4 100644
--- a/framework/programs/run.py
+++ b/framework/programs/run.py
@@ -307,7 +307,7 @@ def resume(input_):
     backend = backends.get_backend('json')(
         args.results_path,
         file_fsync=opts.sync,
-        start_count=len(results.tests) + 1)
+        file_start_count=len(results.tests) + 1)
     # Specifically do not initialize again, everything initialize does is done.
 
     for name in results.tests.iterkeys():
-- 
2.1.1



More information about the Piglit mailing list