Fix Python3 bugs in package_from_installed.py

The package_from_installed.py script didn't run under Python 3. This
change fixes that and this script now _requires_ Python 3. Requiring
Python 3 was simpler than supporting both, and there is no need to
maintain Python 2.7 support. This script is only ever invoked manually
and all machines it is run on should have Python 3.

Changes included using universal_newlines=True so that
subprocess.check_output returns a string instead of bytes, and
by explicitly converting dict.items() to a list.

Similarly, open(name, 'wb') was changed to open(name,'wt', newline='')
to avoid byte/str problems while still avoiding CR/LF translations.

This also changes the default Windows 10 SDK version to something more
recent - the current version that we build with.

To test:

  python3 package_from_installed.py -d 2019

The use of '-d' makes the script run in test mode which is much faster.

Change-Id: I2e3fc4d464f2914ba5ae36f0a78570ea7e5d4f03
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2224506
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Bruce Dawson <brucedawson@chromium.org>
diff --git a/win_toolchain/package_from_installed.py b/win_toolchain/package_from_installed.py
index 6f96925..adef517 100644
--- a/win_toolchain/package_from_installed.py
+++ b/win_toolchain/package_from_installed.py
@@ -60,7 +60,8 @@
   command = (r'C:\Program Files (x86)\Microsoft Visual Studio\Installer'
              r'\vswhere.exe -prerelease')
   marker = 'installationPath: '
-  for line in subprocess.check_output(command).splitlines():
+  output = subprocess.check_output(command, universal_newlines=True)
+  for line in output.splitlines():
     if line.startswith(marker):
       return line[len(marker):]
   raise Exception('VS %s path not found in vswhere output' % (VS_VERSION))
@@ -367,17 +368,17 @@
       f.write('set %s=%s%s\n' % (
           var, BatDirs(dirs), ';%PATH%' if var == 'PATH' else ''))
     f.write('goto :EOF\n')
-  with open(set_env_prefix + '.x86.json', 'wb') as f:
+  with open(set_env_prefix + '.x86.json', 'wt', newline='') as f:
     assert not set(env.keys()) & set(env_x86.keys()), 'dupe keys'
-    json.dump({'env': collections.OrderedDict(env.items() + env_x86.items())},
+    json.dump({'env': collections.OrderedDict(list(env.items()) + list(env_x86.items()))},
               f)
-  with open(set_env_prefix + '.x64.json', 'wb') as f:
+  with open(set_env_prefix + '.x64.json', 'wt', newline='') as f:
     assert not set(env.keys()) & set(env_x64.keys()), 'dupe keys'
-    json.dump({'env': collections.OrderedDict(env.items() + env_x64.items())},
+    json.dump({'env': collections.OrderedDict(list(env.items()) + list(env_x64.items()))},
               f)
-  with open(set_env_prefix + '.arm64.json', 'wb') as f:
+  with open(set_env_prefix + '.arm64.json', 'wt', newline='') as f:
     assert not set(env.keys()) & set(env_arm64.keys()), 'dupe keys'
-    json.dump({'env': collections.OrderedDict(env.items() + env_arm64.items())},
+    json.dump({'env': collections.OrderedDict(list(env.items()) + list(env_arm64.items()))},
               f)
 
 
@@ -397,7 +398,7 @@
     files.append((os.path.join(tempdir, 'win_sdk', 'bin', 'SetEnv.arm64.json'),
                   'win_sdk\\bin\\SetEnv.arm64.json'))
   vs_version_file = os.path.join(tempdir, 'VS_VERSION')
-  with open(vs_version_file, 'wb') as version:
+  with open(vs_version_file, 'wt', newline='') as version:
     print(VS_VERSION, file=version)
   files.append((vs_version_file, 'VS_VERSION'))
 
@@ -423,11 +424,14 @@
 
 
 def main():
+  if sys.version_info[0] < 3:
+    print('This script requires Python 3')
+    sys.exit(10)
   usage = 'usage: %prog [options] 2017|2019'
   parser = optparse.OptionParser(usage)
   parser.add_option('-w', '--winver', action='store', type='string',
-                    dest='winver', default='10.0.14393.0',
-                    help='Windows SDK version, such as 10.0.14393.0')
+                    dest='winver', default='10.0.18362.0',
+                    help='Windows SDK version, such as 10.0.18362.0')
   parser.add_option('-d', '--dryrun', action='store_true', dest='dryrun',
                     default=False,
                     help='scan for file existence and prints statistics')