#!/usr/bin/env python

#depfind.py
# Finds the packages a pdaXrom executable depends upon
# Version 0.1 by Armagon

# Usage: depfind.py executable-name

import os
import sys

objdump = "armv5tel-cacko-linux-objdump"
filelist = "/home/blackmore/downloads/Zaurus/pdaXrom/feed-1.1.0beta1.Z-C700/feed/Filelist" 

print

# read the executable file name
exe = None
if len(sys.argv) == 2:
	exe = sys.argv[1]

# display useage information, if requested
if not exe or exe == "-h" or exe == "--help":
	print sys.argv[0], "finds the dependancies for pdaXrom executables."
	print "You may need to edit some of the file locations in this script."
	print "Make sure you have executed runsdk.sh if you are cross-compiling,"
	print "or have zgcc installed if you are natively compiling, so that"
	print "the appropriate objdump program can be found."
	print
	print "Usage:", sys.argv[0], "exe-name"
	sys.exit(0)


# Read in the names of all the shared libraries that we need
#f = open("needed.txt")
cmd = objdump + " " + exe + " -x | grep NEEDED"
f = os.popen(cmd)

# each element in libs is a list with two values
# the first is the library name
# and the second is the number of packages that contain the library
libs = []

for line in f:
	# Add the name of the library (without the NEEDED part)
	# Note that a newline is retained at the end of each string
	libs.append( [line[14:], 0] )
f.close()

# Now, open the Filelist (or a specially compressed Filelist)
f = open(filelist)
pkg = "Not a valid package"
pkg_name = "Not a valid package"
printed_pkg = 0
pkg_list = []

# print a nice table header
print "PACKAGE".ljust(15), ":  MEETS DEPENDANCIES FOR"

# Go through every line in the filelist
for line in f:
	# If the line starts with Package, remember the package name
	if line.find("Package") == 0:
		pkg = line[8:-1]	# package name, without "Package" or newline
		
		# package name is just the name of package, without the version or architecture
		# (ie. everything up to the first underscore)
		pkg_name = pkg[ : pkg.find("_") ]
		
		# we haven't yet printed out this package
		printed_pkg = 0
	# otherwise, check if the line mentions a shared library
	elif line.find(".so") != -1:
		# Check to see if this shared library is one we are looking for
		for lib in libs:
			if line.find(lib[0]) != -1:
				
				# Only print the package name once
				if not printed_pkg:
					# print the package name, up to the first underscore
					print "\n", pkg_name.ljust(15),  ": ",
					printed_pkg = 1
					pkg_list.append(pkg_name)
				
				# Increment the number of packages this library is found in
				lib[1] += 1
					
				# print the library we were looking up (without the trailing new line)
				print lib[0][:-1] + "; ",
f.close()
print "\n"

# Now, print out any libraries that we could not find
not_found = [ lib[0] for lib in libs if lib[1] == 0 ]
if not_found:
	print "THE FOLLOWING LIBRARIES HAVE UNMET DEPENDANCIES"
	print "".join(not_found)
	print

# Print info about libraries that were found multiple times
multiples = [ lib for lib in libs if lib[1] > 1 ]
if multiples:
	print "THE FOLLOWING LIBRARIES WERE FOUND IN MULTIPLE PACKAGES"
	for lib in multiples:
		print lib[0][:-1] + " was found " + lib[1] + " times."
	print
	
print "Dependancies: ", " ".join(pkg_list)
print
