[Autotest][PY3] Migrating site_utils (db_replica_checker to gmail_lib)

The unittests don't pass, but didn't before either. They are failing
the same way atleast. Hopefully can come back through and delete most of
these as they look obsolete.

BUG=chromium:990593
TEST=dummy_pass, suite:dummy

Change-Id: I316b7c8eae7716a1b95823bccf5aa582199f4c9b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2551379
Reviewed-by: Seewai Fu <seewaifu@google.com>
Reviewed-by: Greg Edelston <gredelston@google.com>
Commit-Queue: Derek Beckett <dbeckett@chromium.org>
Tested-by: Derek Beckett <dbeckett@chromium.org>
diff --git a/site_utils/diagnosis_utils_unittest.py b/site_utils/diagnosis_utils_unittest.py
index da330a2..6c3cc08 100755
--- a/site_utils/diagnosis_utils_unittest.py
+++ b/site_utils/diagnosis_utils_unittest.py
@@ -104,7 +104,7 @@
         self.status = status
         self.locked = locked
 
-    is_available = frontend.Host.is_available.im_func
+    is_available = frontend.Host.is_available.__func__
 
 
 if __name__ == '__main__':
diff --git a/site_utils/dump_to_cloudsql.py b/site_utils/dump_to_cloudsql.py
index 4e15f25..2639a9b 100755
--- a/site_utils/dump_to_cloudsql.py
+++ b/site_utils/dump_to_cloudsql.py
@@ -24,7 +24,9 @@
     --passwd PASSWD  passwd (ignored for CloudSQL)
 """
 
+from __future__ import absolute_import
 from __future__ import division
+from __future__ import print_function
 import argparse
 import collections
 import datetime
@@ -32,6 +34,7 @@
 import re
 import sys
 import time
+import six
 
 
 BYTES_PER_GB = 2**30
@@ -65,7 +68,7 @@
             return
         # Execute command.
         if execute_cmd:
-            self._cursor.execute(self._cmd.decode('utf-8'))
+            self._cursor.execute(six.ensure_text(self._cmd, 'utf-8'))
         self._cmd = ''
         if increment_cmd:
             self.cmd_num += 1
@@ -97,7 +100,7 @@
         Returns:
           A MySQLdb compatible database connection to the Cloud SQL instance.
         """
-        print 'Connecting to Cloud SQL instance %s.' % self._instance
+        print('Connecting to Cloud SQL instance %s.' % self._instance)
         try:
             from google.storage.speckle.python.api import rdbms_googleapi
         except ImportError:
@@ -122,7 +125,7 @@
         Returns:
           A MySQLdb database connection to the local MySQL database.
         """
-        print 'Connecting to mysql at localhost as %s.' % self._user
+        print('Connecting to mysql at localhost as %s.' % self._user)
         try:
             import MySQLdb
         except ImportError:
@@ -172,7 +175,7 @@
           out: A File-like object to write out saved state.
         """
         out.write(self._db_line)
-        for v in self._sets.itervalues():
+        for v in six.itervalues(self._sets):
             out.write(v)
         for l in self._table_lock:
             out.write(l)
@@ -212,7 +215,7 @@
                 # Construct commands from lines and execute them.
                 state.process(line)
                 if manager.cmd_num == cmd_offset and cmd_offset != 0:
-                    print '\nRecreating state at line: %d' % line_num
+                    print('\nRecreating state at line: %d' % line_num)
                     state.write(manager)
                 manager.write(line, manager.cmd_num >= cmd_offset, True)
                 # Print status.
@@ -223,28 +226,28 @@
                 sys.stdout.flush()
             # Handle interrupts and connection failures.
             except KeyboardInterrupt:
-                print ('\nInterrupted while executing command: %d' %
-                       manager.cmd_num)
+                print('\nInterrupted while executing command: %d' %
+                      manager.cmd_num)
                 raise
             except:
-                print '\nFailed while executing command: %d' % manager.cmd_num
+                print('\nFailed while executing command: %d' % manager.cmd_num)
                 delta = int(time.time() - start_time)
-                print 'Total time: %s' % str(datetime.timedelta(seconds=delta))
+                print('Total time: %s' % str(datetime.timedelta(seconds=delta)))
                 if state.breakpoint(line):
                     # Attempt to resume.
-                    print ('Execution can resume from here (line = %d)' %
-                           line_num)
+                    print('Execution can resume from here (line = %d)' %
+                          line_num)
                     manager.cmd_num += 1
                     cmd_offset = manager.cmd_num
-                    print ('Will now attempt to auto-resume at command: %d' %
-                           cmd_offset)
+                    print('Will now attempt to auto-resume at command: %d' %
+                          cmd_offset)
                     manager.disconnect()
                 else:
-                    print 'Execution may fail to resume correctly from here.'
-                    print ('Use --resume=%d to attempt to resume the dump.' %
-                           manager.cmd_num)
+                    print('Execution may fail to resume correctly from here.')
+                    print('Use --resume=%d to attempt to resume the dump.' %
+                          manager.cmd_num)
                     raise
