Skip to content

Commit 65a8740

Browse files
authored
Merge pull request #44 from homebysix/1.9.0
v1.9.0 merge to main
2 parents f75546a + 6e08b20 commit 65a8740

4 files changed

Lines changed: 94 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
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.9.0] - 2021-01-18
6+
7+
### Added
8+
- Added check for any unexpected processor arguments in any AutoPkg processor.
9+
10+
### Removed
11+
- CodeSignatureVerifier processor argument verification (added in v1.8.2) has been replaced by the above.
512

613
## [1.8.2] - 2021-01-18
714

README.md

Lines changed: 6 additions & 6 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.8.2
18+
rev: v1.9.0
1919
hooks:
2020
- id: check-plists
2121
# - id: ...
@@ -52,7 +52,7 @@ After adding a hook to your pre-commit config, it's not a bad idea to run `pre-c
5252

5353
- __check-autopkg-recipes__
5454

55-
This hook checks AutoPkg recipes to ensure they meet various requirements.
55+
This hook checks AutoPkg recipes to ensure they meet various requirements and conventions.
5656

5757
- Optionally specify your preferred AutoPkg recipe and/or override prefix, if you wish to enforce them:
5858
`args: ['--override-prefix=com.yourcompany.autopkg.']`
@@ -65,7 +65,7 @@ After adding a hook to your pre-commit config, it's not a bad idea to run `pre-c
6565
(default: `1.0.0`)
6666
Specifying `0.1.0` will not ignore any MinimumVersion mismatches.
6767

68-
- If you're a purist, you can also enable strict mode. This enforces recipe type conventions, all processor/MinimumVersion mismatches, and forbids `<!-- -->` style comments.
68+
- If you're a purist, you can also enable strict mode. This enforces recipe type conventions, all processor/MinimumVersion mismatches, forbids `<!-- -->` style comments, and ensures all processor input variables (arguments) are valid.
6969
`args: ['--strict']`
7070
(default: False)
7171

@@ -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.8.2
124+
rev: v1.9.0
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.8.2
134+
rev: v1.9.0
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.8.2
146+
rev: v1.9.0
147147
hooks:
148148
- id: check-munki-pkgsinfo
149149
args: [

pre_commit_hooks/check_autopkg_recipes.py

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
requirements."""
55

66
import argparse
7+
import os
78
import plistlib
9+
import sys
10+
from contextlib import contextmanager
811
from distutils.version import LooseVersion
912
from xml.parsers.expat import ExpatError
1013

@@ -15,6 +18,29 @@
1518
)
1619

1720

21+
# Import AutoPkg libraries, but ignore any warnings generated by the import.
22+
@contextmanager
23+
def suppress_stdout():
24+
with open(os.devnull, "w") as devnull:
25+
old_stdout = sys.stdout
26+
sys.stdout = devnull
27+
try:
28+
yield
29+
finally:
30+
sys.stdout = old_stdout
31+
32+
33+
sys.path.append("/Library/AutoPkg")
34+
try:
35+
with suppress_stdout():
36+
from autopkglib import get_processor, processor_names
37+
38+
HAS_AUTOPKGLIB = True
39+
except ImportError:
40+
# Silently skip checks that require autopkglib.
41+
HAS_AUTOPKGLIB = False
42+
43+
1844
def build_argument_parser():
1945
"""Build and return the argument parser."""
2046

@@ -148,26 +174,6 @@ def validate_endofcheckphase(process, filename):
148174
return passed
149175

150176

151-
def validate_codesignatureverifier(process, filename):
152-
"""Ensure CodeSignatureVerifier uses correct arguments."""
153-
154-
passed = True
155-
csv_args = [
156-
x.get("Arguments")
157-
for x in process
158-
if x.get("Processor") == "CodeSignatureVerifier"
159-
]
160-
if csv_args:
161-
if "requirements" in csv_args[0]:
162-
print(
163-
'{}: Found unexpected key "{}" in '
164-
"CodeSignatureVerifier arguments.".format(filename, "requirements")
165-
)
166-
passed = False
167-
168-
return passed
169-
170-
171177
def validate_minimumversion(process, min_vers, ignore_min_vers_before, filename):
172178
"""Ensure MinimumVersion is set appropriately for the processors used."""
173179

@@ -388,6 +394,56 @@ def validate_required_proc_for_types(process, filename):
388394
return passed
389395

390396

397+
def validate_proc_args(process, filename):
398+
"""Warn if invalid processor arguments are used."""
399+
400+
passed = True
401+
402+
# List of argument names (lowercase) that will not be flagged as invalid.
403+
ignored_args = ("note", "notes", "comment", "comments")
404+
405+
# Create dictionary of AutoPkg core processors and their inputs.
406+
core_procs = {}
407+
for proc in processor_names():
408+
if hasattr(get_processor(proc), "input_variables"):
409+
core_procs[proc] = get_processor(proc).input_variables
410+
else:
411+
core_procs[proc] = {}
412+
413+
for proc in process:
414+
if proc["Processor"] not in core_procs:
415+
# Skip input variable validation for non-core processors.
416+
continue
417+
for arg in proc.get("Arguments", {}):
418+
if arg.lower() in ignored_args:
419+
# Skip args in ignored list above.
420+
continue
421+
422+
if not core_procs[proc["Processor"]]:
423+
print(
424+
"{}: Unknown argument {} for processor {}, "
425+
"which does not accept any arguments.".format(
426+
filename,
427+
arg,
428+
proc["Processor"],
429+
)
430+
)
431+
passed = False
432+
elif arg not in core_procs[proc["Processor"]]:
433+
print(
434+
"{}: Unknown argument {} for processor {}. "
435+
"Allowed arguments are: {}".format(
436+
filename,
437+
arg,
438+
proc["Processor"],
439+
", ".join(core_procs[proc["Processor"]]),
440+
)
441+
)
442+
passed = False
443+
444+
return passed
445+
446+
391447
def main(argv=None):
392448
"""Main process."""
393449

@@ -482,9 +538,6 @@ def main(argv=None):
482538
if not validate_endofcheckphase(process, filename):
483539
retval = 1
484540

485-
if not validate_codesignatureverifier(process, filename):
486-
retval = 1
487-
488541
if not validate_no_var_in_app_path(process, filename):
489542
retval = 1
490543

@@ -500,6 +553,10 @@ def main(argv=None):
500553
if not validate_no_superclass_procs(process, filename):
501554
retval = 1
502555

556+
if HAS_AUTOPKGLIB:
557+
if not validate_proc_args(process, filename):
558+
retval = 1
559+
503560
if args.strict:
504561
if not validate_proc_type_conventions(process, filename):
505562
retval = 1

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.8.2",
10+
version="1.9.0",
1111
author="Elliot Jordan",
1212
author_email="elliot@elliotjordan.com",
1313
packages=["pre_commit_hooks"],

0 commit comments

Comments
 (0)