2 # -*- coding: utf-8 -*-
4 # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 from base import utils
34 ver_pat = re.compile("""(\d+.\d+)""", re.IGNORECASE)
35 proc_pat = re.compile(r"""(\d+)""", re.I)
43 def update_ld_output():
46 status, ld_output = utils.run('%s -p' % os.path.join(utils.which('ldconfig'), 'ldconfig'), log_output=False)
49 log.debug("ldconfig failed.")
51 def check_tool(cmd, min_ver=0.0):
52 log.debug("Checking: %s (min ver=%f)" % (cmd, min_ver))
53 status, output = utils.run(cmd)
56 log.debug("Not found!")
61 line = output.splitlines()[0]
65 match_obj = ver_pat.search(line)
67 ver = match_obj.group(1)
68 except AttributeError:
76 log.debug("Ver=%f Min ver=%f" % (v_f, min_ver))
79 log.debug("Found, but newer version required.")
87 def check_lib(lib, min_ver=0):
88 log.debug("Checking for library '%s'..." % lib)
90 if ld_output.find(lib) >= 0:
98 log.debug("Not found.")
101 def check_file(f, dir="/usr/include"):
102 log.debug("Searching for file '%s' in '%s'..." % (f, dir))
103 for w in utils.walkFiles(dir, recurse=True, abs_paths=True, return_folders=False, pattern=f):
104 log.debug("File found at '%s'" % w)
107 log.debug("File not found.")
111 def locate_files(f, dir):
112 log.debug("Searching for file(s) '%s' in '%s'..." % (f, dir))
114 for w in utils.walkFiles(dir, recurse=True, abs_paths=True, return_folders=False, pattern=f):
119 log.debug("Found files: %s" % found)
121 log.debug("No files not found.")
125 def locate_file_contains(f, dir, s):
127 Find a list of files located in a directory
128 that contain a specified sub-string.
130 log.debug("Searching for file(s) '%s' in '%s' that contain '%s'..." % (f, dir, s))
132 for w in utils.walkFiles(dir, recurse=True, abs_paths=True, return_folders=False, pattern=f):
134 if check_file_contains(w, s):
139 log.debug("Found files: %s" % found)
141 log.debug("No files not found.")
145 def check_file_contains(f, s):
146 log.debug("Checking file '%s' for contents '%s'..." % (f, s))
148 if os.path.exists(f):
149 for a in file(f, 'r'):
153 log.debug("'%s' found in file '%s'." % (s.replace('\n', ''), f))
156 log.debug("Contents not found.")
163 def get_process_list():
164 processes = [] # (pid, cmdline), ...
165 for x in utils.walkFiles("/proc", False, True, True):
166 s = proc_pat.search(x)
169 cmdline = file(os.path.join(x, 'cmdline'), 'r').read().replace('\x00', '').replace('\n', '').strip()
174 processes.append((int(s.group(1)), cmdline))
179 def check_ps(process_list):
180 log.debug("Searching any process(es) '%s' in running processes..." % process_list)
181 processes = get_process_list()
184 for pid, cmdline in processes:
186 for p in process_list:
188 log.debug("'%s' found." % cmdline)
191 log.debug("Not found")
198 def get_ps_pid(process):
199 log.debug("Searching for the PID for process '%s' in running processes..." % process)
200 processes = get_process_list()
203 for pid, cmdline in processes:
205 if process in cmdline:
206 log.debug("'%s' found." % cmdline)
209 log.debug("Not found")
216 def check_lsmod(module):
220 lsmod = utils.which('lsmod')
221 status, mod_output = utils.run(os.path.join(lsmod, 'lsmod'), log_output=False)
223 return mod_output.find(module) >= 0