[Piglit] [PATCH] framework: add a --version flag

Dylan Baker baker.dylan.c at gmail.com
Tue Mar 22 18:31:54 UTC 2016


This adds a --version flag to the piglit command, which prints the git
sha and date of the commit.

This command has two ways it works. The first is trigger when piglit
is installed, it uses a generated version.py file which stores the
output of git log (with some options). The second is a fallback option
which queries git locally to calculate the same value.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 CMakeLists.txt                |  3 ++-
 framework/CMakeLists.txt      | 13 +++++++++++++
 framework/core.py             | 12 ++++++++++++
 framework/programs/parsers.py |  1 +
 framework/programs/run.py     |  2 +-
 framework/version.py.in       | 45 +++++++++++++++++++++++++++++++++++++++++++
 piglit                        | 14 +++++++++++++-
 7 files changed, 87 insertions(+), 3 deletions(-)
 create mode 100644 framework/CMakeLists.txt
 create mode 100644 framework/version.py.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3b9f5f3..1157fbe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -484,6 +484,7 @@ include(cmake/piglit_dispatch.cmake)
 include_directories(src)
 add_subdirectory(cmake/target_api)
 add_subdirectory(generated_tests)
+add_subdirectory(framework)
 
 
 ##############################################################################
@@ -498,7 +499,7 @@ install (
 )
 
 install (
-	DIRECTORY framework
+	DIRECTORY framework ${piglit_BINARY_DIR}/framework
 	DESTINATION ${PIGLIT_INSTALL_LIBDIR}
 	FILES_MATCHING PATTERN "*.py"
 )
diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt
new file mode 100644
index 0000000..27e9e44
--- /dev/null
+++ b/framework/CMakeLists.txt
@@ -0,0 +1,13 @@
+find_program(GIT_BIN git)
+if(GIT_BIN)
+	exec_program(
+		${GIT_BIN} ${piglit_SOURCE_DIR}
+		ARGS log --pretty=format:'%H : %cD' -n1
+		OUTPUT_VARIABLE PIGLIT_VERSION
+	)
+
+	configure_file(
+		"${piglit_SOURCE_DIR}/framework/version.py.in"
+		"${piglit_BINARY_DIR}/framework/version.py"
+	)
+endif(GIT_BIN)
diff --git a/framework/core.py b/framework/core.py
index 3906b58..58ab277 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -25,6 +25,7 @@
 from __future__ import (
     absolute_import, division, print_function, unicode_literals
 )
+import collections
 import errno
 import os
 import subprocess
@@ -33,10 +34,21 @@ import sys
 from six.moves import configparser
 
 from framework import exceptions
+try:
+    from framework.version import VERSION
+except ImportError:
+    # Fallback for non compiled version of piglit.
+    _Version = collections.namedtuple('_Version', ['sha', 'date'])
+    VERSION = _Version(*subprocess.check_output(
+        ['git', 'log', '--pretty=format:"%H : %cD"', '-n1'],
+        cwd=os.path.abspath(os.path.join(__file__, '..'))
+    # Convert to unicode, trim the leading and trailing quotes and split
+    ).decode('utf-8')[1:-1].split(' : '))
 
 __all__ = [
     'PIGLIT_CONFIG',
     'PLATFORMS',
+    'VERSION',
     'PiglitConfig',
     'collect_system_info',
     'parse_listfile',
diff --git a/framework/programs/parsers.py b/framework/programs/parsers.py
index 9e1d1e3..701f966 100644
--- a/framework/programs/parsers.py
+++ b/framework/programs/parsers.py
@@ -31,6 +31,7 @@ from __future__ import (
     absolute_import, division, print_function, unicode_literals
 )
 import argparse
+import sys
 
 from framework import core
 
diff --git a/framework/programs/run.py b/framework/programs/run.py
index 581b350..27c055b 100644
--- a/framework/programs/run.py
+++ b/framework/programs/run.py
@@ -309,7 +309,7 @@ def resume(input_):
                         dest="no_retry",
                         action="store_true",
                         help="Do not retry incomplete tests")
-    args = parser.parse_args(input_)
+    args = parser.parse_args(unparsed)
     _disable_windows_exception_messages()
 
     results = backends.load(args.results_path)
diff --git a/framework/version.py.in b/framework/version.py.in
new file mode 100644
index 0000000..ac0be30
--- /dev/null
+++ b/framework/version.py.in
@@ -0,0 +1,45 @@
+# Copyright (c) 2016 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.
+
+"""Provides the version of piglit, a git sha and a commit date. 
+
+This module is generated by cmake at build time, and  is not guaranteed to
+exist or be importable, nor is the API at all guaranteed. Do not import this
+directly, instead use the version provided by the core module instead, which
+properly handles the possibility of this module not existing.
+
+"""
+
+from __future__ import (
+    absolute_import, division, print_function, unicode_literals
+)
+
+__all__ = [
+    'VERSION',
+]
+
+
+class _Version(object):
+    """An object that defines the version of piglit."""
+    def __init__(self, raw):
+        self.sha, self.date = raw.split(' : ')
+
+
+VERSION = _Version("@PIGLIT_VERSION@")
diff --git a/piglit b/piglit
index 514dd3f..610fefb 100755
--- a/piglit
+++ b/piglit
@@ -106,13 +106,18 @@ def setup_module_search_path():
 setup_module_search_path()
 import framework.programs.run as run
 import framework.programs.summary as summary
+from framework import core
 
 
 def main():
     """ Parse argument and call other executables """
     parser = argparse.ArgumentParser()
-    subparsers = parser.add_subparsers()
+    parser.add_argument('--version',
+                        dest='version',
+                        action='store_true',
+                        help='Print the piglit version and exit.')
 
+    subparsers = parser.add_subparsers()
     parse_run = subparsers.add_parser('run',
                                       add_help=False,
                                       help="Run a piglit test")
@@ -148,6 +153,13 @@ def main():
     # example), and then pass the arguments that this parser doesn't know about
     # to that executable
     parsed, args = parser.parse_known_args()
+
+    if parsed.version:
+        print('Piglit Version: {sha} ({date})'.format(sha=core.VERSION.sha,
+                                                      date=core.VERSION.date),
+              flush=True)
+        sys.exit(0)
+
     returncode = parsed.func(args)
     sys.exit(returncode)
 
-- 
2.7.4



More information about the Piglit mailing list