def treatment_final_graph(gr,
                          remove_isolated_nodes=False,
                          remove_sink_nodes=False,
                          remove_source_nodes=False,
                          only_cyclic=False,
                          verbosity=1):
    if only_cyclic:
        for edge, properties in gr.edge_properties.items():
            if not unicode(CYCLE_LABEL) in properties['label']:
                if print_log_info(verbosity):
                    log.info("Remove the edge %s-->%s" % edge)
                gr.del_edge(edge)
    if remove_source_nodes:
        for node, incidence in gr.node_incidence.items():
            if not incidence:
                if print_log_info(verbosity):
                    log.info("Remove the node %s" % node)
                gr.del_node(node)
    if remove_sink_nodes:
        for node, neighbor in gr.node_neighbors.items():
            if not neighbor:
                if print_log_info(verbosity):
                    log.info("Remove the node %s" % node)
                gr.del_node(node)
    if remove_isolated_nodes:
        for node, incidence in gr.node_incidence.items():
            neighbor = gr.node_neighbors.get(node, None)
            if not incidence and not neighbor:
                if print_log_info(verbosity):
                    log.info("Remove the node %s" % node)
                gr.del_node(node)
    return gr
Esempio n. 2
0
def treatment_final_graph(gr, remove_isolated_nodes=False, remove_sink_nodes=False,
                          remove_source_nodes=False, only_cyclic=False, verbosity=1):
    if only_cyclic:
        for edge, properties in gr.edge_properties.items():
            if not unicode(CYCLE_LABEL) in properties['label']:
                if print_log_info(verbosity):
                    log.info("Remove the edge %s-->%s" % edge)
                gr.del_edge(edge)
    if remove_source_nodes:
        for node, incidence in gr.node_incidence.items():
            if not incidence:
                if print_log_info(verbosity):
                    log.info("Remove the node %s" % node)
                gr.del_node(node)
    if remove_sink_nodes:
        for node, neighbor in gr.node_neighbors.items():
            if not neighbor:
                if print_log_info(verbosity):
                    log.info("Remove the node %s" % node)
                gr.del_node(node)
    if remove_isolated_nodes:
        for node, incidence in gr.node_incidence.items():
            neighbor = gr.node_neighbors.get(node, None)
            if not incidence and not neighbor:
                if print_log_info(verbosity):
                    log.info("Remove the node %s" % node)
                gr.del_node(node)
    return gr
def create_graph_apps_dependence(file_name,
                                 include_apps=None,
                                 exclude_apps=None,
                                 exclude_packages=None,
                                 verbosity=1,
                                 show_modules=False,
                                 remove_isolate_nodes=False,
                                 remove_sink_nodes=False,
                                 remove_source_nodes=False,
                                 only_cyclic=False,
                                 scope=None,
                                 use_colors=True,
                                 dotted_scope_local=False,
                                 layout='dot'):
    if only_cyclic:
        remove_isolate_nodes = True
    start_time = datetime.datetime.now()
    gr = create_graph(include_apps, exclude_apps, exclude_packages, verbosity,
                      show_modules, scope, use_colors, dotted_scope_local)
    find_all_cycle(gr, use_colors=use_colors)
    treatment_final_graph(gr,
                          remove_isolate_nodes,
                          remove_sink_nodes,
                          remove_source_nodes,
                          only_cyclic,
                          verbosity=verbosity)
    if file_name:
        print_graph(gr, file_name, layout)
    if print_log_info(verbosity):
        log.info("Duration: %s" % str(datetime.datetime.now() - start_time))
    return gr
def _add_edge(gr, node1, node2, verbosity=1, style="filled"):
    if print_log_info(verbosity):
        log.info('\t %s --> %s' % (node1, node2))
    if not gr.has_edge((node1, node2)):
        gr.add_edge((node1, node2))
        gr.set_edge_label((node1, node2), "(1)")
    else:
        weight = gr.edge_weight((node1, node2)) + 1
        gr.set_edge_weight((node1, node2), weight)
        gr.set_edge_label((node1, node2), "(%s)" % weight)
    attributes = dict(gr.edge_attributes((node1, node2)))
    attributes['style'] = style
    gr.edge_attr[(node1, node2)] = attributes.items()
