package_skill.py raw

   1  #!/usr/bin/env python3
   2  """
   3  Skill Packager - Creates a distributable zip file of a skill folder
   4  
   5  Usage:
   6      python utils/package_skill.py <path/to/skill-folder> [output-directory]
   7  
   8  Example:
   9      python utils/package_skill.py skills/public/my-skill
  10      python utils/package_skill.py skills/public/my-skill ./dist
  11  """
  12  
  13  import sys
  14  import zipfile
  15  from pathlib import Path
  16  from quick_validate import validate_skill
  17  
  18  
  19  def package_skill(skill_path, output_dir=None):
  20      """
  21      Package a skill folder into a zip file.
  22  
  23      Args:
  24          skill_path: Path to the skill folder
  25          output_dir: Optional output directory for the zip file (defaults to current directory)
  26  
  27      Returns:
  28          Path to the created zip file, or None if error
  29      """
  30      skill_path = Path(skill_path).resolve()
  31  
  32      # Validate skill folder exists
  33      if not skill_path.exists():
  34          print(f"āŒ Error: Skill folder not found: {skill_path}")
  35          return None
  36  
  37      if not skill_path.is_dir():
  38          print(f"āŒ Error: Path is not a directory: {skill_path}")
  39          return None
  40  
  41      # Validate SKILL.md exists
  42      skill_md = skill_path / "SKILL.md"
  43      if not skill_md.exists():
  44          print(f"āŒ Error: SKILL.md not found in {skill_path}")
  45          return None
  46  
  47      # Run validation before packaging
  48      print("šŸ” Validating skill...")
  49      valid, message = validate_skill(skill_path)
  50      if not valid:
  51          print(f"āŒ Validation failed: {message}")
  52          print("   Please fix the validation errors before packaging.")
  53          return None
  54      print(f"āœ… {message}\n")
  55  
  56      # Determine output location
  57      skill_name = skill_path.name
  58      if output_dir:
  59          output_path = Path(output_dir).resolve()
  60          output_path.mkdir(parents=True, exist_ok=True)
  61      else:
  62          output_path = Path.cwd()
  63  
  64      zip_filename = output_path / f"{skill_name}.zip"
  65  
  66      # Create the zip file
  67      try:
  68          with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
  69              # Walk through the skill directory
  70              for file_path in skill_path.rglob('*'):
  71                  if file_path.is_file():
  72                      # Calculate the relative path within the zip
  73                      arcname = file_path.relative_to(skill_path.parent)
  74                      zipf.write(file_path, arcname)
  75                      print(f"  Added: {arcname}")
  76  
  77          print(f"\nāœ… Successfully packaged skill to: {zip_filename}")
  78          return zip_filename
  79  
  80      except Exception as e:
  81          print(f"āŒ Error creating zip file: {e}")
  82          return None
  83  
  84  
  85  def main():
  86      if len(sys.argv) < 2:
  87          print("Usage: python utils/package_skill.py <path/to/skill-folder> [output-directory]")
  88          print("\nExample:")
  89          print("  python utils/package_skill.py skills/public/my-skill")
  90          print("  python utils/package_skill.py skills/public/my-skill ./dist")
  91          sys.exit(1)
  92  
  93      skill_path = sys.argv[1]
  94      output_dir = sys.argv[2] if len(sys.argv) > 2 else None
  95  
  96      print(f"šŸ“¦ Packaging skill: {skill_path}")
  97      if output_dir:
  98          print(f"   Output directory: {output_dir}")
  99      print()
 100  
 101      result = package_skill(skill_path, output_dir)
 102  
 103      if result:
 104          sys.exit(0)
 105      else:
 106          sys.exit(1)
 107  
 108  
 109  if __name__ == "__main__":
 110      main()
 111