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