[Piglit] [Patch v3 11/13] framework: consolidate boilerplate in JSNOWriter class

Dylan Baker baker.dylan.c at gmail.com
Fri Jun 20 12:50:37 PDT 2014


This moves all of the code for writing the name, the options, etc out of
run and resume and into a single method of JSONWriter. This should
reduce errors, code duplication, and help abstract a lot of problems
with future changes to the json away.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/programs/run.py | 36 ++++++++----------------------------
 framework/results.py      | 30 ++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 28 deletions(-)

diff --git a/framework/programs/run.py b/framework/programs/run.py
index 298f1e1..9255f64 100644
--- a/framework/programs/run.py
+++ b/framework/programs/run.py
@@ -160,28 +160,18 @@ def run(input_):
     result_filepath = path.join(args.results_path, 'main')
     result_file = open(result_filepath, 'w')
     json_writer = framework.results.JSONWriter(result_file)
-    json_writer.open_dict()
 
-    # Write out command line options for use in resuming.
-    json_writer.write_dict_key('options')
-    json_writer.open_dict()
-    json_writer.write_dict_item('profile', args.test_profile)
-    for key, value in env:
-        json_writer.write_dict_item(key, value)
+    # Create an dictionary to pass to initialize json, it needs the contents of
+    # the env dictionary and profile and platform information
+    options = {'profile': args.test_profile}
     if args.platform:
-        json_writer.write_dict_item('platform', args.platform)
-    json_writer.close_dict()
-
-    json_writer.write_dict_item('name', results.name)
-
-    for key, value in core.collect_system_info().iteritems():
-        json_writer.write_dict_item(key, value)
+        options['platform'] = args.platform
+    json_writer.initialize_json(options, results.name,
+                                core.collect_system_info())
 
     profile = framework.profile.merge_test_profiles(args.test_profile)
     profile.results_dir = args.results_path
 
-    json_writer.write_dict_key('tests')
-    json_writer.open_dict()
     time_start = time.time()
     # Set the dmesg type
     if args.dmesg:
@@ -224,19 +214,9 @@ def resume(input_):
 
     results_path = path.join(args.results_path, "main")
     json_writer = framework.results.JSONWriter(open(results_path, 'w+'))
-    json_writer.open_dict()
-    json_writer.write_dict_key("options")
-    json_writer.open_dict()
-    for key, value in results.options.iteritems():
-        json_writer.write_dict_item(key, value)
-    json_writer.close_dict()
-
-    json_writer.write_dict_item('name', results.name)
-    for key, value in core.collect_system_info().iteritems():
-        json_writer.write_dict_item(key, value)
+    json_writer.initialize_json(results.options, results.name,
+                                core.collect_system_info())
 
-    json_writer.write_dict_key('tests')
-    json_writer.open_dict()
     for key, value in results.tests.iteritems():
         json_writer.write_dict_item(key, value)
         env.exclude_tests.add(key)
diff --git a/framework/results.py b/framework/results.py
index 9823fc5..1edc423 100644
--- a/framework/results.py
+++ b/framework/results.py
@@ -118,6 +118,36 @@ class JSONWriter(object):
         #
         self.__is_collection_empty = []
 
+    def initialize_json(self, options, name, env):
+        """ Write boilerplate json code 
+
+        This writes all of the json except the actuall tests.
+
+        Arguments:
+        options -- any values to be put in the options dictionary, must be a
+                   dict-like object
+        name -- the name of the test
+        env -- any environment information to be written into the results, must
+               be a dict-like object
+
+        """
+        self.open_dict()
+        self.write_dict_item('name', name)
+
+        self.write_dict_key('options')
+        self.open_dict()
+        for key, value in options.iteritems():
+            # Loading a NoneType will break resume, and are a bug
+            assert value is not None, "Value {} is NoneType".format(key)
+            self.write_dict_item(key, value)
+        self.close_dict()
+
+        for key, value in env.iteritems():
+            self.write_dict_item(key, value)
+
+        self.write_dict_key('tests')
+        self.open_dict()
+
     @synchronized_self
     def __write_indent(self):
         if self.__inhibit_next_indent:
-- 
2.0.0



More information about the Piglit mailing list