Skip to content

Commit

Permalink
gen type asserts in python
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Mortari <[email protected]>
Co-authored-by: Matteo Mortari <[email protected]>
Signed-off-by: Isabella do Amaral <[email protected]>
  • Loading branch information
isinyaaa and tarilabs committed Sep 19, 2024
1 parent 56c5ddb commit e55be86
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN yum remove -y nodejs npm
RUN yum module -y reset nodejs
RUN yum module -y enable nodejs:18
# install npm and java for openapi-generator-cli
RUN yum install -y nodejs npm java-11
RUN yum install -y nodejs npm java-11 python3

# Copy the go source
COPY ["Makefile", "main.go", ".openapi-generator-ignore", "openapitools.json", "./"]
Expand Down
70 changes: 70 additions & 0 deletions scripts/gen_type_asserts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import typing as t
from pathlib import Path
from textwrap import dedent


def get_funcs(models: t.Iterable[Path]) -> t.Iterator[str]:
for path in models:
with path.open() as f:
# skip boilerplate
lines = iter(f.readlines())
while not next(lines).startswith("package"):
continue

buf = []
in_func = False
for raw in lines:
line = raw.rstrip()
if not line and not in_func:
continue
buf.append(line)
if line.startswith("func"):
in_func = True
elif line.startswith("}") and in_func:
in_func = False
yield "\n".join(buf)
buf.clear()
path.unlink()


def get_name(func: str) -> str:
# each func is declared as:
# // comment
# func funcName(args) error ...
return func.split("\n")[1].split(" ")[1].split("(")[0]


if __name__ == "__main__":
# model registry repo root
root = Path(__file__).parent.parent.resolve()
src = root / "internal/server/openapi"
print(
dedent("""
/*
* Model Registry REST API
*
* REST API for Model Registry to create and manage ML model metadata
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*
*/
// File generated by scripts/gen_type_assert.sh - DO NOT EDIT
package openapi
import (
model "github.com/kubeflow/model-registry/pkg/openapi"
)
""")
)
print(
"\n".join(
sorted(
get_funcs(path for path in src.rglob("model_*.go")),
key=lambda f: get_name(f),
)
)
)
73 changes: 1 addition & 72 deletions scripts/gen_type_asserts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,7 @@ PROJECT_ROOT=$(realpath "$(dirname "$0")"/..)
ASSERT_FILE_PATH="${PROJECT_ROOT}/internal/server/openapi/type_asserts.go"
PATCH="${PROJECT_ROOT}/patches/type_asserts.patch"

# Remove the existing file identified by env.ASSERT_FILE_PATH
if [ -f "$ASSERT_FILE_PATH" ]; then
rm "$ASSERT_FILE_PATH"
fi

# Create an empty file
touch "$ASSERT_FILE_PATH"

INITIAL_CONTENT=$(
cat <<EOF
/*
* Model Registry REST API
*
* REST API for Model Registry to create and manage ML model metadata
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*
*/
// File generated by scripts/gen_type_assert.sh - DO NOT EDIT
package openapi
import (
model "github.com/kubeflow/model-registry/pkg/openapi"
)
EOF
)

# Create the file and initialize it with the specified content
echo -e "$INITIAL_CONTENT" >"$ASSERT_FILE_PATH"

declare -A assert_functions
pattern="\/\/ (\w+) checks(.|\n)+?\n\}\n"
shell_pattern="\/\/ (\w+) checks[^\/?]+?"

# Iterate over files starting with "model_" in the internal/server/openapi/ folder
for file in "$PROJECT_ROOT"/internal/server/openapi/model_*; do
# Check if the file is a regular file
if [ -f "$file" ]; then

# grab all functions in the file
functions=$(grep -Pzo "$pattern" "${file}" | tr -d '\0')
while [[ $functions =~ $shell_pattern ]]; do

name="${BASH_REMATCH[1]}"
body="${BASH_REMATCH[0]}"

# add function to associative array
assert_functions["$name"]="$body"

# Remove the matched function to process next function
functions=${functions//"$body"/}
done

# Remove the merged file
rm "$file"
fi
done

# get sorted function names in another array
sorted_names=$(for name in "${!assert_functions[@]}"; do
echo "$name"
done | sort)

# iterate over the sorted function names array and print bodies
for name in $sorted_names; do
echo "${assert_functions[$name]}" >>"$ASSERT_FILE_PATH"
done
python3 "${PROJECT_ROOT}/scripts/gen_type_asserts.py" >"$ASSERT_FILE_PATH"

gofmt -w "$ASSERT_FILE_PATH"

Expand Down

0 comments on commit e55be86

Please sign in to comment.