Skip to content

Commit bc092e5

Browse files
authored
feat(examples): move examples to a nested WORKSPACE (#337)
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)
1 parent 2c117e3 commit bc092e5

File tree

21 files changed

+71
-108
lines changed

21 files changed

+71
-108
lines changed

.bazelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# For bazel-in-bazel testing
2+
# Trick bazel into treating BUILD files under examples/* and e2e/* as being regular files
3+
# This lets us glob() up all the files inside the examples to make them inputs to tests
4+
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
5+
# To update these lines, run this command:
6+
# sed -i.bak "/^[^#].*--deleted_packages/s#=.*#=$(find examples/*/* \( -name BUILD -or -name BUILD.bazel \) | xargs -n 1 dirname | paste -sd, -)#" .bazelrc && rm .bazelrc.bak
7+
build --deleted_packages=examples/pip/boto,examples/pip/extras,examples/pip/helloworld
8+
query --deleted_packages=examples/pip/boto,examples/pip/extras,examples/pip/helloworld

examples/pip/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# pip example
2+
3+
This shows how you can point Bazel at your requirements.txt file to get dependencies automatically installed.
4+
5+
It also shows how a monorepo might have multiple python packages which each have a separate requirements.txt file.

examples/pip/WORKSPACE

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
workspace(name = "pip_example")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
http_archive(
6+
name = "rules_python",
7+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.0.2/rules_python-0.0.2.tar.gz",
8+
strip_prefix = "rules_python-0.0.2",
9+
sha256 = "b5668cde8bb6e3515057ef465a35ad712214962f0b3a314e551204266c7be90c",
10+
)
11+
12+
load("@rules_python//python:pip.bzl", "pip_import", "pip_repositories")
13+
14+
pip_repositories()
15+
16+
pip_import(
17+
name = "helloworld_deps",
18+
requirements = "//helloworld:requirements.txt",
19+
)
20+
21+
load("@helloworld_deps//:requirements.bzl", _helloworld_install = "pip_install")
22+
23+
_helloworld_install()
24+
25+
pip_import(
26+
name = "boto_deps",
27+
requirements = "//boto:requirements.txt",
28+
)
29+
30+
load("@boto_deps//:requirements.bzl", _boto_install = "pip_install")
31+
32+
_boto_install()
33+
34+
pip_import(
35+
name = "extras_deps",
36+
requirements = "//extras:requirements.txt",
37+
)
38+
39+
load("@extras_deps//:requirements.bzl", _extras_install = "pip_install")
40+
41+
_extras_install()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@examples_boto//:requirements.bzl", "requirement")
16-
load("//python:defs.bzl", "py_test")
15+
load("@boto_deps//:requirements.bzl", "requirement")
16+
load("@rules_python//python:defs.bzl", "py_test")
1717

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
# limitations under the License.
1414

1515
import boto3
16+
import pip
1617
import unittest
1718

1819

1920
class BotoTest(unittest.TestCase):
2021

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

2429

2530
if __name__ == '__main__':
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ boto3==1.4.7
22
# Release 0.15 appears to have a broken wheel.
33
# See https://github.com/bazelbuild/rules_python/issues/205.
44
docutils!=0.15.post1
5+
pip==9.0.3
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@examples_extras//:requirements.bzl", "requirement")
16-
load("//python:defs.bzl", "py_test")
15+
load("@extras_deps//:requirements.bzl", "requirement")
16+
load("@rules_python//python:defs.bzl", "py_test")
1717

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

0 commit comments

Comments
 (0)