aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-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"