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

[graph] Changed graph info json output #13934

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions conan/cli/formatters/graph/graph_info_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

def filter_graph(graph, package_filter, field_filter=None):
if package_filter is not None:
graph["nodes"] = [n for n in graph["nodes"]
if any(fnmatch.fnmatch(n["ref"] or "", p) for p in package_filter)]
graph["nodes"] = {id_: n for id_, n in graph["nodes"].items()
if any(fnmatch.fnmatch(n["ref"] or "", p) for p in package_filter)}
if field_filter is not None:
if "ref" not in field_filter:
field_filter.append("ref")
result = []
for n in graph["nodes"]:
result = {}
for id_, n in graph["nodes"].items():
new_node = OrderedDict((k, v) for k, v in n.items() if k in field_filter)
result.append(new_node)
result[id_] = new_node
graph["nodes"] = result
return graph

Expand All @@ -31,7 +31,7 @@ def format_graph_info(result):
out.title("Basic graph information")
serial = graph.serialize()
serial = filter_graph(serial, package_filter, field_filter)
for n in serial["nodes"]:
for _, n in serial["nodes"].items():
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
out.writeln(f"{n['ref']}:") # FIXME: This can be empty for consumers and it is ugly ":"
_serial_pretty_printer(n, indent=" ")
if graph.error:
Expand Down
21 changes: 19 additions & 2 deletions conans/client/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ def __repr__(self):
return repr(self.conanfile)

def serialize(self):
def _topics():
franramirez688 marked this conversation as resolved.
Show resolved Hide resolved
ret = self.conanfile.topics
if ret and isinstance(ret, (list, tuple)):
return ret
elif ret:
return [ret]
else:
return None

result = OrderedDict()
result["ref"] = self.ref.repr_notime() if self.ref is not None else "conanfile"
result["id"] = getattr(self, "id") # Must be assigned by graph.serialize()
Expand All @@ -216,6 +225,11 @@ def serialize(self):
result["prev"] = self.prev
from conans.client.installer import build_id
result["build_id"] = build_id(self.conanfile)
result['url'] = self.conanfile.url,
result['homepage'] = self.conanfile.homepage,
result['license'] = self.conanfile.license,
result['author'] = self.conanfile.author,
result['topics'] = _topics()
result["binary"] = self.binary
# TODO: This doesn't match the model, check it
result["invalid_build"] = self.cant_build
Expand All @@ -224,7 +238,8 @@ def serialize(self):
result.update(self.conanfile.serialize())
result["context"] = self.context
result["test"] = self.test
result["requires"] = {n.id: n.ref.repr_notime() for n in self.neighbors()}
result["transitive_deps"] = {d.node.id: d.require.serialize()
franramirez688 marked this conversation as resolved.
Show resolved Hide resolved
for d in self.transitive_deps.values() if d.node is not None}
return result

def overrides(self):
Expand Down Expand Up @@ -381,6 +396,8 @@ def serialize(self):
for i, n in enumerate(self.nodes):
n.id = i
result = OrderedDict()
result["nodes"] = [n.serialize() for n in self.nodes]
result["nodes"] = {n.id: n.serialize() for n in self.nodes}
result["root"] = {self.root.id: repr(self.root.ref)} # TODO: ref of consumer/virtual
result["overrides"] = self.overrides().serialize()
result["resolved-ranges"] = {repr(r): s.repr_notime() for r, s in self.resolved_ranges.items()}
return result
12 changes: 6 additions & 6 deletions conans/test/integration/command/info/info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MyTest(ConanFile):
""")
client.save({"conanfile.py": conanfile})
client.run("graph info . --format=json")
recipe = json.loads(client.stdout)["nodes"][0]
recipe = json.loads(client.stdout)["nodes"]["0"]
assert type(recipe["topics"]) == list
assert recipe["topics"] == ["foo"]
assert type(recipe["provides"]) == list
Expand All @@ -90,7 +90,7 @@ class MyTest(ConanFile):
""")
client2.save({"conanfile.py": conanfile2})
client2.run("graph info . --format=json")
recipe = json.loads(client2.stdout)["nodes"][0]
recipe = json.loads(client2.stdout)["nodes"]["0"]
assert type(recipe["topics"]) == list
assert recipe["topics"] == ["foo"]
assert type(recipe["provides"]) == list
Expand Down Expand Up @@ -163,18 +163,18 @@ def test_json_package_filter(self):
conanfile = GenConanfile("pkg", "0.1").with_setting("build_type")
client.save({"conanfile.py": conanfile})
client.run("graph info . --package-filter=nothing --format=json")
assert '"nodes": []' in client.out
assert '"nodes": {}' in client.out
client.run("graph info . --package-filter=pkg* --format=json")
graph = json.loads(client.stdout)
assert graph["nodes"][0]["ref"] == "pkg/0.1"
assert graph["nodes"]["0"]["ref"] == "pkg/0.1"

def test_json_info_outputs(self):
client = TestClient()
conanfile = GenConanfile("pkg", "0.1").with_setting("build_type")
client.save({"conanfile.py": conanfile})
client.run("graph info . -s build_type=Debug --format=json")
graph = json.loads(client.stdout)
assert graph["nodes"][0]["settings"]["build_type"] == "Debug"
assert graph["nodes"]["0"]["settings"]["build_type"] == "Debug"


class TestAdvancedCliOutput:
Expand All @@ -199,7 +199,7 @@ class pkg(ConanFile):

client.run("graph info . --format=json")
info = json.loads(client.stdout)
assert info["nodes"][0]["python_requires"] == ['tool/0.1#4d670581ccb765839f2239cc8dff8fbd']
assert info["nodes"]["0"]["python_requires"] == ['tool/0.1#4d670581ccb765839f2239cc8dff8fbd']

def test_build_id_info(self):
client = TestClient()
Expand Down
4 changes: 2 additions & 2 deletions conans/test/integration/graph/test_validate_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def package_id(self):
# What happens with a conan info?
t.run(f"graph info --requires=foo/1.0 {settings_gcc} --format=json", redirect_stdout="myjson")
myjson = json.loads(t.load("myjson"))["nodes"]
assert myjson[1]["invalid_build"] == "This doesn't build in GCC"
assert myjson["1"]["invalid_build"] == "This doesn't build in GCC"

t.run(f"graph info --requires=foo/1.0 {settings_clang} --format=json", redirect_stdout="myjson")
myjson = json.loads(t.load("myjson"))["nodes"]
assert myjson[1]["invalid_build"] is False
assert myjson["1"]["invalid_build"] is False


def test_with_options_validate_build_test():
Expand Down