Skip to content

Commit 77ed564

Browse files
authored
[Python] Add codegen (#2507)
## Changes Add the code generator from the bundle schema to the Python code. Currently, it's only enabled for the "jobs" package. There is a new GitHub action to verify that codegen is always up-to-date whenever we update bundle schema. Depends on #2508 ## Tests - A few unit tests - Primarily tested by diffing output
1 parent 7cfc682 commit 77ed564

18 files changed

+1641
-1
lines changed

.github/workflows/push.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,28 @@ jobs:
168168
for file in ./bundle/internal/schema/testdata/fail/*.yml; do
169169
ajv test -s schema.json -d $file --invalid -c=./keywords.js
170170
done
171+
172+
validate-python-codegen:
173+
needs: cleanups
174+
runs-on: ubuntu-latest
175+
176+
steps:
177+
- name: Checkout
178+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
179+
180+
- name: Install uv
181+
uses: astral-sh/setup-uv@0c5e2b8115b80b4c7c5ddf6ffdd634974642d182 # v5.4.1
182+
with:
183+
version: "0.6.5"
184+
185+
- name: Verify that python/codegen is up to date
186+
working-directory: experimental/python
187+
run: |
188+
make codegen
189+
190+
if ! ( git diff --exit-code ); then
191+
echo "Generated Python code is not up-to-date. Please run 'pushd experimental/python && make codegen' and commit the changes."
192+
193+
# TODO block PR if this fails once diffs are fixed
194+
# exit 1
195+
fi

experimental/python/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@ lint:
1616
uv run pyright
1717
uv run ruff format --diff
1818

19+
codegen:
20+
find databricks -name _models | xargs rm -rf
21+
22+
cd codegen; uv run -m pytest codegen_tests
23+
cd codegen; uv run -m codegen.main --output ..
24+
25+
uv run ruff check --fix $(sources) || true
26+
uv run ruff format
27+
1928
test:
2029
uv run python -m pytest databricks_tests --cov=databricks.bundles --cov-report html -vv
2130

2231
build:
2332
rm -rf build dist
2433
uv build .
2534

26-
.PHONY: fmt docs lint test build
35+
.PHONY: fmt docs lint codegen test build
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import TYPE_CHECKING
2+
3+
if TYPE_CHECKING:
4+
from typing_extensions import Self
5+
6+
7+
class CodeBuilder:
8+
def __init__(self):
9+
self._code = ""
10+
11+
def append(self, *args: str) -> "Self":
12+
for arg in args:
13+
self._code += arg
14+
15+
return self
16+
17+
def indent(self):
18+
return self.append(" ")
19+
20+
def newline(self) -> "Self":
21+
return self.append("\n")
22+
23+
def append_list(self, args: list[str], sep: str = ",") -> "Self":
24+
return self.append(sep.join(args))
25+
26+
def append_dict(self, args: dict[str, str], sep: str = ",") -> "Self":
27+
return self.append_list([f"{k}={v}" for k, v in args.items()], sep)
28+
29+
def append_triple_quote(self) -> "Self":
30+
return self.append('"""')
31+
32+
def append_repr(self, value) -> "Self":
33+
return self.append(repr(value))
34+
35+
def build(self):
36+
return self._code

0 commit comments

Comments
 (0)