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