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 | |
| 3 | import sys |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 4 | try: |
| 5 | import __builtin__ |
| 6 | except ImportError: |
| 7 | import builtins as __builtin__ |
| 8 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 9 | import os |
| 10 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 11 | # 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 Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 16 | |
| 17 | save_path_0 = sys.path[0] |
| 18 | del sys.path[0] |
| 19 | |
| 20 | from gen_print import * |
| 21 | from gen_arg import * |
| 22 | from gen_plug_in import * |
| 23 | |
| 24 | # Restore sys.path[0]. |
| 25 | sys.path.insert(0, save_path_0) |
| 26 | |
| 27 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 28 | # Create parser object to process command line parameters and args. |
| 29 | |
| 30 | # Create parser object. |
| 31 | parser = argparse.ArgumentParser( |
| 32 | usage='%(prog)s [OPTIONS] [PLUG_IN_DIR_PATHS]', |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 33 | description="%(prog)s will validate the plug-in packages passed to it." |
| 34 | + " It will also print a list of the absolute plug-in" |
| 35 | + " directory paths for use by the calling program.", |
Michael Walsh | d0741f8 | 2017-12-21 14:04:21 -0600 | [diff] [blame] | 36 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
Michael Walsh | d690150 | 2017-11-14 12:58:37 -0600 | [diff] [blame] | 37 | prefix_chars='-+') |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 38 | |
| 39 | # Create arguments. |
| 40 | parser.add_argument( |
| 41 | 'plug_in_dir_paths', |
| 42 | nargs='?', |
| 43 | default="", |
Michael Walsh | d690150 | 2017-11-14 12:58:37 -0600 | [diff] [blame] | 44 | help=plug_in_dir_paths_help_text + default_string) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 45 | |
| 46 | parser.add_argument( |
| 47 | '--mch_class', |
| 48 | default="obmc", |
Michael Walsh | d690150 | 2017-11-14 12:58:37 -0600 | [diff] [blame] | 49 | help=mch_class_help_text + default_string) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 50 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 51 | # 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 Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 53 | stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)] |
| 54 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 55 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 56 | def exit_function(signal_number=0, |
| 57 | frame=None): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 58 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 59 | 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] | 60 | """ |
| 61 | |
| 62 | dprint_executing() |
| 63 | dprint_var(signal_number) |
| 64 | |
| 65 | qprint_pgm_footer() |
| 66 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 67 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 68 | def signal_handler(signal_number, frame): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 69 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 70 | 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 Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 72 | """ |
| 73 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 74 | # 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 Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 76 | |
| 77 | dprint_executing() |
| 78 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 79 | # 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] | 80 | exit(0) |
| 81 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 82 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 83 | def validate_parms(): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 84 | 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 Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 92 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 93 | def main(): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 94 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 95 | 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 Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 97 | - Declare local variables |
| 98 | - Use "return" instead of "exit". |
| 99 | - Indent 4 chars like you would in any function. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 100 | 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] | 101 | """ |
| 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 | |
| 115 | plug_in_packages_list = return_plug_in_packages_list(plug_in_dir_paths, |
| 116 | mch_class) |
Michael Walsh | c2762f6 | 2019-05-17 15:21:35 -0500 | [diff] [blame] | 117 | qprint_var(plug_in_packages_list) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 118 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 119 | # 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] | 120 | for plug_in_dir_path in plug_in_packages_list: |
| 121 | print(plug_in_dir_path) |
| 122 | |
| 123 | return True |
| 124 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 125 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 126 | # Main |
| 127 | |
| 128 | if not main(): |
| 129 | exit(1) |