From 5944220eee3b8ca0b2d7e328cde0260e6657b265 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Thu, 9 Apr 2020 00:24:51 +0000 Subject: [PATCH] Bug 1628205 - Convert nsinstall.py to python 3. r=rstewart Also enable all config/tests with python3. unit-nsinstall.py was the last one that didn't pass with python 3. Switch the test to using @unittest.skipIf and disable the subprocess test because we purposely broke running nsinstall.py independently with python 2. Differential Revision: https://phabricator.services.mozilla.com/D70160 --HG-- extra : moz-landing-system : lando --- config/config.mk | 2 +- config/nsinstall.py | 33 ++----------- config/tests/python.ini | 1 - config/tests/unit-nsinstall.py | 87 ++++++++++++++++++---------------- 4 files changed, 49 insertions(+), 74 deletions(-) diff --git a/config/config.mk b/config/config.mk index f24d51d9c28b..d2360871c57f 100644 --- a/config/config.mk +++ b/config/config.mk @@ -333,7 +333,7 @@ endif PWD := $(CURDIR) endif -NSINSTALL_PY := $(PYTHON) $(abspath $(MOZILLA_DIR)/config/nsinstall.py) +NSINSTALL_PY := $(PYTHON3) $(abspath $(MOZILLA_DIR)/config/nsinstall.py) ifneq (,$(or $(filter WINNT,$(HOST_OS_ARCH)),$(if $(COMPILE_ENVIRONMENT),,1))) NSINSTALL = $(NSINSTALL_PY) else diff --git a/config/nsinstall.py b/config/nsinstall.py index adf03164ccd4..1d5ead6ff867 100755 --- a/config/nsinstall.py +++ b/config/nsinstall.py @@ -15,6 +15,7 @@ from optparse import OptionParser import mozfile import os import os.path +import six import sys import shutil @@ -154,36 +155,8 @@ def _nsinstall_internal(argv): def nsinstall(argv): - return _nsinstall_internal([unicode(arg, "utf-8") for arg in argv]) + return _nsinstall_internal([six.ensure_text(arg, "utf-8") for arg in argv]) if __name__ == '__main__': - # sys.argv corrupts characters outside the system code page on Windows - # . Use ctypes instead. This is also - # useful because switching to Unicode strings makes python use the wide - # Windows APIs, which is what we want here since the wide APIs normally do a - # better job at handling long paths and such. - if sys.platform == "win32": - import ctypes - from ctypes import wintypes - GetCommandLine = ctypes.windll.kernel32.GetCommandLineW - GetCommandLine.argtypes = [] - GetCommandLine.restype = wintypes.LPWSTR - - CommandLineToArgv = ctypes.windll.shell32.CommandLineToArgvW - CommandLineToArgv.argtypes = [ - wintypes.LPWSTR, ctypes.POINTER(ctypes.c_int)] - CommandLineToArgv.restype = ctypes.POINTER(wintypes.LPWSTR) - - argc = ctypes.c_int(0) - argv_arr = CommandLineToArgv(GetCommandLine(), ctypes.byref(argc)) - # The first argv will be "python", the second will be the .py file - argv = argv_arr[1:argc.value] - else: - # For consistency, do it on Unix as well - if sys.stdin.encoding is not None: - argv = [unicode(arg, sys.stdin.encoding) for arg in sys.argv] - else: - argv = [unicode(arg) for arg in sys.argv] - - sys.exit(_nsinstall_internal(argv[1:])) + sys.exit(_nsinstall_internal(sys.argv[1:])) diff --git a/config/tests/python.ini b/config/tests/python.ini index 2bdfff7281fe..3c29f36fa93e 100644 --- a/config/tests/python.ini +++ b/config/tests/python.ini @@ -1,6 +1,5 @@ [DEFAULT] subsuite = mozbuild -skip-if = python == 3 [test_mozbuild_reading.py] [unit-mozunit.py] diff --git a/config/tests/unit-nsinstall.py b/config/tests/unit-nsinstall.py index 172d68a2916d..aff31e3b7688 100644 --- a/config/tests/unit-nsinstall.py +++ b/config/tests/unit-nsinstall.py @@ -2,6 +2,7 @@ from __future__ import absolute_import import unittest import os +import six import sys import os.path import time @@ -34,9 +35,9 @@ class TestNsinstall(unittest.TestCase): # Unicode strings means non-ASCII children can be deleted properly on # Windows if sys.stdin.encoding is None: - tmpdir = unicode(self.tmpdir) + tmpdir = six.ensure_text(self.tmpdir) else: - tmpdir = unicode(self.tmpdir, sys.stdin.encoding) + tmpdir = six.ensure_text(self.tmpdir, sys.stdin.encoding) rmtree(tmpdir) # utility methods for tests @@ -121,20 +122,19 @@ class TestNsinstall(unittest.TestCase): self.assertEqual(os.stat(testfile).st_mtime, os.stat(destfile).st_mtime) - if sys.platform != "win32": - # can't run this test on windows, don't have real file modes there - def test_nsinstall_m(self): - "Test that nsinstall -m works (set mode)" - testfile = self.touch("testfile") - mode = 0o600 - os.chmod(testfile, mode) - testdir = self.mkdirs("testdir") - self.assertEqual(nsinstall(["-m", "{0:04o}" - .format(mode), testfile, testdir]), 0) - destfile = os.path.join(testdir, "testfile") - self.assert_(os.path.isfile(destfile)) - self.assertEqual(os.stat(testfile).st_mode, - os.stat(destfile).st_mode) + @unittest.skipIf(sys.platform == "win32", "Windows doesn't have real file modes") + def test_nsinstall_m(self): + "Test that nsinstall -m works (set mode)" + testfile = self.touch("testfile") + mode = 0o600 + os.chmod(testfile, mode) + testdir = self.mkdirs("testdir") + self.assertEqual(nsinstall(["-m", "{0:04o}" + .format(mode), testfile, testdir]), 0) + destfile = os.path.join(testdir, "testfile") + self.assert_(os.path.isfile(destfile)) + self.assertEqual(os.stat(testfile).st_mode, + os.stat(destfile).st_mode) def test_nsinstall_d(self): "Test that nsinstall -d works (create directories in target)" @@ -145,35 +145,38 @@ class TestNsinstall(unittest.TestCase): self.assertEqual(nsinstall(["-d", testfile, destdir]), 0) self.assert_(os.path.isdir(os.path.join(destdir, "testfile"))) - if RUN_NON_ASCII_TESTS: - def test_nsinstall_non_ascii(self): - "Test that nsinstall handles non-ASCII files" - filename = u"\u2325\u3452\u2415\u5081" - testfile = self.touch(filename) - testdir = self.mkdirs(u"\u4241\u1D04\u1414") - self.assertEqual(nsinstall([testfile.encode("utf-8"), - testdir.encode("utf-8")]), 0) + @unittest.skipIf(not RUN_NON_ASCII_TESTS, "Skipping non ascii tests") + def test_nsinstall_non_ascii(self): + "Test that nsinstall handles non-ASCII files" + filename = u"\u2325\u3452\u2415\u5081" + testfile = self.touch(filename) + testdir = self.mkdirs(u"\u4241\u1D04\u1414") + self.assertEqual(nsinstall([testfile.encode("utf-8"), + testdir.encode("utf-8")]), 0) - destfile = os.path.join(testdir, filename) - self.assert_(os.path.isfile(destfile)) + destfile = os.path.join(testdir, filename) + self.assert_(os.path.isfile(destfile)) - def test_nsinstall_non_ascii_subprocess(self): - "Test that nsinstall as a subprocess handles non-ASCII files" - filename = u"\u2325\u3452\u2415\u5081" - testfile = self.touch(filename) - testdir = self.mkdirs(u"\u4241\u1D04\u1414") - # We don't use subprocess because it can't handle Unicode on - # Windows . mozprocess calls - # CreateProcessW directly so it's perfect. - p = processhandler.ProcessHandlerMixin([sys.executable, - NSINSTALL_PATH, - testfile, testdir]) - p.run() - rv = p.wait() + # Executing nsinstall.py with python 2 is not supported. + @unittest.skipIf(not RUN_NON_ASCII_TESTS or sys.version_info[0] == 2, + "Skipping non ascii tests") + def test_nsinstall_non_ascii_subprocess(self): + "Test that nsinstall as a subprocess handles non-ASCII files" + filename = u"\u2325\u3452\u2415\u5081" + testfile = self.touch(filename) + testdir = self.mkdirs(u"\u4241\u1D04\u1414") + # We don't use subprocess because it can't handle Unicode on + # Windows . mozprocess calls + # CreateProcessW directly so it's perfect. + p = processhandler.ProcessHandlerMixin([sys.executable, + NSINSTALL_PATH, + testfile, testdir]) + p.run() + rv = p.wait() - self.assertEqual(rv, 0) - destfile = os.path.join(testdir, filename) - self.assert_(os.path.isfile(destfile)) + self.assertEqual(rv, 0) + destfile = os.path.join(testdir, filename) + self.assert_(os.path.isfile(destfile)) # TODO: implement -R, -l, -L and test them!