8000 Various updates build_file_generation example (#869) · bazel-contrib/rules_python@fcd0328 · GitHub
[go: up one dir, main page]

Skip to content

Commit fcd0328

Browse files
authored
Various updates build_file_generation example (#869)
* Various updates build_file_generation example Updating the WORKSPACE file and BUILD file inline documentation. Added new code and new directories for example. Added new unit test for example. Added license headers. * Trying to get CI to run * Updating go and gazelle version - updating gazelle version to 0.28 - updating go version to 1.19.4 * Getting windows to build - added requirements_windows.txt from running //:requirements.update on windows - modified WORKSPACE and BUILD files to include different requirements.update when running the build on Windows
1 parent 222ec4b commit fcd0328

File tree

15 files changed

+503
-160
lines changed

15 files changed

+503
-160
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# This lets us glob() up all the files inside the examples to make them inputs to tests
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, run tools/bazel_integration_test/update_deleted_packages.sh
6-
build --deleted_packages=examples/build_file_generation,examples/bzlmod,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
7-
query --deleted_packages=examples/build_file_generation,examples/bzlmod,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
6+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/get_url,examples/bzlmod,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
7+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/get_url,examples/bzlmod,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
88

99
test --test_output=errors
1010

examples/build_file_generation/BUILD

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1+
# Load various rules so that we can have bazel download
2+
# various rulesets and dependencies.
3+
# The `load` statement imports the symbol for the rule, in the defined
4+
# ruleset. When the symbol is loaded you can use the rule.
15
load("@bazel_gazelle//:def.bzl", "gazelle")
26
load("@pip//:requirements.bzl", "all_whl_requirements")
37
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
48
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
59
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
6-
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
10+
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
711
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
812

913
compile_pip_requirements(
1014
name = "requirements",
1115
extra_args = ["--allow-unsafe"],
1216
requirements_in = "requirements.txt",
1317
requirements_txt = "requirements_lock.txt",
18+
requirements_windows = "requirements_windows.txt",
1419
)
1520

16-
# This rule fetches the metadata for python packages we depend on. That data is
17-
# required for the gazelle_python_manifest rule to update our manifest file.
21+
# This repository rule fetches the metadata for python packages we
22+
# depend on. That data is required for the gazelle_python_manifest
23+
# rule to update our manifest file.
24+
# To see what this rule does, try `bazel run @modules_map//:print`
1825
modules_mapping(
1926
name = "modules_map",
2027
exclude_patterns = [
@@ -52,17 +59,44 @@ gazelle(
5259

5360
# This rule is auto-generated and managed by Gazelle,
5461
# because it found the __init__.py file in this folder.
62+
# See: https://bazel.build/reference/be/python#py_library
5563
py_library(
5664
name = "build_file_generation",
5765
srcs = ["__init__.py"],
5866
visibility = ["//:__subpackages__"],
59-
deps = ["@pip_requests//:pkg"],
67+
deps = [
68+
"//random_number_generator",
69+
"@pip_flask//:pkg",
70+
],
6071
)
6172

73+
# A py_binary is an executable Python program consisting of a collection of .py source files.
74+
# See: https://bazel.build/reference/be/python#py_binary
75+
#
76+
# This rule is auto-generated and managed by Gazelle,
77+
# because it found the __main__.py file in this folder.
78+
# This rule creates a target named //:build_file_generation_bin and you can use
79+
# bazel to run the target:
80+
# `bazel run //:build_file_generation_bin`
6281
py_binary(
6382
name = "build_file_generation_bin",
6483
srcs = ["__main__.py"],
6584
main = "__main__.py",
6685
visibility = ["//:__subpackages__"],
6786
deps = [":build_file_generation"],
6887
)
88+
89+
# A py_test is a Python unit test.
90+
# See: https://bazel.build/reference/be/python#py_test
91+
#
92+
# This rule is auto-generated and managed by Gazelle,
93+
# because it found the __test__.py file in this folder.
94+
# This rule creates a target named //:build_file_generation_test and you can use
95+
# bazel to run the target:
96+
# `bazel test //:build_file_generation_test`
97+
py_test(
98+
name = "build_file_generation_test",
99+
srcs = ["__test__.py"],
100+
main = "__test__.py",
101+
deps = [":build_file_generation"],
102+
)
Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
# Set the name of the bazel workspace.
12
workspace(name = "build_file_generation_example")
23

4+
# Load the http_archive rule so that we can have bazel download
5+
# various rulesets and dependencies.
6+
# The `load` statement imports the symbol for http_archive from the http.bzl
7+
# file. When the symbol is loaded you can use the rule.
38
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
49

510
######################################################################
611
# We need rules_go and bazel_gazelle, to build the gazelle plugin from source.
712
# Setup instructions for this section are at
813
# https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
14+
# You may need to update the version of the rule, which is listed in the above
15+
# documentation.
16+
######################################################################
917

10-
# Note, you could omit the rules_go dependency, if you have some way to statically
11-
# compile the gazelle binary for your workspace and distribute it to users on all
12-
# needed platforms.
18+
# Define an http_archive rule that will download the below ruleset,
19+
# test the sha, and extract the ruleset to you local bazel cache.
1320
http_archive(
1421
name = "io_bazel_rules_go",
1522
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
@@ -19,54 +26,107 @@ http_archive(
1926
],
2027
)
2128

29+
# Download the bazel_gazelle ruleset.
2230
http_archive(
2331
name = "bazel_gazelle",
24-
sha256 = "efbbba6ac1a4fd342d5122cbdfdb82aeb2cf2862e35022c752eaddffada7c3f3",
32+
sha256 = "448e37e0dbf61d6fa8f00aaa12d191745e14f07c31cabfa731f0c8e8a4f41b97",
2533
urls = [
26-
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.27.0/bazel-gazelle-v0.27.0.tar.gz",
27-
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.27.0/bazel-gazelle-v0.27.0.tar.gz",
34+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.28.0/bazel-gazelle-v0.28.0.tar.gz",
35+
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.28.0/bazel-gazelle-v0.28.0.tar.gz",
2836
],
2937
)
3038

39+
# Load rules_go ruleset and expose the toolchain and dep rules.
3140
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
3241
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
3342

43+
# go_rules_dependencies is a function that registers external dependencies
44+
# needed by the Go rules.
45+
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
3446
go_rules_dependencies()
3547

36-
go_register_toolchains(version = "1.18.3")
48+
# go_rules_dependencies is a function that registers external dependencies
49+
# needed by the Go rules.
50+
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
51+
go_register_toolchains(version = "1.19.4")
3752

53+
# The following call configured the gazelle dependencies, Go environment and Go SDK.
3854
gazelle_dependencies()
3955

40-
######################################################################
41-
# Remaining setup is for rules_python
56+
# Remaining setup is for rules_python.
4257

58+
# You do not want to use the following command when you are using a WORKSPACE file
59+
# that is outside of rules_python repository.
60+
# This command allows targets from a local directory to be bound.
61+
# Which allows bazel to use targets defined in base rules_python directory.
62+
# If you are using this example outside of the rules_python git repo,
63+
# use the http_archive command that is commented out below.
64+
# https://bazel.build/reference/be/workspace#local_repository
4365
local_repository(
4466
name = "rules_python",
4567
path = "../..",
4668
)
4769

70+
# When not using this example in the rules_python git repo you would load the python
71+
# ruleset using the following StarLark.
72+
# See https://github.com/bazelbuild/rules_python#getting-started for the latest
73+
# ruleset version.
74+
#
75+
# The following StarLark would replace the `local_repository` rule mentioned above.
76+
#
77+
# load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
78+
# http_archive(
79+
# name = "rules_python",
80+
# sha256 = "497ca47374f48c8b067d786b512ac10a276211810f4a580178ee9b9ad139323a",
81+
# strip_prefix = "rules_python-0.16.1",
82+
# url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.16.1.tar.gz",
83+
# )
84+
85+
# Next we load the toolchain from rules_python.
4886
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
4987

88+
# We now register a hermetic Python interpreter rather than relying on a system-installed interpreter.
89+
# This toolchain will allow bazel to download a specific python version, and use that version
90+
# for compilation.
5091
python_register_toolchains(
5192
name = "python39",
5293
python_version = "3.9",
5394
)
5495

96+
# Load the interpreter and pip_parse rules.
5597
load("@python39//:defs.bzl", "interpreter")
5698
load("@rules_python//python:pip.bzl", "pip_parse")
5799

100+
# This macro wraps the `pip_repository` rule that invokes `pip`, with `incremental` set.
101+
# Accepts a locked/compiled requirements file and installs the dependencies listed within.
102+
# Those dependencies become available in a generated `requirements.bzl` file.
103+
# You can instead check this `requirements.bzl` file into your repo.
58104
pip_parse(
59105
name = "pip",
106+
# (Optional) You can provide a python_interpreter (path) or a python_interpreter_target (a Bazel target, that
107+
# acts as an executable). The latter can be anything that could be used as Python interpreter. E.g.:
108+
# 1. Python interpreter that you compile in the build file.
109+
# 2. Pre-compiled python interpreter included with http_archive.
110+
# 3. Wrapper script, like in the autodetecting python toolchain.
111+
#
112+
# Here, we use the interpreter constant that resolves to the host interpreter from the default Python toolchain.
60113
python_interpreter_target = interpreter,
114+
# Set the location of the lock file.
61115
requirements_lock = "//:requirements_lock.txt",
116+
requirements_windows = "//:requirements_windows.txt",
62117
)
63118

119+
# Load the install_deps macro.
64120
load("@pip//:requirements.bzl", "install_deps")
65121

122+
# Initialize repositories for all packages in requirements_lock.txt.
66123
install_deps()
67124

68125
# The rules_python gazelle extension has some third-party go dependencies
69126
# which we need to fetch in order to compile it.
70127
load("@rules_python//gazelle:deps.bzl", _py_gazelle_deps = "gazelle_deps")
71128

129+
# See: https://github.com/bazelbuild/rules_python/blob/main/gazelle/README.md
130+
# This rule loads and compiles various go dependencies that running gazelle
131+
# for python requirements.
72132
_py_gazelle_deps()
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
import requests
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
214

15+
from flask import Flask, jsonify
16+
from random_number_generator import generate_random_number
317

4-
def main(url):
5-
r = requests.get(url)
6-
print(r.text)
18+
app = Flask(__name__)
19+
20+
@app.route('/random-number', methods=['GET'])
21+
def get_random_number():
22+
return jsonify({'number': generate_random_number.generate_random_number()})
23+
24+
"""Start the python web server"""
25+
def main():
26+
app.run()
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from __init__ import main
216

3-
if __name__ == "__main__":
4-
main("https://example.com")
17+
if __name__ == '__main__':
18+
main()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
from __init__ import app
17+
18+
class TestServer(unittest.TestCase):
19+
def setUp(self):
20+
self.app = app.test_client()
21+
22+
def test_get_random_number(self):
23+
response = self.app.get('/random-number')
24+
self.assertEqual(response.status_code, 200)
25+
self.assertIn('number', response.json)
26+
27+
if __name__ == '__main__':
28+
unittest.main()

0 commit comments

Comments
 (0)
0