Skip to content

Commit d678f58

Browse files
committed
Move uninstall_method check to util module
1 parent 88cae6a commit d678f58

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

pre_commit_hooks/check_munki_pkgsinfo.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
validate_required_keys,
1515
validate_restart_action_key,
1616
validate_shebangs,
17+
validate_uninstall_method,
1718
)
1819

1920

@@ -39,7 +40,7 @@ def build_argument_parser():
3940
)
4041
parser.add_argument("filenames", nargs="*", help="Filenames to check.")
4142
parser.add_argument(
42-
"--munki-repo", default=".", help="path to local munki repo defaults to '.'"
43+
"--munki-repo", default=".", help="path to local munki repo. Defaults to '.'"
4344
)
4445
parser.add_argument(
4546
"--warn-on-missing-icons",
@@ -117,11 +118,15 @@ def main(argv=None):
117118
if not validate_restart_action_key(pkginfo, filename):
118119
retval = 1
119120

121+
# Validate uninstall method.
122+
if not validate_uninstall_method(pkginfo, filename):
123+
retval = 1
124+
120125
# Check for deprecated pkginfo keys.
121126
if not detect_deprecated_keys(pkginfo, filename):
122127
retval = 1
123128

124-
# Check for common mistakes key names.
129+
# Check for common mistakes in key names.
125130
if not detect_typoed_keys(pkginfo, filename):
126131
retval = 1
127132

@@ -194,21 +199,6 @@ def main(argv=None):
194199
print(f"{filename}: {msg}")
195200
retval = 1
196201

197-
# Ensure uninstall method is set correctly if uninstall_script exists.
198-
uninst_method = pkginfo.get("uninstall_method")
199-
if "uninstall_script" in pkginfo and uninst_method != "uninstall_script":
200-
print(
201-
f"{filename}: has an uninstall script, but the uninstall "
202-
f'method is set to "{uninst_method}"'
203-
)
204-
retval = 1
205-
elif "uninstall_script" not in pkginfo and uninst_method == "uninstall_script":
206-
print(
207-
f"{filename}: uninstall_method is set to uninstall_script, "
208-
'but no uninstall script is present"'
209-
)
210-
retval = 1
211-
212202
# Ensure all pkginfo scripts have a proper shebang.
213203
script_types = (
214204
"installcheck_script",

pre_commit_hooks/util.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def detect_typoed_keys(input_dict, filename):
141141

142142

143143
def validate_restart_action_key(pkginfo, filename):
144-
"""Verifies that required_keys are present in pkginfo dictionary."""
144+
"""Verifies that the RestartAction key is set correctly."""
145145
passed = True
146146
allowed_values = (
147147
"RequireShutdown",
@@ -159,6 +159,25 @@ def validate_restart_action_key(pkginfo, filename):
159159
return passed
160160

161161

162+
def validate_uninstall_method(pkginfo, filename):
163+
"""Verifies that uninstall_method and uninstall_script is used appropriately."""
164+
passed = True
165+
uninst_method = pkginfo.get("uninstall_method")
166+
if pkginfo.get("uninstall_script") and uninst_method != "uninstall_script":
167+
print(
168+
f"{filename}: has an uninstall script, but the uninstall "
169+
f'method is set to "{uninst_method}"'
170+
)
171+
passed = False
172+
elif not pkginfo.get("uninstall_script") and uninst_method == "uninstall_script":
173+
print(
174+
f"{filename}: uninstall_method is set to uninstall_script, "
175+
'but no uninstall script is present"'
176+
)
177+
passed = False
178+
return passed
179+
180+
162181
def validate_pkginfo_key_types(pkginfo, filename):
163182
"""Validation of pkginfo key types.
164183

0 commit comments

Comments
 (0)