-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcheck_jamf_scripts.py
More file actions
56 lines (41 loc) · 1.65 KB
/
Copy pathcheck_jamf_scripts.py
File metadata and controls
56 lines (41 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
"""Check Jamf scripts for common issues."""
import argparse
from pre_commit_macadmin_hooks.util import validate_shebangs
def build_argument_parser() -> argparse.ArgumentParser:
"""Build and return the argument parser."""
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("filenames", nargs="*", help="Filenames to check.")
parser.add_argument(
"--valid-shebangs",
nargs="+",
default=[],
help="Add other valid shebangs for your environment",
)
return parser
def main(argv: list[str] | None = None) -> int:
"""Main process."""
# Parse command line arguments.
argparser = build_argument_parser()
args = argparser.parse_args(argv)
retval = 0
for filename in args.filenames:
with open(filename, encoding="utf-8") as openfile:
script_content = openfile.read()
# Ensure script starts with a shebang of some sort.
if not script_content.startswith("#!/"):
print(f"{filename}: missing shebang")
retval = 1
# Ensure we're not using env for root-context scripts.
if script_content.startswith("#!/usr/bin/env"):
print(f"{filename}: using env for root-context scripts is not recommended")
retval = 1
# Ensure all pkginfo scripts have a proper shebang.
if not validate_shebangs(script_content, filename, args.valid_shebangs):
print(f"{filename}: does not start with a valid shebang")
retval = 1
return retval
if __name__ == "__main__":
exit(main())