Fixed progressbar and init logic/command seperation

Progress bar was just shown at the end rather then running as steps...
also removed logic from the command file to clean it up.
This commit is contained in:
TheAuditorTool
2025-09-08 00:24:32 +07:00
parent 5ef25486a6
commit 83e7dbd537
2 changed files with 43 additions and 68 deletions

View File

@@ -20,79 +20,19 @@ def init(offline, skip_docs, skip_deps):
click.echo(" 4. Fetch documentation") click.echo(" 4. Fetch documentation")
click.echo("\n" + "="*60 + "\n") click.echo("\n" + "="*60 + "\n")
# Call the refactored initialization logic # Call the refactored initialization logic with progress callback
result = initialize_project( result = initialize_project(
offline=offline, offline=offline,
skip_docs=skip_docs, skip_docs=skip_docs,
skip_deps=skip_deps skip_deps=skip_deps,
progress_callback=click.echo
) )
stats = result["stats"] stats = result["stats"]
has_failures = result["has_failures"] has_failures = result["has_failures"]
next_steps = result["next_steps"] next_steps = result["next_steps"]
# Display step-by-step results # Results have already been displayed via progress callback
click.echo("[INDEX] Step 1/5: Indexing repository...")
if stats.get("index", {}).get("success"):
click.echo(f" [OK] Indexed {stats['index']['text_files']} text files")
else:
click.echo(f" [FAIL] Failed: {stats['index'].get('error', 'Unknown error')}", err=True)
click.echo("\n[TARGET] Step 2/5: Creating workset...")
if stats.get("workset", {}).get("success"):
click.echo(f" [OK] Workset created with {stats['workset']['files']} files")
elif stats.get("workset", {}).get("files") == 0:
click.echo(" [WARN] No files found to create workset")
else:
click.echo(f" [FAIL] Failed: {stats['workset'].get('error', 'Unknown error')}", err=True)
if not skip_deps and not offline:
click.echo("\n[PACKAGE] Step 3/4: Checking dependencies...")
if stats.get("deps", {}).get("success"):
if stats["deps"]["total"] > 0:
click.echo(f" [OK] Found {stats['deps']['total']} dependencies ({stats['deps']['outdated']} outdated)")
else:
click.echo(" [OK] No dependency files found")
else:
click.echo(f" [FAIL] Failed: {stats['deps'].get('error', 'Unknown error')}", err=True)
else:
click.echo("\n[PACKAGE] Step 3/4: Skipping dependency check (offline/skipped)")
if not skip_docs and not offline:
click.echo("\n[DOCS] Step 4/4: Fetching documentation...")
if stats.get("docs", {}).get("success"):
fetched = stats['docs'].get('fetched', 0)
cached = stats['docs'].get('cached', 0)
if fetched > 0 and cached > 0:
click.echo(f" [OK] Fetched {fetched} new docs, using {cached} cached docs")
elif fetched > 0:
click.echo(f" [OK] Fetched {fetched} docs")
elif cached > 0:
click.echo(f" [OK] Using {cached} cached docs (already up-to-date)")
else:
click.echo(" [WARN] No docs fetched or cached")
# Report any errors from the stats
if stats['docs'].get('errors'):
errors = stats['docs']['errors']
rate_limited = [e for e in errors if "rate limited" in e.lower()]
other_errors = [e for e in errors if "rate limited" not in e.lower()]
if rate_limited:
click.echo(f" [WARN] {len(rate_limited)} packages rate-limited (will retry with delay)")
if other_errors and len(other_errors) <= 3:
for err in other_errors[:3]:
click.echo(f" [WARN] {err}")
elif other_errors:
click.echo(f" [WARN] {len(other_errors)} packages failed to fetch")
click.echo(f" [OK] Created {stats['docs']['capsules']} doc capsules")
elif stats["docs"].get("error") == "Interrupted by user":
click.echo("\n [WARN] Documentation fetch interrupted (Ctrl+C)")
else:
click.echo(f" [FAIL] Failed: {stats['docs'].get('error', 'Unknown error')}", err=True)
else:
click.echo("\n[DOCS] Step 4/4: Skipping documentation (offline/skipped)")
# Summary # Summary
click.echo("\n" + "="*60) click.echo("\n" + "="*60)

View File

