Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/graph templates #15915

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions conan/cli/formatters/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,23 @@ def data(self):
class _Grapher(object):
def __init__(self, deps_graph):
self._deps_graph = deps_graph
self.node_map, self.edges = self._build_graph()
self.node_map, self._edges = self._build_graph()
self._nodes = self.node_map.values()

@property
def nodes(self):
ConanOutput().warning("--format=html rendering using 'graph' object is deprecated and "
ConanOutput().warning("--format=html/dot rendering using 'graph' object is deprecated and "
"will be removed in future 2.X version. Please upgrade your template "
"to use 'deps_graph' instead", warn_tag="deprecated")
return self._nodes

@property
def edges(self):
ConanOutput().warning("--format=html/dot rendering using 'graph' object is deprecated and "
"will be removed in future 2.X version. Please upgrade your template "
"to use 'deps_graph' instead", warn_tag="deprecated")
return self._edges

def _build_graph(self):
graph_nodes = self._deps_graph.by_levels()
build_time_nodes = self._deps_graph.build_time_nodes()
Expand Down Expand Up @@ -95,10 +102,10 @@ def binary_color(node):


def _render_graph(graph, error, template, template_folder):
deps_graph = json.dumps(graph.serialize())
deps_graph = graph.serialize()
graph = _Grapher(graph)
from conans import __version__ as client_version
template = Template(template)
template = Template(template, autoescape=select_autoescape(['html', 'xml']))
return template.render(deps_graph=deps_graph, graph=graph, error=error,
base_template_path=template_folder, version=client_version)

Expand Down Expand Up @@ -126,11 +133,7 @@ def format_graph_html(result):
def format_graph_dot(result):
graph = result["graph"]
conan_api = result["conan_api"]
package_filter = result["package_filter"]
serial = graph.serialize()
# TODO: This is not used, it is necessary to update the renderings to use the serialized graph
# instead of the native graph
serial = filter_graph(serial, package_filter)

template_folder = os.path.join(conan_api.cache_folder, "templates")
user_template = os.path.join(template_folder, "graph.dot")
template = load(user_template) if os.path.isfile(user_template) else graph_info_dot
Expand Down
8 changes: 6 additions & 2 deletions conan/cli/formatters/graph/info_graph_dot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

graph_info_dot = """\
digraph {
{%- for src, dst in graph.edges %}
"{{ src.label }}" -> "{{ dst.label }}"
{%- for node_id, node in deps_graph["nodes"].items() %}
{%- for dep_id, dep in node["dependencies"].items() %}
{%- if dep["direct"] %}
"{{ node["label"] }}" -> "{{ deps_graph["nodes"][dep_id]["label"] }}"
{%- endif %}
{%- endfor %}
{%- endfor %}
}

Expand Down
10 changes: 5 additions & 5 deletions conan/cli/formatters/graph/info_graph_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</div>

<script type="text/javascript">
const graph_data = {{ deps_graph }};
const graph_data = {{ deps_graph | tojson }};
let hide_build = false;
let hide_test = false;
let search_pkg = null;
Expand Down Expand Up @@ -261,13 +261,13 @@
control.removeChild(control.firstChild);
}
if(ids[0] || ids_edges[0]) {
console.log("Selected ", ids_edges[0], global_edges.length);
console.log(" selected", global_edges[ids_edges[0]]);
selected = graph_data["nodes"][ids[0]] || global_edges[ids_edges[0]];
let div = document.createElement('div');
let f = Object.fromEntries(Object.entries(selected).filter(([_, v]) => v != null));
div.innerHTML = "<pre>" + JSON.stringify(f, undefined, 2) + "</pre>";
control.appendChild(div);
div.innerText = JSON.stringify(f, undefined, 2);
let div2 = document.createElement('div');
div2.innerHTML = "<pre>" + div.innerHTML + "</pre>";
control.appendChild(div2);
}
else {
control.innerHTML = "<b>Info</b>: Click on a package or edge for more info";
Expand Down