Skip to content

Commit

Permalink
add check for template/*j2
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Jan 20, 2026
1 parent 9610bb7 commit 15e0f4b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ The target directory name (`created_group`) is constructed as follows:

### 2. File Processing Logic

If your `TEMPLATE` directory only contains a `README.md` and no `.j2` files starting with `GROUP`, the script will now safely exit before creating any new folders.

The script iterates through every file in the `TEMPLATE/` directory:

* **Jinja2 Templates (`.j2`)**: Any file ending in `.j2` is rendered using the Jinja2 engine. The literal string `GROUP` in the filename is replaced by the `created_group` name.
Expand Down Expand Up @@ -131,6 +133,11 @@ To see your local timezone name or offset in your generated files, you can use:

## CHANGELOG

#### v1.0.9

* Added a validation check to ensure at least one `GROUP*.j2` file exists in the `TEMPLATE/` directory before starting.
* Added error message: `"group {name} not configured for script generation"`.

#### v1.0.8

* Updated `created_time` to default to the system's local timezone (timezone-aware) instead of UTC.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import shutil
import sys
import os
import glob
from pathlib import Path
from datetime import datetime
from jinja2 import Environment, FileSystemLoader

# Version History:
# 1.0.0 - 1.0.7: Core logic, naming, force flag, UTC metadata
# 1.0.8: Switched created_time to system local timezone-aware object
__version__ = "1.0.8"
# 1.0.0 - 1.0.8: Core logic, naming, force, metadata, local timezone
# 1.0.9: Added pre-flight check for GROUP*.j2 template files
__version__ = "1.0.9"

def create_sc_group():
parser = argparse.ArgumentParser(description="Automate file creation from templates.")
Expand All @@ -23,9 +24,8 @@ def create_sc_group():

args = parser.parse_args()

# 1. Setup metadata (Local Timezone Aware)
# 1. Setup metadata
script_name = os.path.basename(__file__)
# .astimezone() with no args defaults to the system local timezone
now = datetime.now().astimezone()

# 2. Determine names and force to lowercase
Expand All @@ -41,19 +41,25 @@ def create_sc_group():
print(f"--- {script_name} v{__version__} ---")
print(f"Targeting Group: {created_group}\n")

# 4. Check for existing directory
# 4. Pre-flight Checks
template_dir = Path("TEMPLATE")
if not template_dir.exists():
print(f"Error: {template_dir} directory not found.")
sys.exit(1)

# Check for at least one GROUP*.j2 file
has_template = any(template_dir.glob("GROUP*.j2"))
if not has_template:
print(f"Error: group {created_group} not configured for script generation")
sys.exit(1)

# 5. Check for existing target 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)

# 5. Setup template path
template_dir = Path("TEMPLATE")
if not template_dir.exists():
print(f"Error: {template_dir} directory not found.")
sys.exit(1)

target_dir.mkdir(parents=True, exist_ok=True)

# 6. Initialize Jinja2 Environment
Expand All @@ -77,7 +83,7 @@ def create_sc_group():
continue

if item.suffix == ".j2":
# Replace 'GROUP' with created_group and strip '.j2'
# Logic: GROUP.extension.j2 -> {created_group}.extension
new_filename = item.name.replace("GROUP", created_group).replace(".j2", "")
target_path = target_dir / new_filename

Expand All @@ -88,7 +94,6 @@ def create_sc_group():
print(f" [Rendered] {item.name} -> {new_filename}")

else:
# Copy non-template files directly
shutil.copy2(item, target_dir / item.name)
print(f" [Copied] {item.name}")

Expand Down

0 comments on commit 15e0f4b

Please sign in to comment.