xref: /openbmc/openbmc-test-automation/bin/validate_plug_ins.py (revision 20f38712b324e61a94e174017c487a0af4b373e1)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
27423c01aSMichael Walsh
309679890SGeorge Keishingimport os
4e635ddc0SGeorge Keishingimport sys
5*20f38712SPatrick Williams
636efbc04SGeorge Keishingtry:
77423c01aSMichael Walsh    import __builtin__
836efbc04SGeorge Keishingexcept ImportError:
936efbc04SGeorge Keishing    import builtins as __builtin__
1036efbc04SGeorge Keishing
117423c01aSMichael Walsh
12410b1787SMichael Walsh# python puts the program's directory path in sys.path[0].  In other words, the user ordinarily has no way
13410b1787SMichael Walsh# to override python's choice of a module from its own dir.  We want to have that ability in our environment.
14410b1787SMichael Walsh# However, we don't want to break any established python modules that depend on this behavior.  So, we'll
15410b1787SMichael Walsh# save the value from sys.path[0], delete it, import our modules and then restore sys.path to its original
16410b1787SMichael Walsh# value.
177423c01aSMichael Walsh
187423c01aSMichael Walshsave_path_0 = sys.path[0]
197423c01aSMichael Walshdel sys.path[0]
207423c01aSMichael Walsh
2109679890SGeorge Keishingfrom gen_arg import *  # NOQA
2209679890SGeorge Keishingfrom gen_plug_in import *  # NOQA
23*20f38712SPatrick Williamsfrom gen_print import *  # NOQA
2437c58c8cSGeorge Keishing
257423c01aSMichael Walsh# Restore sys.path[0].
267423c01aSMichael Walshsys.path.insert(0, save_path_0)
277423c01aSMichael Walsh
287423c01aSMichael Walsh
297423c01aSMichael Walsh# Create parser object to process command line parameters and args.
307423c01aSMichael Walsh
317423c01aSMichael Walsh# Create parser object.
327423c01aSMichael Walshparser = argparse.ArgumentParser(
33*20f38712SPatrick Williams    usage="%(prog)s [OPTIONS] [PLUG_IN_DIR_PATHS]",
34004ad3c9SJoy Onyerikwu    description="%(prog)s will validate the plug-in packages passed to it."
35004ad3c9SJoy Onyerikwu    + "  It will also print a list of the absolute plug-in"
36004ad3c9SJoy Onyerikwu    + " directory paths for use by the calling program.",
37d0741f8aSMichael Walsh    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
38*20f38712SPatrick Williams    prefix_chars="-+",
39*20f38712SPatrick Williams)
407423c01aSMichael Walsh
417423c01aSMichael Walsh# Create arguments.
427423c01aSMichael Walshparser.add_argument(
43*20f38712SPatrick Williams    "plug_in_dir_paths",
44*20f38712SPatrick Williams    nargs="?",
457423c01aSMichael Walsh    default="",
46*20f38712SPatrick Williams    help=plug_in_dir_paths_help_text + default_string,
47*20f38712SPatrick Williams)
487423c01aSMichael Walsh
497423c01aSMichael Walshparser.add_argument(
50*20f38712SPatrick Williams    "--mch_class", default="obmc", help=mch_class_help_text + default_string
51*20f38712SPatrick Williams)
527423c01aSMichael Walsh
53410b1787SMichael Walsh# The stock_list will be passed to gen_get_options.  We populate it with the names of stock parm options we
54410b1787SMichael Walsh# want.  These stock parms are pre-defined by gen_get_options.
557423c01aSMichael Walshstock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)]
567423c01aSMichael Walsh
577423c01aSMichael Walsh
58*20f38712SPatrick Williamsdef exit_function(signal_number=0, frame=None):
597423c01aSMichael Walsh    r"""
60410b1787SMichael Walsh    Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
617423c01aSMichael Walsh    """
627423c01aSMichael Walsh
637423c01aSMichael Walsh    dprint_executing()
647423c01aSMichael Walsh    dprint_var(signal_number)
657423c01aSMichael Walsh
667423c01aSMichael Walsh    qprint_pgm_footer()
677423c01aSMichael Walsh
687423c01aSMichael Walsh
697423c01aSMichael Walshdef signal_handler(signal_number, frame):
707423c01aSMichael Walsh    r"""
71410b1787SMichael Walsh    Handle signals.  Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
72410b1787SMichael Walsh    with return code 143 and without calling our exit_function.
737423c01aSMichael Walsh    """
747423c01aSMichael Walsh
75410b1787SMichael Walsh    # Our convention is to set up exit_function with atexit.registr() so there is no need to explicitly call
76410b1787SMichael Walsh    # exit_function from here.
777423c01aSMichael Walsh
787423c01aSMichael Walsh    dprint_executing()
797423c01aSMichael Walsh
80410b1787SMichael Walsh    # Calling exit prevents us from returning to the code that was running when we received the signal.
817423c01aSMichael Walsh    exit(0)
827423c01aSMichael Walsh
837423c01aSMichael Walsh
847423c01aSMichael Walshdef validate_parms():
857423c01aSMichael Walsh    r"""
867423c01aSMichael Walsh    Validate program parameters, etc.  Return True or False accordingly.
877423c01aSMichael Walsh    """
887423c01aSMichael Walsh
897423c01aSMichael Walsh    gen_post_validation(exit_function, signal_handler)
907423c01aSMichael Walsh
917423c01aSMichael Walsh    return True
927423c01aSMichael Walsh
937423c01aSMichael Walsh
947423c01aSMichael Walshdef main():
957423c01aSMichael Walsh    r"""
96410b1787SMichael Walsh    This is the "main" function.  The advantage of having this function vs just doing this in the true
97410b1787SMichael Walsh    mainline is that you can:
987423c01aSMichael Walsh    - Declare local variables
997423c01aSMichael Walsh    - Use "return" instead of "exit".
1007423c01aSMichael Walsh    - Indent 4 chars like you would in any function.
101410b1787SMichael Walsh    This makes coding more consistent, i.e. it's easy to move code from here into a function and vice versa.
1027423c01aSMichael Walsh    """
1037423c01aSMichael Walsh
1047423c01aSMichael Walsh    if not gen_get_options(parser, stock_list):
1057423c01aSMichael Walsh        return False
1067423c01aSMichael Walsh
1077423c01aSMichael Walsh    if not validate_parms():
1087423c01aSMichael Walsh        return False
1097423c01aSMichael Walsh
1107423c01aSMichael Walsh    qprint_pgm_header()
1117423c01aSMichael Walsh
1127423c01aSMichael Walsh    # Access program parameter globals.
1137423c01aSMichael Walsh    global plug_in_dir_paths
1147423c01aSMichael Walsh    global mch_class
1157423c01aSMichael Walsh
116*20f38712SPatrick Williams    plug_in_packages_list = return_plug_in_packages_list(
117*20f38712SPatrick Williams        plug_in_dir_paths, mch_class
118*20f38712SPatrick Williams    )
119c2762f62SMichael Walsh    qprint_var(plug_in_packages_list)
1207423c01aSMichael Walsh
121410b1787SMichael Walsh    # As stated in the help text, this program must print the full paths of each selected plug in.
1227423c01aSMichael Walsh    for plug_in_dir_path in plug_in_packages_list:
1237423c01aSMichael Walsh        print(plug_in_dir_path)
1247423c01aSMichael Walsh
1257423c01aSMichael Walsh    return True
1267423c01aSMichael Walsh
1277423c01aSMichael Walsh
1287423c01aSMichael Walsh# Main
1297423c01aSMichael Walsh
1307423c01aSMichael Walshif not main():
1317423c01aSMichael Walsh    exit(1)
132