Mesa (main): gallium/tools: fixes to option handling

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Jun 20 12:41:34 UTC 2022


Module: Mesa
Branch: main
Commit: 11ac33c2a9326906d6c14d7f07a788cf1eed888d
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=11ac33c2a9326906d6c14d7f07a788cf1eed888d

Author: Matti Hamalainen <ccr at tnsp.org>
Date:   Thu Jun 16 17:01:39 2022 +0300

gallium/tools: fixes to option handling

Not all options in the options object are set by all tools.
Instead of using argparse result objects directly, use separate
with default settings and copy relevant attributes there.

Signed-off-by: Matti Hamalainen <ccr at tnsp.org>
Reviewed-by: Jose Fonseca <jfonseca at vmware.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17108>

---

 src/gallium/tools/trace/dump_state.py | 15 +++++++++++++++
 src/gallium/tools/trace/model.py      | 21 ++++++++++++++++++++-
 src/gallium/tools/trace/parse.py      | 17 +++++++++++++++--
 3 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/src/gallium/tools/trace/dump_state.py b/src/gallium/tools/trace/dump_state.py
index 5683034a14d..366637b5678 100755
--- a/src/gallium/tools/trace/dump_state.py
+++ b/src/gallium/tools/trace/dump_state.py
@@ -789,6 +789,18 @@ class Interpreter(parser.SimpleTraceDumper):
         return self.options.verbosity >= level
     
 
+class DumpStateOptions(parser.ParseOptions):
+
+    def __init__(self, args=None):
+
+        # These will get initialized in ModelOptions.__init__()
+        self.verbosity = None
+        self.call = None
+        self.draw = None
+
+        parser.ParseOptions.__init__(self, args)
+
+
 class Main(parser.Main):
 
     def get_optparser(self):
@@ -804,6 +816,9 @@ class Main(parser.Main):
         optparser.add_argument("-d", "--draw", action="store", type=int, dest="draw", default=0xffffffff, help="dump on this draw")
         return optparser
 
+    def make_options(self, args):
+        return DumpStateOptions(args)
+
     def process_arg(self, stream, options):
         formatter = format.Formatter(sys.stderr)
         parser = Interpreter(stream, options, formatter)
diff --git a/src/gallium/tools/trace/model.py b/src/gallium/tools/trace/model.py
index 8350daf744b..943f97b8c1a 100755
--- a/src/gallium/tools/trace/model.py
+++ b/src/gallium/tools/trace/model.py
@@ -37,6 +37,25 @@ from io import StringIO
 import format
 
 
+class ModelOptions:
+
+    def __init__(self, args=None):
+        # Initialize the options we need to exist,
+        # with some reasonable defaults
+        self.plain = False
+        self.suppress_variants = False
+        self.named_ptrs = False
+        self.method_only = False
+
+        # If args is specified, we assume it is the result object
+        # from ArgumentParser.parse_args(). Copy the attribute values
+        # we have from it, if they exist.
+        if args is not None:
+            for var in self.__dict__:
+                if var in args.__dict__:
+                    self.__dict__[var] = args.__dict__[var]
+
+
 class Node:
     
     def visit(self, visitor):
@@ -238,7 +257,7 @@ class PrettyPrinter:
         self.formatter.text('}')
     
     def visit_pointer(self, node):
-        if "named_ptrs" in self.options and self.options.named_ptrs:
+        if self.options.named_ptrs:
             self.formatter.address(node.ptr_list[node.address])
         else:
             self.formatter.address(node.address)
diff --git a/src/gallium/tools/trace/parse.py b/src/gallium/tools/trace/parse.py
index cbf6896f9df..8afba7c51a4 100755
--- a/src/gallium/tools/trace/parse.py
+++ b/src/gallium/tools/trace/parse.py
@@ -382,7 +382,16 @@ class TraceDumper(SimpleTraceDumper):
         for call in self.call_stack:
             call.visit(self.pretty_printer)
             self.formatter.newline()
-        
+
+
+class ParseOptions(ModelOptions):
+
+    def __init__(self, args=None):
+        # Initialize options local to this module
+        self.plain = False
+
+        ModelOptions.__init__(self, args)
+
 
 class Main:
     '''Common main class for all retrace command line utilities.''' 
@@ -393,6 +402,7 @@ class Main:
     def main(self):
         optparser = self.get_optparser()
         args = optparser.parse_args()
+        options = self.make_options(args)
 
         for fname in args.filename:
             try:
@@ -408,7 +418,10 @@ class Main:
                 print("ERROR: {}".format(str(e)))
                 sys.exit(1)
 
-            self.process_arg(stream, args)
+            self.process_arg(stream, options)
+
+    def make_options(self, args):
+        return ParseOptions(args)
 
     def get_optparser(self):
         optparser = argparse.ArgumentParser(



More information about the mesa-commit mailing list