Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## [1.10.0] - 2021-02-21
## [1.10.1] - 2021-02-21

### Added
- In anticipation of AutoPkg 2.3, now supports checking YAML recipes (must have extension `.recipe.yaml`).
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ For any hook in this repo you wish to use, add the following to your pre-commit

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

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

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

```yaml
- repo: https://github.com/homebysix/pre-commit-macadmin
rev: v1.10.0
rev: v1.10.1
hooks:
- id: check-munki-pkgsinfo
args: [
Expand Down
39 changes: 5 additions & 34 deletions pre_commit_hooks/check_autopkg_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
requirements."""

import argparse
import json
import os
import plistlib
import sys
from contextlib import contextmanager
from distutils.version import LooseVersion
from xml.parsers.expat import ExpatError

from ruamel import yaml

from pre_commit_hooks.util import (
load_autopkg_recipe,
validate_pkginfo_key_types,
validate_required_keys,
validate_restart_action_key,
Expand Down Expand Up @@ -486,35 +482,10 @@ def main(argv=None):
retval = 0
for filename in args.filenames:

if filename.endswith(".yaml"):
try:
# try to read it as yaml
with open(filename, "rb") as f:
recipe = yaml.safe_load(f)
except Exception as err:
print("{}: yaml parsing error: {}".format(filename, err))
retval = 1
break # No need to continue checking this file

elif filename.endswith(".json"):
try:
# try to read it as json
with open(filename, "rb") as f:
recipe = json.load(f)
except Exception as err:
print("{}: json parsing error: {}".format(filename, err))
retval = 1
break # No need to continue checking this file

else:
try:
# try to read it as a plist
with open(filename, "rb") as f:
recipe = plistlib.load(f)
except Exception as err:
print("{}: plist parsing error: {}".format(filename, err))
retval = 1
break # No need to continue checking this file
recipe = load_autopkg_recipe(filename)
if not recipe:
retval = 1
break # No need to continue checking this file

# For future implementation of validate_unused_input_vars()
# with open(filename, "r") as openfile:
Expand Down
22 changes: 9 additions & 13 deletions pre_commit_hooks/forbid_autopkg_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"""This hook prevents AutoPkg overrides from being added to the repo."""

import argparse
import plistlib
from xml.parsers.expat import ExpatError

from pre_commit_hooks.util import load_autopkg_recipe


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

retval = 0
for filename in args.filenames:
try:
with open(filename, "rb") as openfile:
recipe = plistlib.load(openfile)
for req_key in required_keys:
if req_key not in recipe:
print("{}: possible AutoPkg recipe override".format(filename))
retval = 1
break # No need to continue checking this file.

except (ExpatError, ValueError) as err:
print("{}: plist parsing error: {}".format(filename, err))
recipe = load_autopkg_recipe(filename)
if not recipe:
retval = 1
break # No need to continue checking this file.
for req_key in required_keys:
if req_key not in recipe:
print("{}: possible AutoPkg recipe override".format(filename))
retval = 1

return retval

Expand Down
18 changes: 6 additions & 12 deletions pre_commit_hooks/forbid_autopkg_trust_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
repo."""

import argparse
import plistlib
from xml.parsers.expat import ExpatError
from pre_commit_hooks.util import load_autopkg_recipe


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

retval = 0
for filename in args.filenames:
try:
with open(filename, "rb") as openfile:
recipe = plistlib.load(openfile)
if "ParentRecipeTrustInfo" in recipe:
print("{}: trust info in recipe".format(filename))
retval = 1
break # No need to continue checking this file.

except (ExpatError, ValueError) as err:
print("{}: plist parsing error: {}".format(filename, err))
recipe = load_autopkg_recipe(filename)
if not recipe:
retval = 1
elif "ParentRecipeTrustInfo" in recipe:
print("{}: trust info in recipe".format(filename))
retval = 1

return retval
Expand Down
33 changes: 33 additions & 0 deletions pre_commit_hooks/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import json
import plistlib
import sys
from datetime import datetime

from ruamel import yaml


def load_autopkg_recipe(path):
"""Loads an AutoPkg recipe in plist, yaml, or json format."""
recipe = None

if path.endswith(".yaml"):
try:
# try to read it as yaml
with open(path, "rb") as f:
recipe = yaml.safe_load(f)
except Exception as err:
print("{}: yaml parsing error: {}".format(path, err))
elif path.endswith(".json"):
try:
# try to read it as json
with open(path, "rb") as f:
recipe = json.load(f)
except Exception as err:
print("{}: json parsing error: {}".format(path, err))
else:
try:
# try to read it as a plist
with open(path, "rb") as f:
recipe = plistlib.load(f)
except Exception as err:
print("{}: plist parsing error: {}".format(path, err))

return recipe


def validate_required_keys(plist, filename, required_keys):
"""Verifies that required_keys are present in dictionary plist."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
name="pre-commit-macadmin",
description="Pre-commit hooks for Mac admins, client engineers, and IT consultants.",
url="https://github.com/homebysix/pre-commit-macadmin",
version="1.10.0",
version="1.10.1",
author="Elliot Jordan",
author_email="elliot@elliotjordan.com",
packages=["pre_commit_hooks"],
Expand Down