Skip to content

Commit

Permalink
Fix python stub template for python3 on Windows
Browse files Browse the repository at this point in the history
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 #2708

--
Change-Id: I8dc18c80ebba87e965fe6fef9978732e6c35b1f0
Reviewed-on: https://cr.bazel.build/9456
PiperOrigin-RevId: 150734716
MOS_MIGRATED_REVID=150734716
  • Loading branch information
meteorcloudy authored and hermione521 committed Mar 21, 2017
1 parent c0a8444 commit dce45d3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
5 changes: 3 additions & 2 deletions examples/py_native/bin.py
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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%'
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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), \
Expand Down
17 changes: 16 additions & 1 deletion src/test/shell/bazel/bazel_windows_example_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"

0 comments on commit dce45d3

Please sign in to comment.