<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import os.path

dir_ = '/home/razoa/quantum_package/src'


def get_dict_genealogy(all_children=False):

    d_ref = dict()

    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

    if all_children:
        for module in d_ref:
            d_ref[module] = get_all_children(d_ref, d_ref[module], [])

    return d_ref


def module_children_to_all(d_ref,path):

    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:
        needed_module = l_children
        for module in l_children:

            for children in get_all_children(d_ref, d_ref[module], []):
                if children not in needed_module:
                    needed_module.append(children)

        return needed_module


def get_all_children(d_ref, l_module, l=[]):
    """
    From a d_ref (who containt all the data --flatter or not-- create
        an flatten list who contain all the children
    """
    for module in l_module:
        if module not in l:
            l.append(module)
            get_all_children(d_ref, d_ref[module], l)

    return list(set(l))


def reduce_(d_ref, name):

    """
    Take a big list and try to find the lower parent
    available
    """
    import itertools

    a = sorted(get_all_children(d_ref[name]))

    for i in xrange(len(d_ref)):
        for c in itertools.combinations(d_ref, i):

                l = []
                b = sorted(get_all_children(c, l))

                if a == b:
                    return c

#for i in sorted(d_ref):
#    print i, reduce_(i)
#

if __name__ == '__main__':
    import sys

    try:
        path = sys.argv[1]
    except IndexError:
        path = None

    d_ref = get_dict_genealogy()

    l_all_needed_molule = module_children_to_all(d_ref, path)
    print " ".join(sorted(l_all_needed_molule))

#    print d_ref
#
#    d_ref = get_dict_genealogy(True)
#
#    print d_ref
#
#    module_hl_to_ll(d_ref)
<commit_msg>Update only_chidren_to_all_genealogy.py  with relative path<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import os.path

def get_dict_genealogy(all_children=False):

    qpackage_root = os.environ['QPACKAGE_ROOT']
    dir_ = os.path.join(qpackage_root,'src')

    d_ref = dict()

    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

    if all_children:
        for module in d_ref:
            d_ref[module] = get_all_children(d_ref, d_ref[module], [])

    return d_ref


def module_children_to_all(d_ref,path):

    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:
        needed_module = l_children
        for module in l_children:

            for children in get_all_children(d_ref, d_ref[module], []):
                if children not in needed_module:
                    needed_module.append(children)

        return needed_module


def get_all_children(d_ref, l_module, l=[]):
    """
    From a d_ref (who containt all the data --flatter or not-- create
        an flatten list who contain all the children
    """
    for module in l_module:
        if module not in l:
            l.append(module)
            get_all_children(d_ref, d_ref[module], l)

    return list(set(l))


def reduce_(d_ref, name):

    """
    Take a big list and try to find the lower parent
    available
    """
    import itertools

    a = sorted(get_all_children(d_ref[name]))

    for i in xrange(len(d_ref)):
        for c in itertools.combinations(d_ref, i):

                l = []
                b = sorted(get_all_children(c, l))

                if a == b:
                    return c

#for i in sorted(d_ref):
#    print i, reduce_(i)
#

if __name__ == '__main__':
    import sys

    try:
        path = sys.argv[1]
    except IndexError:
        path = None

    d_ref = get_dict_genealogy()

    l_all_needed_molule = module_children_to_all(d_ref, path)
    print " ".join(sorted(l_all_needed_molule))

#    print d_ref
#
#    d_ref = get_dict_genealogy(True)
#
#    print d_ref
#
#    module_hl_to_ll(d_ref)

{'fraction_non_alphanumeric': 0.10458063166701527, 'fraction_numerical': 0.0008366450533361222, 'mean_word_length': 2.109232769830949, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7551053', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1627, 'n_words': 396}