Skip to content

Commit 2a7e68c

Browse files
committed
Share function that loads AutoPkg recipes
1 parent 8b954df commit 2a7e68c

4 files changed

Lines changed: 53 additions & 59 deletions

File tree

pre_commit_hooks/check_autopkg_recipes.py

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
requirements."""
55

66
import argparse
7-
import json
87
import os
9-
import plistlib
108
import sys
119
from contextlib import contextmanager
1210
from distutils.version import LooseVersion
13-
from xml.parsers.expat import ExpatError
14-
15-
from ruamel import yaml
1611

1712
from pre_commit_hooks.util import (
13+
load_autopkg_recipe,
1814
validate_pkginfo_key_types,
1915
validate_required_keys,
2016
validate_restart_action_key,
@@ -486,35 +482,10 @@ def main(argv=None):
486482
retval = 0
487483
for filename in args.filenames:
488484

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
485+
recipe = load_autopkg_recipe(filename)
486+
if not recipe:
487+
retval = 1
488+
break # No need to continue checking this file
518489

519490
# For future implementation of validate_unused_input_vars()
520491
# with open(filename, "r") as openfile:

pre_commit_hooks/forbid_autopkg_overrides.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"""This hook prevents AutoPkg overrides from being added to the repo."""
44

55
import argparse
6-
import plistlib
7-
from xml.parsers.expat import ExpatError
6+
7+
from pre_commit_hooks.util import load_autopkg_recipe
88

99

1010
def build_argument_parser():
@@ -29,18 +29,14 @@ def main(argv=None):
2929

3030
retval = 0
3131
for filename in args.filenames:
32-
try:
33-
with open(filename, "rb") as openfile:
34-
recipe = plistlib.load(openfile)
35-
for req_key in required_keys:
36-
if req_key not in recipe:
37-
print("{}: possible AutoPkg recipe override".format(filename))
38-
retval = 1
39-
break # No need to continue checking this file.
40-
41-
except (ExpatError, ValueError) as err:
42-
print("{}: plist parsing error: {}".format(filename, err))
32+
recipe = load_autopkg_recipe(filename)
33+
if not recipe:
4334
retval = 1
35+
break # No need to continue checking this file.
36+
for req_key in required_keys:
37+
if req_key not in recipe:
38+
print("{}: possible AutoPkg recipe override".format(filename))
39+
retval = 1
4440

4541
return retval
4642

pre_commit_hooks/forbid_autopkg_trust_info.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
repo."""
55

66
import argparse
7-
import plistlib
8-
from xml.parsers.expat import ExpatError
7+
from pre_commit_hooks.util import load_autopkg_recipe
98

109

1110
def build_argument_parser():
@@ -27,16 +26,11 @@ def main(argv=None):
2726

2827
retval = 0
2928
for filename in args.filenames:
30-
try:
31-
with open(filename, "rb") as openfile:
32-
recipe = plistlib.load(openfile)
33-
if "ParentRecipeTrustInfo" in recipe:
34-
print("{}: trust info in recipe".format(filename))
35-
retval = 1
36-
break # No need to continue checking this file.
37-
38-
except (ExpatError, ValueError) as err:
39-
print("{}: plist parsing error: {}".format(filename, err))
29+
recipe = load_autopkg_recipe(filename)
30+
if not recipe:
31+
retval = 1
32+
elif "ParentRecipeTrustInfo" in recipe:
33+
print("{}: trust info in recipe".format(filename))
4034
retval = 1
4135

4236
return retval

pre_commit_hooks/util.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33

4+
import json
5+
import plistlib
46
import sys
57
from datetime import datetime
68

9+
from ruamel import yaml
10+
11+
12+
def load_autopkg_recipe(path):
13+
"""Loads an AutoPkg recipe in plist, yaml, or json format."""
14+
recipe = None
15+
16+
if path.endswith(".yaml"):
17+
try:
18+
# try to read it as yaml
19+
with open(path, "rb") as f:
20+
recipe = yaml.safe_load(f)
21+
except Exception as err:
22+
print("{}: yaml parsing error: {}".format(path, err))
23+
elif path.endswith(".json"):
24+
try:
25+
# try to read it as json
26+
with open(path, "rb") as f:
27+
recipe = json.load(f)
28+
except Exception as err:
29+
print("{}: json parsing error: {}".format(path, err))
30+
else:
31+
try:
32+
# try to read it as a plist
33+
with open(path, "rb") as f:
34+
recipe = plistlib.load(f)
35+
except Exception as err:
36+
print("{}: plist parsing error: {}".format(path, err))
37+
38+
return recipe
39+
740

841
def validate_required_keys(plist, filename, required_keys):
942
"""Verifies that required_keys are present in dictionary plist."""

0 commit comments

Comments
 (0)