def _add_edge(gr, node1, node2, verbosity=1, style="filled"):
    if print_log_info(verbosity):
        log.info('\t %s --> %s' % (node1, node2))
    if not gr.has_edge((node1, node2)):
        gr.add_edge((node1, node2))
        gr.set_edge_label((node1, node2), "(1)")
    else:
        weight = gr.edge_weight((node1, node2)) + 1
        gr.set_edge_weight((node1, node2), weight)
        gr.set_edge_label((node1, node2), "(%s)" % weight)
    attributes = dict(gr.edge_attributes((node1, node2)))
    attributes['style'] = style
    gr.edge_attr[(node1, node2)] = attributes.items()
def create_graph(include_apps=None, exclude_apps=None, exclude_packages=None, verbosity=0,
                 show_modules=False, scope=None, use_colors=True, dotted_scope_local=False):
    gr = digraph()
    applications = get_applications(include_apps, exclude_apps)
    if not show_modules:
        gr.add_nodes(applications)
    pyplete = PyPlete(scope=scope)
    pyplete_global = None
    if dotted_scope_local:
        pyplete_global = PyPlete(scope=SCOPE_GLOBAL)
    for app_source in applications:
        if print_log_info(verbosity):
            log.info("Analizing %s" % app_source)
        _add_edges_to_package(gr, app_source, app_source, applications, pyplete,
                              exclude_packages, show_modules, verbosity,
                              scope, use_colors, pyplete_global)
    return gr
def create_graph_apps_dependence(file_name, include_apps=None, exclude_apps=None,
                                 exclude_packages=None, verbosity=1, show_modules=False,
                                 remove_isolate_nodes=False, remove_sink_nodes=False,
                                 remove_source_nodes=False, only_cyclic=False, scope=None,
                                 use_colors=True, dotted_scope_local=False,
                                 layout='dot'):
    if only_cyclic:
        remove_isolate_nodes = True
    start_time = datetime.datetime.now()
    gr = create_graph(include_apps, exclude_apps, exclude_packages, verbosity, show_modules, scope, use_colors, dotted_scope_local)
    find_all_cycle(gr, use_colors=use_colors)
    treatment_final_graph(gr, remove_isolate_nodes, remove_sink_nodes, remove_source_nodes,
                              only_cyclic, verbosity=verbosity)
    if file_name:
        print_graph(gr, file_name, layout)
    if print_log_info(verbosity):
        log.info("Duration: %s" % str(datetime.datetime.now() - start_time))
    return gr
def create_graph(include_apps=None,
                 exclude_apps=None,
                 exclude_packages=None,
                 verbosity=0,
                 show_modules=False,
                 scope=None,
                 use_colors=True,
                 dotted_scope_local=False):
    gr = digraph()
    applications = get_applications(include_apps, exclude_apps)
    if not show_modules:
        gr.add_nodes(applications)
    pyplete = PyPlete(scope=scope)
    pyplete_global = None
    if dotted_scope_local:
        pyplete_global = PyPlete(scope=SCOPE_GLOBAL)
    for app_source in applications:
        if print_log_info(verbosity):
            log.info("Analizing %s" % app_source)
        _add_edges_to_package(gr, app_source, app_source, applications,
                              pyplete, exclude_packages, show_modules,
                              verbosity, scope, use_colors, pyplete_global)
    return gr
    importables_to_app = []
    try:
        pyplete.get_importables_rest_level(importables_to_app,
                                           package_modules[0],
                                           package_modules[1:],
                                           into_module=False)
    except SyntaxError, e:
        if print_log_error(verbosity):
            log.error("\t File: %s SyntaxError %s" % (package_modules, e))

    for imp in importables_to_app:
        importable_to_app = imp['text']
        importable_type = imp['category']
        if importable_type == 'package':
            if exclude_packages and importable_to_app in exclude_packages:
                if print_log_info(verbosity):
                    log.info('\t Ignore %s' % importable_to_app)
                continue
            subpackage = '%s.%s' % (package, importable_to_app)
            if subpackage not in settings.INSTALLED_APPS:
                _add_edges_to_package(gr,
                                      subpackage,
                                      app_source,
                                      applications,
                                      pyplete,
                                      exclude_packages=exclude_packages,
                                      show_modules=show_modules,
                                      verbosity=verbosity,
                                      scope=scope,
                                      use_colors=use_colors,
                                      pyplete_global=pyplete_global)
                          pyplete_global=None):
    pyplete = pyplete or PyPlete(scope=scope)
    package_modules = package.split(".")
    importables_to_app = []
    try:
        pyplete.get_importables_rest_level(importables_to_app, package_modules[0], package_modules[1:], into_module=False)
    except SyntaxError, e:
        if print_log_error(verbosity):
            log.error("\t File: %s SyntaxError %s" % (package_modules, e))

    for imp  in importables_to_app:
        importable_to_app = imp['text']
        importable_type = imp['category']
        if importable_type == 'package':
            if exclude_packages and importable_to_app in exclude_packages:
                if print_log_info(verbosity):
                    log.info('\t Ignore %s' % importable_to_app)
                continue
            subpackage = '%s.%s' % (package, importable_to_app)
            if subpackage not in settings.INSTALLED_APPS:
                _add_edges_to_package(gr, subpackage, app_source, applications, pyplete,
                                      exclude_packages=exclude_packages,
                                      show_modules=show_modules,
                                      verbosity=verbosity,
                                      scope=scope,
                                      use_colors=use_colors,
                                      pyplete_global=pyplete_global)
        if importable_type != 'module':
            continue
        if show_modules:
            node = package_modules + [importable_to_app]
