tools/fiograph: accommodate job files not ending in .fio
authorVincent Fu <vincent.fu@samsung.com>
Thu, 19 Jan 2023 00:58:08 +0000 (19:58 -0500)
committerVincent Fu <vincent.fu@samsung.com>
Mon, 23 Jan 2023 18:51:16 +0000 (13:51 -0500)
For job files not ending in .fio, fiograph will overwrite the job file
with a graphviz script and then delete it if --keep is not specified.

Fix this by creating temporary files to contain the graphviz script and
image file. Then rename or delete the script and image file as directed
by the user specified options.

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

index 86ed40a810e2708d87167fb1f33f3e71d2b2f5cf..cfb9b041879f21902062bfc4d074fce2cb6cc161 100755 (executable)
@@ -1,4 +1,6 @@
 #!/usr/bin/env python3
+import uuid
+import time
 import errno
 from graphviz import Digraph
 import argparse
@@ -293,13 +295,6 @@ 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'
@@ -312,9 +307,25 @@ def main():
     config_file = configparser.RawConfigParser(allow_no_value=True)
     config_file.read(config_filename)
 
-    fio_to_graphviz(args.file, args.format).render(output_file, view=args.view)
+    temp_filename = uuid.uuid4().hex
+    image_filename = fio_to_graphviz(args.file, args.format).render(temp_filename, view=args.view)
+
+    output_filename_stub = args.file
+    if args.output:
+        output_filename = args.output
+    else:
+        if output_filename_stub.endswith('.fio'):
+            output_filename_stub = output_filename_stub[:-4]
+        output_filename = image_filename.replace(temp_filename, output_filename_stub)
+    if args.view:
+        time.sleep(1)
+        # allow time for the file to be opened before renaming it
+    os.rename(image_filename, output_filename)
+
     if not args.keep:
-        os.remove(output_file)
+        os.remove(temp_filename)
+    else:
+        os.rename(temp_filename, output_filename_stub + '.gv')
 
 
 main()