[Piglit] [PATCH 2/2] utils: add utility to update result names.

Dylan Baker baker.dylan.c at gmail.com
Tue Jun 9 16:43:03 PDT 2015


This little helper utility fixes the names of json results to be the
same as the name of the folder they are stored in.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/backends/json.py |  7 ++--
 framework/summary.py       |  1 +
 utils/__init__.py          |  0
 utils/fix_name.py          | 96 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 101 insertions(+), 3 deletions(-)
 create mode 100644 utils/__init__.py
 create mode 100755 utils/fix_name.py

diff --git a/framework/backends/json.py b/framework/backends/json.py
index 48a34a8..dae93c8 100644
--- a/framework/backends/json.py
+++ b/framework/backends/json.py
@@ -38,6 +38,7 @@ from .register import Registry
 __all__ = [
     'REGISTRY',
     'JSONBackend',
+    'write',
 ]
 
 # The current version of the JSON results
@@ -150,7 +151,7 @@ class JSONBackend(FileBackend):
         shutil.rmtree(os.path.join(self._dest, 'tests'))
 
     @staticmethod
-    def _write(f, name, data):
+    def write(f, name, data):
         json.dump({name: data}, f, default=piglit_encoder)
 
 
@@ -300,7 +301,7 @@ def _update_results(results, filepath):
     # Move the old results, and write the current results
     try:
         os.rename(filepath, filepath + '.old')
-        _write(results, filepath)
+        write(results, filepath)
     except OSError:
         print("WARNING: Could not write updated results {}".format(filepath),
               file=sys.stderr)
@@ -308,7 +309,7 @@ def _update_results(results, filepath):
     return results
 
 
-def _write(results, file_):
+def write(results, file_):
     """WRite the values of the results out to a file."""
     with open(file_, 'w') as f:
         json.dump({k:v for k, v in results.__dict__.iteritems()},
diff --git a/framework/summary.py b/framework/summary.py
index ecd59f1..f4ee590 100644
--- a/framework/summary.py
+++ b/framework/summary.py
@@ -484,6 +484,7 @@ class Summary:
                         'Two or more of your results have the same "name" '
                         'attribute. Try changing one or more of the "name" '
                         'values in your json files.\n'
+                        'utils/fix_names.py can help with this problem.\n'
                         'Duplicate value: {}'.format(name))
                 else:
                     raise e
diff --git a/utils/__init__.py b/utils/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/utils/fix_name.py b/utils/fix_name.py
new file mode 100755
index 0000000..c90fb25
--- /dev/null
+++ b/utils/fix_name.py
@@ -0,0 +1,96 @@
+#!/usr/bin/python2
+
+# Copyright (c) 2015 Intel Corporation
+
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+"""Fix the name value in one or more json results to match the folder name.
+
+This utility can be used on results to fix the name in the json dictionary to
+match the name of the folder. Can be run with multiple names. It can resolve
+both Unix ~ and shell variables
+
+example:
+fix_name.py ~/piglit-results/*
+
+"""
+
+from __future__ import print_function, division, absolute_import
+import argparse
+import os
+import sys
+
+sys.path.insert(0, os.path.abspath(os.path.dirname('__file__')))
+
+from framework.backends import load
+from framework.backends.json import write
+
+
+def enumerate_directories(directories):
+    """Generator that walks directories and returns the name of each directory
+    that contains a .json file.
+
+    """
+    def resolve_vars(name):
+        return os.path.expanduser(os.path.expandvars(name))
+
+    for directory in directories:
+        for dirpath, _, filenames in os.walk(resolve_vars(directory)):
+            for filename in filenames:
+                if os.path.splitext(filename)[1] == '.json':
+                    yield (os.path.basename(dirpath),
+                           os.path.join(dirpath, filename))
+
+
+def needs_update(dirname, results):
+    """Compares the value of 'name', and the name of the directory.
+
+    If the two names are the same returns False, otherwise returns True
+
+    """
+    return results.name != dirname
+
+
+def parse():
+    """Parse command line arguments."""
+    parser = argparse.ArgumentParser()
+    parser.add_argument('directories',
+                        nargs='+',
+                        help='directories containing results to search for')
+    return parser.parse_args()
+
+
+def update(results, dirname, filepath):
+    """Update the results, write them, and print the output."""
+    results.name = dirname
+    write(results, filepath)
+    print('Updating name of {} to {}'.format(filepath, dirname))
+
+
+def main():
+    """main function."""
+    args = parse()
+    for dirname, filepath in enumerate_directories(args.directories):
+        results = load(filepath)
+        if needs_update(dirname, results):
+            update(results, dirname, filepath)
+
+
+if __name__ == '__main__':
+    main()
-- 
2.4.2



More information about the Piglit mailing list