From 84770287e44ae8aad3a5ac53c6389cc67c0e830c Mon Sep 17 00:00:00 2001 From: Linus Vogel Date: Thu, 23 Apr 2026 20:08:58 +0200 Subject: [PATCH] implemented recursive file search for identifying states in the current environment checked out in the state repository --- pillar_tool/git/repository.py | 2 +- pillar_tool/routers/environment.py | 20 ++++++++++++++++++++ pillar_tool/util/files.py | 22 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 pillar_tool/util/files.py diff --git a/pillar_tool/git/repository.py b/pillar_tool/git/repository.py index 8931239..25bdbdb 100644 --- a/pillar_tool/git/repository.py +++ b/pillar_tool/git/repository.py @@ -50,7 +50,6 @@ class COCallbacks(CheckoutCallbacks): print(f"why: {why}") - def checkout_remote_branch(config: Config, branch_name: str) -> None: """Checkout a remote branch from the state repository. @@ -119,3 +118,4 @@ def checkout_remote_branch(config: Config, branch_name: str) -> None: print(f"Failed to checkout branch: {exc}") raise ValueError(f"Failed to checkout branch: {exc}") + diff --git a/pillar_tool/routers/environment.py b/pillar_tool/routers/environment.py index 7ac75b6..94b8fce 100644 --- a/pillar_tool/routers/environment.py +++ b/pillar_tool/routers/environment.py @@ -12,6 +12,7 @@ from starlette.responses import JSONResponse from pillar_tool.db import Host from pillar_tool.db.models.top_data import Environment, EnvironmentAssignment +from pillar_tool.util.files import recursive_list_dir from pillar_tool.util.validation import validate_environment_name from pillar_tool.util import config, Config from pillar_tool.git.repository import checkout_remote_branch @@ -189,5 +190,24 @@ def environment_patch(req: Request, name: str) -> JSONResponse: # Attempt to check the requested branch out try: checkout_remote_branch(cfg, name) + + # TODO: read the file tree and find all sls files and init.sls files to enumerate the available states + + # Branch has been checked out + print(f"Reading states that are available in '{name}':") + print(f"{cfg.git.state_repo_path}:") + all_files = recursive_list_dir(cfg.git.state_repo_path) + sls_files = filter(lambda f: f.endswith(".sls"), all_files) + state_file_paths = map(lambda x: x.replace("/init.sls", "").replace(".sls", ""), sls_files) + state_names = map(lambda x: x.replace(f"{cfg.git.state_repo_path}/", "").replace("/", "."), state_file_paths) + + for state_name in state_names: + print(f"Checking state '{state_name}'") + + # TODO: the environment should be a field of a state in the database + # TODO: ensure that each named state exists in the current environment + + except Exception as exc: + print(f"Failed to import environment: {exc}") raise HTTPException(status_code=404, detail=str(exc)) \ No newline at end of file diff --git a/pillar_tool/util/files.py b/pillar_tool/util/files.py new file mode 100644 index 0000000..525aee8 --- /dev/null +++ b/pillar_tool/util/files.py @@ -0,0 +1,22 @@ +from os import listdir +from os.path import isfile, isdir, realpath + + +def recursive_list_dir(path) -> list[str]: + try: + # place to accumulate all the state files + file_list = [] + + # list all the files that are in the directory pointed to by path + current_contents = [ f"{path}/{x}" for x in listdir(path) if not x.endswith("/.git") or not isdir(x) ] + + for file in current_contents: + if isdir(file): + file_list.extend(recursive_list_dir(file)) + elif isfile(file): + file_list.append(file) + + return file_list + + except Exception: + raise \ No newline at end of file