|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import re |
| 4 | +import sys |
| 5 | +import yaml |
| 6 | + |
| 7 | +def validate_skill(skill_md_path): |
| 8 | + print(f"Checking {skill_md_path}...") |
| 9 | + try: |
| 10 | + with open(skill_md_path, "r", encoding="utf-8") as f: |
| 11 | + content = f.read() |
| 12 | + except Exception as e: |
| 13 | + print(f"Error reading {skill_md_path}: {e}") |
| 14 | + return False |
| 15 | + |
| 16 | + # Extract frontmatter |
| 17 | + match = re.search(r"^---\n(.*?)\n---", content, re.DOTALL) |
| 18 | + if not match: |
| 19 | + print(f"Error: No YAML frontmatter found in {skill_md_path}") |
| 20 | + return False |
| 21 | + |
| 22 | + try: |
| 23 | + frontmatter = yaml.safe_load(match.group(1)) |
| 24 | + except Exception as e: |
| 25 | + print(f"Error parsing YAML in {skill_md_path}: {e}") |
| 26 | + return False |
| 27 | + |
| 28 | + if not isinstance(frontmatter, dict): |
| 29 | + print(f"Error: Frontmatter in {skill_md_path} is not a dictionary") |
| 30 | + return False |
| 31 | + |
| 32 | + errors = [] |
| 33 | + |
| 34 | + # Check license |
| 35 | + license = frontmatter.get("license") |
| 36 | + if license != "Apache-2.0": |
| 37 | + errors.append(f"Expected 'license: Apache-2.0', got '{license}'") |
| 38 | + |
| 39 | + # Check metadata |
| 40 | + metadata = frontmatter.get("metadata") |
| 41 | + if not isinstance(metadata, dict): |
| 42 | + errors.append("Missing or invalid 'metadata' block") |
| 43 | + else: |
| 44 | + # Check publisher |
| 45 | + publisher = metadata.get("publisher") |
| 46 | + if publisher != "google": |
| 47 | + errors.append(f"Expected 'metadata.publisher: google', got '{publisher}'") |
| 48 | + |
| 49 | + # Check version |
| 50 | + version = metadata.get("version") |
| 51 | + if not version: |
| 52 | + errors.append("Missing 'metadata.version'") |
| 53 | + elif not re.match(r"^v[0-9]+$", str(version)): |
| 54 | + errors.append(f"Invalid 'metadata.version' format '{version}'. Expected format like 'v1'") |
| 55 | + |
| 56 | + if errors: |
| 57 | + for err in errors: |
| 58 | + print(f" FAILED: {err}") |
| 59 | + return False |
| 60 | + |
| 61 | + print(" PASSED") |
| 62 | + return True |
| 63 | + |
| 64 | +def main(): |
| 65 | + skills_dir = "skills" |
| 66 | + if not os.path.exists(skills_dir): |
| 67 | + print(f"Error: {skills_dir} directory not found") |
| 68 | + sys.exit(1) |
| 69 | + |
| 70 | + failed = False |
| 71 | + for root, _, files in os.walk(skills_dir): |
| 72 | + for file in files: |
| 73 | + if file == "SKILL.md": |
| 74 | + skill_md_path = os.path.join(root, file) |
| 75 | + if not validate_skill(skill_md_path): |
| 76 | + failed = True |
| 77 | + |
| 78 | + if failed: |
| 79 | + sys.exit(1) |
| 80 | + else: |
| 81 | + print("All skills validated successfully.") |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments