Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b13e4cd

Browse filesBrowse files
committed
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.
1 parent 4528038 commit b13e4cd
Copy full SHA for b13e4cd

File tree

Expand file treeCollapse file tree

13 files changed

+414
-155
lines changed
Filter options
Expand file treeCollapse file tree

13 files changed

+414
-155
lines changed

‎.bazelrc

Copy file name to clipboardExpand all lines: .bazelrc
+2-2Lines 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

Copy file name to clipboardExpand all lines: examples/build_file_generation/BUILD
+37-4Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
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(
@@ -13,8 +17,10 @@ compile_pip_requirements(
1317
requirements_txt = "requirements_lock.txt",
1418
)
1519

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.
20+
# This repository rule fetches the metadata for python packages we
21+
# depend on. That data is required for the gazelle_python_manifest
22+
# rule to update our manifest file.
23+
# To see what this rule does, try `bazel run @modules_map//:print`
1824
modules_mapping(
1925
name = "modules_map",
2026
exclude_patterns = [
@@ -52,17 +58,44 @@ gazelle(
5258

5359
# This rule is auto-generated and managed by Gazelle,
5460
# because it found the __init__.py file in this folder.
61+
# See: https://bazel.build/reference/be/python#py_library
5562
py_library(
5663
name = "build_file_generation",
5764
srcs = ["__init__.py"],
5865
visibility = ["//:__subpackages__"],
59-
deps = ["@pip_requests//:pkg"],
66+
deps = [
67+
"//random_number_generator",
68+
"@pip_flask//:pkg",
69+
],
6070
)
6171

72+
# A py_binary is an executable Python program consisting of a collection of .py source files.
73+
# See: https://bazel.build/reference/be/python#py_binary
74+
#
75+
# This rule is auto-generated and managed by Gazelle,
76+
# because it found the __main__.py file in this folder.
77+
# This rule creates a target named //:build_file_generation_bin and you can use
78+
# bazel to run the target:
79+
# `bazel run //:build_file_generation_bin`
6280
py_binary(
6381
name = "build_file_generation_bin",
6482
srcs = ["__main__.py"],
6583
main = "__main__.py",
6684
visibility = ["//:__subpackages__"],
6785
deps = [":build_file_generation"],
6886
)
87+
88+
# A py_test is a Python unit test.
89+
# See: https://bazel.build/reference/be/python#py_test
90+
#
91+
# This rule is auto-generated and managed by Gazelle,
92+
# because it found the __test__.py file in this folder.
93+
# This rule creates a target named //:build_file_generation_test and you can use
94+
# bazel to run the target:
95+
# `bazel test //:build_file_generation_test`
96+
py_test(
97+
name = "build_file_generation_test",
98+
srcs = ["__test__.py"],
99+
main = "__test__.py",
100+
deps = [":build_file_generation"],
101+
)
+64-5Lines changed: 64 additions & 5 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,6 +26,7 @@ http_archive(
1926
],
2027
)
2128

29+
# Download the bazel_gazelle ruleset.
2230
http_archive(
2331
name = "bazel_gazelle",
2432
sha256 = "efbbba6ac1a4fd342d5122cbdfdb82aeb2cf2862e35022c752eaddffada7c3f3",
@@ -28,45 +36,96 @@ http_archive(
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

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
3651
go_register_toolchains(version = "1.18.3")
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",
62116
)
63117

118+
# Load the install_deps macro.
64119
load("@pip//:requirements.bzl", "install_deps")
65120

121+
# Initialize repositories for all packages in requirements_lock.txt.
66122
install_deps()
67123

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

128+
# See: https://github.com/bazelbuild/rules_python/blob/main/gazelle/README.md
129+
# This rule loads and compiles various go dependencies that running gazelle
130+
# for python requirements.
72131
_py_gazelle_deps()
+24-4Lines 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()
+16-2Lines 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()
+28Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.