-    print '\nDone.'
+    print('\nDone.')
 
 
 if __name__ == '__main__':
@@ -273,6 +276,6 @@
         connection = LocalSQLConnectionFactory(args.remote, args.user,
                                                args.passwd)
     if args.resume:
-        print 'Resuming execution at command: %d' % options.resume
+        print('Resuming execution at command: %d' % options.resume)
     dump_to_cloudsql(args.mysqldump, MySQLConnectionManager(connection),
                      args.resume)
diff --git a/site_utils/dut_status.py b/site_utils/dut_status.py
index 930d409..5f07c03 100755
--- a/site_utils/dut_status.py
+++ b/site_utils/dut_status.py
@@ -106,6 +106,10 @@
 
 """
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import argparse
 import sys
 import time
@@ -169,7 +173,7 @@
 
     """
     fmt = '%-30s %-2s  %-19s  %s'
-    print fmt % ('hostname', 'S', 'last checked', 'URL')
+    print(fmt % ('hostname', 'S', 'last checked', 'URL'))
     for history in history_list:
         status, event = history.last_diagnosis()
         if not _include_status(status, arguments):
@@ -178,23 +182,23 @@
         url = '---'
         if event is not None:
             datestr = time_utils.epoch_time_to_date_string(
-                    event.start_time)
+                event.start_time)
             url = event.job_url
 
-        print fmt % (history.hostname,
+        print(fmt % (history.hostname,
                      _DIAGNOSIS_IDS[status],
                      datestr,
-                     url)
+                     url))
 
 
 def _print_event_summary(event):
     """Print a one-line summary of a job or special task."""
     start_time = time_utils.epoch_time_to_date_string(
-            event.start_time)
-    print '    %s  %s %s' % (
-            start_time,
-            _DIAGNOSIS_IDS[event.diagnosis],
-            event.job_url)
+        event.start_time)
+    print('    %s  %s %s' % (
+          start_time,
+          _DIAGNOSIS_IDS[event.diagnosis],
+          event.job_url))
 
 
 def _print_hosts(history_list, arguments):
@@ -214,7 +218,7 @@
         status, _ = history.last_diagnosis()
         if not _include_status(status, arguments):
             continue
-        print history.hostname
+        print(history.hostname)
         if arguments.full_history:
             for event in history:
                 _print_event_summary(event)
@@ -240,8 +244,9 @@
     """
     if (arguments.duration is not None and
             arguments.since is not None and arguments.until is not None):
-        print >>sys.stderr, ('FATAL: Can specify at most two of '
-                             '--since, --until, and --duration')
+        print('FATAL: Can specify at most two of '
+              '--since, --until, and --duration',
+              file=sys.stderr)
         sys.exit(1)
     if (arguments.until is None and (arguments.since is None or
                                      arguments.duration is None)):
@@ -282,12 +287,12 @@
                     afe, hostname, arguments.since, arguments.until)
             histories.append(h)
         except:
-            print >>sys.stderr, ('WARNING: Ignoring unknown host %s' %
-                                  hostname)
+            print('WARNING: Ignoring unknown host %s' %
+                  hostname, file=sys.stderr)
             saw_error = True
     if saw_error:
         # Create separation from the output that follows
-        print >>sys.stderr
+        print(file=sys.stderr)
     return histories
 
 
@@ -314,8 +319,8 @@
     """
     if arguments.board or arguments.pool or arguments.model:
         if arguments.hostnames:
-            print >>sys.stderr, ('FATAL: Hostname arguments provided '
-                                 'with --board or --pool')
+            print('FATAL: Hostname arguments provided '
+                  'with --board or --pool', file=sys.stderr)
             sys.exit(1)
 
         labels = labellib.LabelsMapping()
@@ -327,7 +332,7 @@
     else:
         histories = _get_host_histories(afe, arguments)
     if not histories:
-        print >>sys.stderr, 'FATAL: no valid hosts found'
+        print('FATAL: no valid hosts found', file=sys.stderr)
         sys.exit(1)
     return histories
 
diff --git a/site_utils/gmail_lib.py b/site_utils/gmail_lib.py
index 997c7b8..f5df276 100755
--- a/site_utils/gmail_lib.py
+++ b/site_utils/gmail_lib.py
@@ -16,6 +16,10 @@
      > Line 2
      Ctrl-D to end standard input.
 """
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import argparse
 import base64
 import httplib2
@@ -196,7 +200,7 @@
                         help='Email addresses separated by space.')
     args = parser.parse_args()
     if not args.recipients or not args.subject:
-        print 'Requires both recipients and subject.'
+        print('Requires both recipients and subject.')
         sys.exit(1)
 
     message_text = sys.stdin.read()
@@ -207,9 +211,9 @@
             if random.random() < args.probability:
                 recipients.append(r)
         if recipients:
-            print 'Randomly selected recipients %s' % recipients
+            print('Randomly selected recipients %s' % recipients)
         else:
-            print 'Random filtering removed all recipients. Sending nothing.'
+            print('Random filtering removed all recipients. Sending nothing.')
             sys.exit(0)
     else:
         recipients = args.recipients