2015-04-30 17:36:36 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-05-04 12:30:31 +02:00
|
|
|
"""
|
2015-05-04 15:03:53 +02:00
|
|
|
Create the NEEDED_MODULE
|
|
|
|
aka the genealogy (children module, subchildren module and so on),
|
2015-05-04 12:30:31 +02:00
|
|
|
of a NEEDED_CHILDREN_MODULES file
|
|
|
|
|
|
|
|
Usage:
|
2015-05-04 15:36:58 +02:00
|
|
|
module_handler.py print_genealogy [<NEEDED_CHILDREN_MODULES>]
|
|
|
|
module_handler.py check_dependencies [<module_name>...]
|
|
|
|
module_handler.py create_png [<NEEDED_CHILDREN_MODULES>]
|
2015-05-04 15:03:53 +02:00
|
|
|
|
|
|
|
Options:
|
2015-05-04 15:36:58 +02:00
|
|
|
print_genealogy Print the genealogy of the NEEDED_CHILDREN_MODULES
|
|
|
|
aka (children, subchildren, etc)
|
|
|
|
create_png Create a png of the file
|
|
|
|
NEEDED_CHILDREN_MODULES The path of NEEDED_CHILDREN_MODULES
|
|
|
|
by default try to open the file in the current path
|
2015-05-04 12:30:31 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
from docopt import docopt
|
|
|
|
|
2015-04-30 17:36:36 +02:00
|
|
|
import os
|
2015-05-04 14:44:30 +02:00
|
|
|
import sys
|
2015-04-30 17:36:36 +02:00
|
|
|
import os.path
|
2015-05-05 10:45:44 +02:00
|
|
|
from qp_utils import cache
|
2015-04-30 20:31:27 +02:00
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
|
|
|
|
@cache
|
|
|
|
def get_dict_genealogy():
|
|
|
|
"""Loop over MODULE in QPACKAGE_ROOT/src, open all the NEEDED_CHILDREN_MODULES
|
|
|
|
and create a dict[MODULE] = [sub module needed, ...]
|
|
|
|
"""
|
2015-04-30 17:36:36 +02:00
|
|
|
d_ref = dict()
|
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
qpackage_root = os.environ['QPACKAGE_ROOT']
|
|
|
|
dir_ = os.path.join(qpackage_root, 'src')
|
|
|
|
|
2015-04-30 17:36:36 +02:00
|
|
|
for o in os.listdir(dir_):
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(os.path.join(dir_, o, "NEEDED_CHILDREN_MODULES"), "r") as f:
|
|
|
|
l_children = f.read().split()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
d_ref[o] = l_children
|
|
|
|
|
|
|
|
return d_ref
|
|
|
|
|
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
def module_genealogy(path):
|
|
|
|
"""
|
|
|
|
Take a name of a NEEDED_CHILDREN_MODULES
|
|
|
|
and return a list of all the {sub, subsub, ...}children
|
|
|
|
"""
|
2015-04-30 17:36:36 +02:00
|
|
|
try:
|
|
|
|
with open(path, "r") as f:
|
|
|
|
l_children = f.read().split()
|
2015-05-04 15:36:58 +02:00
|
|
|
except IOError as e:
|
|
|
|
print >> sys.stderr, e
|
|
|
|
sys.exit(1)
|
2015-04-30 17:36:36 +02:00
|
|
|
else:
|
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
needed_module = get_it_and_children(l_children)
|
2015-04-30 17:36:36 +02:00
|
|
|
|
|
|
|
return needed_module
|
|
|
|
|
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
def get_it_and_children(l_module):
|
2015-04-30 17:36:36 +02:00
|
|
|
"""
|
2015-05-04 10:30:22 +02:00
|
|
|
From a list of module return the module and all of the genealogy
|
2015-04-30 17:36:36 +02:00
|
|
|
"""
|
2015-05-04 10:30:22 +02:00
|
|
|
d_ref = get_dict_genealogy()
|
|
|
|
|
|
|
|
l = []
|
2015-04-30 17:36:36 +02:00
|
|
|
for module in l_module:
|
|
|
|
if module not in l:
|
|
|
|
l.append(module)
|
2015-05-04 14:44:30 +02:00
|
|
|
try:
|
|
|
|
l.extend(get_it_and_children(d_ref[module]))
|
|
|
|
except KeyError:
|
|
|
|
print >> sys.stderr, "`{0}` in not a good submodule name".format(module)
|
|
|
|
print >> sys.stderr, "Check the corresponding NEEDED_CHILDREN_MODULES"
|
|
|
|
sys.exit(1)
|
2015-04-30 17:36:36 +02:00
|
|
|
|
|
|
|
return list(set(l))
|
|
|
|
|
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
def get_all_children(l_module):
|
|
|
|
"""
|
|
|
|
From a list of module return all the genealogy
|
|
|
|
"""
|
|
|
|
|
|
|
|
it_and_all = get_it_and_children(l_module)
|
|
|
|
return [children for children in it_and_all if children not in l_module]
|
|
|
|
|
2015-04-30 17:36:36 +02:00
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
def reduce_(l_module):
|
2015-04-30 17:36:36 +02:00
|
|
|
"""
|
2015-05-04 10:30:22 +02:00
|
|
|
Take a l_module and try to find the lower combinaitions
|
|
|
|
of module with the same genealogy
|
2015-04-30 17:36:36 +02:00
|
|
|
"""
|
|
|
|
import itertools
|
2015-05-04 10:30:22 +02:00
|
|
|
d_ref = get_dict_genealogy()
|
2015-04-30 17:36:36 +02:00
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
target_genealogy = sorted(get_all_children(l_module))
|
2015-04-30 17:36:36 +02:00
|
|
|
|
|
|
|
for i in xrange(len(d_ref)):
|
|
|
|
for c in itertools.combinations(d_ref, i):
|
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
guess_genealogy = sorted(get_it_and_children(d_ref, c))
|
2015-04-30 17:36:36 +02:00
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
if target_genealogy == guess_genealogy:
|
2015-04-30 17:36:36 +02:00
|
|
|
return c
|
|
|
|
|
|
|
|
|
2015-05-04 12:30:31 +02:00
|
|
|
def create_png_from_path(path):
|
|
|
|
" Change a path like this into a module list"
|
|
|
|
"path = /home/razoa/quantum_package/src/Molden/NEEDED_CHILDREN_MODULES"
|
|
|
|
|
|
|
|
l_module = os.path.split(path)[0].split("/")[-1]
|
|
|
|
create_png([l_module])
|
|
|
|
|
|
|
|
|
|
|
|
def create_png(l_module):
|
|
|
|
"""Create the png of the dependancy tree for a l_module"""
|
|
|
|
|
|
|
|
# Init
|
|
|
|
import pydot
|
|
|
|
all_ready_done = []
|
|
|
|
|
|
|
|
def draw_module_edge(module, l_children):
|
|
|
|
"Draw all the module recursifly"
|
|
|
|
|
|
|
|
if module not in all_ready_done:
|
|
|
|
for children in l_children:
|
|
|
|
# Add Edge
|
|
|
|
edge = pydot.Edge(module, children)
|
|
|
|
graph.add_edge(edge)
|
|
|
|
# Recurs
|
|
|
|
draw_module_edge(children, d_ref[children])
|
|
|
|
all_ready_done.append(module)
|
|
|
|
|
|
|
|
# Init
|
|
|
|
graph = pydot.Dot(graph_type='digraph')
|
|
|
|
d_ref = get_dict_genealogy()
|
|
|
|
|
|
|
|
# Create all the edge
|
|
|
|
for module in l_module:
|
|
|
|
node_a = pydot.Node(module, fontcolor="red")
|
|
|
|
graph.add_node(node_a)
|
|
|
|
draw_module_edge(module, d_ref[module])
|
|
|
|
|
|
|
|
# Save
|
|
|
|
path = '{0}.png'.format("_".join(l_module))
|
2015-05-04 15:36:58 +02:00
|
|
|
print "png saved in {0}".format(path)
|
2015-05-04 12:30:31 +02:00
|
|
|
graph.write_png(path)
|
|
|
|
|
2015-04-30 17:36:36 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2015-05-04 12:30:31 +02:00
|
|
|
arguments = docopt(__doc__)
|
|
|
|
|
|
|
|
if not arguments['<NEEDED_CHILDREN_MODULES>']:
|
|
|
|
dir_ = os.getcwd()
|
|
|
|
path = os.path.join(dir_, "NEEDED_CHILDREN_MODULES")
|
|
|
|
else:
|
|
|
|
path = os.path.abspath(arguments['<NEEDED_CHILDREN_MODULES>'])
|
|
|
|
path = os.path.expanduser(path)
|
|
|
|
path = os.path.expandvars(path)
|
2015-04-30 17:36:36 +02:00
|
|
|
|
2015-05-04 15:03:53 +02:00
|
|
|
if arguments['print_genealogy']:
|
|
|
|
l_all_needed_molule = module_genealogy(path)
|
|
|
|
print " ".join(sorted(l_all_needed_molule))
|
2015-05-04 12:30:31 +02:00
|
|
|
|
2015-05-04 15:36:58 +02:00
|
|
|
elif arguments["check_dependencies"]:
|
|
|
|
l_module = arguments['<module_name>']
|
|
|
|
if l_module:
|
|
|
|
l_all_needed_molule = get_it_and_children(l_module)
|
|
|
|
else:
|
|
|
|
l_all_needed_molule = module_genealogy(path)
|
|
|
|
|
|
|
|
elif arguments["create_png"]:
|
2015-05-04 12:30:31 +02:00
|
|
|
create_png_from_path(path)
|