A batch of py2-py3 compat updates for blinkpy and testing/scripts

This code was chosen because it lies on the codepath for wptrunner in
python3.

Change-Id: Ideb794de0ae03c984ca7f3ecddc59d63d8c737b6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2679737
Commit-Queue: Luke Z <[email protected]>
Reviewed-by: Stephen McGruer <[email protected]>
Reviewed-by: Dirk Pranke <[email protected]>
Cr-Commit-Position: refs/heads/master@{#853301}
diff --git a/testing/xvfb.py b/testing/xvfb.py
index 043cfda9..c7849e6 100755
--- a/testing/xvfb.py
+++ b/testing/xvfb.py
@@ -6,6 +6,8 @@
 """Runs tests with Xvfb and Openbox or Weston on Linux and normally on other
    platforms."""
 
+from __future__ import print_function
+
 import os
 import os.path
 import psutil
@@ -39,13 +41,13 @@
 
   thread.join(timeout_in_seconds)
   if thread.is_alive():
-    print >> sys.stderr, '%s running after SIGTERM, trying SIGKILL.' % name
+    print('%s running after SIGTERM, trying SIGKILL.\n' % name, file=sys.stderr)
     proc.kill()
 
   thread.join(timeout_in_seconds)
   if thread.is_alive():
-    print >> sys.stderr, \
-      '%s running after SIGTERM and SIGKILL; good luck!' % name
+    print('%s running after SIGTERM and SIGKILL; good luck!\n' % name,
+          file=sys.stderr)
 
 
 def launch_dbus(env):
@@ -78,7 +80,7 @@
         env[m.group(1)] = m.group(2)
     return int(env['DBUS_SESSION_BUS_PID'])
   except (subprocess.CalledProcessError, OSError, KeyError, ValueError) as e:
-    print 'Exception while running dbus_launch: %s' % e
+    print('Exception while running dbus_launch: %s' % e)
 
 
 # TODO(crbug.com/949194): Encourage setting flags to False.
@@ -123,7 +125,7 @@
   use_weston = False
   if '--use-weston' in cmd:
     if use_xvfb:
-      print >> sys.stderr, 'Unable to use Weston with xvfb.'
+      print('Unable to use Weston with xvfb.\n', file=sys.stderr)
       return 1
     use_weston = True
     cmd.remove('--use-weston')
@@ -216,10 +218,10 @@
 
     return test_env.run_executable(cmd, env, stdoutfile)
   except OSError as e:
-    print >> sys.stderr, 'Failed to start Xvfb or Openbox: %s' % str(e)
+    print('Failed to start Xvfb or Openbox: %s\n' % str(e), file=sys.stderr)
     return 1
   except _XvfbProcessError as e:
-    print >> sys.stderr, 'Xvfb fail: %s' % str(e)
+    print('Xvfb fail: %s\n' % str(e), file=sys.stderr)
     return 1
   finally:
     kill(openbox_proc, 'openbox')
@@ -277,10 +279,10 @@
     env['WAYLAND_DISPLAY'] = weston_proc_display
     return test_env.run_executable(cmd, env, stdoutfile)
   except OSError as e:
-    print >> sys.stderr, 'Failed to start Weston: %s' % str(e)
+    print('Failed to start Weston: %s\n' % str(e), file=sys.stderr)
     return 1
   except _WestonProcessError as e:
-    print >> sys.stderr, 'Weston fail: %s' % str(e)
+    print('Weston fail: %s\n' % str(e), file=sys.stderr)
     return 1
   finally:
     kill(weston_proc, 'weston')
@@ -378,22 +380,22 @@
   if not runtime_dir:
     runtime_dir = '/tmp/xdg-tmp-dir/'
     if not os.path.exists(runtime_dir):
-      os.makedirs(runtime_dir, 0700)
+      os.makedirs(runtime_dir, 0o700)
     env['XDG_RUNTIME_DIR'] = runtime_dir
 
 
 def main():
   usage = 'Usage: xvfb.py [command [--no-xvfb or --use-weston] args...]'
   if len(sys.argv) < 2:
-    print >> sys.stderr, usage
+    print(usage + '\n', file=sys.stderr)
     return 2
 
   # If the user still thinks the first argument is the execution directory then
   # print a friendly error message and quit.
   if os.path.isdir(sys.argv[1]):
-    print >> sys.stderr, (
-        'Invalid command: \"%s\" is a directory' % sys.argv[1])
-    print >> sys.stderr, usage
+    print('Invalid command: \"%s\" is a directory\n' % sys.argv[1],
+          file=sys.stderr)
+    print(usage + '\n', file=sys.stderr)
     return 3
 
   return run_executable(sys.argv[1:], os.environ.copy())