diff --git a/bin/setup b/bin/setup index 2c7ceb3a4..87defd628 100755 --- a/bin/setup +++ b/bin/setup @@ -24,7 +24,7 @@ fi TOP_DIR=$(git rev-parse --show-toplevel) PNPM_VERSION="10.2.0" -NODE_VERSION="23" +NODE_VERSION="22" NVM_VERSION="0.40.1" AUDIENCE="eng" diff --git a/py/bin/generate_schema_types b/py/bin/generate_schema_types index 1e770e8d3..670de032a 100755 --- a/py/bin/generate_schema_types +++ b/py/bin/generate_schema_types @@ -17,17 +17,6 @@ uv run --directory "${TOP_DIR}/py" datamodel-codegen # Sanitize the generated schema. python3 "${TOP_DIR}/py/bin/sanitize_schemas.py" "${SCHEMA_FILE}" -# Add a generated by `generate_schema_types` comment. -sed -i '' '1i\ -# DO NOT EDIT: Generated by `generate_schema_types` from `genkit-schemas.json`. -' "${SCHEMA_FILE}" - -# Add license header. -addlicense \ - -c "Google LLC" \ - -s=only \ - "${SCHEMA_FILE}" - # Checks and formatting. uv run --directory "${TOP_DIR}/py" \ ruff format "${TOP_DIR}" diff --git a/py/bin/sanitize_schemas.py b/py/bin/sanitize_schemas.py index 94c7aaa00..a270f3ecf 100644 --- a/py/bin/sanitize_schemas.py +++ b/py/bin/sanitize_schemas.py @@ -5,6 +5,8 @@ import ast import sys +from datetime import datetime +from pathlib import Path class ModelConfigRemover(ast.NodeTransformer): @@ -47,26 +49,48 @@ def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef: # noqa: N802 return node +def add_header(content: str) -> str: + """Add the generated header to the content.""" + header = """# Copyright {year} Google LLC +# SPDX-License-Identifier: Apache-2.0 + +# DO NOT EDIT: Generated by `generate_schema_types` from `genkit-schemas.json`. + +from __future__ import annotations +""" + return header.format(year=datetime.now().year) + content + + def process_file(filename: str) -> None: - """Process a Python file to remove model_config from RootModel classes.""" - with open(filename) as f: - source = f.read() + """Process a Python file to remove model_config from RootModel classes + and add the generated header.""" + path = Path(filename) + if not path.is_file(): + raise FileNotFoundError(f'{filename} does not exist or is not a file') - tree = ast.parse(source) + # Read the file + content = path.read_text(encoding='utf-8') + # Then process the AST + tree = ast.parse(content) transformer = ModelConfigRemover() modified_tree = transformer.visit(tree) if transformer.modified: + # Write back the modified content. ast.fix_missing_locations(modified_tree) modified_source = ast.unparse(modified_tree) - with open(filename, 'w') as f: - f.write(modified_source) + src = add_header(modified_source) + print(src) + # print('# foo\n' + src) + path.write_text(src, encoding='utf-8') print( f'Modified {filename}: Removed model_config from RootModel classes' ) else: - print(f'No modifications needed in {filename}') + # Even if no AST modifications, still write back to add the header. + path.write_text(add_header(content), encoding='utf-8') + print(f'Added header to {filename}') def main() -> None: diff --git a/py/packages/genkit/src/genkit/core/schemas.py b/py/packages/genkit/src/genkit/core/schemas.py index bf7768aa3..e823fe2cc 100644 --- a/py/packages/genkit/src/genkit/core/schemas.py +++ b/py/packages/genkit/src/genkit/core/schemas.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 # DO NOT EDIT: Generated by `generate_schema_types` from `genkit-schemas.json`. + from __future__ import annotations from enum import Enum