George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 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 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 19 | import sys |
| 20 | import os |
Sridevi Ramesh | 47375aa | 2022-12-08 05:05:31 -0600 | [diff] [blame] | 21 | |
| 22 | save_path_0 = sys.path[0] |
| 23 | del sys.path[0] |
| 24 | |
George Keishing | 0967989 | 2022-12-08 08:21:52 -0600 | [diff] [blame] | 25 | from gen_arg import * # NOQA |
| 26 | from gen_print import * # NOQA |
| 27 | from gen_valid import * # NOQA |
| 28 | from gen_misc import * # NOQA |
| 29 | from gen_cmd import * # NOQA |
George Keishing | 37c58c8 | 2022-12-08 07:42:54 -0600 | [diff] [blame] | 30 | |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 31 | # Restore sys.path[0]. |
| 32 | sys.path.insert(0, save_path_0) |
| 33 | |
| 34 | |
| 35 | parser = argparse.ArgumentParser( |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 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, |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 40 | prefix_chars='-+') |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 41 | |
| 42 | parser.add_argument( |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 43 | '--prop_dir_path', |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 44 | default=os.environ.get("PROP_DIR_PATH", os.getcwd()), |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 45 | help='The path to the directory that contains the properties file.' |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 46 | + ' The default value is environment variable "PROP_DIR_PATH", if' |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 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( |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 50 | '--prop_file_name', |
| 51 | help='The path to a properties file that contains the parameters to' |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 52 | + ' pass to the program. If the properties file has a ".properties"' |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 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' |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 56 | + ' positional parms, use a parm name of "pos". For example: pos=this' |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 57 | ' value') |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 58 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 59 | parser.add_argument( |
| 60 | 'program_name', |
| 61 | help='The name of the program to be run.') |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 62 | |
| 63 | # Populate stock_list with options we want. |
| 64 | stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)] |
| 65 | |
| 66 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 67 | def exit_function(signal_number=0, |
| 68 | frame=None): |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 69 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 70 | Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT). |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 71 | """ |
| 72 | |
| 73 | dprint_executing() |
| 74 | dprint_var(signal_number) |
| 75 | |
| 76 | qprint_pgm_footer() |
| 77 | |
| 78 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 79 | def signal_handler(signal_number, |
| 80 | frame): |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 81 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 82 | Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately |
| 83 | with return code 143 and without calling our exit_function. |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 84 | """ |
| 85 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 86 | # Our convention is to set up exit_function with atexit.register() so there is no need to explicitly |
| 87 | # call exit_function from here. |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 88 | |
| 89 | dprint_executing() |
| 90 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 91 | # Calling exit prevents us from returning to the code that was running when we received the signal. |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 92 | exit(0) |
| 93 | |
| 94 | |
| 95 | def validate_parms(): |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 96 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 97 | Validate program parameters, etc. Return True or False (i.e. pass/fail) accordingly. |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 98 | """ |
| 99 | |
| 100 | global prop_dir_path |
| 101 | global prop_file_path |
| 102 | |
| 103 | if not valid_dir_path(prop_dir_path): |
| 104 | return False |
| 105 | prop_dir_path = add_trailing_slash(prop_dir_path) |
| 106 | |
| 107 | if not valid_value(prop_file_name): |
| 108 | return False |
| 109 | |
| 110 | prop_file_path = prop_dir_path + prop_file_name |
| 111 | |
| 112 | # If properties file is not found, try adding ".properties" extension. |
| 113 | if not os.path.isfile(prop_file_path): |
| 114 | alt_prop_file_path = prop_file_path + ".properties" |
| 115 | if os.path.isfile(alt_prop_file_path): |
| 116 | prop_file_path = alt_prop_file_path |
| 117 | |
| 118 | if not valid_file_path(prop_file_path): |
| 119 | return False |
| 120 | |
| 121 | if not valid_value(program_name): |
| 122 | return False |
| 123 | |
| 124 | gen_post_validation(exit_function, signal_handler) |
| 125 | |
| 126 | return True |
| 127 | |
| 128 | |
| 129 | def main(): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 130 | |
Michael Walsh | 2179160 | 2018-03-23 16:45:00 -0500 | [diff] [blame] | 131 | if not gen_get_options(parser, stock_list): |
| 132 | return False |
| 133 | |
| 134 | if not validate_parms(): |
| 135 | return False |
| 136 | |
| 137 | qprint_pgm_header() |
| 138 | |
| 139 | # Get the parameters from the properties file. |
| 140 | properties = my_parm_file(prop_file_path) |
| 141 | # The parms (including program name) need to go into a list. |
| 142 | parms = [program_name] |
| 143 | for key, value in properties.items(): |
| 144 | if key == "pos": |
| 145 | # Process positional parm(s). |
| 146 | parms.extend(value.split()) |
| 147 | else: |
| 148 | parms.append("--" + key + "=" + escape_bash_quotes(value)) |
| 149 | |
| 150 | # parm_string is only created for display in non-quiet mode. |
| 151 | parm_string = " ".join(parms[1:]) |
| 152 | cmd_buf = program_name + " " + parm_string |
| 153 | qprint_issuing(cmd_buf) |
| 154 | if not test_mode: |
| 155 | os.execvp(program_name, parms) |
| 156 | |
| 157 | return True |
| 158 | |
| 159 | |
| 160 | # Main |
| 161 | |
| 162 | if not main(): |
| 163 | exit(1) |