Sync with MokoStandards: remove local scripts and reusable workflows

Co-authored-by: jmiller-moko <230051081+jmiller-moko@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-18 01:36:58 +00:00
parent 6d40b1fc20
commit 09a4b0b490
31 changed files with 375 additions and 7534 deletions

View File

@@ -1,168 +0,0 @@
#!/usr/bin/env python3
"""
Validate Joomla manifest files.
Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
This file is part of a Moko Consulting project.
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (./LICENSE.md).
FILE INFORMATION
DEFGROUP: Moko-Cassiopeia.Scripts
INGROUP: Scripts.Validate
REPO: https://github.com/mokoconsulting-tech/moko-cassiopeia
PATH: /scripts/validate/manifest.py
VERSION: 01.00.00
BRIEF: Validate Joomla extension manifest files
"""
import argparse
import sys
from pathlib import Path
# Add lib directory to path
sys.path.insert(0, str(Path(__file__).parent.parent / "lib"))
try:
import common
import joomla_manifest
except ImportError:
print("ERROR: Cannot import required libraries", file=sys.stderr)
sys.exit(1)
def validate_manifest_file(manifest_path: Path, verbose: bool = False) -> bool:
"""
Validate a single manifest file.
Args:
manifest_path: Path to manifest file
verbose: Show detailed output
Returns:
True if valid, False otherwise
"""
try:
info = joomla_manifest.parse_manifest(manifest_path)
is_valid, warnings = joomla_manifest.validate_manifest(manifest_path)
if verbose:
common.log_section(f"Manifest: {manifest_path}")
common.log_kv("Type", info.extension_type)
common.log_kv("Name", info.name)
common.log_kv("Version", info.version)
if warnings:
common.log_warn(f"Warnings ({len(warnings)}):")
for warning in warnings:
print(f" - {warning}")
# Output JSON for machine consumption
result = {
"status": "ok" if is_valid else "error",
"manifest": str(manifest_path),
"ext_type": info.extension_type,
"name": info.name,
"version": info.version,
"warnings": warnings
}
if not verbose:
common.json_output(result)
if is_valid:
if not verbose:
print(f"manifest: ok ({manifest_path})")
else:
common.log_success("Manifest is valid")
return True
else:
common.log_error(f"Manifest validation failed: {manifest_path}")
return False
except SystemExit:
common.log_error(f"Failed to parse manifest: {manifest_path}")
return False
def main() -> int:
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Validate Joomla extension manifest files",
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"-s", "--src-dir",
default="src",
help="Source directory to search for manifests (default: src)"
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="Show detailed output"
)
parser.add_argument(
"manifest",
nargs="?",
help="Specific manifest file to validate (optional)"
)
args = parser.parse_args()
try:
if args.manifest:
# Validate specific manifest
manifest_path = Path(args.manifest)
if not manifest_path.is_file():
common.die(f"Manifest file not found: {args.manifest}")
success = validate_manifest_file(manifest_path, args.verbose)
return 0 if success else 1
else:
# Find and validate all manifests in src directory
manifests = joomla_manifest.find_all_manifests(args.src_dir)
if not manifests:
common.die(f"No manifest files found in {args.src_dir}")
if args.verbose:
common.log_section("Validating Manifests")
common.log_info(f"Found {len(manifests)} manifest(s)")
print()
all_valid = True
for manifest in manifests:
if not validate_manifest_file(manifest, args.verbose):
all_valid = False
if args.verbose:
print()
if all_valid:
common.log_success(f"All {len(manifests)} manifest(s) are valid")
else:
common.log_error("Some manifests failed validation")
return 0 if all_valid else 1
except Exception as e:
common.log_error(f"Validation failed: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())

View File

@@ -1,212 +0,0 @@
#!/usr/bin/env python3
"""
Scan for accidentally committed secrets and credentials.
Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
This file is part of a Moko Consulting project.
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (./LICENSE.md).
FILE INFORMATION
DEFGROUP: Script.Validate
INGROUP: Security
REPO: https://github.com/mokoconsulting-tech/moko-cassiopeia
PATH: /scripts/validate/no_secrets.py
VERSION: 01.00.00
BRIEF: Scan for accidentally committed secrets and credentials
NOTE: High-signal pattern detection to prevent credential exposure
"""
import argparse
import json
import os
import re
import sys
from pathlib import Path
from typing import List, Dict
# Add lib directory to path
sys.path.insert(0, str(Path(__file__).parent.parent / "lib"))
try:
import common
except ImportError:
print("ERROR: Cannot import required libraries", file=sys.stderr)
sys.exit(1)
# High-signal patterns only. Any match is a hard fail.
SECRET_PATTERNS = [
# Private keys
r'-----BEGIN (RSA|DSA|EC|OPENSSH) PRIVATE KEY-----',
r'PuTTY-User-Key-File-',
# AWS keys
r'AKIA[0-9A-Z]{16}',
r'ASIA[0-9A-Z]{16}',
# GitHub tokens
r'ghp_[A-Za-z0-9]{36}',
r'gho_[A-Za-z0-9]{36}',
r'github_pat_[A-Za-z0-9_]{20,}',
# Slack tokens
r'xox[baprs]-[0-9A-Za-z-]{10,48}',
# Stripe keys
r'sk_live_[0-9a-zA-Z]{20,}',
]
# Directories to exclude from scanning
EXCLUDE_DIRS = {
'vendor',
'node_modules',
'dist',
'build',
'.git',
}
def scan_file(filepath: Path, patterns: List[re.Pattern]) -> List[Dict[str, str]]:
"""
Scan a file for secret patterns.
Args:
filepath: Path to file to scan
patterns: Compiled regex patterns to search for
Returns:
List of matches with file, line number, and content
"""
hits = []
try:
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
for line_num, line in enumerate(f, 1):
for pattern in patterns:
if pattern.search(line):
hits.append({
'file': str(filepath),
'line': line_num,
'content': line.strip()[:100] # Limit to 100 chars
})
except Exception as e:
common.log_warn(f"Could not read {filepath}: {e}")
return hits
def scan_directory(src_dir: str, patterns: List[re.Pattern]) -> List[Dict[str, str]]:
"""
Recursively scan directory for secrets.
Args:
src_dir: Directory to scan
patterns: Compiled regex patterns
Returns:
List of all matches
"""
src_path = Path(src_dir)
all_hits = []
for item in src_path.rglob("*"):
# Skip directories
if not item.is_file():
continue
# Skip excluded directories
if any(excluded in item.parts for excluded in EXCLUDE_DIRS):
continue
# Skip binary files (heuristic)
try:
with open(item, 'rb') as f:
chunk = f.read(1024)
if b'\x00' in chunk: # Contains null bytes = likely binary
continue
except Exception:
continue
# Scan the file
hits = scan_file(item, patterns)
all_hits.extend(hits)
return all_hits
def main() -> int:
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Scan for accidentally committed secrets and credentials"
)
parser.add_argument(
"-s", "--src-dir",
default=os.environ.get("SRC_DIR", "src"),
help="Source directory to scan (default: src)"
)
args = parser.parse_args()
# Check if source directory exists
if not Path(args.src_dir).is_dir():
result = {
"status": "fail",
"error": "src directory missing"
}
common.json_output(result)
return 1
# Compile patterns
compiled_patterns = [re.compile(pattern) for pattern in SECRET_PATTERNS]
# Scan directory
hits = scan_directory(args.src_dir, compiled_patterns)
if hits:
# Limit to first 50 hits
hits = hits[:50]
result = {
"status": "fail",
"error": "secret_pattern_detected",
"hits": [{"hit": f"{h['file']}:{h['line']}: {h['content']}"} for h in hits]
}
print(json.dumps(result))
# Also print human-readable output
print("\nERROR: Potential secrets detected!", file=sys.stderr)
print(f"\nFound {len(hits)} potential secret(s):", file=sys.stderr)
for hit in hits[:10]: # Show first 10 in detail
print(f" {hit['file']}:{hit['line']}", file=sys.stderr)
print(f" {hit['content']}", file=sys.stderr)
if len(hits) > 10:
print(f" ... and {len(hits) - 10} more", file=sys.stderr)
print("\nPlease remove any secrets and use environment variables or secret management instead.", file=sys.stderr)
return 1
result = {
"status": "ok",
"src_dir": args.src_dir
}
common.json_output(result)
print("no_secrets: ok")
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@@ -1,169 +0,0 @@
#!/usr/bin/env python3
"""
Detect Windows-style path separators (backslashes).
Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
This file is part of a Moko Consulting project.
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (./LICENSE.md).
FILE INFORMATION
DEFGROUP: Script.Validate
INGROUP: Path.Normalization
REPO: https://github.com/mokoconsulting-tech/moko-cassiopeia
PATH: /scripts/validate/paths.py
VERSION: 01.00.00
BRIEF: Detect Windows-style path separators (backslashes)
NOTE: Ensures cross-platform path compatibility
"""
import mimetypes
import subprocess
import sys
from pathlib import Path
from typing import List, Tuple, Dict
# Add lib directory to path
sys.path.insert(0, str(Path(__file__).parent.parent / "lib"))
try:
import common
except ImportError:
print("ERROR: Cannot import required libraries", file=sys.stderr)
sys.exit(1)
def get_tracked_files() -> List[str]:
"""
Get list of files tracked by git.
Returns:
List of file paths
"""
try:
result = common.run_command(
["git", "ls-files", "-z"],
capture_output=True,
check=True
)
files = [f for f in result.stdout.split('\0') if f.strip()]
return files
except subprocess.CalledProcessError:
return []
def is_binary_file(filepath: str) -> bool:
"""
Check if a file is likely binary.
Args:
filepath: Path to file
Returns:
True if likely binary
"""
# Check mime type
mime_type, _ = mimetypes.guess_type(filepath)
if mime_type and mime_type.startswith(('application/', 'audio/', 'image/', 'video/')):
return True
# Check for null bytes (heuristic for binary files)
try:
with open(filepath, 'rb') as f:
chunk = f.read(1024)
if b'\x00' in chunk:
return True
except Exception:
return True
return False
def find_backslashes_in_file(filepath: str) -> List[Tuple[int, str]]:
"""
Find lines with backslashes in a file.
Args:
filepath: Path to file
Returns:
List of (line_number, line_content) tuples
"""
backslashes = []
try:
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
for line_num, line in enumerate(f, 1):
if '\\' in line:
backslashes.append((line_num, line.rstrip()))
except Exception as e:
common.log_warn(f"Could not read {filepath}: {e}")
return backslashes
def main() -> int:
"""Main entry point."""
tracked_files = get_tracked_files()
if not tracked_files:
print("No files to check")
return 0
hits: Dict[str, List[Tuple[int, str]]] = {}
for filepath in tracked_files:
# Skip binary files
if is_binary_file(filepath):
continue
# Find backslashes
backslashes = find_backslashes_in_file(filepath)
if backslashes:
hits[filepath] = backslashes
if hits:
print("ERROR: Windows-style path literals detected", file=sys.stderr)
print("", file=sys.stderr)
print(f"Found backslashes in {len(hits)} file(s):", file=sys.stderr)
for filepath, lines in hits.items():
print("", file=sys.stderr)
print(f" File: {filepath}", file=sys.stderr)
print(" Lines with backslashes:", file=sys.stderr)
# Show first 5 lines
for line_num, line_content in lines[:5]:
print(f" {line_num}: {line_content[:80]}", file=sys.stderr)
if len(lines) > 5:
print(f" ... and {len(lines) - 5} more", file=sys.stderr)
print("", file=sys.stderr)
print("To fix:", file=sys.stderr)
print(" 1. Run: python3 scripts/fix/paths.py", file=sys.stderr)
print(" 2. Or manually replace backslashes (\\) with forward slashes (/)", file=sys.stderr)
print(" 3. Ensure paths use POSIX separators for cross-platform compatibility", file=sys.stderr)
print("", file=sys.stderr)
return 2
print("paths: ok")
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@@ -1,218 +0,0 @@
#!/usr/bin/env python3
"""
Validate PHP syntax in files.
Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
This file is part of a Moko Consulting project.
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (./LICENSE.md).
FILE INFORMATION
DEFGROUP: Moko-Cassiopeia.Scripts
INGROUP: Scripts.Validate
REPO: https://github.com/mokoconsulting-tech/moko-cassiopeia
PATH: /scripts/validate/php_syntax.py
VERSION: 01.00.00
BRIEF: Validate PHP syntax in all PHP files
"""
import argparse
import subprocess
import sys
from pathlib import Path
from typing import List, Tuple
# Add lib directory to path
sys.path.insert(0, str(Path(__file__).parent.parent / "lib"))
try:
import common
except ImportError:
print("ERROR: Cannot import required libraries", file=sys.stderr)
sys.exit(1)
def check_php_file(file_path: Path) -> Tuple[bool, str]:
"""
Check PHP syntax of a single file.
Args:
file_path: Path to PHP file
Returns:
Tuple of (is_valid, error_message)
"""
try:
result = subprocess.run(
["php", "-l", str(file_path)],
capture_output=True,
text=True,
timeout=10
)
if result.returncode == 0:
return (True, "")
else:
return (False, result.stderr or result.stdout)
except subprocess.TimeoutExpired:
return (False, "Syntax check timed out")
except Exception as e:
return (False, str(e))
def find_php_files(src_dir: str, exclude_dirs: List[str] = None) -> List[Path]:
"""
Find all PHP files in a directory.
Args:
src_dir: Directory to search
exclude_dirs: Directories to exclude
Returns:
List of PHP file paths
"""
if exclude_dirs is None:
exclude_dirs = ["vendor", "node_modules", ".git"]
src_path = Path(src_dir)
if not src_path.is_dir():
return []
php_files = []
for php_file in src_path.rglob("*.php"):
# Check if file is in an excluded directory
if any(excluded in php_file.parts for excluded in exclude_dirs):
continue
php_files.append(php_file)
return sorted(php_files)
def main() -> int:
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Validate PHP syntax in all PHP files",
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"-s", "--src-dir",
default="src",
help="Source directory to search for PHP files (default: src)"
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="Show detailed output"
)
parser.add_argument(
"--exclude",
action="append",
help="Directories to exclude (can be specified multiple times)"
)
parser.add_argument(
"files",
nargs="*",
help="Specific files to check (optional)"
)
args = parser.parse_args()
# Check if PHP is available
common.require_cmd("php")
try:
# Determine which files to check
if args.files:
php_files = [Path(f) for f in args.files]
for f in php_files:
if not f.is_file():
common.die(f"File not found: {f}")
else:
exclude_dirs = args.exclude or ["vendor", "node_modules", ".git"]
php_files = find_php_files(args.src_dir, exclude_dirs)
if not php_files:
common.die(f"No PHP files found in {args.src_dir}")
if args.verbose:
common.log_section("PHP Syntax Validation")
common.log_info(f"Checking {len(php_files)} PHP file(s)")
print()
errors = []
for php_file in php_files:
is_valid, error_msg = check_php_file(php_file)
if is_valid:
if args.verbose:
common.log_success(f"OK: {php_file}")
else:
errors.append((php_file, error_msg))
if args.verbose:
common.log_error(f"FAILED: {php_file}")
if error_msg:
print(f" {error_msg}")
# Output results
if args.verbose:
print()
if errors:
result = {
"status": "error",
"total": len(php_files),
"passed": len(php_files) - len(errors),
"failed": len(errors),
"errors": [{"file": str(f), "error": e} for f, e in errors]
}
if not args.verbose:
common.json_output(result)
common.log_error(f"PHP syntax check failed: {len(errors)} error(s)")
if not args.verbose:
for file_path, error_msg in errors:
print(f"ERROR: {file_path}")
if error_msg:
print(f" {error_msg}")
return 1
else:
result = {
"status": "ok",
"total": len(php_files),
"passed": len(php_files)
}
if not args.verbose:
common.json_output(result)
print(f"php_syntax: ok ({len(php_files)} file(s) checked)")
else:
common.log_success(f"All {len(php_files)} PHP file(s) are valid")
return 0
except Exception as e:
common.log_error(f"Validation failed: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())

View File

@@ -1,140 +0,0 @@
#!/usr/bin/env python3
"""
Detect TAB characters in YAML files where they are not allowed.
Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
This file is part of a Moko Consulting project.
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (./LICENSE.md).
FILE INFORMATION
DEFGROUP: Script.Validate
INGROUP: Code.Quality
REPO: https://github.com/mokoconsulting-tech/moko-cassiopeia
PATH: /scripts/validate/tabs.py
VERSION: 01.00.00
BRIEF: Detect TAB characters in YAML files where they are not allowed
NOTE: YAML specification forbids tab characters
"""
import subprocess
import sys
from pathlib import Path
from typing import List, Tuple
# Add lib directory to path
sys.path.insert(0, str(Path(__file__).parent.parent / "lib"))
try:
import common
except ImportError:
print("ERROR: Cannot import required libraries", file=sys.stderr)
sys.exit(1)
def get_yaml_files() -> List[str]:
"""
Get list of YAML files tracked by git.
Returns:
List of YAML file paths
"""
try:
result = common.run_command(
["git", "ls-files", "*.yml", "*.yaml"],
capture_output=True,
check=True
)
files = [f.strip() for f in result.stdout.split('\n') if f.strip()]
return files
except subprocess.CalledProcessError:
return []
def check_tabs_in_file(filepath: str) -> List[Tuple[int, str]]:
"""
Check for tab characters in a file.
Args:
filepath: Path to file to check
Returns:
List of (line_number, line_content) tuples with tabs
"""
tabs_found = []
try:
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
for line_num, line in enumerate(f, 1):
if '\t' in line:
tabs_found.append((line_num, line.rstrip()))
except Exception as e:
common.log_warn(f"Could not read {filepath}: {e}")
return tabs_found
def main() -> int:
"""Main entry point."""
yaml_files = get_yaml_files()
if not yaml_files:
print("No files to check")
return 0
bad_files = []
all_violations = {}
for filepath in yaml_files:
tabs = check_tabs_in_file(filepath)
if tabs:
bad_files.append(filepath)
all_violations[filepath] = tabs
print(f"TAB found in {filepath}", file=sys.stderr)
print(" Lines with tabs:", file=sys.stderr)
# Show first 5 lines with tabs
for line_num, line_content in tabs[:5]:
print(f" {line_num}: {line_content[:80]}", file=sys.stderr)
if len(tabs) > 5:
print(f" ... and {len(tabs) - 5} more", file=sys.stderr)
print("", file=sys.stderr)
if bad_files:
print("", file=sys.stderr)
print("ERROR: Tabs found in repository files", file=sys.stderr)
print("", file=sys.stderr)
print("YAML specification forbids tab characters.", file=sys.stderr)
print(f"Found tabs in {len(bad_files)} file(s):", file=sys.stderr)
for f in bad_files:
print(f" - {f}", file=sys.stderr)
print("", file=sys.stderr)
print("To fix:", file=sys.stderr)
print(" 1. Run: python3 scripts/fix/tabs.py", file=sys.stderr)
print(" 2. Or manually replace tabs with spaces in your editor", file=sys.stderr)
print(" 3. Configure your editor to use spaces (not tabs) for YAML files", file=sys.stderr)
print("", file=sys.stderr)
return 2
print("tabs: ok")
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@@ -1,216 +0,0 @@
#!/usr/bin/env python3
"""
Validate GitHub Actions workflow files.
Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
This file is part of a Moko Consulting project.
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (./LICENSE.md).
FILE INFORMATION
DEFGROUP: Script.Validate
INGROUP: CI.Validation
REPO: https://github.com/mokoconsulting-tech/moko-cassiopeia
PATH: /scripts/validate/workflows.py
VERSION: 01.00.00
BRIEF: Validate GitHub Actions workflow files
NOTE: Checks YAML syntax, structure, and best practices
"""
import sys
from pathlib import Path
# Add lib directory to path
sys.path.insert(0, str(Path(__file__).parent.parent / "lib"))
try:
import common
except ImportError:
print("ERROR: Cannot import required libraries", file=sys.stderr)
sys.exit(1)
def validate_yaml_syntax(filepath: Path) -> bool:
"""
Validate YAML syntax of a workflow file.
Args:
filepath: Path to workflow file
Returns:
True if valid
"""
try:
import yaml
except ImportError:
common.log_warn("PyYAML module not installed. Install with: pip3 install pyyaml")
return True # Skip validation if yaml not available
try:
with open(filepath, 'r', encoding='utf-8') as f:
yaml.safe_load(f)
print(f"✓ Valid YAML: {filepath.name}")
return True
except yaml.YAMLError as e:
print(f"✗ YAML Error in {filepath.name}: {e}", file=sys.stderr)
return False
except Exception as e:
print(f"✗ Error reading {filepath.name}: {e}", file=sys.stderr)
return False
def check_no_tabs(filepath: Path) -> bool:
"""
Check that file contains no tab characters.
Args:
filepath: Path to file
Returns:
True if no tabs found
"""
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
if '\t' in content:
common.log_error(f"✗ File contains tab characters: {filepath.name}")
return False
except Exception as e:
common.log_warn(f"Could not read {filepath}: {e}")
return False
return True
def check_workflow_structure(filepath: Path) -> bool:
"""
Check workflow file structure for required keys.
Args:
filepath: Path to workflow file
Returns:
True if structure is valid
"""
errors = 0
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Check for required top-level keys
if 'name:' not in content and not content.startswith('name:'):
common.log_warn(f"Missing 'name:' in {filepath.name}")
if 'on:' not in content and not content.startswith('on:'):
common.log_error(f"✗ Missing 'on:' trigger in {filepath.name}")
errors += 1
if 'jobs:' not in content and not content.startswith('jobs:'):
common.log_error(f"✗ Missing 'jobs:' in {filepath.name}")
errors += 1
except Exception as e:
common.log_error(f"Error reading {filepath}: {e}")
return False
return errors == 0
def validate_workflow_file(filepath: Path) -> bool:
"""
Validate a single workflow file.
Args:
filepath: Path to workflow file
Returns:
True if valid
"""
common.log_info(f"Validating: {filepath.name}")
errors = 0
# Check YAML syntax
if not validate_yaml_syntax(filepath):
errors += 1
# Check for tabs
if not check_no_tabs(filepath):
errors += 1
# Check structure
if not check_workflow_structure(filepath):
errors += 1
if errors == 0:
common.log_info(f"{filepath.name} passed all checks")
return True
else:
common.log_error(f"{filepath.name} failed {errors} check(s)")
return False
def main() -> int:
"""Main entry point."""
common.log_info("GitHub Actions Workflow Validation")
common.log_info("===================================")
print()
workflows_dir = Path(".github/workflows")
if not workflows_dir.is_dir():
common.log_error(f"Workflows directory not found: {workflows_dir}")
return 1
# Find all workflow files
workflow_files = []
for pattern in ["*.yml", "*.yaml"]:
workflow_files.extend(workflows_dir.glob(pattern))
if not workflow_files:
common.log_warn("No workflow files found")
return 0
total = len(workflow_files)
passed = 0
failed = 0
for workflow in workflow_files:
if validate_workflow_file(workflow):
passed += 1
else:
failed += 1
print()
common.log_info("===================================")
common.log_info("Summary:")
common.log_info(f" Total workflows: {total}")
common.log_info(f" Passed: {passed}")
common.log_info(f" Failed: {failed}")
common.log_info("===================================")
if failed > 0:
common.log_error("Workflow validation failed")
return 1
common.log_info("All workflows validated successfully")
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@@ -1,206 +0,0 @@
#!/usr/bin/env python3
"""
Validate XML well-formedness.
Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
This file is part of a Moko Consulting project.
SPDX-License-Identifier: GPL-3.0-or-later
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (./LICENSE.md).
FILE INFORMATION
DEFGROUP: Moko-Cassiopeia.Scripts
INGROUP: Scripts.Validate
REPO: https://github.com/mokoconsulting-tech/moko-cassiopeia
PATH: /scripts/validate/xml_wellformed.py
VERSION: 01.00.00
BRIEF: Validate XML well-formedness in all XML files
"""
import argparse
import sys
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import List, Tuple
# Add lib directory to path
sys.path.insert(0, str(Path(__file__).parent.parent / "lib"))
try:
import common
except ImportError:
print("ERROR: Cannot import required libraries", file=sys.stderr)
sys.exit(1)
def check_xml_file(file_path: Path) -> Tuple[bool, str]:
"""
Check if an XML file is well-formed.
Args:
file_path: Path to XML file
Returns:
Tuple of (is_valid, error_message)
"""
try:
ET.parse(file_path)
return (True, "")
except ET.ParseError as e:
return (False, str(e))
except Exception as e:
return (False, str(e))
def find_xml_files(src_dir: str, exclude_dirs: List[str] = None) -> List[Path]:
"""
Find all XML files in a directory.
Args:
src_dir: Directory to search
exclude_dirs: Directories to exclude
Returns:
List of XML file paths
"""
if exclude_dirs is None:
exclude_dirs = ["vendor", "node_modules", ".git"]
src_path = Path(src_dir)
if not src_path.is_dir():
return []
xml_files = []
for xml_file in src_path.rglob("*.xml"):
# Check if file is in an excluded directory
if any(excluded in xml_file.parts for excluded in exclude_dirs):
continue
xml_files.append(xml_file)
return sorted(xml_files)
def main() -> int:
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Validate XML well-formedness in all XML files",
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"-s", "--src-dir",
default="src",
help="Source directory to search for XML files (default: src)"
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="Show detailed output"
)
parser.add_argument(
"--exclude",
action="append",
help="Directories to exclude (can be specified multiple times)"
)
parser.add_argument(
"files",
nargs="*",
help="Specific files to check (optional)"
)
args = parser.parse_args()
try:
# Determine which files to check
if args.files:
xml_files = [Path(f) for f in args.files]
for f in xml_files:
if not f.is_file():
common.die(f"File not found: {f}")
else:
exclude_dirs = args.exclude or ["vendor", "node_modules", ".git"]
xml_files = find_xml_files(args.src_dir, exclude_dirs)
if not xml_files:
common.die(f"No XML files found in {args.src_dir}")
if args.verbose:
common.log_section("XML Well-formedness Validation")
common.log_info(f"Checking {len(xml_files)} XML file(s)")
print()
errors = []
for xml_file in xml_files:
is_valid, error_msg = check_xml_file(xml_file)
if is_valid:
if args.verbose:
common.log_success(f"OK: {xml_file}")
else:
errors.append((xml_file, error_msg))
if args.verbose:
common.log_error(f"FAILED: {xml_file}")
if error_msg:
print(f" {error_msg}")
# Output results
if args.verbose:
print()
if errors:
result = {
"status": "error",
"src_dir": args.src_dir,
"xml_count": len(xml_files),
"passed": len(xml_files) - len(errors),
"failed": len(errors),
"errors": [{"file": str(f), "error": e} for f, e in errors]
}
if not args.verbose:
common.json_output(result)
common.log_error(f"XML validation failed: {len(errors)} error(s)")
if not args.verbose:
for file_path, error_msg in errors:
print(f"ERROR: {file_path}")
if error_msg:
print(f" {error_msg}")
return 1
else:
result = {
"status": "ok",
"src_dir": args.src_dir,
"xml_count": len(xml_files)
}
if not args.verbose:
common.json_output(result)
print(f"xml_wellformed: ok")
else:
common.log_success(f"All {len(xml_files)} XML file(s) are well-formed")
return 0
except Exception as e:
common.log_error(f"Validation failed: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())