blob: a01e25157ced7da4ca0e360c35f80e446e755232 [file] [log] [blame]
Michael Walsh7423c012016-10-04 10:27:21 -05001#!/usr/bin/env python
2
3r"""
4This module provides functions which are useful for running plug-ins.
5"""
6
7import sys
8import os
9import commands
10import glob
11
12import gen_print as gp
13import gen_misc as gm
14
15# Some help text that is common to more than one program.
16plug_in_dir_paths_help_text = \
17 'This is a colon-separated list of plug-in directory paths. If one' +\
18 ' of the entries in the list is a plain directory name (i.e. no' +\
19 ' path info), it will be taken to be a native plug-in. In that case,' +\
20 ' %(prog)s will search for the native plug-in in the "plug-ins"' +\
21 ' subdirectory of each path in the PATH environment variable until it' +\
22 ' is found. Also, integrated plug-ins will automatically be appended' +\
23 ' to your plug_in_dir_paths list. An integrated plug-in is any plug-in' +\
24 ' found using the PATH variable that contains a file named "integrated".'
25
26mch_class_help_text = \
27 'The class of machine that we are testing (e.g. "op" = "open power",' +\
28 ' "obmc" = "open bmc", etc).'
29
30PATH_LIST = gm.return_path_list()
31
32
Michael Walsh7423c012016-10-04 10:27:21 -050033def get_plug_in_base_paths():
34
35 r"""
36 Get plug-in base paths and return them as a list.
37
38 This function searches the PATH_LIST (created from PATH environment
39 variable) for any paths that have a "plug_ins" subdirectory. All such
40 paths are considered plug_in_base paths.
41 """
42
43 global PATH_LIST
44
45 plug_in_base_path_list = []
46
47 for path in PATH_LIST:
48 candidate_plug_in_base_path = path + "plug_ins/"
49 if os.path.isdir(candidate_plug_in_base_path):
50 plug_in_base_path_list.append(candidate_plug_in_base_path)
51
52 return plug_in_base_path_list
53
Michael Walsh7423c012016-10-04 10:27:21 -050054# Define global plug_in_base_path_list and call get_plug_in_base_paths to set
55# its value.
56plug_in_base_path_list = get_plug_in_base_paths()
57
58
Michael Walsh7423c012016-10-04 10:27:21 -050059def find_plug_in_package(plug_in_name):
60
61 r"""
62 Find and return the normalized directory path of the specified plug in.
63 This is done by searching the global plug_in_base_path_list.
64
65 Description of arguments:
66 plug_in_name The unqualified name of the plug-in
67 package.
68 """
69
70 global plug_in_base_path_list
71 for plug_in_base_dir_path in plug_in_base_path_list:
72 candidate_plug_in_dir_path = os.path.normpath(plug_in_base_dir_path +
73 plug_in_name) + \
Michael Walsh6cd9cbe2017-06-30 16:59:36 -050074 os.sep
Michael Walsh7423c012016-10-04 10:27:21 -050075 if os.path.isdir(candidate_plug_in_dir_path):
76 return candidate_plug_in_dir_path
77
78 return ""
79
Michael Walsh7423c012016-10-04 10:27:21 -050080
Michael Walsh7423c012016-10-04 10:27:21 -050081def validate_plug_in_package(plug_in_dir_path,
82 mch_class="obmc"):
83
84 r"""
85 Validate the plug in package and return the normalized plug-in directory
86 path.
87
88 Description of arguments:
89 plug_in_dir_path The "relative" or absolute path to a plug
90 in package directory.
91 mch_class The class of machine that we are testing
92 (e.g. "op" = "open power", "obmc" = "open
93 bmc", etc).
94 """
95
96 gp.dprint_executing()
97
98 if os.path.isabs(plug_in_dir_path):
99 # plug_in_dir_path begins with a slash so it is an absolute path.
100 candidate_plug_in_dir_path = os.path.normpath(plug_in_dir_path) +\
Michael Walsh6cd9cbe2017-06-30 16:59:36 -0500101 os.sep
Michael Walsh7423c012016-10-04 10:27:21 -0500102 if not os.path.isdir(candidate_plug_in_dir_path):
103 gp.print_error_report("Plug-in directory path \"" +
104 plug_in_dir_path + "\" does not exist.\n")
105 exit(1)
106 else:
107 # The plug_in_dir_path is actually a simple name (e.g.
108 # "OBMC_Sample")...
109 candidate_plug_in_dir_path = find_plug_in_package(plug_in_dir_path)
110 if candidate_plug_in_dir_path == "":
111 global PATH_LIST
112 gp.print_error_report("Plug-in directory path \"" +
113 plug_in_dir_path + "\" could not be found" +
114 " in any of the following directories:\n" +
115 gp.sprint_var(PATH_LIST))
116 exit(1)
117 # Make sure that this plug-in supports us...
118 supports_file_path = candidate_plug_in_dir_path + "supports_" + mch_class
119 if not os.path.exists(supports_file_path):
120 gp.print_error_report("The following file path could not be" +
121 " found:\n" +
122 gp.sprint_varx("supports_file_path",
123 supports_file_path) +
124 "\nThis file is necessary to indicate that" +
125 " the given plug-in supports the class of" +
126 " machine we are testing, namely \"" +
127 mch_class + "\".\n")
128 exit(1)
129
130 return candidate_plug_in_dir_path
131
Michael Walsh7423c012016-10-04 10:27:21 -0500132
Michael Walsh7423c012016-10-04 10:27:21 -0500133def return_integrated_plug_ins(mch_class="obmc"):
134
135 r"""
136 Return a list of integrated plug-ins. Integrated plug-ins are plug-ins
137 which are selected without regard for whether the user has specified them.
138 In other words, they are "integrated" into the program suite. The
139 programmer designates a plug-in as integrated by putting a file named
140 "integrated" into the plug-in package directory.
141
142 Description of arguments:
143 mch_class The class of machine that we are testing
144 (e.g. "op" = "open power", "obmc" = "open
145 bmc", etc).
146 """
147
148 global plug_in_base_path_list
149
150 integrated_plug_ins_list = []
151
Michael Walsh6cd9cbe2017-06-30 16:59:36 -0500152 DEBUG_SKIP_INTEGRATED = int(os.getenv('DEBUG_SKIP_INTEGRATED', '0'))
153
154 if DEBUG_SKIP_INTEGRATED:
155 return integrated_plug_ins_list
156
Michael Walsh7423c012016-10-04 10:27:21 -0500157 for plug_in_base_path in plug_in_base_path_list:
158 # Get a list of all plug-in paths that support our mch_class.
159 mch_class_candidate_list = glob.glob(plug_in_base_path +
160 "*/supports_" + mch_class)
161 for candidate_path in mch_class_candidate_list:
162 integrated_plug_in_dir_path = os.path.dirname(candidate_path) +\
Michael Walsh6cd9cbe2017-06-30 16:59:36 -0500163 os.sep
Michael Walsh7423c012016-10-04 10:27:21 -0500164 integrated_file_path = integrated_plug_in_dir_path + "integrated"
165 if os.path.exists(integrated_file_path):
166 plug_in_name = \
167 os.path.basename(os.path.dirname(candidate_path))
168 if plug_in_name not in integrated_plug_ins_list:
169 # If this plug-in has not already been added to the list...
170 integrated_plug_ins_list.append(plug_in_name)
171
172 return integrated_plug_ins_list
173
Michael Walsh7423c012016-10-04 10:27:21 -0500174
Michael Walsh7423c012016-10-04 10:27:21 -0500175def return_plug_in_packages_list(plug_in_dir_paths,
176 mch_class="obmc"):
177
178 r"""
179 Return a list of plug-in packages given the plug_in_dir_paths string.
180 This function calls validate_plug_in_package so it will fail if
181 plug_in_dir_paths contains any invalid plug-ins.
182
183 Description of arguments:
184 plug_in_dir_path The "relative" or absolute path to a plug
185 in package directory.
186 mch_class The class of machine that we are testing
187 (e.g. "op" = "open power", "obmc" = "open
188 bmc", etc).
189 """
190
191 if plug_in_dir_paths != "":
192 plug_in_packages_list = plug_in_dir_paths.split(":")
193 else:
194 plug_in_packages_list = []
195
196 # Get a list of integrated plug-ins (w/o full path names).
197 integrated_plug_ins_list = return_integrated_plug_ins(mch_class)
198 # Put both lists together in plug_in_packages_list with no duplicates.
199 # NOTE: This won't catch duplicates if the caller specifies the full path
200 # name of a native plug-in but that should be rare enough.
201
202 plug_in_packages_list = plug_in_packages_list + integrated_plug_ins_list
203
204 plug_in_packages_list = \
205 list(set([validate_plug_in_package(path, mch_class)
206 for path in plug_in_packages_list]))
207
208 return plug_in_packages_list