Skip to content

Commit

Permalink
feat(examples): move examples to a nested WORKSPACE (#337)
Browse files Browse the repository at this point in the history
This lets users understand the example in isolation. They can copy/paste the example directory
and it works correctly.

This refactors the existing examples which are quite weak, only really demonstrating pip usage.
This makes room for examples demonstrating other features (like protocol buffers) or package
managers (like poetry).

In a later commit I'll add bazel-integration-testing so we get a test target that confirms
the examples build (including their WORKSPACE being self-contained)
  • Loading branch information
alexeagle authored Jul 17, 2020
1 parent 2c117e3 commit bc092e5
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 108 deletions.
8 changes: 8 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# For bazel-in-bazel testing
# Trick bazel into treating BUILD files under examples/* and e2e/* as being regular files
# This lets us glob() up all the files inside the examples to make them inputs to tests
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
# To update these lines, run this command:
# sed -i.bak "/^[^#].*--deleted_packages/s#=.*#=$(find examples/*/* \( -name BUILD -or -name BUILD.bazel \) | xargs -n 1 dirname | paste -sd, -)#" .bazelrc && rm .bazelrc.bak
build --deleted_packages=examples/pip/boto,examples/pip/extras,examples/pip/helloworld
query --deleted_packages=examples/pip/boto,examples/pip/extras,examples/pip/helloworld
5 changes: 5 additions & 0 deletions examples/pip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# pip example

This shows how you can point Bazel at your requirements.txt file to get dependencies automatically installed.

It also shows how a monorepo might have multiple python packages which each have a separate requirements.txt file.
41 changes: 41 additions & 0 deletions examples/pip/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
workspace(name = "pip_example")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_python",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.0.2/rules_python-0.0.2.tar.gz",
strip_prefix = "rules_python-0.0.2",
sha256 = "b5668cde8bb6e3515057ef465a35ad712214962f0b3a314e551204266c7be90c",
)

load("@rules_python//python:pip.bzl", "pip_import", "pip_repositories")

pip_repositories()

pip_import(
name = "helloworld_deps",
requirements = "//helloworld:requirements.txt",
)

load("@helloworld_deps//:requirements.bzl", _helloworld_install = "pip_install")

_helloworld_install()

pip_import(
name = "boto_deps",
requirements = "//boto:requirements.txt",
)

load("@boto_deps//:requirements.bzl", _boto_install = "pip_install")

_boto_install()

pip_import(
name = "extras_deps",
requirements = "//extras:requirements.txt",
)

load("@extras_deps//:requirements.bzl", _extras_install = "pip_install")

_extras_install()
4 changes: 2 additions & 2 deletions examples/boto/BUILD → examples/pip/boto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@examples_boto//:requirements.bzl", "requirement")
load("//python:defs.bzl", "py_test")
load("@boto_deps//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_test")

package(default_visibility = ["//visibility:public"])

Expand Down
5 changes: 5 additions & 0 deletions examples/boto/boto_test.py → examples/pip/boto/boto_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
# limitations under the License.

import boto3
import pip
import unittest


class BotoTest(unittest.TestCase):

def test_version(self):
# Just the minimal assertion that the boto3 import worked
self.assertEqual(boto3.__version__, '1.4.7')
# Regression test that the pip version is the one requested
# see https://github.com/bazelbuild/rules_python/pull/1#discussion_r138349892
self.assertEqual(pip.__version__, '9.0.3')


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ boto3==1.4.7
# Release 0.15 appears to have a broken wheel.
# See https://github.com/bazelbuild/rules_python/issues/205.
docutils!=0.15.post1
pip==9.0.3
4 changes: 2 additions & 2 deletions examples/extras/BUILD → examples/pip/extras/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@examples_extras//:requirements.bzl", "requirement")
load("//python:defs.bzl", "py_test")
load("@extras_deps//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_test")

package(default_visibility = ["//visibility:public"])

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions examples/helloworld/BUILD → examples/pip/helloworld/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@examples_helloworld//:requirements.bzl", "requirement")
load("//python:defs.bzl", "py_library", "py_test")
load("@helloworld_deps//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_library", "py_test")

package(default_visibility = ["//visibility:public"])

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 0 additions & 26 deletions examples/version/BUILD

This file was deleted.

1 change: 0 additions & 1 deletion examples/version/requirements.txt

This file was deleted.

26 changes: 0 additions & 26 deletions examples/version/version_test.py

This file was deleted.

2 changes: 1 addition & 1 deletion experimental/examples/wheel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ py_library(
"//experimental/examples/wheel/lib:module_with_data",
# Example dependency which is not packaged in the wheel
# due to "packages" filter on py_package rule.
"//examples/helloworld",
"//tests/load_from_macro:foo",
],
)

Expand Down
21 changes: 0 additions & 21 deletions internal_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,3 @@ def rules_python_internal_deps():
name = "piptool_deps",
requirements = "@rules_python//python:requirements.txt",
)

examples()

def examples():
"""Fetches all required dependencies for rules_python examples."""
pip_import(
name = "examples_helloworld",
requirements = "@rules_python//examples/helloworld:requirements.txt",
)
pip_import(
name = "examples_version",
requirements = "@rules_python//examples/version:requirements.txt",
)
pip_import(
name = "examples_boto",
requirements = "@rules_python//examples/boto:requirements.txt",
)
pip_import(
name = "examples_extras",
requirements = "@rules_python//examples/extras:requirements.txt",
)
29 changes: 2 additions & 27 deletions internal_setup.bzl
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
"""Setup for rules_python tests, tools, and examples."""

load(
"@examples_boto//:requirements.bzl",
_boto_install = "pip_install",
)
load(
"@examples_extras//:requirements.bzl",
_extras_install = "pip_install",
)

# Imports for examples.
load(
"@examples_helloworld//:requirements.bzl",
_helloworld_install = "pip_install",
)
load(
"@examples_version//:requirements.bzl",
_version_install = "pip_install",
)
"""Setup for rules_python tests and tools."""

# Requirements for building our piptool.
load(
Expand All @@ -26,13 +7,7 @@ load(
)

def rules_python_internal_setup():
"""Setup for rules_python tests, tools, and examples."""
"""Setup for rules_python tests and tools."""

# Requirements for building our piptool.
_piptool_install()

# Imports for examples.
_helloworld_install()
_version_install()
_boto_install()
_extras_install()
2 changes: 2 additions & 0 deletions tests/load_from_macro/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ py_library(
name = "foo",
srcs = ["foo.py"],
tags = TAGS,
# Allow a test to verify an "outside package" doesn't get included
visibility = ["//experimental/examples/wheel:__pkg__"],
)

0 comments on commit bc092e5

Please sign in to comment.