@@ -8,7 +8,8 @@ from theauditor.security import sanitize_config_path, SecurityError
def initialize_project( def initialize_project(
offline: bool = False, offline: bool = False,
skip_docs: bool = False, skip_docs: bool = False,
skip_deps: bool = False skip_deps: bool = False,
progress_callback: Any = None
) -> Dict[str, Any]: ) -> Dict[str, Any]:
""" """
Initialize TheAuditor for first-time use by running all setup steps. Initialize TheAuditor for first-time use by running all setup steps.
@@ -43,6 +44,8 @@ def initialize_project(
stats = {} stats = {}
# 1. Index # 1. Index
if progress_callback:
progress_callback("[1/4] Indexing repository...")
try: try:
# Sanitize paths from config before use # Sanitize paths from config before use
manifest_path = str(sanitize_config_path(config["paths"]["manifest"], "paths", "manifest", ".")) manifest_path = str(sanitize_config_path(config["paths"]["manifest"], "paths", "manifest", "."))
@@ -65,18 +68,26 @@ def initialize_project(
"text_files": index_stats.get("text_files", 0), "text_files": index_stats.get("text_files", 0),
"success": True "success": True
} }
if progress_callback:
progress_callback(f" ✓ Indexed {stats['index']['text_files']} text files")
except SecurityError as e: except SecurityError as e:
stats["index"] = {"success": False, "error": f"Security violation: {str(e)}"} stats["index"] = {"success": False, "error": f"Security violation: {str(e)}"}
except Exception as e: except Exception as e:
stats["index"] = {"success": False, "error": str(e)} stats["index"] = {"success": False, "error": str(e)}
if progress_callback:
progress_callback(f" ✗ Failed: {str(e)[:60]}")
# 2. Workset # 2. Workset
if progress_callback:
progress_callback("\n[2/4] Creating workset...")
try: try:
# Skip if indexing failed or found no files # Skip if indexing failed or found no files
if not stats.get("index", {}).get("success"): if not stats.get("index", {}).get("success"):
raise Exception("Skipping - indexing failed") raise Exception("Skipping - indexing failed")
if stats.get("index", {}).get("text_files", 0) == 0: if stats.get("index", {}).get("text_files", 0) == 0:
stats["workset"] = {"success": False, "files": 0} stats["workset"] = {"success": False, "files": 0}
if progress_callback:
progress_callback(" ⚠ No files found")
else: else:
# Sanitize paths from config before use # Sanitize paths from config before use
db_path = str(sanitize_config_path(config["paths"]["db"], "paths", "db", ".")) db_path = str(sanitize_config_path(config["paths"]["db"], "paths", "db", "."))
@@ -97,13 +108,19 @@ def initialize_project(
"coverage": result.get("coverage", 0), "coverage": result.get("coverage", 0),
"success": True "success": True
} }
if progress_callback:
progress_callback(f" ✓ Created workset with {stats['workset']['files']} files")
except SecurityError as e: except SecurityError as e:
stats["workset"] = {"success": False, "error": f"Security violation: {str(e)}"} stats["workset"] = {"success": False, "error": f"Security violation: {str(e)}"}
except Exception as e: except Exception as e:
stats["workset"] = {"success": False, "error": str(e)} stats["workset"] = {"success": False, "error": str(e)}
if progress_callback:
progress_callback(f" ✗ Failed: {str(e)[:60]}")
# 3. Dependencies # 3. Dependencies
if not skip_deps and not offline: if not skip_deps and not offline:
if progress_callback:
progress_callback("\n[3/4] Checking dependencies...")
try: try:
deps_list = parse_dependencies(root_path=".") deps_list = parse_dependencies(root_path=".")
@@ -115,22 +132,32 @@ def initialize_project(
"outdated": outdated, "outdated": outdated,
"success": True "success": True
} }
if progress_callback:
progress_callback(f" ✓ Found {len(deps_list)} dependencies ({outdated} outdated)")
else: else:
stats["deps"] = {"total": 0, "success": True} stats["deps"] = {"total": 0, "success": True}
if progress_callback:
progress_callback(" ✓ No dependency files found")
except Exception as e: except Exception as e:
stats["deps"] = {"success": False, "error": str(e)} stats["deps"] = {"success": False, "error": str(e)}
if progress_callback:
progress_callback(f" ✗ Failed: {str(e)[:60]}")
else: else:
stats["deps"] = {"skipped": True} stats["deps"] = {"skipped": True}
# 4. Documentation # 4. Documentation
if not skip_docs and not offline: if not skip_docs and not offline:
if progress_callback:
progress_callback("\n[4/4] Fetching documentation...")
try: try:
deps_list = parse_dependencies(root_path=".") deps_list = parse_dependencies(root_path=".")
if deps_list: if deps_list:
# Limit to first 50 deps for init command to avoid hanging # Limit to first 250 deps for init command to avoid excessive runtime
if len(deps_list) > 50: if len(deps_list) > 250:
deps_list = deps_list[:50] deps_list = deps_list[:250]
if progress_callback:
progress_callback(" Limiting to first 250 packages for speed...")
# Fetch with progress indicator # Fetch with progress indicator
fetch_result = fetch_docs(deps_list) fetch_result = fetch_docs(deps_list)
@@ -147,12 +174,20 @@ def initialize_project(
"success": True, "success": True,
"errors": errors "errors": errors
} }
if progress_callback:
progress_callback(f" ✓ Fetched {fetched} docs, created {stats['docs']['capsules']} capsules")
else: else:
stats["docs"] = {"success": True, "fetched": 0, "capsules": 0} stats["docs"] = {"success": True, "fetched": 0, "capsules": 0}
if progress_callback:
progress_callback(" ✓ No dependencies to document")
except KeyboardInterrupt: except KeyboardInterrupt:
stats["docs"] = {"success": False, "error": "Interrupted by user"} stats["docs"] = {"success": False, "error": "Interrupted by user"}
if progress_callback:
progress_callback("\n ⚠ Interrupted by user (Ctrl+C)")
except Exception as e: except Exception as e:
stats["docs"] = {"success": False, "error": str(e)} stats["docs"] = {"success": False, "error": str(e)}
if progress_callback:
progress_callback(f" ✗ Failed: {str(e)[:60]}")
else: else:
stats["docs"] = {"skipped": True} stats["docs"] = {"skipped": True}