#!/usr/bin/python2.5
#
# Parses the python file passed as parameter
# and prints the modules imported in that file
#
# NOTE: conditional import are included in the list and __import__ are ignored
#
# Author: Sandro Tosi <morph@debian.org>
# Date: 2008-11-13
# License: Public Domain
#
# Other refs:
#  * https://svn.enthought.com/svn/sandbox/grin/trunk/examples/grinimports.py
#  * http://code.activestate.com/recipes/533146/
#  * pyflakes code (checker.py)

# http://docs.python.org/library/compiler.html
# deprecated in 2.6, removed in 3.0, but we are on 2.5...
import compiler
import sys
# using it to type checking the object in the AST
from compiler.ast import Import, From

# creates the syntax tree
mod = compiler.parseFile(sys.argv[1])

nodes = mod.node.nodes

for node in nodes:
    if isinstance(node,Import):
        for name, alias in node.names:
            print "Import stmt: "+name
    if isinstance(node,From):
        print "From stmt: " + node.modname
