tools/fiograph: improve default config file search
authorVincent Fu <vincent.fu@samsung.com>
Wed, 18 Jan 2023 16:14:14 +0000 (11:14 -0500)
committerVincent Fu <vincent.fu@samsung.com>
Thu, 19 Jan 2023 00:52:15 +0000 (19:52 -0500)
When a config file is not explicitly specified the current default is to
search for fiograph.conf only in the current directory. Change this to
try to use fiograph.conf in the directory where fiograph.py is located
when fiograph.conf is not found in the current directory.

Signed-off-by: Vincent Fu <vincent.fu@samsung.com>
tools/fiograph/fiograph.py

index 03c2cacd2d05211667c2a95c0cdba13f0d872dc9..86ed40a810e2708d87167fb1f33f3e71d2b2f5cf 100755 (executable)
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+import errno
 from graphviz import Digraph
 import argparse
 import configparser
@@ -283,7 +284,6 @@ def setup_commandline():
                         help='keep the graphviz script file')
     parser.add_argument('--config', action='store',
                         type=str,
-                        default='fiograph.conf',
                         help='the configuration filename')
     args = parser.parse_args()
     return args
@@ -292,14 +292,26 @@ def setup_commandline():
 def main():
     global config_file
     args = setup_commandline()
+
     if args.output is None:
         output_file = args.file
         if output_file.endswith('.fio'):
             output_file = output_file[:-4]
     else:
         output_file = args.output
+
+    if args.config is None:
+        if os.path.exists('fiograph.conf'):
+            config_filename = 'fiograph.conf'
+        else:
+            config_filename = os.path.join(os.path.dirname(__file__), 'fiograph.conf')
+            if not os.path.exists(config_filename):
+                raise FileNotFoundError("Cannot locate configuration file")
+    else:
+        config_filename = args.config
     config_file = configparser.RawConfigParser(allow_no_value=True)
-    config_file.read(args.config)
+    config_file.read(config_filename)
+
     fio_to_graphviz(args.file, args.format).render(output_file, view=args.view)
     if not args.keep:
         os.remove(output_file)