[Autotest][Py3] Migrating client/tools/* to Python3

BUG=chromium:990593
TEST=dummy_Pass

Change-Id: Id925878365a3ec24a820833334f260703de580b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2546859
Tested-by: Derek Beckett <dbeckett@chromium.org>
Reviewed-by: Greg Edelston <gredelston@google.com>
Commit-Queue: Derek Beckett <dbeckett@chromium.org>
diff --git a/client/tools/autotest b/client/tools/autotest
index 0406cc0..2d4aad8 100755
--- a/client/tools/autotest
+++ b/client/tools/autotest
@@ -1,5 +1,10 @@
 #!/usr/bin/python2
-import sys,os
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import os
+import sys
 
 autodir = None
 autotest_conf = os.path.realpath('/etc/autotest.conf')
@@ -12,7 +17,7 @@
             autodir = path
 
 if not autodir:
-    print "Autotest home dir NOT FOUND"
+    print("Autotest home dir NOT FOUND")
     sys.exit()
 
 autotest = os.path.join(autodir, 'bin/autotest')
@@ -21,7 +26,7 @@
 
 if len(sys.argv) == 1 or sys.argv[1] == 'start':
     if os.path.exists(state):
-        print "Restarting partially completed autotest job"
+        print("Restarting partially completed autotest job")
         os.system(autotest + ' --continue ' + control)
     else:
-        print "No autotest jobs outstanding"
+        print("No autotest jobs outstanding")
diff --git a/client/tools/avgtime b/client/tools/avgtime
index 4145594..283567b 100755
--- a/client/tools/avgtime
+++ b/client/tools/avgtime
@@ -1,5 +1,12 @@
 #!/usr/bin/python2
-import sys, os, re
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import os
+import re
+import sys
+
 
 def avg_deviation(values):
     sum = 0
@@ -29,7 +36,7 @@
 
     list.append((user, system, elapsed, cpu))
 
-print "   user: %0.2fs (%0.2f%%)" % avg_deviation([x[0] for x in list])
-print " system: %0.2fs (%0.2f%%)" % avg_deviation([x[1] for x in list])
-print "elapsed: %0.2fs (%0.2f%%)" % avg_deviation([x[2] for x in list])
-print "    cpu: %d%% (%0.2f%%)" % avg_deviation([x[3] for x in list])
+print("   user: %0.2fs (%0.2f%%)" % avg_deviation([x[0] for x in list]))
+print(" system: %0.2fs (%0.2f%%)" % avg_deviation([x[1] for x in list]))
+print("elapsed: %0.2fs (%0.2f%%)" % avg_deviation([x[2] for x in list]))
+print("    cpu: %d%% (%0.2f%%)" % avg_deviation([x[3] for x in list]))
diff --git a/client/tools/crash_handler.py b/client/tools/crash_handler.py
index c052a66..194e605 100755
--- a/client/tools/crash_handler.py
+++ b/client/tools/crash_handler.py
@@ -5,7 +5,21 @@
 @copyright Red Hat Inc 2009
 @author Lucas Meneghel Rodrigues <lmr@redhat.com>
 """
-import sys, os, commands, glob, shutil, syslog, re, time, random, string
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import commands
+import glob
+import os
+import random
+import re
+import shutil
+import six
+import string
+import sys
+import syslog
+import time
 
 
 def generate_random_string(length):
@@ -90,10 +104,10 @@
                 results_dir_list.append(pid_dir_dict[pid])
             pid = get_parent_pid(pid)
     else:
-        results_dir_list = pid_dir_dict.values()
+        results_dir_list = list(pid_dir_dict.values())
 
     return (results_dir_list or
-            pid_dir_dict.values() or
+            list(pid_dir_dict.values()) or
             [os.path.join("/tmp", core_dir_basename)])
 
 
@@ -144,7 +158,7 @@
                    (exe_path, path, gdb_command_path))
         backtrace = commands.getoutput(gdb_cmd)
         # Sanitize output before passing it to the report
-        backtrace = backtrace.decode('utf-8', 'ignore')
+        backtrace = six.ensure_text(backtrace, 'utf-8', 'ignore')
     else:
         exe_path = "Unknown"
         backtrace = ("Could not determine backtrace for core file %s" % path)
@@ -190,7 +204,7 @@
         try:
             crashed_pid, crash_time, uid, signal, hostname, exe = sys.argv[1:]
             full_functionality = True
-        except ValueError, e:
+        except ValueError as e:
             # Probably due a kernel bug, we can't exactly map the parameters
             # passed to this script. So we have to reduce the functionality
             # of the script (just write the core at a fixed place).
@@ -216,5 +230,5 @@
             syslog.syslog("Application %s, PID %s crashed" % (exe, crashed_pid))
         write_cores(core_file, results_dir_list)
 
-    except Exception, e:
+    except Exception as e:
         syslog.syslog("Crash handler had a problem: %s" % e)
diff --git a/client/tools/diffprofile b/client/tools/diffprofile
index 3d7f3ce..bc7bdb6 100755
--- a/client/tools/diffprofile
+++ b/client/tools/diffprofile
@@ -2,7 +2,13 @@
 # Copyright Martin J. Bligh (mbligh@google.com)
 # Released under the GPL, v2
 
-import os, sys, re
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import os
+import re
+import sys
 
 results_per_sign = 10
 
@@ -30,21 +36,21 @@
 
 for (key, count) in parse_lines(sys.argv[1]):
     # Oprofile seems to be ... erm ... broken. Keys can appear > once ;-(
-    if orig.has_key(key):
+    if key in orig:
         orig[key] += count
     else:
         orig[key] = count
-    if diff.has_key(key):
+    if key in diff:
         diff[key] -= count
     else:
         diff[key] = -count
 
 for (key, count) in parse_lines(sys.argv[2]):
-    if new.has_key(key):
+    if key in new:
         new[key] += count
     else:
         new[key] = count
-    if diff.has_key(key):
+    if key in diff:
         diff[key] += count
     else:
         diff[key] = count
@@ -54,18 +60,18 @@
 
 # Now sort and print the diffs.
 def print_key(key):
-    if orig.has_key(key) and orig[key] > 0:
+    if key in orig and orig[key] > 0:
         pct = (100 * diff[key]) / orig[key]
     else:
         pct = 0
-    print "%10d  %6.1f%% %s" % (diff[key], pct, key)
+    print("%10d  %6.1f%% %s" % (diff[key], pct, key))
 
-keys = sorted(diff.keys(), key=lambda x : diff[x], reverse = True)
+keys = sorted(list(diff.keys()), key=lambda x: diff[x], reverse=True)
 
 for key in keys[:results_per_sign]:
     print_key(key)
 
-print "\n...\n"
+print("\n...\n")
 
 for key in keys[len(keys)-results_per_sign:]:
     print_key(key)
diff --git a/client/tools/html_report.py b/client/tools/html_report.py
index 2bba72c..783b93e 100755
--- a/client/tools/html_report.py
+++ b/client/tools/html_report.py
@@ -7,7 +7,10 @@
 @author: Dror Russo (drusso@redhat.com)
 """
 
-import os, sys, re, getopt, time, datetime, commands
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+import os, sys, re, getopt, time, datetime, subprocess
 import common
 
 
@@ -1416,8 +1419,8 @@
     else:   #if no output file defined, print html file to console
         output = sys.stdout
     # create html page
-    print >> output, html_prefix
-    print >> output, '<h2 id=\"page_title\">Autotest job execution report</h2>'
+    print(html_prefix, file=output)
+    print('<h2 id=\"page_title\">Autotest job execution report</h2>', file=output)
 
     # formating date and time to print
     t = datetime.datetime.now()
@@ -1444,20 +1447,20 @@
 
     kvm_ver_str = metadata.get('kvmver', None)
 
-    print >> output, '<table class="stats2">'
-    print >> output, '<tr><td>HOST</td><td>:</td><td>%s</td></tr>' % host
-    print >> output, '<tr><td>RESULTS DIR</td><td>:</td><td>%s</td></tr>'  % tag
-    print >> output, '<tr><td>DATE</td><td>:</td><td>%s</td></tr>' % now.ctime()
-    print >> output, '<tr><td>STATS</td><td>:</td><td>%s</td></tr>'% stat_str
-    print >> output, '<tr><td></td><td></td><td></td></tr>'
+    print('<table class="stats2">', file=output)
+    print('<tr><td>HOST</td><td>:</td><td>%s</td></tr>' % host, file=output)
+    print('<tr><td>RESULTS DIR</td><td>:</td><td>%s</td></tr>'  % tag, file=output)
+    print('<tr><td>DATE</td><td>:</td><td>%s</td></tr>' % now.ctime(), file=output)
+    print('<tr><td>STATS</td><td>:</td><td>%s</td></tr>'% stat_str, file=output)
+    print('<tr><td></td><td></td><td></td></tr>', file=output)
     if kvm_ver_str is not None:
-        print >> output, '<tr><td>KVM VERSION</td><td>:</td><td>%s</td></tr>' % kvm_ver_str
-    print >> output, '</table>'
+        print('<tr><td>KVM VERSION</td><td>:</td><td>%s</td></tr>' % kvm_ver_str, file=output)
+    print('</table>', file=output)
 
     ## print test results
-    print >> output, '<br>'
-    print >> output, '<h2 id=\"page_sub_title\">Test Results</h2>'
-    print >> output, '<h2 id=\"comment\">click on table headers to asc/desc sort</h2>'
+    print('<br>', file=output)
+    print('<h2 id=\"page_sub_title\">Test Results</h2>', file=output)
+    print('<h2 id=\"comment\">click on table headers to asc/desc sort</h2>', file=output)
     result_table_prefix = """<table
 id="t1" class="stats table-autosort:4 table-autofilter table-stripeclass:alternate table-page-number:t1page table-page-count:t1pages table-filtered-rowcount:t1filtercount table-rowcount:t1allcount">
 <thead class="th table-sorted-asc table-sorted-desc">
@@ -1471,24 +1474,24 @@
 </tr></thead>
 <tbody>
 """
-    print >> output, result_table_prefix
+    print(result_table_prefix, file=output)
     def print_result(result, indent):
         while result != []:
             r = result.pop(0)
             res = results[r][2]
-            print >> output, '<tr>'
-            print >> output, '<td align="left">%s</td>' % res['time']
-            print >> output, '<td align="left" style="padding-left:%dpx">%s</td>' % (indent * 20, res['title'])
+            print('<tr>', file=output)
+            print('<td align="left">%s</td>' % res['time'], file=output)
+            print('<td align="left" style="padding-left:%dpx">%s</td>' % (indent * 20, res['title']), file=output)
             if res['status'] == 'GOOD':
-                print >> output, '<td align=\"left\"><b><font color="#00CC00">PASS</font></b></td>'
+                print('<td align=\"left\"><b><font color="#00CC00">PASS</font></b></td>', file=output)
             elif res['status'] == 'FAIL':
-                print >> output, '<td align=\"left\"><b><font color="red">FAIL</font></b></td>'
+                print('<td align=\"left\"><b><font color="red">FAIL</font></b></td>', file=output)
             elif res['status'] == 'ERROR':
-                print >> output, '<td align=\"left\"><b><font color="red">ERROR!</font></b></td>'
+                print('<td align=\"left\"><b><font color="red">ERROR!</font></b></td>', file=output)
             else:
-                print >> output, '<td align=\"left\">%s</td>' % res['status']
+                print('<td align=\"left\">%s</td>' % res['status'], file=output)
             # print exec time (seconds)
-            print >> output, '<td align="left">%s</td>' % res['exec_time_sec']
+            print('<td align="left">%s</td>' % res['exec_time_sec'], file=output)
             # print log only if test failed..
             if res['log']:
                 #chop all '\n' from log text (to prevent html errors)
@@ -1501,39 +1504,39 @@
                 updated_tag = rx2.sub('_', res['title'])
 
                 html_body_text = '<html><head><title>%s</title></head><body>%s</body></html>' % (str(updated_tag), log_text)
-                print >> output, '<td align=\"left\"><A HREF=\"#\" onClick=\"popup(\'%s\',\'%s\')\">Info</A></td>' % (str(updated_tag), str(html_body_text))
+                print('<td align=\"left\"><A HREF=\"#\" onClick=\"popup(\'%s\',\'%s\')\">Info</A></td>' % (str(updated_tag), str(html_body_text)), file=output)
             else:
-                print >> output, '<td align=\"left\"></td>'
+                print('<td align=\"left\"></td>', file=output)
             # print execution time
-            print >> output, '<td align="left"><A HREF=\"%s\">Debug</A></td>' % os.path.join(dirname, res['subdir'], "debug")
+            print('<td align="left"><A HREF=\"%s\">Debug</A></td>' % os.path.join(dirname, res['subdir'], "debug"), file=output)
 
-            print >> output, '</tr>'
+            print('</tr>', file=output)
             print_result(results[r][1], indent + 1)
 
     print_result(results[""][1], 0)
-    print >> output, "</tbody></table>"
+    print("</tbody></table>", file=output)
 
 
-    print >> output, '<h2 id=\"page_sub_title\">Host Info</h2>'
-    print >> output, '<h2 id=\"comment\">click on each item to expend/collapse</h2>'
+    print('<h2 id=\"page_sub_title\">Host Info</h2>', file=output)
+    print('<h2 id=\"comment\">click on each item to expend/collapse</h2>', file=output)
     ## Meta list comes here..
-    print >> output, '<p>'
-    print >> output, '<A href="#" class="button" onClick="expandTree(\'meta_tree\');return false;">Expand All</A>'
-    print >> output, '&nbsp;&nbsp;&nbsp'
-    print >> output, '<A class="button" href="#" onClick="collapseTree(\'meta_tree\'); return false;">Collapse All</A>'
-    print >> output, '</p>'
+    print('<p>', file=output)
+    print('<A href="#" class="button" onClick="expandTree(\'meta_tree\');return false;">Expand All</A>', file=output)
+    print('&nbsp;&nbsp;&nbsp', file=output)
+    print('<A class="button" href="#" onClick="collapseTree(\'meta_tree\'); return false;">Collapse All</A>', file=output)
+    print('</p>', file=output)
 
-    print >> output, '<ul class="mktree" id="meta_tree">'
+    print('<ul class="mktree" id="meta_tree">', file=output)
     counter = 0
-    keys = metadata.keys()
+    keys = list(metadata.keys())
     keys.sort()
     for key in keys:
         val = metadata[key]
-        print >> output, '<li id=\"meta_headline\">%s' % key
-        print >> output, '<ul><table class="meta_table"><tr><td align="left">%s</td></tr></table></ul></li>' % val
-    print >> output, '</ul>'
+        print('<li id=\"meta_headline\">%s' % key, file=output)
+        print('<ul><table class="meta_table"><tr><td align="left">%s</td></tr></table></ul></li>' % val, file=output)
+    print('</ul>', file=output)
 
-    print >> output, "</body></html>"
+    print("</body></html>", file=output)
     if output_file_name:
         output.close()
 
@@ -1653,13 +1656,13 @@
     """
     Print stand alone program usage.
     """
-    print 'usage:',
-    print 'make_html_report.py -r <result_directory> [-f output_file] [-R]'
-    print '(e.g. make_html_reporter.py -r '\
-          '/usr/local/autotest/client/results/default -f /tmp/myreport.html)'
-    print 'add "-R" for an html report with relative-paths (relative '\
-          'to results directory)'
-    print ''
+    print('usage:',)
+    print('make_html_report.py -r <result_directory> [-f output_file] [-R]')
+    print('(e.g. make_html_reporter.py -r '\
+          '/usr/local/autotest/client/results/default -f /tmp/myreport.html)')
+    print('add "-R" for an html report with relative-paths (relative '
+          'to results directory)')
+    print('')
     sys.exit(1)
 
 
@@ -1672,7 +1675,7 @@
     @param key: Specific key we're retrieving.
     """
     keyval_pattern = os.path.join(result_dir, "kvm.*", "keyval")
-    keyval_lines = commands.getoutput(r"grep -h '\b%s\b.*=' %s"
+    keyval_lines = subprocess.getoutput(r"grep -h '\b%s\b.*=' %s"
                                       % (key, keyval_pattern))
     if not keyval_lines:
         return "Unknown"
@@ -1782,7 +1785,7 @@
             create_report(dirname, html_path, output_file_name)
             sys.exit(0)
         else:
-            print 'Invalid result directory <%s>' % dirname
+            print('Invalid result directory <%s>' % dirname)
             sys.exit(1)
     else:
         usage()
diff --git a/client/tools/make_clean b/client/tools/make_clean
index 058c286..4283977 100755
--- a/client/tools/make_clean
+++ b/client/tools/make_clean
@@ -1,4 +1,8 @@
 #!/usr/bin/python2
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import os
 
 def purge_src(top_dir):
@@ -19,7 +23,7 @@
         else:
             cmd = 'cd %s; make clean > /dev/null 2>&1 ' % src_path
 
-        print 'Cleaning %s test dir' % dir
+        print('Cleaning %s test dir' % dir)
         os.system(cmd)
 
 if os.path.isdir('tmp'):
diff --git a/client/tools/oprofile_diff b/client/tools/oprofile_diff
index ed9e503..2c13124 100755
--- a/client/tools/oprofile_diff
+++ b/client/tools/oprofile_diff
@@ -1,5 +1,10 @@
 #!/usr/bin/python2
-import re, sys
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import re
+import sys
 
 def parse_file(filename):
     data = {}
@@ -14,16 +19,16 @@
 data2 = parse_file(sys.argv[2])
 
 delta = {}
-for symbol in data1.keys() + data2.keys():
+for symbol in list(data1.keys()) + list(data2.keys()):
     delta[symbol] = data1.get(symbol, 0) - data2.get(symbol, 0)
 
 def lookup_delta(symbol):
     return delta[symbol]
 
-sorted_deltas = sorted(delta.keys(), key=lookup_delta, reverse=True)
+sorted_deltas = sorted(list(delta.keys()), key=lookup_delta, reverse=True)
 
 if len(sorted_deltas) > 40:
     sorted_deltas = sorted_deltas[0:20] + sorted_deltas[-20:]
 
 for symbol in sorted_deltas:
-    print '%2.3f %s' % (delta[symbol], symbol)
+    print('%2.3f %s' % (delta[symbol], symbol))