78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Checks the skills: front matter, name, description and, with --rendered, that no marker in double braces is left.
|
|
|
|
Usage: python3 scripts/validate.py [--rendered] [skills folder]
|
|
Only the Python 3 standard library is used.
|
|
"""
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
NAME = re.compile(r'^[a-z0-9]+(-[a-z0-9]+)*$')
|
|
MARKER = re.compile(r'\{\{[A-Z][A-Z0-9_]*\}\}')
|
|
|
|
|
|
def front_matter(text):
|
|
if not text.startswith('---\n'):
|
|
return None, text
|
|
end = text.find('\n---\n', 4)
|
|
if end < 0:
|
|
return None, text
|
|
data = {}
|
|
for line in text[4:end].splitlines():
|
|
if ':' in line:
|
|
key, value = line.split(':', 1)
|
|
data[key.strip()] = value.strip()
|
|
return data, text[end + 5:]
|
|
|
|
|
|
def check(folder, rendered):
|
|
errors = []
|
|
names = sorted(n for n in os.listdir(folder) if os.path.isdir(os.path.join(folder, n)))
|
|
if not names:
|
|
errors.append('no skills in %s' % folder)
|
|
for name in names:
|
|
path = os.path.join(folder, name, 'SKILL.md')
|
|
if not os.path.isfile(path):
|
|
errors.append('%s: SKILL.md is missing' % name)
|
|
continue
|
|
with open(path, encoding='utf-8') as f:
|
|
text = f.read()
|
|
meta, body = front_matter(text)
|
|
if meta is None:
|
|
errors.append('%s: no front matter' % name)
|
|
continue
|
|
if meta.get('name') != name:
|
|
errors.append('%s: name must match the folder' % name)
|
|
if not NAME.match(meta.get('name', '')) or len(meta.get('name', '')) > 64:
|
|
errors.append('%s: name must be lowercase letters, digits and hyphens, up to 64' % name)
|
|
desc = meta.get('description', '')
|
|
if not desc or len(desc) > 1024:
|
|
errors.append('%s: description must have 1 to 1024 characters' % name)
|
|
if '<' in desc or '>' in desc:
|
|
errors.append('%s: description must not contain < or >' % name)
|
|
if len(body.strip()) < 200:
|
|
errors.append('%s: body is too short' % name)
|
|
if text.count('```') % 2:
|
|
errors.append('%s: a code block is not closed' % name)
|
|
if rendered:
|
|
left = sorted(set(MARKER.findall(text)))
|
|
if left:
|
|
errors.append('%s: markers left: %s' % (name, ', '.join(left)))
|
|
return names, errors
|
|
|
|
|
|
def main(argv):
|
|
rendered = '--rendered' in argv
|
|
args = [a for a in argv if not a.startswith('--')]
|
|
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
folder = args[0] if args else os.path.join(root, 'skills')
|
|
names, errors = check(folder, rendered)
|
|
for e in errors:
|
|
print('error: ' + e)
|
|
print('%d skills, %d errors' % (len(names), len(errors)))
|
|
return 1 if errors else 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main(sys.argv[1:]))
|