2008-02-27 15:14:28 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import exceptions
|
2008-02-27 21:06:55 +00:00
|
|
|
import getopt
|
|
|
|
import os
|
|
|
|
import re
|
2008-02-27 15:14:28 +00:00
|
|
|
import rpm
|
|
|
|
import select
|
|
|
|
import subprocess
|
2008-02-27 21:06:55 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def get_rlms(root):
|
|
|
|
rlm_re = re.compile(r'^rlm_')
|
|
|
|
version_re = re.compile(r'-[0-9.]+\.so$')
|
|
|
|
names = os.listdir(root)
|
|
|
|
names = [x for x in names if rlm_re.search(x)]
|
|
|
|
names = [x for x in names if not version_re.search(x)]
|
|
|
|
names.sort()
|
|
|
|
return names
|
2008-02-27 15:14:28 +00:00
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
debug = False
|
|
|
|
verbose = False
|
|
|
|
|
|
|
|
exclude_rpms = ['glibc']
|
|
|
|
|
2008-02-27 21:06:55 +00:00
|
|
|
build = '2.0.2-1.fc8'
|
|
|
|
root_template = '/var/tmp/freeradius-%s-root-jdennis/usr/lib/freeradius'
|
2008-02-28 23:41:44 +00:00
|
|
|
libdirs = ['/lib','/usr/lib']
|
2008-02-27 15:14:28 +00:00
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def get_rpm_nvr_from_header(hdr):
|
|
|
|
'Given an RPM header return the package NVR as a string'
|
|
|
|
name = hdr['name']
|
|
|
|
version = hdr['version']
|
|
|
|
release = hdr['release']
|
|
|
|
|
|
|
|
return "%s-%s-%s" % (name, version, release)
|
|
|
|
|
|
|
|
def get_rpm_hdr_by_file_path(path):
|
|
|
|
if path is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
hdr = None
|
|
|
|
try:
|
|
|
|
ts = rpm.ts()
|
|
|
|
mi = ts.dbMatch(rpm.RPMTAG_BASENAMES, path)
|
|
|
|
for hdr in mi: break
|
|
|
|
except Exception, e:
|
|
|
|
print >> sys.stderr, "failed to retrieve rpm hdr for %s, %s" %(path, e)
|
|
|
|
hdr = None
|
|
|
|
return hdr
|
|
|
|
|
|
|
|
def get_rpm_nvr_by_file_path(path):
|
|
|
|
if path is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
hdr = get_rpm_hdr_by_file_path(path)
|
|
|
|
if not hdr:
|
|
|
|
print >> sys.stderr, "failed to retrieve rpm info for %s" %(path)
|
|
|
|
nvr = get_rpm_nvr_from_header(hdr)
|
|
|
|
return nvr
|
|
|
|
|
|
|
|
def get_rpm_name_by_file_path(path):
|
|
|
|
if path is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
hdr = get_rpm_hdr_by_file_path(path)
|
|
|
|
if not hdr:
|
|
|
|
print >> sys.stderr, "failed to retrieve rpm info for %s" %(path)
|
|
|
|
name = hdr['name']
|
|
|
|
return name
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class CmdError(exceptions.Exception):
|
|
|
|
def __init__(self, errno, msg):
|
|
|
|
self.errno = errno
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
|
|
|
|
class Command:
|
|
|
|
def __init__(self, cmd):
|
|
|
|
self.cmd = cmd
|
|
|
|
self.sub_process = None
|
|
|
|
self.bufsize = 1024
|
|
|
|
self.stdout_buf = ''
|
|
|
|
self.stderr_buf = ''
|
|
|
|
self.stdout_lines = []
|
|
|
|
self.stderr_lines = []
|
|
|
|
|
|
|
|
def run(self, stdout_callback=None, stderr_callback=None):
|
|
|
|
self.sub_process = subprocess.Popen(self.cmd, \
|
|
|
|
stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, \
|
|
|
|
close_fds=True, shell=True)
|
|
|
|
self.stdout = self.sub_process.stdout
|
|
|
|
self.stderr = self.sub_process.stderr
|
|
|
|
|
|
|
|
read_watch = [self.stdout, self.stderr]
|
|
|
|
while read_watch:
|
|
|
|
readable = select.select(read_watch, [], [])[0]
|
|
|
|
for fd in readable:
|
|
|
|
if fd == self.stdout:
|
|
|
|
data = os.read(fd.fileno(), self.bufsize)
|
|
|
|
if not data:
|
|
|
|
read_watch.remove(fd)
|
|
|
|
else:
|
|
|
|
self.stdout_buf += data
|
|
|
|
for line in self.burst_lines('stdout_buf'):
|
|
|
|
if stdout_callback: stdout_callback(line)
|
|
|
|
self.stdout_lines.append(line)
|
|
|
|
if fd == self.stderr:
|
|
|
|
data = os.read(fd.fileno(), self.bufsize)
|
|
|
|
if not data:
|
|
|
|
read_watch.remove(fd)
|
|
|
|
else:
|
|
|
|
self.stderr_buf += data
|
|
|
|
for line in self.burst_lines('stderr_buf'):
|
|
|
|
if stdout_callback: stderr_callback(line)
|
|
|
|
self.stderr_lines.append(line)
|
|
|
|
|
|
|
|
self.returncode = self.sub_process.wait()
|
|
|
|
if self.returncode:
|
|
|
|
raise CmdError(self.returncode, "cmd \"%s\"\nreturned status %d\n%s" % (self.cmd, self.returncode, ''.join(self.stderr_lines)))
|
|
|
|
|
|
|
|
return self.returncode
|
|
|
|
|
|
|
|
def burst_lines(self, what):
|
|
|
|
buf = getattr(self, what)
|
|
|
|
start = 0
|
|
|
|
end = buf.find('\n', start)
|
|
|
|
while end >= 0:
|
|
|
|
end += 1 # include newline
|
|
|
|
line = buf[start:end]
|
|
|
|
yield line
|
|
|
|
start = end
|
|
|
|
end = buf.find('\n', start)
|
|
|
|
buf = buf[start:]
|
|
|
|
setattr(self, what, buf)
|
|
|
|
|
2008-02-28 23:41:44 +00:00
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def get_so_requires(path):
|
|
|
|
requires = {}
|
|
|
|
cmd = 'ldd %s' % (path)
|
|
|
|
so_re = re.compile(r'^\s*(\S+)\s+=>\s+(\S+)')
|
|
|
|
|
|
|
|
c = Command(cmd)
|
|
|
|
status = c.run()
|
|
|
|
|
|
|
|
for line in c.stdout_lines:
|
|
|
|
line = line.strip()
|
|
|
|
match = so_re.search(line)
|
|
|
|
if match:
|
|
|
|
so_name = match.group(1)
|
|
|
|
if match.group(2).startswith('/'):
|
|
|
|
so_path = match.group(2)
|
|
|
|
else:
|
|
|
|
so_path = None
|
|
|
|
|
|
|
|
requires[so_name] = so_path
|
|
|
|
return requires
|
|
|
|
|
|
|
|
def get_so_needed(path):
|
|
|
|
needed = []
|
|
|
|
cmd = 'readelf -d %s' % (path)
|
|
|
|
so_re = re.compile(r'\(NEEDED\)\s+Shared library:\s+\[([^\]]+)\]')
|
|
|
|
|
|
|
|
c = Command(cmd)
|
|
|
|
status = c.run()
|
|
|
|
|
|
|
|
for line in c.stdout_lines:
|
|
|
|
line = line.strip()
|
|
|
|
match = so_re.search(line)
|
|
|
|
if match:
|
|
|
|
so_name = match.group(1)
|
|
|
|
needed.append(so_name)
|
|
|
|
return needed
|
|
|
|
|
|
|
|
def format_size(size):
|
|
|
|
if size > 1000000000:
|
|
|
|
return '%.1f GB' % (size/1000000000.0)
|
|
|
|
if size > 1000000:
|
|
|
|
return '%.1f MB' % (size/1000000.0)
|
|
|
|
if size > 1000:
|
|
|
|
return '%.1f KB' % (size/1000.0)
|
|
|
|
return '%d' % (size)
|
2008-02-27 15:14:28 +00:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class RPM_Prop:
|
|
|
|
def __init__(self, path=None, name=None):
|
|
|
|
self.name = name
|
|
|
|
self.paths = {}
|
2008-02-28 23:41:44 +00:00
|
|
|
self.rpm_hdr = None
|
|
|
|
self.used_by = {}
|
2008-02-27 15:14:28 +00:00
|
|
|
if path:
|
|
|
|
self.register_path(path)
|
2008-02-28 23:41:44 +00:00
|
|
|
if not self.rpm_hdr:
|
|
|
|
self.rpm_hdr = get_rpm_hdr_by_file_path(path)
|
|
|
|
if self.rpm_hdr:
|
|
|
|
if not self.name:
|
|
|
|
self.name = self.rpm_hdr[rpm.RPMTAG_NAME]
|
|
|
|
self.size = self.rpm_hdr[rpm.RPMTAG_SIZE]
|
2008-02-27 15:14:28 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "name=%s paths=%s" % (self.name, ','.join(self.paths.keys()))
|
|
|
|
|
|
|
|
def register_path(self, path, name=None):
|
|
|
|
if debug: print "%s.register_path: path=%s" % (self.__class__.__name__, path)
|
|
|
|
return self.paths.setdefault(path, path)
|
|
|
|
|
|
|
|
class RPM_Collection:
|
|
|
|
def __init__(self):
|
|
|
|
self.names = {}
|
|
|
|
self.paths = {}
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
text = ''
|
|
|
|
names = self.get_names()
|
|
|
|
for name in names:
|
|
|
|
text += "%s: %s\n" % (name, self.names[name])
|
|
|
|
return text
|
|
|
|
|
|
|
|
def register_path(self, path):
|
|
|
|
if debug: print "%s.register_path: path=%s" % (self.__class__.__name__, path)
|
2008-02-27 21:06:55 +00:00
|
|
|
rpm_prop = self.paths.get(path)
|
|
|
|
if not rpm_prop:
|
|
|
|
rpm_prop = self.paths.setdefault(path, RPM_Prop(path=path))
|
2008-02-27 15:14:28 +00:00
|
|
|
self.names.setdefault(rpm_prop.name, rpm_prop)
|
|
|
|
return rpm_prop
|
|
|
|
|
|
|
|
def get_names(self):
|
|
|
|
names = self.names.keys()
|
|
|
|
names.sort()
|
|
|
|
return names
|
|
|
|
|
2008-02-28 23:41:44 +00:00
|
|
|
def get_name(self, name):
|
|
|
|
return self.names.get(name)
|
|
|
|
|
2008-02-27 15:14:28 +00:00
|
|
|
class SO_File:
|
|
|
|
def __init__(self, name=None, path=None):
|
|
|
|
self.name = name
|
|
|
|
self.path = path
|
|
|
|
self.rpm = None
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.rpm:
|
|
|
|
rpm_name = self.rpm.name
|
|
|
|
else:
|
|
|
|
rpm_name = None
|
|
|
|
return "name=%s rpm=%s" % (self.name, rpm_name)
|
|
|
|
|
|
|
|
class SO_Collection:
|
|
|
|
def __init__(self):
|
|
|
|
self.names = {}
|
|
|
|
self.paths = {}
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
text = ''
|
|
|
|
names = self.get_names()
|
|
|
|
for name in names:
|
|
|
|
text += "%s: %s\n" % (name, self.names[name])
|
|
|
|
return text
|
|
|
|
|
|
|
|
def register_path(self, path, name=None):
|
|
|
|
if debug: print "%s.register_path: path=%s" % (self.__class__.__name__, path)
|
2008-02-27 21:06:55 +00:00
|
|
|
so_prop = self.paths.get(path)
|
|
|
|
if not so_prop:
|
|
|
|
so_prop = self.paths.setdefault(path, SO_File(name, path=path))
|
2008-02-27 15:14:28 +00:00
|
|
|
self.names.setdefault(name, so_prop)
|
|
|
|
return so_prop
|
|
|
|
|
|
|
|
def get_names(self):
|
|
|
|
names = self.names.keys()
|
|
|
|
names.sort()
|
|
|
|
return names
|
|
|
|
|
|
|
|
class LoadableModule:
|
|
|
|
def __init__(self, path, name=None):
|
|
|
|
if name is None:
|
|
|
|
name = os.path.basename(path)
|
|
|
|
self.name = name
|
|
|
|
self.path = path
|
2008-02-27 21:06:55 +00:00
|
|
|
self.rpm_names = {}
|
2008-02-27 15:14:28 +00:00
|
|
|
self.sos = SO_Collection()
|
2008-02-28 23:41:44 +00:00
|
|
|
self.get_so_requires()
|
2008-02-27 15:14:28 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
text = '%s\n' % (self.name)
|
2008-02-27 21:06:55 +00:00
|
|
|
text += " RPM's: %s\n" % (','.join(self.get_rpm_names()))
|
2008-02-27 15:14:28 +00:00
|
|
|
text += " SO's: %s\n" % (','.join(self.sos.get_names()))
|
|
|
|
return text
|
|
|
|
|
2008-02-28 23:41:44 +00:00
|
|
|
def get_so_requires(self):
|
|
|
|
requires = get_so_requires(self.path)
|
|
|
|
needed = get_so_needed(self.path)
|
|
|
|
#print "%s requires=%s" % (self.name, requires)
|
|
|
|
#print "%s needed=%s" % (self.name, needed)
|
|
|
|
|
|
|
|
for so_name, so_path in requires.items():
|
|
|
|
if so_name not in needed: continue
|
|
|
|
if so_path:
|
|
|
|
so_prop = self.sos.register_path(so_path, so_name)
|
|
|
|
rpm_prop = rpms.register_path(so_prop.path)
|
|
|
|
rpm_prop.used_by[self.name] = 1
|
|
|
|
self.rpm_names.setdefault(rpm_prop.name, rpm_prop.name)
|
|
|
|
so_prop.rpm = rpm_prop
|
|
|
|
else:
|
|
|
|
so_prop = None
|
|
|
|
if verbose: print "found so='%s' %s" % (so_name, so_prop)
|
2008-02-27 15:14:28 +00:00
|
|
|
|
|
|
|
def register_so(self, so):
|
|
|
|
if debug: print "%s.register_so: so=%s" % (self.__class__.__name__, so)
|
|
|
|
self.sos.setdefault(so, so)
|
|
|
|
self.names.setdefault(so.name, so)
|
|
|
|
return so
|
|
|
|
|
2008-02-27 21:06:55 +00:00
|
|
|
def get_rpm_names(self):
|
|
|
|
rpm_names = self.rpm_names.keys()
|
|
|
|
rpm_names.sort()
|
|
|
|
return rpm_names
|
|
|
|
|
2008-02-27 15:14:28 +00:00
|
|
|
def get_sos(self):
|
|
|
|
sos = self.sos.keys()
|
|
|
|
sos.sort(lambda a,b: cmp(a.name, b.name))
|
|
|
|
return sos
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
2008-02-27 21:06:55 +00:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "b:v", ['build=','verbose'])
|
|
|
|
for o, a in opts:
|
|
|
|
if o in ['-b', '--build']:
|
|
|
|
build = a
|
|
|
|
elif o in ['-v', '--verbose']:
|
|
|
|
verbose = True
|
|
|
|
else:
|
|
|
|
print >> sys.stderr, "Unknown arg: %s" % o
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
root = root_template % build
|
|
|
|
modules = get_rlms(root)
|
|
|
|
module_paths = [os.path.join(root,x) for x in modules]
|
|
|
|
rpms = RPM_Collection()
|
|
|
|
|
2008-02-27 15:14:28 +00:00
|
|
|
lms = []
|
|
|
|
for module_path in module_paths[:]:
|
|
|
|
lm = LoadableModule(module_path)
|
|
|
|
lms.append(lm)
|
|
|
|
|
|
|
|
|
2008-02-27 21:06:55 +00:00
|
|
|
print "RLM Modules(%s): %s\n" % (len(modules), ','.join(modules))
|
|
|
|
|
2008-02-27 15:14:28 +00:00
|
|
|
for lm in lms:
|
2008-02-27 21:06:55 +00:00
|
|
|
rpm_names = [x for x in lm.get_rpm_names() if x not in exclude_rpms]
|
|
|
|
if rpm_names:
|
2008-02-27 15:14:28 +00:00
|
|
|
print lm.name
|
2008-02-27 21:06:55 +00:00
|
|
|
print ' %s' % (','.join(rpm_names))
|
2008-02-27 15:14:28 +00:00
|
|
|
|
|
|
|
print "--------------"
|
|
|
|
|
2008-02-28 23:41:44 +00:00
|
|
|
rpm_props = [x for x in rpms.names.values() if len(x.used_by) and x.name not in exclude_rpms]
|
|
|
|
rpm_props.sort(lambda a,b: cmp(a.name, b.name))
|
|
|
|
for rpm_prop in rpm_props:
|
|
|
|
used_by = rpm_prop.used_by.keys()
|
|
|
|
used_by.sort()
|
|
|
|
print "%s: %s" % (rpm_prop.name, ','.join(used_by))
|
|
|
|
|
|
|
|
print "--------------"
|
|
|
|
|
|
|
|
rpm_props.sort(lambda a,b: cmp(a.size, b.size))
|
|
|
|
for rpm_prop in rpm_props:
|
|
|
|
print '%10s %s' % (format_size(rpm_prop.size), rpm_prop.name)
|
|
|
|
|
|
|
|
|
|
|
|
print "--------------"
|
|
|
|
|
2008-02-27 15:14:28 +00:00
|
|
|
for lm in lms:
|
|
|
|
print lm
|