Skip to content

Commit

Permalink
script rendering templates IN PROGRESS
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pczajka committed Jul 9, 2024
1 parent d54e2f2 commit 8796464
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/render_all_templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ jobs:
python-version: '3.12'
- name: Install Snowflake CLI
run: python -m pip install git+https://github.com/snowflakedb/snowflake-cli.git@932adb508f2be468da234fa0423244a27de99662

- name: Run help
run: snow init -h
- name: Render all templates
run: python .github/workflows/scripts/render_all_templates.py .
71 changes: 71 additions & 0 deletions .github/workflows/scripts/render_all_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import subprocess
import sys
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import List

EXCLUDE_PATHS = [".git", ".github"]


def _yield_template_yml_files(path: Path):
if path.name in EXCLUDE_PATHS:
return

if path.name == "template.yml":
yield path
if path.is_dir():
for child in path.iterdir():
yield from _yield_template_yml_files(child)


def _failed_render_error(template_root: Path) -> None:
print(f"Error while rendering template {template_root}")
sys.exit(1)


def _render_template(template_root: Path):
print(f"Rendering {template_root}")
with TemporaryDirectory() as tmpdir:
project_path = Path(tmpdir) / "project"

# overestimate number of variables in the template
max_number_of_variables = (
(template_root / "template.yml").read_text().count("\n")
)
# 42 can be parsed by all supported types (int/float/string)
process_input = "42\n" * max_number_of_variables

snow = subprocess.Popen(
[
"snow",
"init",
str(project_path),
"--template-source",
str(template_root),
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
# reasonable 60s timeout
try:
snow.communicate(input=process_input.encode(), timeout=60)
except subprocess.TimeoutExpired:
print("Timed out after 60s!")
_failed_render_error(template_root)
if snow.returncode:
print(f"Rendering finished with {snow.returncode}")
_failed_render_error(template_root)


if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} PATH")
sys.exit(1)

root_path = Path(sys.argv[1])
template_yml_list: List[Path] = list(_yield_template_yml_files(root_path))
for template_yml in template_yml_list:
template_root = template_yml.parent
_render_template(template_root)

print("OK")

0 comments on commit 8796464

Please sign in to comment.