2015-04-30 17:36:36 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
2015-05-04 10:30:22 +02:00
|
|
|
from functools import wraps
|
2015-04-30 17:36:36 +02:00
|
|
|
|
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
def cache(func):
|
|
|
|
saved = {}
|
|
|
|
|
|
|
|
@wraps(func)
|
|
|
|
def newfunc(*args):
|
|
|
|
if args in saved:
|
|
|
|
return saved[args]
|
|
|
|
|
|
|
|
result = func(*args)
|
|
|
|
saved[args] = result
|
|
|
|
return result
|
|
|
|
return newfunc
|
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
|
|
|
if not path:
|
|
|
|
dir_ = os.getcwd()
|
|
|
|
path = os.path.join(dir_, "NEEDED_CHILDREN_MODULES")
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(path, "r") as f:
|
|
|
|
l_children = f.read().split()
|
|
|
|
except IOError:
|
|
|
|
return []
|
|
|
|
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 10:30:22 +02:00
|
|
|
l.extend(get_it_and_children(d_ref[module]))
|
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
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
|
|
|
|
try:
|
|
|
|
path = sys.argv[1]
|
|
|
|
except IndexError:
|
|
|
|
path = None
|
|
|
|
|
2015-05-04 10:30:22 +02:00
|
|
|
l_all_needed_molule = module_genealogy(path)
|
2015-04-30 17:36:36 +02:00
|
|
|
print " ".join(sorted(l_all_needed_molule))
|