George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 2 | |
George Keishing | 0967989 | 2022-12-08 08:21:52 -0600 | [diff] [blame] | 3 | import os |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 4 | import sys |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 5 | |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 6 | try: |
| 7 | import __builtin__ |
| 8 | except ImportError: |
| 9 | import builtins as __builtin__ |
| 10 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 11 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 12 | # python puts the program's directory path in sys.path[0]. In other words, the user ordinarily has no way |
| 13 | # to override python's choice of a module from its own dir. We want to have that ability in our environment. |
| 14 | # However, we don't want to break any established python modules that depend on this behavior. So, we'll |
| 15 | # save the value from sys.path[0], delete it, import our modules and then restore sys.path to its original |
| 16 | # value. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 17 | |
| 18 | save_path_0 = sys.path[0] |
| 19 | del sys.path[0] |
| 20 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 21 | from gen_arg import * # NOQA |
| 22 | from gen_plug_in import * # NOQA |
| 23 | from gen_print import * # NOQA |
George Keishing | 37c58c8 | 2022-12-08 07:42:54 -0600 | [diff] [blame] | 24 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 25 | # Restore sys.path[0]. |
| 26 | sys.path.insert(0, save_path_0) |
| 27 | |
| 28 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 29 | # Create parser object to process command line parameters and args. |
| 30 | |
| 31 | # Create parser object. |
| 32 | parser = argparse.ArgumentParser( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 33 | usage="%(prog)s [OPTIONS] [PLUG_IN_DIR_PATHS]", |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 34 | description="%(prog)s will validate the plug-in packages passed to it." |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 35 | + " It will also print a list of the absolute plug-in" |
| 36 | + " directory paths for use by the calling program.", |
Michael Walsh | d0741f8 | 2017-12-21 14:04:21 -0600 | [diff] [blame] | 37 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 38 | prefix_chars="-+", |
| 39 | ) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 40 | |
| 41 | # Create arguments. |
| 42 | parser.add_argument( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 43 | "plug_in_dir_paths", |
| 44 | nargs="?", |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 45 | default="", |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 46 | help=plug_in_dir_paths_help_text + default_string, |
| 47 | ) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 48 | |
| 49 | parser.add_argument( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 50 | "--mch_class", default="obmc", help=mch_class_help_text + default_string |
| 51 | ) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 52 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 53 | # The stock_list will be passed to gen_get_options. We populate it with the names of stock parm options we |
| 54 | # want. These stock parms are pre-defined by gen_get_options. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 55 | stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)] |
| 56 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 57 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 58 | def exit_function(signal_number=0, frame=None): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 59 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 60 | Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT). |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 61 | """ |
| 62 | |
| 63 | dprint_executing() |
| 64 | dprint_var(signal_number) |
| 65 | |
| 66 | qprint_pgm_footer() |
| 67 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 68 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 69 | def signal_handler(signal_number, frame): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 70 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 71 | Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately |
| 72 | with return code 143 and without calling our exit_function. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 73 | """ |
| 74 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 75 | # Our convention is to set up exit_function with atexit.registr() so there is no need to explicitly call |
| 76 | # exit_function from here. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 77 | |
| 78 | dprint_executing() |
| 79 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 80 | # Calling exit prevents us from returning to the code that was running when we received the signal. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 81 | exit(0) |
| 82 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 83 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 84 | def validate_parms(): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 85 | r""" |
| 86 | Validate program parameters, etc. Return True or False accordingly. |
| 87 | """ |
| 88 | |
| 89 | gen_post_validation(exit_function, signal_handler) |
| 90 | |
| 91 | return True |
| 92 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 93 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 94 | def main(): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 95 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 96 | This is the "main" function. The advantage of having this function vs just doing this in the true |
| 97 | mainline is that you can: |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 98 | - Declare local variables |
| 99 | - Use "return" instead of "exit". |
| 100 | - Indent 4 chars like you would in any function. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 101 | This makes coding more consistent, i.e. it's easy to move code from here into a function and vice versa. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 102 | """ |
| 103 | |
| 104 | if not gen_get_options(parser, stock_list): |
| 105 | return False |
| 106 | |
| 107 | if not validate_parms(): |
| 108 | return False |
| 109 | |
| 110 | qprint_pgm_header() |
| 111 | |
| 112 | # Access program parameter globals. |
| 113 | global plug_in_dir_paths |
| 114 | global mch_class |
| 115 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 116 | plug_in_packages_list = return_plug_in_packages_list( |
| 117 | plug_in_dir_paths, mch_class |
| 118 | ) |
Michael Walsh | c2762f6 | 2019-05-17 15:21:35 -0500 | [diff] [blame] | 119 | qprint_var(plug_in_packages_list) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 120 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 121 | # As stated in the help text, this program must print the full paths of each selected plug in. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 122 | for plug_in_dir_path in plug_in_packages_list: |
| 123 | print(plug_in_dir_path) |
| 124 | |
| 125 | return True |
| 126 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 127 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 128 | # Main |
| 129 | |
| 130 | if not main(): |
| 131 | exit(1) |