Modernize auto_status_file, etc.
Modernize auto_status_file.py and process_plug_in_packages.py to utilize
innovations illustrated in python_pgm_template.
Change-Id: I9367d930c2ee49a53da5490f53378582f8be4776
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/bin/auto_status_file.py b/bin/auto_status_file.py
index 5147b9d..774903a 100755
--- a/bin/auto_status_file.py
+++ b/bin/auto_status_file.py
@@ -8,21 +8,13 @@
import subprocess
import re
-save_path_0 = sys.path[0]
-del sys.path[0]
+save_dir_path = sys.path.pop(0)
-from gen_arg import *
-from gen_print import *
-from gen_valid import *
-from gen_misc import *
-from gen_cmd import *
-from var_funcs import *
+modules = ['gen_arg', 'gen_print', 'gen_valid', 'gen_misc', 'gen_cmd', 'var_funcs']
+for module in modules:
+ exec("from " + module + " import *")
-# Restore sys.path[0].
-sys.path.insert(0, save_path_0)
-
-# Set exit_on_error for gen_valid functions.
-set_exit_on_error(True)
+sys.path.insert(0, save_dir_path)
parser = argparse.ArgumentParser(
usage='%(prog)s [OPTIONS]',
@@ -97,34 +89,6 @@
stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)]
-def exit_function(signal_number=0,
- frame=None):
- r"""
- Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
- """
-
- dprint_executing()
- dprint_var(signal_number)
-
- qprint_pgm_footer()
-
-
-def signal_handler(signal_number,
- frame):
- r"""
- Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
- with return code 143 and without calling our exit_function.
- """
-
- # Our convention is to set up exit_function with atexit.register() so there is no need to explicitly
- # call exit_function from here.
-
- dprint_executing()
-
- # Calling exit prevents us from returning to the code that was running when we received the signal.
- exit(0)
-
-
def validate_parms():
r"""
Validate program parameters, etc.
@@ -166,8 +130,6 @@
# Set deprecated but still used AUTOSCRIPT_STATUS_FILE_PATH value.
os.environ['AUTOSCRIPT_STATUS_FILE_PATH'] = status_file_path
- gen_post_validation(exit_function, signal_handler)
-
def script_func(command_string, status_file_path):
r"""
@@ -226,11 +188,7 @@
def main():
- gen_get_options(parser, stock_list)
-
- validate_parms()
-
- qprint_pgm_header()
+ gen_setup()
global ret_code_str
ret_code_str = re.sub("\\.py$", "", pgm_name) + "_ret_code"
diff --git a/bin/process_plug_in_packages.py b/bin/process_plug_in_packages.py
index 9941892..d0e22f3 100755
--- a/bin/process_plug_in_packages.py
+++ b/bin/process_plug_in_packages.py
@@ -1,34 +1,20 @@
#!/usr/bin/env python
+r"""
+See help text for details.
+"""
+
import sys
-try:
- import __builtin__
-except ImportError:
- import builtins as __builtin__
import subprocess
import os
-import argparse
-# python puts the program's directory path in sys.path[0]. In other words, the user ordinarily has no way
-# to override python's choice of a module from its own dir. We want to have that ability in our environment.
-# However, we don't want to break any established python modules that depend on this behavior. So, we'll
-# save the value from sys.path[0], delete it, import our modules and then restore sys.path to its original
-# value.
+save_dir_path = sys.path.pop(0)
-save_path_0 = sys.path[0]
-del sys.path[0]
+modules = ['gen_arg', 'gen_print', 'gen_valid', 'gen_plug_in', 'gen_cmd', 'gen_misc']
+for module in modules:
+ exec("from " + module + " import *")
-from gen_print import *
-from gen_valid import *
-from gen_arg import *
-from gen_plug_in import *
-from gen_cmd import *
-from gen_misc import *
-
-# Restore sys.path[0].
-sys.path.insert(0, save_path_0)
-
-# Create parser object to process command line parameters and args.
+sys.path.insert(0, save_dir_path)
# Create parser object.
parser = argparse.ArgumentParser(
@@ -120,58 +106,24 @@
default="obmc",
help=mch_class_help_text + default_string)
-# The stock_list will be passed to gen_get_options. We populate it with the names of stock parm options we
-# want. These stock parms are pre-defined by gen_get_options.
+# Populate stock_list with options we want.
stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)]
-def exit_function(signal_number=0,
- frame=None):
- r"""
- Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
- """
-
- dprint_executing()
- dprint_var(signal_number)
-
- qprint_pgm_footer()
-
-
-def signal_handler(signal_number, frame):
- r"""
- Handle signals. Without a function to catch a SIGTERM or SIGINT, our program would terminate immediately
- with return code 143 and without calling our exit_function.
- """
-
- # Our convention is to set up exit_function with atexit.registr() so there is no need to explicitly call
- # exit_function from here.
-
- dprint_executing()
-
- # Calling exit prevents us from returning to the code that was running when we received the signal.
- exit(0)
-
-
def validate_parms():
r"""
Validate program parameters, etc. Return True or False accordingly.
"""
- if not valid_value(call_point):
- return False
+ valid_value(call_point)
global allow_shell_rc
- if not valid_integer(allow_shell_rc):
- return False
+ valid_integer(allow_shell_rc)
# Convert to hex string for consistency in printout.
allow_shell_rc = "0x%08x" % int(allow_shell_rc, 0)
set_pgm_arg(allow_shell_rc)
- gen_post_validation(exit_function, signal_handler)
-
- return True
-
def run_pgm(plug_in_dir_path,
call_point,
@@ -262,22 +214,8 @@
def main():
- r"""
- This is the "main" function. The advantage of having this function vs just doing this in the true
- mainline is that you can:
- - Declare local variables
- - Use "return" instead of "exit".
- - Indent 4 chars like you would in any function.
- This makes coding more consistent, i.e. it's easy to move code from here into a function and vice versa.
- """
- if not gen_get_options(parser, stock_list):
- return False
-
- if not validate_parms():
- return False
-
- qprint_pgm_header()
+ gen_setup()
# Access program parameter globals.
global plug_in_dir_paths
@@ -312,14 +250,9 @@
+ " by caller.\n")
break
- if ret_code == 0:
- return True
- else:
+ if ret_code != 0:
print_error("At least one plug-in failed.\n")
- return False
+ exit(1)
-# Main
-
-if not main():
- exit(1)
+main()