blob: 8e3ed1ddea8487d1a0740ca2667119eb5b7c7f3f [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walsh7423c012016-10-04 10:27:21 -05002
George Keishing09679892022-12-08 08:21:52 -06003import os
George Keishinge635ddc2022-12-08 07:38:02 -06004import sys
George Keishing36efbc02018-12-12 10:18:23 -06005try:
6 import __builtin__
7except ImportError:
8 import builtins as __builtin__
9
Michael Walsh7423c012016-10-04 10:27:21 -050010
Michael Walsh410b1782019-10-22 15:56:18 -050011# python puts the program's directory path in sys.path[0]. In other words, the user ordinarily has no way
12# to override python's choice of a module from its own dir. We want to have that ability in our environment.
13# However, we don't want to break any established python modules that depend on this behavior. So, we'll
14# save the value from sys.path[0], delete it, import our modules and then restore sys.path to its original
15# value.
Michael Walsh7423c012016-10-04 10:27:21 -050016
17save_path_0 = sys.path[0]
18del sys.path[0]
19
George Keishing09679892022-12-08 08:21:52 -060020from gen_print import * # NOQA
21from gen_arg import * # NOQA
22from gen_plug_in import * # NOQA
George Keishing37c58c82022-12-08 07:42:54 -060023
Michael Walsh7423c012016-10-04 10:27:21 -050024# Restore sys.path[0].
25sys.path.insert(0, save_path_0)
26
27
Michael Walsh7423c012016-10-04 10:27:21 -050028# Create parser object to process command line parameters and args.
29
30# Create parser object.
31parser = argparse.ArgumentParser(
George Keishinge635ddc2022-12-08 07:38:02 -060032 usage='%(prog)s [OPTIONS] [PLUG_IN_DIR_PATHS]',
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050033 description="%(prog)s will validate the plug-in packages passed to it."
George Keishinge635ddc2022-12-08 07:38:02 -060034 + " It will also print a list of the absolute plug-in"
35 + " directory paths for use by the calling program.",
Michael Walshd0741f82017-12-21 14:04:21 -060036 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
George Keishinge635ddc2022-12-08 07:38:02 -060037 prefix_chars='-+')
Michael Walsh7423c012016-10-04 10:27:21 -050038
39# Create arguments.
40parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060041 'plug_in_dir_paths',
42 nargs='?',
Michael Walsh7423c012016-10-04 10:27:21 -050043 default="",
George Keishinge635ddc2022-12-08 07:38:02 -060044 help=plug_in_dir_paths_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050045
46parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060047 '--mch_class',
48 default="obmc",
49 help=mch_class_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050050
Michael Walsh410b1782019-10-22 15:56:18 -050051# The stock_list will be passed to gen_get_options. We populate it with the names of stock parm options we
52# want. These stock parms are pre-defined by gen_get_options.
Michael Walsh7423c012016-10-04 10:27:21 -050053stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)]
54
Michael Walsh7423c012016-10-04 10:27:21 -050055
George Keishinge635ddc2022-12-08 07:38:02 -060056def exit_function(signal_number=0,
57 frame=None):
Michael Walsh7423c012016-10-04 10:27:21 -050058 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050059 Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
Michael Walsh7423c012016-10-04 10:27:21 -050060 """
61
62 dprint_executing()
63 dprint_var(signal_number)
64
65 qprint_pgm_footer()
66
Michael Walsh7423c012016-10-04 10:27:21 -050067
Michael Walsh7423c012016-10-04 10:27:21 -050068def signal_handler(signal_number, frame):
Michael Walsh7423c012016-10-04 10:27:21 -050069 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050070 Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
71 with return code 143 and without calling our exit_function.
Michael Walsh7423c012016-10-04 10:27:21 -050072 """
73
Michael Walsh410b1782019-10-22 15:56:18 -050074 # Our convention is to set up exit_function with atexit.registr() so there is no need to explicitly call
75 # exit_function from here.
Michael Walsh7423c012016-10-04 10:27:21 -050076
77 dprint_executing()
78
Michael Walsh410b1782019-10-22 15:56:18 -050079 # Calling exit prevents us from returning to the code that was running when we received the signal.
Michael Walsh7423c012016-10-04 10:27:21 -050080 exit(0)
81
Michael Walsh7423c012016-10-04 10:27:21 -050082
Michael Walsh7423c012016-10-04 10:27:21 -050083def validate_parms():
Michael Walsh7423c012016-10-04 10:27:21 -050084 r"""
85 Validate program parameters, etc. Return True or False accordingly.
86 """
87
88 gen_post_validation(exit_function, signal_handler)
89
90 return True
91
Michael Walsh7423c012016-10-04 10:27:21 -050092
Michael Walsh7423c012016-10-04 10:27:21 -050093def main():
Michael Walsh7423c012016-10-04 10:27:21 -050094 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050095 This is the "main" function. The advantage of having this function vs just doing this in the true
96 mainline is that you can:
Michael Walsh7423c012016-10-04 10:27:21 -050097 - Declare local variables
98 - Use "return" instead of "exit".
99 - Indent 4 chars like you would in any function.
Michael Walsh410b1782019-10-22 15:56:18 -0500100 This makes coding more consistent, i.e. it's easy to move code from here into a function and vice versa.
Michael Walsh7423c012016-10-04 10:27:21 -0500101 """
102
103 if not gen_get_options(parser, stock_list):
104 return False
105
106 if not validate_parms():
107 return False
108
109 qprint_pgm_header()
110
111 # Access program parameter globals.
112 global plug_in_dir_paths
113 global mch_class
114
George Keishinge635ddc2022-12-08 07:38:02 -0600115 plug_in_packages_list = return_plug_in_packages_list(plug_in_dir_paths,
116 mch_class)
Michael Walshc2762f62019-05-17 15:21:35 -0500117 qprint_var(plug_in_packages_list)
Michael Walsh7423c012016-10-04 10:27:21 -0500118
Michael Walsh410b1782019-10-22 15:56:18 -0500119 # As stated in the help text, this program must print the full paths of each selected plug in.
Michael Walsh7423c012016-10-04 10:27:21 -0500120 for plug_in_dir_path in plug_in_packages_list:
121 print(plug_in_dir_path)
122
123 return True
124
Michael Walsh7423c012016-10-04 10:27:21 -0500125
Michael Walsh7423c012016-10-04 10:27:21 -0500126# Main
127
128if not main():
129 exit(1)