Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions generate_protos
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ do
--python_out=src/ \
--grpc_python_out=src/ \
--mypy_out=src/ \
--mypy_grpc_out=src/ \
${FILE}

# FILE_NAME=$(basename -- "$FILE")
Expand All @@ -27,6 +28,7 @@ do
--proto_path=./schema/ \
--python_out=src/ \
--mypy_out=src/ \
--mypy_grpc_out=src/ \
${FILE}

# FILE_NAME=$(basename -- "$FILE")
Expand Down
61 changes: 32 additions & 29 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
[build-system]
requires = ["setuptools >= 61.0"]
build-backend = "setuptools.build_meta"
requires = ["setuptools >= 61.0"]

[project]
name = "druncschema"
authors = [
{name = "Pierre Lasorak", email = "p.lasorak@imperial.ac.uk"},
{name = "Pawel Plesniak", email = "pawel.plesniak15@imperial.ac.uk"},
{name = "Claudia Su", email = "claudia.su@physics.ox.ac.uk"},
{name = "Emir Muhammad", email = "e.muhammad@imperial.ac.uk"},
{name = "James Turner", email = "james.turner@imperial.ac.uk"},
{name = "Aruash Karimi", email = "a.karimi@imperial.ac.uk"},
{name = "Miruna Serian", email = "m.serian@imperial.ac.uk"},
]
dependencies = [
"click",
"grpcio",
"grpcio-status",
"grpcio-tools",
"googleapis-common-protos",
"rich",
]
description = "Schema definitions and compilation script for drunc"
version = "1.1.0"
name = "druncschema"
readme = "docs/README.md"
requires-python = ">=3.10"
dependencies = [
"click",
"grpcio",
"grpcio-status",
"grpcio-tools",
"googleapis-common-protos",
"rich",
]
authors = [
{name = "Pierre Lasorak", email = "p.lasorak@imperial.ac.uk"},
{name = "Pawel Plesniak", email = "pawel.plesniak15@imperial.ac.uk"},
{name = "Claudia Su", email = "claudia.su@physics.ox.ac.uk"}
]
version = "1.1.0"

[project.optional-dependencies]
dev = ["ruff", "pre-commit", "pytest", "pytest-cov", "mypy-protobuf", "types-protobuf"]
Expand All @@ -36,25 +40,24 @@ where = ["src"]
addopts = "-v --tb=short --cov=druncschema --cov=src/druncschema tests/"

[tool.coverage.run]
source = ["druncschema"]
omit = ["tests/*", "scripts/*"]

source = ["druncschema"]

[tool.ruff]
exclude = [
"src/druncschema.egg-info",
"src/druncschema/*_pb2.py*",
"src/druncschema/*_pb2_grpc.py",
"src/druncschema.egg-info",
"src/druncschema/*_pb2.py*",
"src/druncschema/*_pb2_grpc.py*",
]

[tool.ruff.lint]
pydocstyle.convention = "google"
select = [
"D", # pydocstyle
"E", # pycodestyle
"F", # Pyflakes
"I", # isort
"UP", # pyupgrade
"RUF", # ruff-specific rules
"R", # refactoring suggestions
"D", # pydocstyle
"E", # pycodestyle
"F", # Pyflakes
"I", # isort
"UP", # pyupgrade
"RUF", # ruff-specific rules
"R", # refactoring suggestions
]
pydocstyle.convention = "google"
148 changes: 81 additions & 67 deletions scripts/generate_protos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

"""Defines command to compile *.proto files.

Compiles buffers into
Compiles buffers into
- *pb2.py.
- *pb2.pyi.
- pb2_grpc.py.
- *pb2_grpc.py.
- *pb2_grpc.pyi.
"""

import importlib
import logging
import os
import subprocess
from importlib.resources import files
from pathlib import Path

import click
Expand All @@ -32,17 +32,21 @@
width = os.get_terminal_size()[0]
except OSError:
width = 150

log.addHandler(RichHandler(
console=Console(width=width),
omit_repeated_times=False,
markup=True,
rich_tracebacks=True,
show_path=False,
tracebacks_width=width,
))

compiled_extensions = ["_pb2.py", "_pb2.pyi", "_pb2_grpc.py"]

log.addHandler(
RichHandler(
console=Console(width=width),
omit_repeated_times=False,
markup=True,
rich_tracebacks=True,
show_path=False,
tracebacks_width=width,
)
)

compiled_extensions = ["_pb2.py", "_pb2.pyi", "_pb2_grpc.py", "_pb2_grpc.pyi"]


