Make test_env.py compatible w/ Python 3

Makes test_env.py compatible with both Python 2 and 3. Also adds a very
simple test to ensure that it remains compatible until some other test
switches to Python 3.

As a side effect of adding the test, also makes
testing/scripts/common.py compatible with Python 3 since it's
necessary for running isolated scripts.

Bug: 898348
Change-Id: Ib58cf4c4f85df60a432764d7f744bc3fee09ae3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1796525
Reviewed-by: Dirk Pranke <[email protected]>
Commit-Queue: Brian Sheedy <[email protected]>
Cr-Commit-Position: refs/heads/master@{#695825}
diff --git a/testing/test_env.py b/testing/test_env.py
index eac46bf..4c3332a 100755
--- a/testing/test_env.py
+++ b/testing/test_env.py
@@ -5,6 +5,8 @@
 
 """Sets environment variables needed to run a chromium unit test."""
 
+from __future__ import print_function
+
 import io
 import os
 import signal
@@ -167,12 +169,12 @@
     p = subprocess.Popen(symbolize_command, stderr=subprocess.PIPE, env=env)
     (_, stderr) = p.communicate()
   except OSError as e:
-    print >> sys.stderr, 'Exception while symbolizing snippets: %s' % e
+    print('Exception while symbolizing snippets: %s' % e, file=sys.stderr)
     raise
 
   if p.returncode != 0:
-    print >> sys.stderr, "Error: failed to symbolize snippets in JSON:\n"
-    print >> sys.stderr, stderr
+    print("Error: failed to symbolize snippets in JSON:\n", file=sys.stderr)
+    print(stderr, file=sys.stderr)
     raise subprocess.CalledProcessError(p.returncode, symbolize_command)
 
 
@@ -185,7 +187,7 @@
   Returns:
     integer returncode of the subprocess.
   """
-  print('Running %r in %r (env: %r)' % (argv, cwd, env))
+  print(('Running %r in %r (env: %r)' % (argv, cwd, env)))
   assert stdoutfile
   with io.open(stdoutfile, 'wb') as writer, \
       io.open(stdoutfile, 'rb', 1) as reader:
@@ -199,7 +201,7 @@
       time.sleep(0.1)
     # Read the remaining.
     sys.stdout.write(reader.read())
-    print('Command %r returned exit code %d' % (argv, process.returncode))
+    print(('Command %r returned exit code %d' % (argv, process.returncode)))
     return process.returncode
 
 
@@ -213,7 +215,7 @@
     integer returncode of the subprocess.
   """
   if log:
-    print('Running %r in %r (env: %r)' % (argv, cwd, env))
+    print(('Running %r in %r (env: %r)' % (argv, cwd, env)))
   process = _popen(argv, env=env, cwd=cwd, stderr=subprocess.STDOUT)
   forward_signals([process])
   return wait_with_signals(process)
@@ -228,12 +230,12 @@
   Returns:
     integer returncode of the subprocess.
   """
-  print('Running %r in %r (env: %r)' % (argv, cwd, env))
+  print(('Running %r in %r (env: %r)' % (argv, cwd, env)))
   process = _popen(
       argv, env=env, cwd=cwd, stderr=file_handle, stdout=file_handle)
   forward_signals([process])
   exit_code = wait_with_signals(process)
-  print('Command returned exit code %d' % exit_code)
+  print(('Command returned exit code %d' % exit_code))
   return exit_code
 
 
@@ -342,11 +344,11 @@
       if env_var_name in env:
           env_to_print[env_var_name] = env[env_var_name]
 
-  print('Additional test environment:\n%s\n'
+  print(('Additional test environment:\n%s\n'
         'Command: %s\n' % (
         '\n'.join('    %s=%s' %
-            (k, v) for k, v in sorted(env_to_print.iteritems())),
-        ' '.join(cmd)))
+            (k, v) for k, v in sorted(env_to_print.items())),
+        ' '.join(cmd))))
   sys.stdout.flush()
   env.update(extra_env or {})
   try:
@@ -371,7 +373,7 @@
     else:
       return run_command(cmd, env=env, log=False)
   except OSError:
-    print >> sys.stderr, 'Failed to start %s' % cmd
+    print('Failed to start %s' % cmd, file=sys.stderr)
     raise