From d8432ddffb7b29585aa23a886dec662f130f91bf Mon Sep 17 00:00:00 2001 From: badra001 Date: Fri, 16 Jan 2026 10:23:44 -0500 Subject: [PATCH] check for dir, dont create unless force --- .../sso-tools/sso-create-sc-group.py | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/local-app/python-tools/sso-tools/sso-create-sc-group.py b/local-app/python-tools/sso-tools/sso-create-sc-group.py index d1426dad..7d9cc202 100755 --- a/local-app/python-tools/sso-tools/sso-create-sc-group.py +++ b/local-app/python-tools/sso-tools/sso-create-sc-group.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import argparse import shutil +import sys from pathlib import Path from jinja2 import Environment, FileSystemLoader @@ -8,41 +9,47 @@ # 1.0.0: Initial release # 1.0.1: Added shebang, exposed variables to Jinja2 # 1.0.2: Enforced lowercase for group and created_group names -__version__ = "1.0.2" +# 1.0.3: Added directory existence check and --force flag +__version__ = "1.0.3" def create_sc_group(): parser = argparse.ArgumentParser(description="Automate file creation from templates.") parser.add_argument("-b", "--business-label", required=True, help="Required business label") parser.add_argument("-a", "--application-label", help="Optional application label") parser.add_argument("-g", "--group", help="Group name (defaults to current directory name)") + parser.add_argument("-f", "--force", action="store_true", help="Overwrite existing directory if it exists") parser.add_argument("-v", "--version", action="version", version=f"%(prog)s {__version__}") args = parser.parse_args() - # 1. Determine the 'group' name and force to lowercase + # 1. Determine names and force to lowercase raw_group = args.group if args.group else Path.cwd().name group_name = raw_group.lower() - # 2. Define the 'created_group' string and force to lowercase if args.application_label: created_group = f"{args.business_label}-{args.application_label}-{group_name}".lower() else: created_group = f"{args.business_label}-{group_name}".lower() - # 3. Setup paths - template_dir = Path("TEMPLATE") + # 2. Check for existing directory target_dir = Path(created_group) + if target_dir.exists() and not args.force: + print(f"Error: Directory '{created_group}' already exists.") + print("Use --force or -f to recreate the files.") + sys.exit(1) + # 3. Setup template path + template_dir = Path("TEMPLATE") if not template_dir.exists(): print(f"Error: {template_dir} directory not found.") - return + sys.exit(1) target_dir.mkdir(parents=True, exist_ok=True) # 4. Initialize Jinja2 Environment env = Environment(loader=FileSystemLoader(str(template_dir))) - # 5. Expose variables to Jinja2 templates (all relevant strings are now lowercase) + # 5. Context variables render_vars = { "business_label": args.business_label, "application_label": args.application_label, @@ -50,20 +57,17 @@ def create_sc_group(): "created_group": created_group } - print(f"Creating files for: {created_group}") + print(f"{'Recreating' if args.force and target_dir.exists() else 'Creating'} files for: {created_group}") - # 6. Process files in TEMPLATE/ + # 6. Process files for item in template_dir.iterdir(): if item.is_dir(): - continue # Skips subdirectories + continue if item.suffix == ".j2": - # Rule: Replace 'GROUP' with created_group and strip '.j2' - # Note: We replace the literal string 'GROUP' in the filename new_filename = item.name.replace("GROUP", created_group).replace(".j2", "") target_path = target_dir / new_filename - # Render the template with variables template = env.get_template(item.name) output_content = template.render(render_vars) @@ -71,7 +75,6 @@ def create_sc_group(): print(f" [Rendered] {item.name} -> {new_filename}") else: - # Rule: Copy files not ending in .j2 directly shutil.copy2(item, target_dir / item.name) print(f" [Copied] {item.name}") @@ -79,4 +82,3 @@ def create_sc_group(): if __name__ == "__main__": create_sc_group() -