blob: 2cfd3e03a1d3801f83e8c3a25c624b0079fa8ea0 [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
George Keishingc191ed72019-06-10 07:45:59 -05009try:
10 import commands
11except ImportError:
12 import subprocess
13
Michael Walsh7423c012016-10-04 10:27:21 -050014import glob
15
16import gen_print as gp
17import gen_misc as gm
18
19# Some help text that is common to more than one program.
20plug_in_dir_paths_help_text = \
21 'This is a colon-separated list of plug-in directory paths. If one' +\
22 ' of the entries in the list is a plain directory name (i.e. no' +\
23 ' path info), it will be taken to be a native plug-in. In that case,' +\
24 ' %(prog)s will search for the native plug-in in the "plug-ins"' +\
25 ' subdirectory of each path in the PATH environment variable until it' +\
26 ' is found. Also, integrated plug-ins will automatically be appended' +\
27 ' to your plug_in_dir_paths list. An integrated plug-in is any plug-in' +\
28 ' found using the PATH variable that contains a file named "integrated".'
29
30mch_class_help_text = \
31 'The class of machine that we are testing (e.g. "op" = "open power",' +\
32 ' "obmc" = "open bmc", etc).'
33
34PATH_LIST = gm.return_path_list()
35
36
Michael Walsh7423c012016-10-04 10:27:21 -050037def get_plug_in_base_paths():
Michael Walsh7423c012016-10-04 10:27:21 -050038 r"""
39 Get plug-in base paths and return them as a list.
40
41 This function searches the PATH_LIST (created from PATH environment
42 variable) for any paths that have a "plug_ins" subdirectory. All such
43 paths are considered plug_in_base paths.
44 """
45
46 global PATH_LIST
47
48 plug_in_base_path_list = []
49
50 for path in PATH_LIST:
51 candidate_plug_in_base_path = path + "plug_ins/"
52 if os.path.isdir(candidate_plug_in_base_path):
53 plug_in_base_path_list.append(candidate_plug_in_base_path)
54
55 return plug_in_base_path_list
56
Gunnar Mills096cd562018-03-26 10:19:12 -050057
Michael Walsh7423c012016-10-04 10:27:21 -050058# Define global plug_in_base_path_list and call get_plug_in_base_paths to set
59# its value.
60plug_in_base_path_list = get_plug_in_base_paths()
61
62
Michael Walsh7423c012016-10-04 10:27:21 -050063def find_plug_in_package(plug_in_name):
Michael Walsh7423c012016-10-04 10:27:21 -050064 r"""
65 Find and return the normalized directory path of the specified plug in.
66 This is done by searching the global plug_in_base_path_list.
67
68 Description of arguments:
69 plug_in_name The unqualified name of the plug-in
70 package.
71 """
72
73 global plug_in_base_path_list
74 for plug_in_base_dir_path in plug_in_base_path_list:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050075 candidate_plug_in_dir_path = os.path.normpath(plug_in_base_dir_path
76 + plug_in_name) + \
Michael Walsh6cd9cbe2017-06-30 16:59:36 -050077 os.sep
Michael Walsh7423c012016-10-04 10:27:21 -050078 if os.path.isdir(candidate_plug_in_dir_path):
79 return candidate_plug_in_dir_path
80
81 return ""
82
Michael Walsh7423c012016-10-04 10:27:21 -050083
Michael Walsh7423c012016-10-04 10:27:21 -050084def validate_plug_in_package(plug_in_dir_path,
85 mch_class="obmc"):
Michael Walsh7423c012016-10-04 10:27:21 -050086 r"""
87 Validate the plug in package and return the normalized plug-in directory
88 path.
89
90 Description of arguments:
91 plug_in_dir_path The "relative" or absolute path to a plug
92 in package directory.
93 mch_class The class of machine that we are testing
94 (e.g. "op" = "open power", "obmc" = "open
95 bmc", etc).
96 """
97
98 gp.dprint_executing()
99
100 if os.path.isabs(plug_in_dir_path):
101 # plug_in_dir_path begins with a slash so it is an absolute path.
102 candidate_plug_in_dir_path = os.path.normpath(plug_in_dir_path) +\
Michael Walsh6cd9cbe2017-06-30 16:59:36 -0500103 os.sep
Michael Walsh7423c012016-10-04 10:27:21 -0500104 if not os.path.isdir(candidate_plug_in_dir_path):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500105 gp.print_error_report("Plug-in directory path \""
106 + plug_in_dir_path + "\" does not exist.\n")
Michael Walsh7423c012016-10-04 10:27:21 -0500107 exit(1)
108 else:
109 # The plug_in_dir_path is actually a simple name (e.g.
110 # "OBMC_Sample")...
111 candidate_plug_in_dir_path = find_plug_in_package(plug_in_dir_path)
112 if candidate_plug_in_dir_path == "":
113 global PATH_LIST
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500114 gp.print_error_report("Plug-in directory path \""
115 + plug_in_dir_path + "\" could not be found"
116 + " in any of the following directories:\n"
117 + gp.sprint_var(PATH_LIST))
Michael Walsh7423c012016-10-04 10:27:21 -0500118 exit(1)
119 # Make sure that this plug-in supports us...
120 supports_file_path = candidate_plug_in_dir_path + "supports_" + mch_class
121 if not os.path.exists(supports_file_path):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500122 gp.print_error_report("The following file path could not be"
123 + " found:\n"
124 + gp.sprint_varx("supports_file_path",
125 supports_file_path)
126 + "\nThis file is necessary to indicate that"
127 + " the given plug-in supports the class of"
128 + " machine we are testing, namely \""
129 + mch_class + "\".\n")
Michael Walsh7423c012016-10-04 10:27:21 -0500130 exit(1)
131
132 return candidate_plug_in_dir_path
133
Michael Walsh7423c012016-10-04 10:27:21 -0500134
Michael Walsh7423c012016-10-04 10:27:21 -0500135def return_integrated_plug_ins(mch_class="obmc"):
Michael Walsh7423c012016-10-04 10:27:21 -0500136 r"""
137 Return a list of integrated plug-ins. Integrated plug-ins are plug-ins
138 which are selected without regard for whether the user has specified them.
139 In other words, they are "integrated" into the program suite. The
140 programmer designates a plug-in as integrated by putting a file named
141 "integrated" into the plug-in package directory.
142
143 Description of arguments:
144 mch_class The class of machine that we are testing
145 (e.g. "op" = "open power", "obmc" = "open
146 bmc", etc).
147 """
148
149 global plug_in_base_path_list
150
151 integrated_plug_ins_list = []
152
Michael Walsh6cd9cbe2017-06-30 16:59:36 -0500153 DEBUG_SKIP_INTEGRATED = int(os.getenv('DEBUG_SKIP_INTEGRATED', '0'))
154
155 if DEBUG_SKIP_INTEGRATED:
156 return integrated_plug_ins_list
157
Michael Walsh7423c012016-10-04 10:27:21 -0500158 for plug_in_base_path in plug_in_base_path_list:
159 # Get a list of all plug-in paths that support our mch_class.
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500160 mch_class_candidate_list = glob.glob(plug_in_base_path
161 + "*/supports_" + mch_class)
Michael Walsh7423c012016-10-04 10:27:21 -0500162 for candidate_path in mch_class_candidate_list:
163 integrated_plug_in_dir_path = os.path.dirname(candidate_path) +\
Michael Walsh6cd9cbe2017-06-30 16:59:36 -0500164 os.sep
Michael Walsh7423c012016-10-04 10:27:21 -0500165 integrated_file_path = integrated_plug_in_dir_path + "integrated"
166 if os.path.exists(integrated_file_path):
167 plug_in_name = \
168 os.path.basename(os.path.dirname(candidate_path))
169 if plug_in_name not in integrated_plug_ins_list:
170 # If this plug-in has not already been added to the list...
171 integrated_plug_ins_list.append(plug_in_name)
172
173 return integrated_plug_ins_list
174
Michael Walsh7423c012016-10-04 10:27:21 -0500175
Michael Walsh7423c012016-10-04 10:27:21 -0500176def return_plug_in_packages_list(plug_in_dir_paths,
177 mch_class="obmc"):
Michael Walsh7423c012016-10-04 10:27:21 -0500178 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