#!/usr/bin/env python

# Copyright (c) 2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

"""NARVAL dependencies checker

This is a short script that can checks modules/transforms/cookbooks
dependencies.
It can be useful, when installing a new pacakge, to see what is or
isn't needed.
"""



from narval.lib import *
import string

USAGE = """
Usage: check_deps.py  ARGS
ARGS is a list of files or directories.
    - files are supposed to be NARVAL Cookbooks
    - directories are supposed to be NARVAL packages
"""

def check_cbk(filename,deps_dict):
    tree = read_domtree_from_file(filename)

    cbk = tree.documentElement.getAttributeNS('','name')
    deps_dict[cbk] = []
    for recipe in tree.documentElement.childNodes:
        for child in recipe.childNodes:
            if child.nodeName == 'step':
                target = child.getAttributeNS('','target')
                type = child.getAttributeNS('','type')
                if type == 'action':
                    group_type = 'module'
                elif type == 'recipe':
                    group_type = 'Cookbook'
                elif type == 'transform':
                    group_type = 'transform'
                group = string.split(target,'.')[0]
                needed = (group,group_type)
                if needed not in deps_dict[cbk]:
                    deps_dict[cbk].append(needed)


def print_deps(dict):
    print "-"*20
    for key,value in dict.items():
        print key," needs : "
        for dep in value:
            print "\t -> ",dep[0], "(",dep[1],")"
    print "-"*20

if __name__ == '__main__':
    import getopt, sys, os

    short_list = ''
    long_list = ['help']
    optlist, args = [], []
    try:
        optlist, args = getopt.getopt(sys.argv[1:], short_list, long_list)
    except getopt.error,e :
        print 'Warning:',e
        print USAGE
        sys.exit(1)

    if len(sys.argv) == 1:
        print USAGE
        sys.exit(0)
    
    for opt, val in optlist :
        if opt == '--help' :
            print USAGE
            sys.exit(0)
    
    deps_dict = {}
    for file in sys.argv[1:]:
        if os.path.isdir(file):
            try:
                path = file + '/recipes/'
                for cbk in os.listdir(path):
                    if cbk[-4:] == '.xml':
                        check_cbk(path+cbk,deps_dict)
            except IOError, err:
                print "Can't open : ",path, " doesn't look like a NARVAL module"
        else:
            check_cbk(file,deps_dict)

    print_deps(deps_dict)