def _add_edges_to_package(gr,
                          package,
                          app_source,
                          applications,
                          pyplete=None,
                          exclude_packages=None,
                          show_modules=False,
                          verbosity=1,
                          scope=None,
                          use_colors=True,
                          pyplete_global=None):
    pyplete = pyplete or PyPlete(scope=scope)
    package_modules = package.split(".")
    importables_to_app = []
    try:
        pyplete.get_importables_rest_level(importables_to_app,
                                           package_modules[0],
                                           package_modules[1:],
                                           into_module=False)
    except SyntaxError as e:
        if print_log_error(verbosity):
            log.error("\t File: %s SyntaxError %s" % (package_modules, e))

    for imp in importables_to_app:
        importable_to_app = imp['text']
        importable_type = imp['category']
        if importable_type == 'package':
            if exclude_packages and importable_to_app in exclude_packages:
                if print_log_info(verbosity):
                    log.info('\t Ignore %s' % importable_to_app)
                continue
            subpackage = '%s.%s' % (package, importable_to_app)
            if subpackage not in settings.INSTALLED_APPS:
                _add_edges_to_package(gr,
                                      subpackage,
                                      app_source,
                                      applications,
                                      pyplete,
                                      exclude_packages=exclude_packages,
                                      show_modules=show_modules,
                                      verbosity=verbosity,
                                      scope=scope,
                                      use_colors=use_colors,
                                      pyplete_global=pyplete_global)
        if importable_type != 'module':
            continue
        if show_modules:
            node = package_modules + [importable_to_app]
            node_source = _add_node_init('.'.join(node), applications)
            if not gr.has_node(node_source):
                _add_node_module(gr,
                                 node_source,
                                 applications,
                                 app_source=app_source,
                                 use_colors=use_colors)
        else:
            node_source = None
        code = pyplete.get_imp_loader_from_path(
            package_modules[0],
            package_modules[1:] + [importable_to_app])[0].get_source()
        try:
            imports_code = pyplete.get_pysmell_modules_to_text(
                code)['POINTERS']
        except SyntaxError as e:
            if print_log_error(verbosity):
                log.error("\t File: %s SyntaxError %s" %
                          (package_modules + [importable_to_app], e))
            continue
        for import_code in list(imports_code.values()):
            if isinstance(import_code, list):
                for i in import_code:
                    _add_edge_from_import_code(gr,
                                               applications,
                                               app_source,
                                               package_modules,
                                               i,
                                               code,
                                               node_source=node_source,
                                               pyplete=pyplete,
                                               show_modules=show_modules,
                                               verbosity=verbosity,
                                               scope=scope,
                                               use_colors=use_colors,
                                               pyplete_global=pyplete_global)
            else:
                _add_edge_from_import_code(gr,
                                           applications,
                                           app_source,
                                           package_modules,
                                           import_code,
                                           code,
                                           node_source=node_source,
                                           pyplete=pyplete,
                                           show_modules=show_modules,
                                           verbosity=verbosity,
                                           scope=scope,
                                           use_colors=use_colors,
                                           pyplete_global=pyplete_global)