Skip to content

Commit

Permalink
[#118] Add example with python and bazel toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
mengdaming committed May 25, 2023
1 parent 2d40c33 commit 4d9f408
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/python-bazel/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test --test_output=errors

# Do NOT implicitly create empty __init__.py files in the runfiles tree.
# By default, these are created in every directory containing Python source code
# or shared libraries, and every parent directory of those directories,
# excluding the repo root directory. With this flag set, we are responsible for
# creating (possibly empty) __init__.py files and adding them to the srcs of
# Python targets as required.
build --incompatible_default_to_explicit_init_py
1 change: 1 addition & 0 deletions examples/python-bazel/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.2.0
3 changes: 3 additions & 0 deletions examples/python-bazel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bazel-*/
pytest/
.pytest_cache/
9 changes: 9 additions & 0 deletions examples/python-bazel/.tcr/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
config:
git:
auto-push: false
polling-period: 2s
mob-timer:
duration: 5m0s
tcr:
language: python
toolchain: bazel
9 changes: 9 additions & 0 deletions examples/python-bazel/.tcr/language/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
toolchains:
default: bazel
compatible-with: [ bazel ]
source-files:
directories: [ src ]
patterns: [ '(?i)^.*\.py$' ]
test-files:
directories: [ tests ]
patterns: [ '(?i)^.*\.py$' ]
11 changes: 11 additions & 0 deletions examples/python-bazel/.tcr/toolchain/bazel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
build:
- os: [ darwin, linux, windows ]
arch: [ "386", amd64, arm64 ]
command: bazel
arguments: [ build, "..." ]
test:
- os: [ darwin, linux, windows ]
arch: [ "386", amd64, arm64 ]
command: bazel
arguments: [ test, "..." ]
test-result-dir: bazel-testlogs/src
18 changes: 18 additions & 0 deletions examples/python-bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test")

py_library(
name = "hello_world",
srcs = glob(["src/**/*.py"]),
visibility = ["//visibility:public"],
)

py_pytest_test(
name = "hello_world_test",
size = "small",
srcs = glob(["tests/*.py"]),
imports = ["src"],
deps = [
":hello_world",
],
)
50 changes: 50 additions & 0 deletions examples/python-bazel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Using TCR with Python and Bazel

## Prerequisites

- macOS, Linux or Windows
- [git](https://git-scm.com/) client
- [curl](https://curl.se/download.html) command line utility
- [python](https://www.python.org/downloads/)
- [Bazel](https://bazel.build/) (previously installed and available from the command line)

## Instructions

### 1 - Open a terminal

> ***Note to Windows users***
>
> Use a **git bash** terminal for running the commands below.
> _Windows CMD and PowerShell are not supported_
### 2 - Launch TCR

> ***Reminder***: the command below should be run from
> [examples/python-bazel](.)
> directory
From the built-in terminal:

```shell
./tcrw
```

### Cheat Sheet

Here are the main shortcuts available once TCR utility is running:

| Shortcut | Description |
|-----------|-----------------------------------------------|
| `d` / `D` | Enter driver role (from main menu) |
| `n` / `N` | Enter navigator role (from main menu) |
| `p` / `P` | Toggle on/off git auto-push (from main menu) |
| `l` / `L` | Pull from remote (from main menu) |
| `s` / `S` | Push to remote (from main menu) |
| `q` / `Q` | Quit current role - Quit TCR (from main menu) |
| `t` / `T` | Query timer status (from driver role only) |
| `?` | List available options |

### Additional Details

Refer to [tcr.md](../../doc/tcr.md) page for additional details and explanations about TCR
available subcommands and options
24 changes: 24 additions & 0 deletions examples/python-bazel/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
workspace(name = "hello_world_python")

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

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

http_archive(
name = "rules_python_pytest",
sha256 = "334a0ac91a0d6a87df499cdf9b70b525754dc8ca4873763116d67177f759389f",
strip_prefix = "rules_python_pytest-1.0.2",
url = "https://github.com/caseyduquettesc/rules_python_pytest/archive/v1.0.2.tar.gz",
)

load("@rules_python//python:repositories.bzl", "py_repositories")
load("@rules_python_pytest//python_pytest:repositories.bzl", "rules_python_pytest_dependencies")

rules_python_pytest_dependencies()

py_repositories()
9 changes: 9 additions & 0 deletions examples/python-bazel/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
addopts = "-vvs --junitxml=\"pytest/result.xml\""
testpaths = [
"tests"
]
2 changes: 2 additions & 0 deletions examples/python-bazel/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest == 7.0.1; python_version < '3.7'
pytest == 7.3.1; python_version >= '3.7'
Empty file.
2 changes: 2 additions & 0 deletions examples/python-bazel/src/hello_world/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def say_hello(name: str) -> str:
return f'Hello {name}!'
4 changes: 4 additions & 0 deletions examples/python-bazel/tcrw
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

base_dir="$(cd "$(dirname -- "$0")" && pwd)"
. "${base_dir}/../tcr/tcr.sh"
9 changes: 9 additions & 0 deletions examples/python-bazel/tests/hello_world_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

from hello_world.hello_world import say_hello


class TestHelloWorld:

def test_say_hello(self) -> None:
assert ('Hello Sue!' == say_hello('Sue'))

0 comments on commit 4d9f408

Please sign in to comment.