Skip to content

Commit bd7619b

Browse files
authored
Merge pull request #46 from homebysix/1.10.0
v1.10.1 merge to main
2 parents 3b279ac + 9955ab9 commit bd7619b

7 files changed

Lines changed: 59 additions & 65 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
44

5-
## [1.10.0] - 2021-02-21
5+
## [1.10.1] - 2021-02-21
66

77
### Added
88
- In anticipation of AutoPkg 2.3, now supports checking YAML recipes (must have extension `.recipe.yaml`).

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ For any hook in this repo you wish to use, add the following to your pre-commit
1515

1616
```yaml
1717
- repo: https://github.com/homebysix/pre-commit-macadmin
18-
rev: v1.10.0
18+
rev: v1.10.1
1919
hooks:
2020
- id: check-plists
2121
# - id: ...
@@ -121,7 +121,7 @@ When combining arguments that take lists (for example: `--required-keys`, `--cat
121121

122122
```yaml
123123
- repo: https://github.com/homebysix/pre-commit-macadmin
124-
rev: v1.10.0
124+
rev: v1.10.1
125125
hooks:
126126
- id: check-munki-pkgsinfo
127127
args: ['--catalogs', 'testing', 'stable', '--']
@@ -131,7 +131,7 @@ But if you also use the `--categories` argument, you would move the trailing `--
131131

132132
```yaml
133133
- repo: https://github.com/homebysix/pre-commit-macadmin
134-
rev: v1.10.0
134+
rev: v1.10.1
135135
hooks:
136136
- id: check-munki-pkgsinfo
137137
args: ['--catalogs', 'testing', 'stable', '--categories', 'Design', 'Engineering', 'Web Browsers', '--']
@@ -143,7 +143,7 @@ If it looks better to your eye, feel free to use a multi-line list for long argu
143143

144144
```yaml
145145
- repo: https://github.com/homebysix/pre-commit-macadmin
146-
rev: v1.10.0
146+
rev: v1.10.1
147147
hooks:
148148
- id: check-munki-pkgsinfo
149149
args: [

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."""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
name="pre-commit-macadmin",
88
description="Pre-commit hooks for Mac admins, client engineers, and IT consultants.",
99
url="https://github.com/homebysix/pre-commit-macadmin",
10-
version="1.10.0",
10+
version="1.10.1",
1111
author="Elliot Jordan",
1212
author_email="elliot@elliotjordan.com",
1313
packages=["pre_commit_hooks"],

0 commit comments

Comments
 (0)