#!/usr/bin/python
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
# 
# $Id: moddeps,v 1.1 2005/07/25 21:25:16 kdart Exp $
#
#    Copyright (C) 1999-2004  Keith Dart <kdart@kdart.com>
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.

"""
Find all module dependencies by using Pythons natural import chain.

Usage:
    moddeps <module>

"""

import sys

def get_unique_modules():
    d = {}
    for name in map(lambda m: m.__name__, filter(None, sys.modules.values())):
        d[name] = None
    rv = d.keys()
    rv.sort()
    return rv

def print_modules(modlist):
    for name in modlist:
        mod = sys.modules[name]
        if hasattr(mod, "__file__"):
            print "%35.35s -> %s" % (name, mod.__file__)
        else:
            print "%35.35s -> (built-in)" % (name, )

def main(argv):
    try:
        modname = argv[1]
    except IndexError:
        print "Base modules:"
        print get_unique_modules()
        return 2
    before = get_unique_modules()
    try:
        mod = __import__(modname)
    except ImportError:
        print "No such module."
        return 1
    after = get_unique_modules()
    for modname in before:
        after.remove(modname)
    print_modules(after)
    return 0

sys.exit(main(sys.argv))

