aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Yun Peng <pcloudy@google.com>2017-03-21 10:28:13 +0000
committerGravatar Yue Gan <yueg@google.com>2017-03-21 12:54:05 +0000
commitdce45d3151742338057f88f0a459ff442e05726f (patch)
tree233b71733956b7851f81970ea8fad75c45b4eacd
parentc0a84443526539c36557a6b6bfd0e41623d62fd0 (diff)
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
-rw-r--r--examples/py_native/bin.py5
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt15
-rwxr-xr-xsrc/test/shell/bazel/bazel_windows_example_test.sh17
3 files changed, 29 insertions, 8 deletions
diff --git a/examples/py_native/bin.py b/examples/py_native/bin.py
index 7b656278f6..45c68b26e5 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 0e28ce16f7..3951f49068 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 20c94fb5e5..7f6f739454 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" <<EOF
+startup --batch
+build --cpu=x64_windows_msvc
+EOF
}
# An assertion that execute a binary from a sub directory (to test runfiles)
@@ -141,5 +144,17 @@ function test_native_python() {
assert_test_fails //examples/py_native:fail
}
+function test_native_python_with_python3() {
+ PYTHON3_PATH=${PYTHON3_PATH:-/c/Program Files/Anaconda3}
+ if [ ! -x "${PYTHON3_PATH}/python.exe" ]; then
+ warn "Python3 binary not found under $PYTHON3_PATH, please set PYTHON3_PATH correctly"
+ else
+ # Shutdown bazel to ensure python path get updated.
+ export BAZEL_PYTHON="${PYTHON3_PATH}/python.exe"
+ export PATH="${PYTHON3_PATH}:$PATH"
+ test_native_python
+ fi
+}
+
run_suite "examples on Windows"