Fix Python 3 issues
authorBruce Forstall <Bruce_Forstall@msn.com>
Thu, 13 Dec 2018 22:53:31 +0000 (14:53 -0800)
committerBruce Forstall <Bruce_Forstall@msn.com>
Thu, 13 Dec 2018 22:53:31 +0000 (14:53 -0800)
Fixes dotnet/coreclr#21433

Commit migrated from https://github.com/dotnet/coreclr/commit/bba070bf2f7d975e9b0c6b45a601e0a43b6a6b38

src/coreclr/tests/scripts/format.py
src/coreclr/tests/scripts/run-corefx-tests.py
src/coreclr/tests/scripts/run-pmi-diffs.py

index 658e7f9..b622cf4 100644 (file)
 ################################################################################
 
 
-import urllib
 import argparse
 import os
 import sys
 import tarfile
 import zipfile
 import subprocess
-import urllib2
 import shutil
 
+# Version specific imports
+
+if sys.version_info.major < 3:
+    import urllib
+else:
+    import urllib.request
+
 def expandPath(path):
     return os.path.abspath(os.path.expanduser(path))
 
 def del_rw(action, name, exc):
-    os.chmod(name, 0651)
+    os.chmod(name, 0o651)
     os.remove(name)
 
 def main(argv):
@@ -42,13 +47,13 @@ def main(argv):
     args, unknown = parser.parse_known_args(argv)
 
     if unknown:
-        print('Ignorning argument(s): ', ','.join(unknown))
+        print('Ignoring argument(s): ', ','.join(unknown))
 
     if args.coreclr is None:
         print('Specify --coreclr')
         return -1
     if args.os is None:
-        print('Specifiy --os')
+        print('Specify --os')
         return -1
     if args.arch is None:
         print('Specify --arch')
@@ -97,10 +102,8 @@ def main(argv):
         print('Unknown os ', os)
         return -1
 
-    response = urllib2.urlopen(dotnetcliUrl)
-    request_url = response.geturl()
-    testfile = urllib.URLopener()
-    testfile.retrieve(request_url, dotnetcliFilename)
+    urlretrieve = urllib.urlretrieve if sys.version_info.major < 3 else urllib.request.urlretrieve
+    urlretrieve(dotnetcliUrl, dotnetcliFilename)
 
     if not os.path.isfile(dotnetcliFilename):
         print("Did not download .Net CLI!")
@@ -145,7 +148,7 @@ def main(argv):
     bootstrapUrl = "https://raw.githubusercontent.com/dotnet/jitutils/master/" + bootstrapFilename
 
     bootstrapPath = os.path.join(coreclr, bootstrapFilename)
-    testfile.retrieve(bootstrapUrl, bootstrapPath)
+    urlretrieve(bootstrapUrl, bootstrapPath)
 
     if not os.path.isfile(bootstrapPath):
         print("Did not download bootstrap!")
@@ -155,7 +158,7 @@ def main(argv):
 
     if platform == 'Linux' or platform == 'OSX':
         print("Making bootstrap executable")
-        os.chmod(bootstrapPath, 0751)
+        os.chmod(bootstrapPath, 0o751)
 
     print(bootstrapPath)
 
index 57b21f3..76adfe2 100644 (file)
@@ -50,7 +50,7 @@ Is_windows = (os.name == 'nt')
 ##########################################################################
 
 def del_rw(action, name, exc):
-    os.chmod(name, 0651)
+    os.chmod(name, 0o651)
     os.remove(name)
 
 ##########################################################################
@@ -181,7 +181,7 @@ def log(message):
         message (str): message to be printed
     """
 
-    print '[%s]: %s' % (sys.argv[0], message)
+    print('[%s]: %s' % (sys.argv[0], message))
 
 def copy_files(source_dir, target_dir):
     """ Copy any files in the source_dir to the target_dir.
index acf15fc..2be7b8e 100644 (file)
@@ -26,12 +26,17 @@ import os
 import re
 import shutil
 import subprocess
-import urllib
-import urllib2
 import sys
 import tarfile
 import zipfile
 
+# Version specific imports
+
+if sys.version_info.major < 3:
+    import urllib
+else:
+    import urllib.request
+
 ##########################################################################
 # Globals
 ##########################################################################
@@ -67,7 +72,7 @@ Clr_os = 'Windows_NT' if Is_windows else Unix_name_map[os.uname()[0]]
 ##########################################################################
 
 def del_rw(action, name, exc):
-    os.chmod(name, 0651)
+    os.chmod(name, 0o651)
     os.remove(name)
 
 ##########################################################################
@@ -221,7 +226,7 @@ def log(message):
         message (str): message to be printed
     """
 
-    print '[%s]: %s' % (sys.argv[0], message)
+    print('[%s]: %s' % (sys.argv[0], message))
 
 def copy_files(source_dir, target_dir):
     """ Copy any files in the source_dir to the target_dir.
@@ -453,10 +458,8 @@ def do_pmi_diffs():
     log('Downloading: %s => %s' % (dotnetcliUrl, dotnetcliFilename))
 
     if not testing:
-        response = urllib2.urlopen(dotnetcliUrl)
-        request_url = response.geturl()
-        testfile = urllib.URLopener()
-        testfile.retrieve(request_url, dotnetcliFilename)
+        urlretrieve = urllib.urlretrieve if sys.version_info.major < 3 else urllib.request.urlretrieve
+        urlretrieve(dotnetcliUrl, dotnetcliFilename)
 
         if not os.path.isfile(dotnetcliFilename):
             log('ERROR: Did not download .Net CLI')