Enable buildifier presubmit check (#245)
This migrates the repo to satisfy buildifier and adds buildifier as a presubmit.
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml
index feb2bae..6422631 100644
--- a/.bazelci/presubmit.yml
+++ b/.bazelci/presubmit.yml
@@ -4,6 +4,9 @@
# TODO(#144): When Bazel 0.29 is released, the docgen workflow will work, and
# we can go back to using `...` for build_targets.
---
+buildifier:
+ version: latest
+ warnings: "all"
all_targets: &all_targets
build_targets:
- "//examples/..."
diff --git a/BUILD b/BUILD
index 1bf1f64..3fb3f8d 100644
--- a/BUILD
+++ b/BUILD
@@ -15,7 +15,10 @@
licenses(["notice"]) # Apache 2.0
-exports_files(["LICENSE", "version.bzl"])
+exports_files([
+ "LICENSE",
+ "version.bzl",
+])
filegroup(
name = "distribution",
diff --git a/WORKSPACE b/WORKSPACE
index b9ccadd..0b84e07 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -22,21 +22,25 @@
http_archive(
name = "bazel_federation",
- url = "https://github.com/bazelbuild/bazel-federation/releases/download/0.0.1/bazel_federation-0.0.1.tar.gz",
sha256 = "506dfbfd74ade486ac077113f48d16835fdf6e343e1d4741552b450cfc2efb53",
+ url = "https://github.com/bazelbuild/bazel-federation/releases/download/0.0.1/bazel_federation-0.0.1.tar.gz",
)
load("@bazel_federation//:repositories.bzl", "rules_python_deps")
+
rules_python_deps()
-load("@bazel_federation//setup:rules_python.bzl", "rules_python_setup")
-rules_python_setup(use_pip=True)
+load("@bazel_federation//setup:rules_python.bzl", "rules_python_setup")
+
+rules_python_setup(use_pip = True)
# Everything below this line is used only for developing rules_python. Users
# should not copy it to their WORKSPACE.
load("//:internal_deps.bzl", "rules_python_internal_deps")
+
rules_python_internal_deps()
load("//:internal_setup.bzl", "rules_python_internal_setup")
+
rules_python_internal_setup()
diff --git a/distro/BUILD b/distro/BUILD
index f1bd290..ac1f5c1 100644
--- a/distro/BUILD
+++ b/distro/BUILD
@@ -2,9 +2,9 @@
default_visibility = ["//visibility:private"],
)
-load("@rules_python//:version.bzl", "version")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@rules_pkg//releasing:defs.bzl", "print_rel_notes")
+load("@rules_python//:version.bzl", "version")
# Build the artifact to put on the github release page.
pkg_tar(
diff --git a/docs/BUILD b/docs/BUILD
index 70b6a5c..90335fc 100644
--- a/docs/BUILD
+++ b/docs/BUILD
@@ -31,10 +31,10 @@
bzl_library(
name = "bazel_python_tools",
srcs = [
+ "@bazel_tools//tools/python:private/defs.bzl",
"@bazel_tools//tools/python:srcs_version.bzl",
"@bazel_tools//tools/python:toolchain.bzl",
"@bazel_tools//tools/python:utils.bzl",
- "@bazel_tools//tools/python:private/defs.bzl",
],
)
@@ -49,8 +49,8 @@
stardoc(
name = "core-docs",
- input = "//python:defs.bzl",
out = "python.md",
+ input = "//python:defs.bzl",
deps = [":defs"],
)
@@ -60,12 +60,12 @@
stardoc(
name = "pip-docs",
- input = "//python:pip.bzl",
out = "pip.md",
+ input = "//python:pip.bzl",
)
stardoc(
name = "whl-docs",
- input = "//python:whl.bzl",
out = "whl.md",
+ input = "//python:whl.bzl",
)
diff --git a/examples/extras/BUILD b/examples/extras/BUILD
index 349b12a..babfc1b 100644
--- a/examples/extras/BUILD
+++ b/examples/extras/BUILD
@@ -21,10 +21,10 @@
py_test(
name = "extras_test",
srcs = ["extras_test.py"],
+ python_version = "PY2",
deps = [
requirement("google-cloud-language"),
# Make sure that we can resolve the "extra" dependency
requirement("googleapis-common-protos[grpc]"),
],
- python_version = "PY2",
)
diff --git a/examples/helloworld/BUILD b/examples/helloworld/BUILD
index 43ac6c1..285bc9d 100644
--- a/examples/helloworld/BUILD
+++ b/examples/helloworld/BUILD
@@ -27,6 +27,6 @@
py_test(
name = "helloworld_test",
srcs = ["helloworld_test.py"],
- deps = [":helloworld"],
python_version = "PY2",
+ deps = [":helloworld"],
)
diff --git a/experimental/examples/wheel/BUILD b/experimental/examples/wheel/BUILD
index eedcd0c..1e4dcec 100644
--- a/experimental/examples/wheel/BUILD
+++ b/experimental/examples/wheel/BUILD
@@ -15,8 +15,8 @@
licenses(["notice"]) # Apache 2.0
-load("//python:defs.bzl", "py_library", "py_test")
load("//experimental/python:wheel.bzl", "py_package", "py_wheel")
+load("//python:defs.bzl", "py_library", "py_test")
py_library(
name = "main",
@@ -91,13 +91,13 @@
# Package data. We're building "custom_package_root-0.0.1-py3-none-any.whl"
distribution = "example_custom_package_root",
python_tag = "py3",
+ strip_path_prefixes = [
+ "experimental",
+ ],
version = "0.0.1",
deps = [
- ":example_pkg"
+ ":example_pkg",
],
- strip_path_prefixes = [
- "experimental"
- ]
)
py_wheel(
@@ -105,14 +105,14 @@
# Package data. We're building "custom_custom_package_root_multi_prefix-0.0.1-py3-none-any.whl"
distribution = "example_custom_package_root_multi_prefix",
python_tag = "py3",
- version = "0.0.1",
- deps = [
- ":example_pkg"
- ],
strip_path_prefixes = [
"experimental/examples/wheel/lib",
- "experimental/examples/wheel"
- ]
+ "experimental/examples/wheel",
+ ],
+ version = "0.0.1",
+ deps = [
+ ":example_pkg",
+ ],
)
py_wheel(
@@ -120,25 +120,25 @@
# Package data. We're building "custom_custom_package_root_multi_prefix_reverse_order-0.0.1-py3-none-any.whl"
distribution = "example_custom_package_root_multi_prefix_reverse_order",
python_tag = "py3",
+ strip_path_prefixes = [
+ "experimental/examples/wheel",
+ "experimental/examples/wheel/lib", # this is not effective, because the first prefix takes priority
+ ],
version = "0.0.1",
deps = [
- ":example_pkg"
+ ":example_pkg",
],
- strip_path_prefixes = [
- "experimental/examples/wheel" ,
- "experimental/examples/wheel/lib" # this is not effective, because the first prefix takes priority
- ]
)
py_test(
name = "wheel_test",
srcs = ["wheel_test.py"],
data = [
+ ":custom_package_root",
+ ":custom_package_root_multi_prefix",
+ ":custom_package_root_multi_prefix_reverse_order",
":customized",
":minimal_with_py_library",
":minimal_with_py_package",
- ":custom_package_root",
- ":custom_package_root_multi_prefix",
- ":custom_package_root_multi_prefix_reverse_order"
- ]
+ ],
)
diff --git a/experimental/python/wheel.bzl b/experimental/python/wheel.bzl
index a9d35d9..7f3c6fe 100644
--- a/experimental/python/wheel.bzl
+++ b/experimental/python/wheel.bzl
@@ -18,14 +18,16 @@
# input_file.short_path is sometimes relative ("../${repository_root}/foobar")
# which is not a valid path within a zip file. Fix that.
short_path = input_file.short_path
- if short_path.startswith('..') and len(short_path) >= 3:
+ if short_path.startswith("..") and len(short_path) >= 3:
# Path separator. '/' on linux.
separator = short_path[2]
+
# Consume '../' part.
short_path = short_path[3:]
+
# Find position of next '/' and consume everything up to that character.
pos = short_path.find(separator)
- short_path = short_path[pos+1:]
+ short_path = short_path[pos + 1:]
return short_path
def _input_file_to_arg(input_file):
@@ -67,7 +69,6 @@
This rule is intended to be used as data dependency to py_wheel rule
""",
attrs = {
- "deps": attr.label_list(),
"packages": attr.string_list(
mandatory = False,
allow_empty = True,
@@ -76,6 +77,7 @@
Sub-packages are automatically included.
""",
),
+ "deps": attr.label_list(),
},
)
@@ -150,6 +152,72 @@
data_runfiles = ctx.runfiles(files = [outfile]),
)]
+def _concat_dicts(*dicts):
+ result = {}
+ for d in dicts:
+ result.update(d)
+ return result
+
+_distribution_attrs = {
+ "abi": attr.string(
+ default = "none",
+ doc = "Python ABI tag. 'none' for pure-Python wheels.",
+ ),
+ "distribution": attr.string(
+ mandatory = True,
+ doc = """
+Name of the distribution.
+
+This should match the project name onm PyPI. It's also the name that is used to
+refer to the package in other packages' dependencies.
+""",
+ ),
+ # TODO(pstradomski): Support non-pure wheels
+ "platform": attr.string(
+ default = "any",
+ doc = "Supported platforms. 'any' for pure-Python wheel.",
+ ),
+ "python_tag": attr.string(
+ default = "py3",
+ doc = "Supported Python major version. 'py2' or 'py3'",
+ values = ["py2", "py3"],
+ ),
+ "version": attr.string(
+ mandatory = True,
+ doc = "Version number of the package",
+ ),
+}
+
+_requirement_attrs = {
+ "extra_requires": attr.string_list_dict(
+ doc = "List of optional requirements for this package",
+ ),
+ "requires": attr.string_list(
+ doc = "List of requirements for this package",
+ ),
+}
+
+_entrypoint_attrs = {
+ "console_scripts": attr.string_dict(
+ doc = """\
+console_script entry points, e.g. 'experimental.examples.wheel.main:main'.
+""",
+ ),
+}
+
+_other_attrs = {
+ "author": attr.string(default = ""),
+ "author_email": attr.string(default = ""),
+ "classifiers": attr.string_list(),
+ "description_file": attr.label(allow_single_file = True),
+ "homepage": attr.string(default = ""),
+ "license": attr.string(default = ""),
+ "strip_path_prefixes": attr.string_list(
+ default = [],
+ doc = "path prefixes to strip from files added to the generated package",
+ ),
+}
+
py_wheel = rule(
implementation = _py_wheel_impl,
doc = """
@@ -196,9 +264,10 @@
)
</code>
""",
- attrs = {
- "deps": attr.label_list(
- doc = """\
+ attrs = _concat_dicts(
+ {
+ "deps": attr.label_list(
+ doc = """\
Targets to be included in the distribution.
The targets to package are usually `py_library` rules or filesets (for packaging data files).
@@ -208,64 +277,16 @@
`py_binary` rules. `py_binary` targets would wrap a executable script that
tries to locate `.runfiles` directory which is not packaged in the wheel.
""",
- ),
- # Attributes defining the distribution
- "distribution": attr.string(
- mandatory = True,
- doc = """
-Name of the distribution.
-
-This should match the project name onm PyPI. It's also the name that is used
-to refer to the package in other packages' dependencies.
-""",
- ),
- "version": attr.string(
- mandatory = True,
- doc = "Version number of the package",
- ),
- "python_tag": attr.string(
- default = "py3",
- doc = "Supported Python major version. 'py2' or 'py3'",
- values = ["py2", "py3"],
- ),
- "abi": attr.string(
- default = "none",
- doc = "Python ABI tag. 'none' for pure-Python wheels.",
- ),
- # TODO(pstradomski): Support non-pure wheels
- "platform": attr.string(
- default = "any",
- doc = "Supported platforms. 'any' for pure-Python wheel.",
- ),
- # Other attributes
- "author": attr.string(default = ""),
- "author_email": attr.string(default = ""),
- "homepage": attr.string(default = ""),
- "license": attr.string(default = ""),
- "classifiers": attr.string_list(),
- "description_file": attr.label(allow_single_file = True),
- "strip_path_prefixes": attr.string_list(
- default = [],
- doc = "path prefixes to strip from files added to the generated package",
- ),
- # Requirements
- "requires": attr.string_list(
- doc = "List of requirements for this package",
- ),
- "extra_requires": attr.string_list_dict(
- doc = "List of optional requirements for this package",
- ),
- # Entry points
- "console_scripts": attr.string_dict(
- doc = """\
-console_script entry points, e.g. 'experimental.examples.wheel.main:main'.
-""",
- ),
- # Implementation details.
- "_wheelmaker": attr.label(
- executable = True,
- cfg = "host",
- default = "//experimental/rules_python:wheelmaker",
- ),
- },
+ ),
+ "_wheelmaker": attr.label(
+ executable = True,
+ cfg = "host",
+ default = "//experimental/rules_python:wheelmaker",
+ ),
+ },
+ _distribution_attrs,
+ _requirement_attrs,
+ _entrypoint_attrs,
+ _other_attrs,
+ ),
)
diff --git a/internal_deps.bzl b/internal_deps.bzl
index 1873e66..8e3df65 100644
--- a/internal_deps.bzl
+++ b/internal_deps.bzl
@@ -1,9 +1,11 @@
+"""Dependencies that are needed for rules_python tests and tools."""
+
load("@bazel_federation//:repositories.bzl", "bazel_stardoc", "rules_pkg")
load("@bazel_federation//:third_party_repositories.bzl", "futures_2_whl", "futures_3_whl", "google_cloud_language_whl", "grpc_whl", "mock_whl", "subpar")
load("@rules_python//python:pip.bzl", "pip_import")
-
def rules_python_internal_deps():
+ """Fetches all required dependencies for rules_python tests and tools."""
bazel_stardoc()
subpar()
@@ -21,15 +23,15 @@
# For packaging and distribution
rules_pkg()
-
def piptool():
+ """Fetches all required dependencies for pip."""
pip_import(
name = "piptool_deps",
requirements = "@rules_python//python:requirements.txt",
)
-
def examples():
+ """Fetches all required dependencies for rules_python examples."""
pip_import(
name = "examples_helloworld",
requirements = "@rules_python//examples/helloworld:requirements.txt",
diff --git a/internal_setup.bzl b/internal_setup.bzl
index 281bac0..c8df847 100644
--- a/internal_setup.bzl
+++ b/internal_setup.bzl
@@ -1,7 +1,12 @@
-# Requirements for building our piptool.
+"""Setup for rules_python tests, tools, and examples."""
+
load(
- "@piptool_deps//:requirements.bzl",
- _piptool_install = "pip_install",
+ "@examples_boto//:requirements.bzl",
+ _boto_install = "pip_install",
+)
+load(
+ "@examples_extras//:requirements.bzl",
+ _extras_install = "pip_install",
)
# Imports for examples.
@@ -13,17 +18,16 @@
"@examples_version//:requirements.bzl",
_version_install = "pip_install",
)
-load(
- "@examples_boto//:requirements.bzl",
- _boto_install = "pip_install",
-)
-load(
- "@examples_extras//:requirements.bzl",
- _extras_install = "pip_install",
-)
+# Requirements for building our piptool.
+load(
+ "@piptool_deps//:requirements.bzl",
+ _piptool_install = "pip_install",
+)
def rules_python_internal_setup():
+ """Setup for rules_python tests, tools, and examples."""
+
# Requirements for building our piptool.
_piptool_install()
diff --git a/packaging/BUILD b/packaging/BUILD
index cd37589..5f69d37 100644
--- a/packaging/BUILD
+++ b/packaging/BUILD
@@ -14,9 +14,9 @@
licenses(["notice"]) # Apache 2.0
-load("//python:defs.bzl", "py_library", "py_test")
-load("@subpar//:subpar.bzl", "par_binary")
load("@piptool_deps//:requirements.bzl", "requirement")
+load("@subpar//:subpar.bzl", "par_binary")
+load("//python:defs.bzl", "py_library", "py_test")
py_library(
name = "whl",
@@ -30,8 +30,8 @@
name = "whl_test",
srcs = ["whl_test.py"],
data = [
- "@futures_3_1_1_whl//file",
"@futures_2_2_0_whl//file",
+ "@futures_3_1_1_whl//file",
"@google_cloud_language_whl//file",
"@grpc_whl//file",
"@mock_whl//file",
diff --git a/python/BUILD b/python/BUILD
index cb092f8..e5891c0 100644
--- a/python/BUILD
+++ b/python/BUILD
@@ -47,7 +47,10 @@
# Needed to define bzl_library targets for docgen. (We don't define the
# bzl_library target here because it'd give our users a transitive dependency
# on Skylib.)
-exports_files(["private/reexports.bzl"], visibility = ["//docs:__pkg__"])
+exports_files(
+ ["private/reexports.bzl"],
+ visibility = ["//docs:__pkg__"],
+)
# This target can be used to inspect the current Python major version. To use,
# put it in the `flag_values` attribute of a `config_setting` and test it
diff --git a/python/defs.bzl b/python/defs.bzl
index 20ebb59..1e36de3 100644
--- a/python/defs.bzl
+++ b/python/defs.bzl
@@ -19,8 +19,8 @@
migrated to Starlark, their implementations will be moved here.
"""
-load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
load("@bazel_tools//tools/python:srcs_version.bzl", _find_requirements = "find_requirements")
+load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
load(":private/reexports.bzl", "internal_PyInfo", "internal_PyRuntimeInfo")
# Exports of native-defined providers.
@@ -47,6 +47,8 @@
Args:
**attrs: Rule attributes
"""
+
+ # buildifier: disable=native-python
native.py_library(**_add_tags(attrs))
def py_binary(**attrs):
@@ -55,6 +57,8 @@
Args:
**attrs: Rule attributes
"""
+
+ # buildifier: disable=native-python
native.py_binary(**_add_tags(attrs))
def py_test(**attrs):
@@ -63,6 +67,8 @@
Args:
**attrs: Rule attributes
"""
+
+ # buildifier: disable=native-python
native.py_test(**_add_tags(attrs))
def py_runtime(**attrs):
@@ -71,6 +77,8 @@
Args:
**attrs: Rule attributes
"""
+
+ # buildifier: disable=native-python
native.py_runtime(**_add_tags(attrs))
# Re-exports of Starlark-defined symbols in @bazel_tools//tools/python.
diff --git a/python/pip.bzl b/python/pip.bzl
index b69eb93..cc69425 100644
--- a/python/pip.bzl
+++ b/python/pip.bzl
@@ -52,9 +52,7 @@
),
},
implementation = _pip_import_impl,
-)
-
-"""A rule for importing <code>requirements.txt</code> dependencies into Bazel.
+ doc = """A rule for importing <code>requirements.txt</code> dependencies into Bazel.
This rule imports a <code>requirements.txt</code> file and generates a new
<code>requirements.bzl</code> file. This is used via the <code>WORKSPACE</code>
@@ -94,10 +92,12 @@
Args:
requirements: The label of a requirements.txt file.
-"""
+""",
+)
def pip_repositories():
"""Pull in dependencies needed to use the packaging rules."""
+
# At the moment this is a placeholder, in that it does not actually pull in
# any dependencies. However, it does do some validation checking.
#
diff --git a/python/private/reexports.bzl b/python/private/reexports.bzl
index cb01b85..f827564 100644
--- a/python/private/reexports.bzl
+++ b/python/private/reexports.bzl
@@ -34,7 +34,10 @@
"""
# Don't use underscore prefix, since that would make the symbol local to this
-# file only.
-
+# file only. Use a non-conventional name to emphasize that this is not a public
+# symbol.
+# buildifier: disable=name-conventions
internal_PyInfo = PyInfo
+
+# buildifier: disable=name-conventions
internal_PyRuntimeInfo = PyRuntimeInfo
diff --git a/python/python.bzl b/python/python.bzl
index f94d7ab..3e739ca 100644
--- a/python/python.bzl
+++ b/python/python.bzl
@@ -22,7 +22,9 @@
"""
def py_library(*args, **kwargs):
- """See the Bazel core [py_library](
+ """Deprecated py_library rule.
+
+ See the Bazel core [py_library](
https://docs.bazel.build/versions/master/be/python.html#py_library)
documentation.
@@ -30,10 +32,14 @@
`--incompatible_load_python_rules_from_bzl` is enabled. Please use the
symbols in `@rules_python//python:defs.bzl` instead.
"""
+
+ # buildifier: disable=native-python
native.py_library(*args, **kwargs)
def py_binary(*args, **kwargs):
- """See the Bazel core [py_binary](
+ """Deprecated py_binary rule.
+
+ See the Bazel core [py_binary](
https://docs.bazel.build/versions/master/be/python.html#py_binary)
documentation.
@@ -41,10 +47,14 @@
`--incompatible_load_python_rules_from_bzl` is enabled. Please use the
symbols in `@rules_python//python:defs.bzl` instead.
"""
+
+ # buildifier: disable=native-python
native.py_binary(*args, **kwargs)
def py_test(*args, **kwargs):
- """See the Bazel core [py_test](
+ """Deprecated py_test rule.
+
+ See the Bazel core [py_test](
https://docs.bazel.build/versions/master/be/python.html#py_test)
documentation.
@@ -52,4 +62,6 @@
`--incompatible_load_python_rules_from_bzl` is enabled. Please use the
symbols in `@rules_python//python:defs.bzl` instead.
"""
+
+ # buildifier: disable=native-python
native.py_test(*args, **kwargs)
diff --git a/python/repositories.bzl b/python/repositories.bzl
index 997bdea..61052a4 100644
--- a/python/repositories.bzl
+++ b/python/repositories.bzl
@@ -5,6 +5,7 @@
def py_repositories():
"""Pull in dependencies needed to use the core Python rules."""
+
# At the moment this is a placeholder hook, in that it does not actually
# pull in any dependencies. Users should still call this function to make
# it less likely that they need to update their WORKSPACE files, in case
diff --git a/python/runfiles/BUILD b/python/runfiles/BUILD
index da34778..fa824ad 100644
--- a/python/runfiles/BUILD
+++ b/python/runfiles/BUILD
@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-
# We'd like to alias the runfiles target @bazel_tools//tools/python/runfiles.
# However, we need its source file to exist in the runfiles tree under this
# repo's name, so that it can be imported as
diff --git a/python/whl.bzl b/python/whl.bzl
index 3f869c2..174352a 100644
--- a/python/whl.bzl
+++ b/python/whl.bzl
@@ -37,12 +37,12 @@
whl_library = repository_rule(
attrs = {
+ "extras": attr.string_list(),
+ "requirements": attr.string(),
"whl": attr.label(
mandatory = True,
allow_single_file = True,
),
- "requirements": attr.string(),
- "extras": attr.string_list(),
"_script": attr.label(
executable = True,
default = Label("//tools:whltool.par"),
@@ -50,9 +50,7 @@
),
},
implementation = _whl_impl,
-)
-
-"""A rule for importing <code>.whl</code> dependencies into Bazel.
+ doc = """A rule for importing <code>.whl</code> dependencies into Bazel.
<b>This rule is currently used to implement <code>pip_import</code>,
it is not intended to work standalone, and the interface may change.</b>
@@ -77,4 +75,5 @@
extras: A subset of the "extras" available from this <code>.whl</code> for which
<code>requirements</code> has the dependencies.
-"""
+""",
+)
diff --git a/tools/BUILD b/tools/BUILD
index 405a9bd..3d89ce5 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -16,7 +16,10 @@
licenses(["notice"]) # Apache 2.0
# These files are generated and updated by ./update_tools.sh
-exports_files(["piptool.par", "whltool.par"])
+exports_files([
+ "piptool.par",
+ "whltool.par",
+])
filegroup(
name = "distribution",