Knock together a handy py_project macro

This commit is contained in:
Reid 'arrdem' McKenzie 2021-08-03 19:27:57 -06:00
parent 6c6b0a060f
commit 4d8cd34cbc
2 changed files with 101 additions and 66 deletions

View file

@ -6,6 +6,7 @@ load("//tools/python:defs.bzl",
"py_unittest", "py_unittest",
"py_pytest", "py_pytest",
"py_resources", "py_resources",
"py_project",
) )
load("@arrdem_source_pypi//:requirements.bzl", load("@arrdem_source_pypi//:requirements.bzl",

View file

@ -10,102 +10,102 @@ load("@rules_python//python:defs.bzl",
def py_requirement(*args, **kwargs): def py_requirement(*args, **kwargs):
"""A re-export of requirement()""" """A re-export of requirement()"""
return _py_requirement(*args, **kwargs) return _py_requirement(*args, **kwargs)
def py_test(python_version=None, **kwargs): def py_test(python_version=None, **kwargs):
"""A re-export of py_test()""" """A re-export of py_test()"""
if python_version and python_version != "PY3": if python_version and python_version != "PY3":
fail("py3k only!") fail("py3k only!")
return _py_test( return _py_test(
python_version="PY3", python_version="PY3",
**kwargs, **kwargs,
) )
def py_pytest(name, srcs, deps, main=None, python_version=None, args=None, **kwargs): def py_pytest(name, srcs, deps, main=None, python_version=None, args=None, **kwargs):
"""A py_test target which uses pytest.""" """A py_test target which uses pytest."""
if python_version and python_version != "PY3": if python_version and python_version != "PY3":
fail("py3k only!") fail("py3k only!")
f = "//tools/python:bzl_pytest_shim.py" f = "//tools/python:bzl_pytest_shim.py"
deps = [ deps = [
py_requirement("pytest"), py_requirement("pytest"),
py_requirement("jedi"), py_requirement("jedi"),
py_requirement("pytest-pudb"), py_requirement("pytest-pudb"),
] + deps ] + deps
srcs = [f] + srcs srcs = [f] + srcs
t = py_test( t = py_test(
name = name, name = name,
srcs = srcs, srcs = srcs,
main = f, main = f,
args = args, args = args,
python_version="PY3", python_version="PY3",
deps = deps, deps = deps,
**kwargs, **kwargs,
) )
# FIXME (arrdem 2020-09-27): # FIXME (arrdem 2020-09-27):
# This really needs to be a py_image_test. # This really needs to be a py_image_test.
# Not clear how to achieve that. # Not clear how to achieve that.
# py_image( # py_image(
# name = name + ".containerized", # name = name + ".containerized",
# main = f, # main = f,
# args = args, # args = args,
# srcs = srcs, # srcs = srcs,
# deps = deps, # deps = deps,
# **kwargs, # **kwargs,
# ) # )
return t return t
def py_unittest(srcs=[], **kwargs): def py_unittest(srcs=[], **kwargs):
"""A helper for running unittest tests""" """A helper for running unittest tests"""
f = "//tools/python:bzl_unittest_shim.py" f = "//tools/python:bzl_unittest_shim.py"
return py_test( return py_test(
main = f, main = f,
srcs = [f] + srcs, srcs = [f] + srcs,
**kwargs **kwargs
) )
def py_binary(python_version=None, main=None, srcs=None, **kwargs): def py_binary(python_version=None, main=None, srcs=None, **kwargs):
"""A re-export of py_binary()""" """A re-export of py_binary()"""
if python_version and python_version != "PY3": if python_version and python_version != "PY3":
fail("py3k only!") fail("py3k only!")
srcs = srcs or [] srcs = srcs or []
if main not in srcs: if main not in srcs:
srcs = [main] + srcs srcs = [main] + srcs
return _py_binary( return _py_binary(
python_version = "PY3", python_version = "PY3",
main = main, main = main,
srcs = srcs, srcs = srcs,
**kwargs, **kwargs,
) )
def py_library(srcs_version=None, **kwargs): def py_library(srcs_version=None, **kwargs):
"""A re-export of py_library()""" """A re-export of py_library()"""
if srcs_version and srcs_version != "PY3": if srcs_version and srcs_version != "PY3":
fail("py3k only!") fail("py3k only!")
return _py_library( return _py_library(
srcs_version="PY3", srcs_version="PY3",
**kwargs **kwargs
) )
ResourceGroupInfo = provider( ResourceGroupInfo = provider(
@ -144,3 +144,37 @@ py_resources = rule(
), ),
}, },
) )
def py_project(name=None,
lib_srcs=None,
lib_deps=None,
test_srcs=None,
test_deps=None):
"""
A helper for defining conventionally-formatted python project.
Assumes that there's a src/python tree, and a src/test tree.
Each test_*.py source generates its own implicit test target. This allows
for automatic test parallelism.
"""
lib_srcs = lib_srcs or native.glob(["src/python/**/*.py"])
test_srcs = test_srcs or native.glob(["test/python/**/*.py"])
py_library(
name=name,
srcs=lib_srcs,
deps=lib_deps,
imports=["src/python"],
)
for src in test_srcs:
if "test_" in src:
py_pytest(
name=name + ".test." + str(hash(src)).replace("-", "") + "." + src.split("/")[-1],
srcs=test_srcs,
deps=[name] + test_deps,
imports=["test/python"],
)