44requirements."""
55
66import argparse
7+ import json
78import os
89import plistlib
910import sys
1011from contextlib import contextmanager
1112from distutils .version import LooseVersion
1213from xml .parsers .expat import ExpatError
1314
15+ from ruamel import yaml
16+
1417from pre_commit_hooks .util import (
1518 validate_pkginfo_key_types ,
1619 validate_required_keys ,
@@ -280,6 +283,30 @@ def validate_no_superclass_procs(process, filename):
280283 return passed
281284
282285
286+ # def validate_unused_input_vars(recipe, recipe_text, filename):
287+ # """Warn if any input variables are not referenced in the recipe."""
288+
289+ # # List of variables that are commonly allowed to be unreferenced (lowercase).
290+ # ignored_vars = (
291+ # "name",
292+ # "pkginfo",
293+ # )
294+
295+ # passed = True
296+ # for input_var, _ in recipe.get("Input", {}).items():
297+ # if input_var.lower() in ignored_vars:
298+ # continue
299+ # subst = "%" + input_var + "%"
300+ # if subst not in recipe_text:
301+ # print(
302+ # "{}: WARNING: Input variable {} not referenced in recipe.".format(
303+ # filename, input_var
304+ # )
305+ # )
306+
307+ # return passed
308+
309+
283310def validate_no_var_in_app_path (process , filename ):
284311 """Ensure %NAME% is not used in app paths that should be hard coded."""
285312
@@ -458,14 +485,40 @@ def main(argv=None):
458485
459486 retval = 0
460487 for filename in args .filenames :
461- try :
462- with open (filename , "rb" ) as openfile :
463- recipe = plistlib .load (openfile )
464488
465- except (ExpatError , ValueError ) as err :
466- print ("{}: plist parsing error: {}" .format (filename , err ))
467- retval = 1
468- break # No need to continue checking this file
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
518+
519+ # For future implementation of validate_unused_input_vars()
520+ # with open(filename, "r") as openfile:
521+ # recipe_text = openfile.read()
469522
470523 # Top level keys that all AutoPkg recipes should contain.
471524 required_keys = ["Identifier" ]
@@ -475,7 +528,11 @@ def main(argv=None):
475528
476529 # Ensure the recipe identifier isn't duplicated.
477530 if recipe ["Identifier" ] in seen_identifiers :
478- print ('{}: Identifier "{}" is shared by another recipe in this repo.' )
531+ print (
532+ '{}: Identifier "{}" is shared by another recipe in this repo.' .format (
533+ filename , recipe ["Identifier" ]
534+ )
535+ )
479536 retval = 1
480537 else :
481538 seen_identifiers .append (recipe ["Identifier" ])
@@ -494,6 +551,13 @@ def main(argv=None):
494551 )
495552 retval = 1
496553
554+ # Validate that all input variables are used.
555+ # (Disabled for now because it's a little too opinionated, and doesn't take into account
556+ # whether environmental variables are used in custom processors.)
557+ # if args.strict:
558+ # if not validate_unused_input_vars(recipe, recipe_text, filename):
559+ # retval = 1
560+
497561 # If the Input key contains a pkginfo dict, make a best effort to validate its contents.
498562 input_key = recipe .get ("Input" , recipe .get ("input" , recipe .get ("INPUT" )))
499563 if input_key and "pkginfo" in input_key :
0 commit comments