Skip to content

Commit

Permalink
Add load() statements for Python rules in third_party
Browse files Browse the repository at this point in the history
This replaces all direct uses of the native Python rules underneath the
third_party/ directory with load()s of the internal wrapper macros. These
macros are needed for compatibility with
`--incompatible_load_python_rules_from_bzl`.

Work toward #9006.

RELNOTES: None
  • Loading branch information
brandjon committed Aug 1, 2019
1 parent 299e3f0 commit 4ab7cdd
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 2 deletions.
8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ new_local_repository(
path = "./third_party/protobuf/3.6.1/",
)

# This is a mock version of bazelbuild/rules_python that contains only
# @rules_python//python:defs.bzl. It is used by protobuf.
# TODO(#9029): We could potentially replace this with the real @rules_python.
local_repository(
name = "rules_python",
path = "./third_party/rules_python",
)

local_repository(
name = "googleapis",
path = "./third_party/googleapis/",
Expand Down
4 changes: 4 additions & 0 deletions third_party/def_parser/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
# merging from upstream becomes an issue.
load("//tools/python:private/defs.bzl", "py_test")

licenses(["notice"]) # 3-clause BSD

package(
Expand Down
2 changes: 2 additions & 0 deletions third_party/protobuf/3.6.1/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ config_setting(

load(":protobuf.bzl", "py_proto_library")
load(":compiler_config_setting.bzl", "create_compiler_config_setting")
# Needed for #9006. Hopefully a future upstream version will include this line.
load("@rules_python//python:defs.bzl", "py_library")

filegroup(
name = "srcs",
Expand Down
7 changes: 5 additions & 2 deletions third_party/protobuf/3.6.1/protobuf.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Needed for #9006. Hopefully a future upstream version will include this line.
load("@rules_python//python:defs.bzl", "py_library", "py_test")

def _GetPath(ctx, path):
if ctx.label.workspace_root:
return ctx.label.workspace_root + "/" + path
Expand Down Expand Up @@ -383,7 +386,7 @@ def py_proto_library(
if default_runtime and not default_runtime in py_libs + deps:
py_libs = py_libs + [default_runtime]

native.py_library(
py_library(
name = name,
srcs = outs + py_extra_srcs,
deps = py_libs + deps,
Expand All @@ -406,7 +409,7 @@ def internal_protobuf_py_tests(
"""
for m in modules:
s = "python/google/protobuf/internal/%s.py" % m
native.py_test(
py_test(
name = "py_%s" % m,
srcs = [s],
main = s,
Expand Down
4 changes: 4 additions & 0 deletions third_party/py/abseil/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
# merging from upstream becomes an issue.
load("//tools/python:private/defs.bzl", "py_library")

licenses(["notice"])

filegroup(
Expand Down
4 changes: 4 additions & 0 deletions third_party/py/concurrent/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
# merging from upstream becomes an issue.
load("//tools/python:private/defs.bzl", "py_library")

licenses(["notice"])

filegroup(
Expand Down
4 changes: 4 additions & 0 deletions third_party/py/gflags/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
# merging from upstream becomes an issue.
load("//tools/python:private/defs.bzl", "py_library")

licenses(["notice"])

filegroup(
Expand Down
4 changes: 4 additions & 0 deletions third_party/py/mock/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Needed for #9006. Consider replacing label with @rules_python as per #9029 if
# merging from upstream becomes an issue.
load("//tools/python:private/defs.bzl", "py_library")

licenses(["notice"])

filegroup(
Expand Down
5 changes: 5 additions & 0 deletions third_party/py/six/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Needed for #9006. We can't easily replace this label with a @rules_python
# label (#9029) because this file is used in both @io_bazel and @bazel_tools,
# the latter of which can't reference other repos.
load("//tools/python:private/defs.bzl", "py_library")

licenses(["notice"])

filegroup(
Expand Down
7 changes: 7 additions & 0 deletions third_party/rules_python/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# A stub version of bazelbuild/rules_python that contains only the defs.bzl
# file, which is needed to access native Python rules under
# --incompatible_load_python_rules_from_bzl (#9006).
#
# TODO(#9029): Maybe replace this with the real thing.

workspace(name = "rules_python")
1 change: 1 addition & 0 deletions third_party/rules_python/python/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file intentionally left blank.
36 changes: 36 additions & 0 deletions third_party/rules_python/python/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This file is selectively copied from //tools/python:private/defs.bzl."""

_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"

def _add_tags(attrs):
if "tags" in attrs and attrs["tags"] != None:
attrs["tags"] += [_MIGRATION_TAG]
else:
attrs["tags"] = [_MIGRATION_TAG]
return attrs

def py_library(**attrs):
native.py_library(**_add_tags(attrs))

def py_binary(**attrs):
native.py_binary(**_add_tags(attrs))

def py_test(**attrs):
native.py_test(**_add_tags(attrs))

def py_runtime(**attrs):
native.py_runtime(**_add_tags(attrs))

0 comments on commit 4ab7cdd

Please sign in to comment.