blob: 9c0a1fda6b827cae8187e79c2659fae09ac0c6ba [file] [log] [blame]
Michael Walsh7423c012016-10-04 10:27:21 -05001#!/usr/bin/env python
2
3import sys
4import __builtin__
5import 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
14save_path_0 = sys.path[0]
15del sys.path[0]
16
17from gen_print import *
18from gen_arg import *
19from gen_plug_in import *
20
21# Restore sys.path[0].
22sys.path.insert(0, save_path_0)
23
24
Michael Walsh7423c012016-10-04 10:27:21 -050025# Create parser object to process command line parameters and args.
26
27# Create parser object.
28parser = argparse.ArgumentParser(
29 usage='%(prog)s [OPTIONS] [PLUG_IN_DIR_PATHS]',
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050030 description="%(prog)s will validate the plug-in packages passed to it."
31 + " It will also print a list of the absolute plug-in"
32 + " directory paths for use by the calling program.",
Michael Walshd0741f82017-12-21 14:04:21 -060033 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Michael Walshd6901502017-11-14 12:58:37 -060034 prefix_chars='-+')
Michael Walsh7423c012016-10-04 10:27:21 -050035
36# Create arguments.
37parser.add_argument(
38 'plug_in_dir_paths',
39 nargs='?',
40 default="",
Michael Walshd6901502017-11-14 12:58:37 -060041 help=plug_in_dir_paths_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050042
43parser.add_argument(
44 '--mch_class',
45 default="obmc",
Michael Walshd6901502017-11-14 12:58:37 -060046 help=mch_class_help_text + default_string)
Michael Walsh7423c012016-10-04 10:27:21 -050047
48# The stock_list will be passed to gen_get_options. We populate it with the
49# names of stock parm options we want. These stock parms are pre-defined by
50# gen_get_options.
51stock_list = [("test_mode", 0), ("quiet", 1), ("debug", 0)]
52
Michael Walsh7423c012016-10-04 10:27:21 -050053
Michael Walsh7423c012016-10-04 10:27:21 -050054def exit_function(signal_number=0,
55 frame=None):
Michael Walsh7423c012016-10-04 10:27:21 -050056 r"""
57 Execute whenever the program ends normally or with the signals that we
58 catch (i.e. TERM, INT).
59 """
60
61 dprint_executing()
62 dprint_var(signal_number)
63
64 qprint_pgm_footer()
65
Michael Walsh7423c012016-10-04 10:27:21 -050066
Michael Walsh7423c012016-10-04 10:27:21 -050067def signal_handler(signal_number, frame):
Michael Walsh7423c012016-10-04 10:27:21 -050068 r"""
69 Handle signals. Without a function to catch a SIGTERM or SIGINT, our
70 program would terminate immediately with return code 143 and without
71 calling our exit_function.
72 """
73
74 # Our convention is to set up exit_function with atexit.registr() so
75 # there is no need to explicitly call exit_function from here.
76
77 dprint_executing()
78
79 # Calling exit prevents us from returning to the code that was running
80 # when we received the signal.
81 exit(0)
82
Michael Walsh7423c012016-10-04 10:27:21 -050083
Michael Walsh7423c012016-10-04 10:27:21 -050084def validate_parms():
Michael Walsh7423c012016-10-04 10:27:21 -050085 r"""
86 Validate program parameters, etc. Return True or False accordingly.
87 """
88
89 gen_post_validation(exit_function, signal_handler)
90
91 return True
92
Michael Walsh7423c012016-10-04 10:27:21 -050093
Michael Walsh7423c012016-10-04 10:27:21 -050094def main():
Michael Walsh7423c012016-10-04 10:27:21 -050095 r"""
96 This is the "main" function. The advantage of having this function vs
97 just doing this in the true mainline is that you can:
98 - Declare local variables
99 - Use "return" instead of "exit".
100 - Indent 4 chars like you would in any function.
101 This makes coding more consistent, i.e. it's easy to move code from here
102 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 qpvar(plug_in_packages_list)
120
121 # As stated in the help text, this program must print the full paths of
122 # each selected plug in.
123 for plug_in_dir_path in plug_in_packages_list:
124 print(plug_in_dir_path)
125
126 return True
127
Michael Walsh7423c012016-10-04 10:27:21 -0500128
Michael Walsh7423c012016-10-04 10:27:21 -0500129# Main
130
131if not main():
132 exit(1)