merge with master
[platform/upstream/hplip.git] / installer / dcheck.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
5 #
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.
10 #
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.
15 #
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
19 #
20 # Author: Don Welch
21 #
22
23 # Std Lib
24 import os
25 import os.path
26 import re
27 import sys
28
29 # Local
30 from base.g import *
31 from base import utils
32
33
34 ver_pat = re.compile("""(\d+.\d+)""", re.IGNORECASE)
35 proc_pat = re.compile(r"""(\d+)""", re.I)
36
37 ld_output = ''
38 #ps_output = ''
39 mod_output = ''
40
41
42
43 def update_ld_output():
44     # For library checks
45     global ld_output
46     status, ld_output = utils.run('%s -p' % os.path.join(utils.which('ldconfig'), 'ldconfig'), log_output=False)
47
48     if status != 0:
49         log.debug("ldconfig failed.")
50
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)
54
55     if status != 0:
56         log.debug("Not found!")
57         return False
58     else:
59         if min_ver:
60             try:
61                 line = output.splitlines()[0]
62             except IndexError:
63                 line = ''
64             log.debug(line)
65             match_obj = ver_pat.search(line)
66             try:
67                 ver = match_obj.group(1)
68             except AttributeError:
69                 ver = ''
70
71             try:
72                 v_f = float(ver)
73             except ValueError:
74                 return False
75             else:
76                 log.debug("Ver=%f Min ver=%f" % (v_f, min_ver))
77
78                 if v_f < min_ver:
79                     log.debug("Found, but newer version required.")
80
81                 return v_f >= min_ver
82         else:
83             log.debug("Found.")
84             return True
85
86
87 def check_lib(lib, min_ver=0):
88     log.debug("Checking for library '%s'..." % lib)
89
90     if ld_output.find(lib) >= 0:
91         log.debug("Found.")
92
93         #if min_ver:
94         #    pass
95         #else:
96         return True
97     else:
98         log.debug("Not found.")
99         return False
100
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)
105         return True
106
107     log.debug("File not found.")
108     return False
109
110
111 def locate_files(f, dir):
112     log.debug("Searching for file(s) '%s' in '%s'..." % (f, dir))
113     found = []
114     for w in utils.walkFiles(dir, recurse=True, abs_paths=True, return_folders=False, pattern=f):
115         log.debug(w)
116         found.append(w)
117
118     if found:
119         log.debug("Found files: %s" % found)
120     else:
121         log.debug("No files not found.")
122
123     return found
124
125 def locate_file_contains(f, dir, s):
126     """
127         Find a list of files located in a directory
128         that contain a specified sub-string.
129     """
130     log.debug("Searching for file(s) '%s' in '%s' that contain '%s'..." % (f, dir, s))
131     found = []
132     for w in utils.walkFiles(dir, recurse=True, abs_paths=True, return_folders=False, pattern=f):
133
134         if check_file_contains(w, s):
135             log.debug(w)
136             found.append(w)
137
138     if found:
139         log.debug("Found files: %s" % found)
140     else:
141         log.debug("No files not found.")
142
143     return found
144
145 def check_file_contains(f, s):
146     log.debug("Checking file '%s' for contents '%s'..." % (f, s))
147     try:
148         if os.path.exists(f):
149             for a in file(f, 'r'):
150                 update_spinner()
151
152                 if s in a:
153                     log.debug("'%s' found in file '%s'." % (s.replace('\n', ''), f))
154                     return True
155
156         log.debug("Contents not found.")
157         return False
158
159     finally:
160         cleanup_spinner()
161
162
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) 
167         if s is not None:
168             try:
169                 cmdline = file(os.path.join(x, 'cmdline'), 'r').read().replace('\x00', '').replace('\n', '').strip()
170             except IOError:
171                 cmdline = None
172                 
173             if cmdline:
174                 processes.append((int(s.group(1)), cmdline))
175
176     return processes
177
178
179 def check_ps(process_list):
180     log.debug("Searching any process(es) '%s' in running processes..." % process_list)
181     processes = get_process_list()
182
183     try:
184         for pid, cmdline in processes:
185             update_spinner()
186             for p in process_list:
187                 if p in cmdline:
188                     log.debug("'%s' found." % cmdline)
189                     return True
190
191         log.debug("Not found")
192         return False
193
194     finally:
195         cleanup_spinner()
196
197
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()
201
202     try:
203         for pid, cmdline in processes:
204             update_spinner()
205             if process in cmdline:
206                 log.debug("'%s' found." % cmdline)
207                 return pid
208
209         log.debug("Not found")
210         return 0
211
212     finally:
213         cleanup_spinner()
214
215
216 def check_lsmod(module):
217     global mod_output
218
219     if not mod_output:
220         lsmod = utils.which('lsmod')
221         status, mod_output = utils.run(os.path.join(lsmod, 'lsmod'), log_output=False)
222
223     return mod_output.find(module) >= 0