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