From dce45d3151742338057f88f0a459ff442e05726f Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Tue, 21 Mar 2017 10:28:13 +0000 Subject: [PATCH] Fix python stub template for python3 on Windows 1. Adding UNC prefix after os.path.join, so that paths will be normalized and absolute. 2. Make example/py_native/bin.py work in python3 3. Add a shell test for building python binary with python3 4. Run all tests in batch mode Fix https://github.com/bazelbuild/bazel/issues/2708 -- Change-Id: I8dc18c80ebba87e965fe6fef9978732e6c35b1f0 Reviewed-on: https://cr.bazel.build/9456 PiperOrigin-RevId: 150734716 MOS_MIGRATED_REVID=150734716 --- examples/py_native/bin.py | 5 +++-- .../lib/bazel/rules/python/stub_template.txt | 15 ++++++++++----- .../shell/bazel/bazel_windows_example_test.sh | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/examples/py_native/bin.py b/examples/py_native/bin.py index 7b656278f6f9e4..45c68b26e525d2 100644 --- a/examples/py_native/bin.py +++ b/examples/py_native/bin.py @@ -1,6 +1,7 @@ +# pylint: disable=superfluous-parens """A tiny example binary for the native Python rules of Bazel.""" from examples.py_native.lib import GetNumber from fib import Fib -print "The number is %d" % GetNumber() -print "Fib(5) == %d" % Fib(5) +print("The number is %d" % GetNumber()) +print("Fib(5) == %d" % Fib(5)) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt index 0e28ce16f73251..3951f49068f684 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt @@ -13,10 +13,13 @@ def IsWindows(): return os.name == 'nt' def GetWindowsPathWithUNCPrefix(path): - """Changes the path, so that it uses unicode Windows paths""" + """ + Adding UNC prefix after getting a normalized absolute Windows path, + it's no-op for non-Windows platforms or if running under python2. + """ path = path.strip() - # No need to add prefix for non-Windows platforms, + # No need to add prefix for non-Windows platforms. # And \\?\ doesn't work in python 2 if not IsWindows() or sys.version_info[0] < 3: return path @@ -26,6 +29,7 @@ def GetWindowsPathWithUNCPrefix(path): if path.startswith(unicode_prefix): return path + # os.path.abspath returns a normalized absolute path return unicode_prefix + os.path.abspath(path) PYTHON_BINARY = '%python_binary%' @@ -93,10 +97,8 @@ def FindModuleSpace(): def CreateModuleSpace(): ZIP_RUNFILES_DIRECTORY_NAME = "runfiles" temp_dir = tempfile.mkdtemp("", "Bazel.runfiles_") - # mkdtemp return absolute name - temp_dir = GetWindowsPathWithUNCPrefix(temp_dir) zf = zipfile.ZipFile(GetWindowsPathWithUNCPrefix(os.path.dirname(__file__))) - zf.extractall(temp_dir) + zf.extractall(GetWindowsPathWithUNCPrefix(temp_dir)) return os.path.join(temp_dir, ZIP_RUNFILES_DIRECTORY_NAME) # Returns repository roots to add to the import path. @@ -120,6 +122,8 @@ def Main(): python_path_entries = CreatePythonPathEntries(python_imports, module_space) python_path_entries += GetRepositoriesImports(module_space, %import_all%) + python_path_entries = [GetWindowsPathWithUNCPrefix(d) for d in python_path_entries] + old_python_path = os.environ.get('PYTHONPATH') python_path = os.pathsep.join(python_path_entries) if old_python_path: @@ -138,6 +142,7 @@ def Main(): rel_path = rel_path.replace("/", os.sep) main_filename = os.path.join(module_space, rel_path) + main_filename = GetWindowsPathWithUNCPrefix(main_filename) assert os.path.exists(main_filename), \ 'Cannot exec() %r: file not found.' % main_filename assert os.access(main_filename, os.R_OK), \ diff --git a/src/test/shell/bazel/bazel_windows_example_test.sh b/src/test/shell/bazel/bazel_windows_example_test.sh index 20c94fb5e56fee..7f6f739454463c 100755 --- a/src/test/shell/bazel/bazel_windows_example_test.sh +++ b/src/test/shell/bazel/bazel_windows_example_test.sh @@ -33,8 +33,11 @@ fi function set_up() { copy_examples - EXTRA_BAZELRC="build --cpu=x64_windows_msvc" setup_bazelrc + cat >>"$TEST_TMPDIR/bazelrc" <