game_report: Adapt the scripts to work in Borealis

The current Borealis image has Python 3.6 installed, but the original
scripts were written for Python 3.7+. Several minor fixes were applied
to make it possible to use these scripts in Borealis.

BUG=b:172581250
TEST=Tested with both crostini and borealis guests

Change-Id: I4aafebf273cfe1be777cb202009b80fbf3f19034
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2606919
Commit-Queue: Robert Tarasov <tutankhamen@chromium.org>
Tested-by: Robert Tarasov <tutankhamen@chromium.org>
Reviewed-by: Po-Hsien Wang <pwang@chromium.org>
diff --git a/contrib/gfx/prepare_game_report.py b/contrib/gfx/prepare_game_report.py
index 52a7039..43ea85c 100755
--- a/contrib/gfx/prepare_game_report.py
+++ b/contrib/gfx/prepare_game_report.py
@@ -65,27 +65,6 @@
     f.write(json.dumps(data, indent=2))
 
 
-def parse_json_file(file_name):
-  """Parses the given JSON file and returns the result as a dictionary object"""
-  try:
-    with open(file_name) as json_file:
-      return json.loads(json_file)
-  except Exception as e:
-    return None
-
-
-def parse_script_stdout_json(script, args):
-  """Parses script's standart output JSON and returns the result as a dictionary object"""
-  try:
-    cmd = [os.path.join(os.path.dirname(os.path.realpath(__file__)), script)
-          ] + args
-    result = subprocess.run(
-        cmd, check=True, encoding='utf-8', capture_output=True)
-    return json.loads(result.stdout)
-  except Exception as e:
-    return None
-
-
 def main(args):
   defaultTraceFileName = os.path.join(TMP_DIR, DEFAULT_TRACE_FNAME)
   parser = argparse.ArgumentParser(description=__doc__)
@@ -103,8 +82,7 @@
       '--output-dir',
       default=TMP_DIR,
       dest='output_dir',
-      help=f'the output directory for the result file (default: {TMP_DIR})'
-  )
+      help=f'the output directory for the result file (default: {TMP_DIR})')
   parser.add_argument(
       '--trace-file',
       default=defaultTraceFileName,
@@ -133,7 +111,8 @@
 
     # Retrieving the game information from Steam.
     print('Retrieving the game information from Steam...')
-    game_info = parse_script_stdout_json('steam_game_info.py', [opts.gameid])
+    game_info = utils.parse_script_stdout_json('steam_game_info.py',
+                                               [opts.gameid])
     if not game_info:
       utils.panic(
           f'Unable to retrieve information for the game with steamid {opts.gameid}'
@@ -198,15 +177,15 @@
               input('Paste "Platform" string from chrome://version: ')
       }
       print('Collecting cros container system information...')
-      system_info['guest'] = parse_script_stdout_json('cros_container_info.py',
-                                                      [])
+      system_info['guest'] = utils.parse_script_stdout_json(
+          'cros_container_info.py', [])
       save_json(system_info, os.path.join(opts.temp_dir, SYSTEM_INFO_FNAME))
 
     if (game_info['can_start'] and
         yes_or_no('Did you manage to create the trace file?')):
       print(f'Preparing the trace file information for {opts.trace_file}...')
-      trace_info = parse_script_stdout_json('trace_file_info.py',
-                                            [opts.trace_file])
+      trace_info = utils.parse_script_stdout_json('trace_file_info.py',
+                                                  [opts.trace_file])
       if trace_info == None:
         utils.panic('Unable to retrieve the game trace information')
       file_time = datetime.datetime.fromtimestamp(
diff --git a/contrib/gfx/trace_file_info.py b/contrib/gfx/trace_file_info.py
index b6e7637..6850177 100755
--- a/contrib/gfx/trace_file_info.py
+++ b/contrib/gfx/trace_file_info.py
@@ -27,7 +27,11 @@
   try:
     cmd = ['apitrace', 'info', trace_fname]
     result = subprocess.run(
-        cmd, check=True, encoding='utf-8', capture_output=True)
+        cmd,
+        check=True,
+        encoding='utf-8',
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE)
 
     res = json.loads(result.stdout)
     data_results = {
diff --git a/contrib/gfx/utils.py b/contrib/gfx/utils.py
index 1b56933..71e6566 100644
--- a/contrib/gfx/utils.py
+++ b/contrib/gfx/utils.py
@@ -6,7 +6,10 @@
 
 from __future__ import print_function
 
+#import json
+import os
 import sys
+import subprocess
 import traceback
 
 
@@ -22,3 +25,35 @@
   traceback.print_exc(file=sys.stderr)
   print('-' * 60, file=sys.stderr)
   sys.exit(exit_code)
+
+
+def parse_json_file(file_name):
+  """Parses the given JSON file and returns the result as a dictionary object"""
+  try:
+    with open(file_name) as json_file:
+      return json.loads(json_file)
+  except Exception as e:
+    print('ERROR: ' + str(e))
+    return None
+
+
+def parse_cmd_stdout_json(cmd):
+  """Parses command's standard output JSON and returns the result as a dictionary object"""
+  try:
+    result = subprocess.run(
+        cmd,
+        check=True,
+        encoding='utf-8',
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE)
+    return json.loads(result.stdout)
+  except Exception as e:
+    print('ERROR: ' + str(e))
+    return None
+
+
+def parse_script_stdout_json(script, args):
+  """Parses script's standard output JSON and returns the result as a dictionary object"""
+  cmd = [os.path.join(os.path.dirname(os.path.realpath(__file__)), script)
+        ] + args
+  return parse_cmd_stdout_json(cmd)