Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | See help text for details (--help or -h option).. |
| 5 | |
| 6 | Example properties file content: |
| 7 | quiet=n |
| 8 | test_mode=y |
| 9 | pos=file1 file2 file3 |
| 10 | |
| 11 | Example call: |
| 12 | |
| 13 | prop_call.py --prop_file_name=prop_file my_program |
| 14 | |
| 15 | The result is that the following command will be run: |
| 16 | my_program --test_mode=y --quiet=n file1 file2 file3 |
| 17 | """ |
| 18 | |
| 19 | import sys |
| 20 | import os |
| 21 | |
| 22 | save_path_0 = sys.path[0] |
| 23 | del sys.path[0] |
| 24 | |
| 25 | from gen_arg import * |
| 26 | from gen_print import * |
| 27 | from gen_valid import * |
| 28 | from gen_misc import * |
| 29 | from gen_cmd import * |
| 30 | |
| 31 | # Restore sys.path[0]. |
| 32 | sys.path.insert(0, save_path_0) |
| 33 | |
| 34 | |
| 35 | parser = argparse.ArgumentParser( |
| 36 | usage='%(prog)s [OPTIONS]', |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 37 | description="%(prog)s will call a program using parameters retrieved" |
| 38 | + " from the given properties file.", |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 39 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 40 | prefix_chars='-+') |
| 41 | |
| 42 | parser.add_argument( |
| 43 | '--prop_dir_path', |
| 44 | default=os.environ.get("PROP_DIR_PATH", os.getcwd()), |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 45 | help='The path to the directory that contains the properties file.' |
| 46 | + ' The default value is environment variable "PROP_DIR_PATH", if' |
| 47 | + ' set. Otherwise, it is the current working directory.') |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 48 | |
| 49 | parser.add_argument( |
| 50 | '--prop_file_name', |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 51 | help='The path to a properties file that contains the parameters to' |
| 52 | + ' pass to the program. If the properties file has a ".properties"' |
| 53 | + ' extension, the caller need not specify the extension. The format' |
| 54 | + ' of each line in the properties file should be as follows:' |
| 55 | + ' <parm_name=parm_value>. Do not quote the parm value. To specify' |
| 56 | + ' positional parms, use a parm name of "pos". For example: pos=this' |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 57 | ' value') |
| 58 | |
| 59 | parser.add_argument( |
| 60 | 'program_name', |
| 61 | help='The name of the program to be run.') |
| 62 | |
| 63 | # Populate stock_list with options we want. |
| 64 | stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)] |
| 65 | |
| 66 | |
| 67 | def exit_function(signal_number=0, |
| 68 | frame=None): |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 69 | r""" |
| 70 | Execute whenever the program ends normally or with the signals that we |
| 71 | catch (i.e. TERM, INT). |
| 72 | """ |
| 73 | |
| 74 | dprint_executing() |
| 75 | dprint_var(signal_number) |
| 76 | |
| 77 | qprint_pgm_footer() |
| 78 | |
| 79 | |
| 80 | def signal_handler(signal_number, |
| 81 | frame): |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 82 | r""" |
| 83 | Handle signals. Without a function to catch a SIGTERM or SIGINT, our |
| 84 | program would terminate immediately with return code 143 and without |
| 85 | calling our exit_function. |
| 86 | """ |
| 87 | |
| 88 | # Our convention is to set up exit_function with atexit.register() so |
| 89 | # there is no need to explicitly call exit_function from here. |
| 90 | |
| 91 | dprint_executing() |
| 92 | |
| 93 | # Calling exit prevents us from returning to the code that was running |
| 94 | # when we received the signal. |
| 95 | exit(0) |
| 96 | |
| 97 | |
| 98 | def validate_parms(): |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 99 | r""" |
| 100 | Validate program parameters, etc. Return True or False (i.e. pass/fail) |
| 101 | accordingly. |
| 102 | """ |
| 103 | |
| 104 | global prop_dir_path |
| 105 | global prop_file_path |
| 106 | |
| 107 | if not valid_dir_path(prop_dir_path): |
| 108 | return False |
| 109 | prop_dir_path = add_trailing_slash(prop_dir_path) |
| 110 | |
| 111 | if not valid_value(prop_file_name): |
| 112 | return False |
| 113 | |
| 114 | prop_file_path = prop_dir_path + prop_file_name |
| 115 | |
| 116 | # If properties file is not found, try adding ".properties" extension. |
| 117 | if not os.path.isfile(prop_file_path): |
| 118 | alt_prop_file_path = prop_file_path + ".properties" |
| 119 | if os.path.isfile(alt_prop_file_path): |
| 120 | prop_file_path = alt_prop_file_path |
| 121 | |
| 122 | if not valid_file_path(prop_file_path): |
| 123 | return False |
| 124 | |
| 125 | if not valid_value(program_name): |
| 126 | return False |
| 127 | |
| 128 | gen_post_validation(exit_function, signal_handler) |
| 129 | |
| 130 | return True |
| 131 | |
| 132 | |
| 133 | def main(): |
| 134 | |
| 135 | if not gen_get_options(parser, stock_list): |
| 136 | return False |
| 137 | |
| 138 | if not validate_parms(): |
| 139 | return False |
| 140 | |
| 141 | qprint_pgm_header() |
| 142 | |
| 143 | # Get the parameters from the properties file. |
| 144 | properties = my_parm_file(prop_file_path) |
| 145 | # The parms (including program name) need to go into a list. |
| 146 | parms = [program_name] |
| 147 | for key, value in properties.items(): |
| 148 | if key == "pos": |
| 149 | # Process positional parm(s). |
| 150 | parms.extend(value.split()) |
| 151 | else: |
| 152 | parms.append("--" + key + "=" + escape_bash_quotes(value)) |
| 153 | |
| 154 | # parm_string is only created for display in non-quiet mode. |
| 155 | parm_string = " ".join(parms[1:]) |
| 156 | cmd_buf = program_name + " " + parm_string |
| 157 | qprint_issuing(cmd_buf) |
| 158 | if not test_mode: |
| 159 | os.execvp(program_name, parms) |
| 160 | |
| 161 | return True |
| 162 | |
| 163 | |
| 164 | # Main |
| 165 | |
| 166 | if not main(): |
| 167 | exit(1) |