Skip to content

Commit db5544e

Browse files
committed
Add support for yaml and json AutoPkg recipes
1 parent 4ba979d commit db5544e

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
description: This hook checks AutoPkg recipes to ensure they contain required top-level keys.
1212
entry: check-autopkg-recipes
1313
language: python
14-
files: '\.recipe$'
14+
files: '\.recipe(\.yaml|\.json)?$'
1515
types: [text]
1616

1717
- id: check-git-config-email

pre_commit_hooks/check_autopkg_recipes.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
requirements."""
55

66
import argparse
7+
import json
78
import os
89
import plistlib
910
import sys
1011
from contextlib import contextmanager
1112
from distutils.version import LooseVersion
1213
from xml.parsers.expat import ExpatError
1314

15+
from ruamel import yaml
16+
1417
from pre_commit_hooks.util import (
1518
validate_pkginfo_key_types,
1619
validate_required_keys,
@@ -482,17 +485,40 @@ def main(argv=None):
482485

483486
retval = 0
484487
for filename in args.filenames:
485-
try:
486-
with open(filename, "rb") as openfile:
487-
recipe = plistlib.load(openfile)
488-
# For future implementation of validate_unused_input_vars()
489-
# with open(filename, "r") as openfile:
490-
# recipe_text = openfile.read()
491-
492-
except (ExpatError, ValueError) as err:
493-
print("{}: plist parsing error: {}".format(filename, err))
494-
retval = 1
495-
break # No need to continue checking this file
488+
489+
if filename.endswith(".yaml"):
490+
try:
491+
# try to read it as yaml
492+
with open(filename, "rb") as f:
493+
recipe = yaml.safe_load(f)
494+
except Exception as err:
495+
print("{}: yaml parsing error: {}".format(filename, err))
496+
retval = 1
497+
break # No need to continue checking this file
498+
499+
elif filename.endswith(".json"):
500+
try:
501+
# try to read it as json
502+
with open(filename, "rb") as f:
503+
recipe = json.load(f)
504+
except Exception as err:
505+
print("{}: json parsing error: {}".format(filename, err))
506+
retval = 1
507+
break # No need to continue checking this file
508+
509+
else:
510+
try:
511+
# try to read it as a plist
512+
with open(filename, "rb") as f:
513+
recipe = plistlib.load(f)
514+
except Exception as err:
515+
print("{}: plist parsing error: {}".format(filename, err))
516+
retval = 1
517+
break # No need to continue checking this file
518+
519+
# For future implementation of validate_unused_input_vars()
520+
# with open(filename, "r") as openfile:
521+
# recipe_text = openfile.read()
496522

497523
# Top level keys that all AutoPkg recipes should contain.
498524
required_keys = ["Identifier"]

0 commit comments

Comments
 (0)