def in_dev_mode():
"""Check if in dev mode.

Expand All @@ -53,43 +57,49 @@ def in_dev_mode():
return True
return False


def generate_protos(
source_path: Path,
druncschema_root: Path,
proto_files: list[Path],
output_dir: Path,
subdir: Path) -> None:
source_path: Path,
druncschema_root: Path,
proto_files: list[Path],
output_dir: Path,
subdir: Path,
) -> None:
"""Compiles the protobuf messages."""
for proto_file in proto_files:
log.info(f"Processing file {proto_file!s} to output dir {output_dir!s}")
if not str(proto_file).endswith(".proto"):
raise Exception(f"File names must end in a '.proto', received {proto_file}")
try:
cmd = " ".join([
f"source {source_path}; cd {druncschema_root}; "
"python -m grpc_tools.protoc",
"-I'./schema'",
f"--python_out={output_dir!s}",
f"--grpc_python_out={output_dir!s}",
f"--mypy_out={output_dir!s}",
str(proto_file),
])
cmd = " ".join(
[
f"source {source_path}; cd {druncschema_root}; "
"python -m grpc_tools.protoc",
"-I'./schema'",
f"--python_out={output_dir!s}",
f"--grpc_python_out={output_dir!s}",
f"--mypy_out={output_dir!s}",
f"--mypy_grpc_out={output_dir!s}",
str(proto_file),
]
)
log.debug(cmd)
subprocess.run(
cmd,
capture_output=True,
text=True,
check=True,
shell=True,
executable="/bin/bash"
executable="/bin/bash",
)
except subprocess.CalledProcessError as e:
log.exception(e)
log.error(e.stderr)
output_files = [
output_dir / Path("druncschema") / subdir / Path(
Path(proto_file.name).stem + extension
)
output_dir
/ Path("druncschema")
/ subdir
/ Path(Path(proto_file.name).stem + extension)
for extension in compiled_extensions
]
for output_file in output_files:
Expand All @@ -100,24 +110,27 @@ def generate_protos(
else:
log.debug(f"Generated {output_file}")


def clear_previous_compiled_schema(output_dir: Path, output_files: list[Path]) -> None:
"""Delete previous results of schema compilation."""
for output_file in output_files:
for compiled_extension in compiled_extensions:
existing_file = output_dir / output_file.with_name(
output_file.stem + compiled_extension)
output_file.stem + compiled_extension
)
if existing_file.exists():
log.info(f"Deleting file {existing_file}")
existing_file.unlink()


def call_generate_protos(
source_path: Path,
druncschema_root: Path,
output_dir: Path,
subdir: Path,
clean: bool,
do_not_compile: bool
) -> None:
source_path: Path,
druncschema_root: Path,
output_dir: Path,
subdir: Path,
clean: bool,
do_not_compile: bool,
) -> None:
"""Clear existing compiled buffers, compile new buffers.

List *.proto files.
Expand All @@ -126,15 +139,14 @@ def call_generate_protos(
"""
proto_relative_path = Path("schema/druncschema")
proto_files = [
proto_relative_path / subdir / Path(f.name)
proto_relative_path / subdir / Path(f.name)
for f in (druncschema_root / proto_relative_path / subdir).glob("*.proto")
]
if not proto_files:
return
if clean:
clear_previous_compiled_schema(
output_dir / Path("druncschema") / subdir,
proto_files
output_dir / Path("druncschema") / subdir, proto_files
)
if get_files(output_dir):
e = Exception("Not all files removed, exiting")
Expand All @@ -144,17 +156,28 @@ def call_generate_protos(
generate_protos(source_path, druncschema_root, proto_files, output_dir, subdir)
return

def get_subdirs(path: Path) ->list[str]:

def get_subdirs(path: Path) -> list[str]:
"""Generate list of relevant directories."""
return [Path(p.name) for p in path.iterdir()
if p.is_dir() and not str(p.name).endswith("__")
and not str(p.name).endswith("apps")]
return [
Path(p.name)
for p in path.iterdir()
if p.is_dir()
and not str(p.name).endswith("__")
and not str(p.name).endswith("apps")
]


def get_files(path: Path) ->list[Path]:
def get_files(path: Path) -> list[Path]:
"""Generate list of *.proto files."""
return [Path(p.name) for p in path.iterdir()
if p.is_file() and not str(p.name).endswith("__.py")
and not str(p.name).endswith("typed")]
return [
Path(p.name)
for p in path.iterdir()
if p.is_file()
and not str(p.name).endswith("__.py")
and not str(p.name).endswith("typed")
]


@click.command()
@click.option(
Expand All @@ -168,7 +191,7 @@ def get_files(path: Path) ->list[Path]:
"-c",
"--clean",
is_flag=True,
help="Explicitly deletes the existing compiled schemas " \
help="Explicitly deletes the existing compiled schemas "
"before starting the compile the new ones",
)
@click.option(
Expand All @@ -181,13 +204,13 @@ def main(
log_level: str,
clean: bool,
do_not_compile: bool,
) -> None:
) -> None:
"""Compile the protobuf message schema into the relevant python code."""
log.setLevel(log_level)

if not in_dev_mode():
e = Exception(
"This command is only available in developer mode." \
"This command is only available in developer mode."
"See the druncschema wiki for further clarification."
)
log.exception(e)
Expand All @@ -198,7 +221,7 @@ def main(
)
log.exception(e)

druncschema_root = Path(f'{os.environ["DBT_AREA_ROOT"]}/sourcecode/druncschema')
druncschema_root = Path(f"{os.environ['DBT_AREA_ROOT']}/sourcecode/druncschema")
log.debug(f"Found druncschema directory at {druncschema_root}")

output_dir = druncschema_root / "src/"
Expand All @@ -209,23 +232,14 @@ def main(

subdir = Path()
call_generate_protos(
source_path,
druncschema_root,
output_dir,
subdir,
clean,
do_not_compile
source_path, druncschema_root, output_dir, subdir, clean, do_not_compile
)

subdirs = get_subdirs(druncschema_root / Path("schema/druncschema"))
for subdir in subdirs:
call_generate_protos(
source_path,
druncschema_root,
output_dir,
subdir,
clean,
do_not_compile
source_path, druncschema_root, output_dir, subdir, clean, do_not_compile
)


main()
21 changes: 21 additions & 0 deletions src/druncschema/authoriser_pb2_grpc.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""@generated by mypy-protobuf. Do not edit manually!
isort:skip_file
"""

import abc as _abc_1
import typing as _typing
from collections import abc as _abc

import grpc as _grpc
from grpc import aio as _aio

_T = _typing.TypeVar("_T")

class _MaybeAsyncIterator(
_abc.AsyncIterator[_T], _abc.Iterator[_T], metaclass=_abc_1.ABCMeta
): ...
class _ServicerContext(_grpc.ServicerContext, _aio.ServicerContext): # type: ignore[misc, type-arg]
...

GRPC_GENERATED_VERSION: str
GRPC_VERSION: str
Loading