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 7 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
10 changes: 3 additions & 7 deletions conan/cli/formatters/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from conans.util.files import load


# FIXME: Check all this code when format_graph_[html/dot] use serialized graph

class _PrinterGraphItem(object):
def __init__(self, _id, node, is_build_time_node):
self.id = _id
Expand All @@ -39,19 +41,13 @@ def is_build_requires(self):
return self._is_build_time_node

def data(self):

def ensure_iterable(value):
if isinstance(value, (list, tuple)):
return value
return value,

return {
'build_id': build_id(self._conanfile),
'url': self._conanfile.url,
'homepage': self._conanfile.homepage,
'license': self._conanfile.license,
'author': self._conanfile.author,
'topics': ensure_iterable(self._conanfile.topics) if self._conanfile.topics else None
'topics': self._conanfile.topics
}


Expand Down
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
8 changes: 6 additions & 2 deletions conans/client/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,11 @@ def serialize(self):
result["info_invalid"] = getattr(getattr(self.conanfile, "info", None), "invalid", None)
# Adding the conanfile information: settings, options, etc
result.update(self.conanfile.serialize())
result.pop("requires", None) # superseded by "dependencies" (graph.transitive_deps)
result["dependencies"] = {d.node.id: d.require.serialize()
for d in self.transitive_deps.values() if d.node is not None}
result["context"] = self.context
result["test"] = self.test
result["requires"] = {n.id: n.ref.repr_notime() for n in self.neighbors()}
return result

def overrides(self):
Expand Down Expand Up @@ -381,6 +383,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: 5 additions & 7 deletions conans/model/conan_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,12 @@ def serialize(self):
for a in ("name", "user", "channel", "url", "license",
"author", "description", "homepage", "build_policy", "upload_policy",
"revision_mode", "provides", "deprecated", "win_bash", "win_bash_run",
"default_options", "options_description",):
"default_options", "options_description"):
v = getattr(self, a, None)
if v is not None:
result[a] = v
if self.version is not None:
result["version"] = str(self.version)
if self.topics is not None:
result["topics"] = list(self.topics)
result[a] = v

result["version"] = str(self.version) if self.version is not None else None
result["topics"] = list(self.topics) if self.topics is not None else None
result["package_type"] = str(self.package_type)

settings = self.settings
Expand Down
39 changes: 28 additions & 11 deletions conans/test/integration/command/create_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def system_requirements(self):
client.run("create . --name=pkg --version=0.1 --user=lasote")
assert "pkg/0.1@lasote:" in client.out


def test_error_create_name_version(self):
client = TestClient()
client.save({"conanfile.py": GenConanfile().with_name("hello").with_version("1.2")})
Expand All @@ -87,7 +86,7 @@ def test_create_user_channel(self):
self.assertIn("pkg/0.1@lasote/channel: Generating the package", client.out)
client.run("list * -c")
self.assertIn("pkg/0.1@lasote/channel", client.out)

# test default without user and channel
client.run("create . ")
self.assertIn("pkg/0.1: Generating the package", client.out)
Expand Down Expand Up @@ -308,7 +307,7 @@ def package_info(self):
self.client.save({"conanfile.py": conanfile})
self.client.run("create . --format=json")
data = json.loads(self.client.stdout)
cpp_info_data = data["graph"]["nodes"][1]["cpp_info"]
cpp_info_data = data["graph"]["nodes"]["1"]["cpp_info"]
self.assertIn("libpkg1", cpp_info_data["pkg1"]["libs"])
self.assertListEqual([], cpp_info_data["pkg1"]["requires"])
self.assertIn("libpkg2", cpp_info_data["pkg2"]["libs"])
Expand Down Expand Up @@ -370,7 +369,7 @@ def test_create_format_json():
....
},
{'ref': 'hello/0.1#18d5440ae45afc4c36139a160ac071c7',
'requires': {'2': 'pkg/0.2#db78b8d06a78af5c3ac56706f133098d'},
'dependencies': {'2': 'pkg/0.2#db78b8d06a78af5c3ac56706f133098d'},
franramirez688 marked this conversation as resolved.
Show resolved Hide resolved
....
},
{'ref': 'pkg/0.2#44a1a27ac2ea1fbcf434a05c4d57388d',
Expand Down Expand Up @@ -437,7 +436,7 @@ class MyTest(ConanFile):
pkg_pkg_ref = 'pkg/0.2#db78b8d06a78af5c3ac56706f133098d'
consumer_info = hello_pkg_info = pkg_pkg_info = None

for n in nodes:
for _, n in nodes.items():
ref = n["ref"]
if ref == consumer_ref:
consumer_info = n
Expand All @@ -456,21 +455,39 @@ class MyTest(ConanFile):
assert consumer_info["settings"] == {'arch': 'x86', 'build_type': 'Debug', 'compiler': 'gcc',
'compiler.libcxx': 'libstdc++', 'compiler.version': '12',
'os': 'Linux'}
assert consumer_info["requires"] == {'1': hello_pkg_ref}
consumer_deps = {
'1': {'ref': 'hello/0.1', 'run': 'False', 'libs': 'True', 'skip': 'False',
'test': 'False', 'force': 'False', 'direct': 'True', 'build': 'False',
'transitive_headers': 'None', 'transitive_libs': 'None', 'headers': 'True',
'package_id_mode': 'None', 'visible': 'True'},
'2': {'ref': 'pkg/0.2', 'run': 'False', 'libs': 'True', 'skip': 'False', 'test': 'False',
'force': 'False', 'direct': 'False', 'build': 'False', 'transitive_headers': 'None',
'transitive_libs': 'None', 'headers': 'True', 'package_id_mode': 'None',
'visible': 'True'}
}
assert consumer_info["dependencies"] == consumer_deps
# hello/0.1 pkg information
assert hello_pkg_info["package_id"] == "8eba237c0fb239fcb7daa47979ab99258eaaa7d1"
assert hello_pkg_info["prev"] == "d95380a07c35273509dfc36b26f6cec1"
assert hello_pkg_info["settings"] == {}
assert hello_pkg_info["options"] == {}
assert hello_pkg_info["requires"] == {'2': pkg_pkg_ref}
hello_pkg_info_deps = {
"2": {
"ref": "pkg/0.2", "run": "False", "libs": "True", "skip": "False", "test": "False",
"force": "False", "direct": "True", "build": "False", "transitive_headers": "None",
"transitive_libs": "None", "headers": "True", "package_id_mode": "semver_mode",
"visible": "True"
}
}
assert hello_pkg_info["dependencies"] == hello_pkg_info_deps
# pkg/0.2 pkg information
assert pkg_pkg_info["package_id"] == "fb1439470288b15b2da269ed97b1a5f2f5d1f766"
assert pkg_pkg_info["prev"] == "6949b0f89941d2a5994f9e6e4a89a331"
assert pkg_pkg_info["author"] == 'John Doe'
assert pkg_pkg_info["settings"] == {'build_type': 'Debug', 'compiler': 'gcc',
'compiler.libcxx': 'libstdc++', 'compiler.version': '12'}
assert pkg_pkg_info["options"] == {'fPIC': 'True', 'shared': 'False'}
assert pkg_pkg_info["requires"] == {}
assert pkg_pkg_info["dependencies"] == {}


def test_create_format_json_and_deps_cpp_info():
Expand All @@ -495,7 +512,7 @@ def test_create_format_json_and_deps_cpp_info():
'objects': None,
'properties': {'pkg_config_aliases': ['compo1_alias'],
'pkg_config_name': 'compo1'},
'requires': None,
'dependencies': None,
'resdirs': None,
'sharedlinkflags': None,
'srcdirs': None,
Expand All @@ -519,7 +536,7 @@ def test_create_format_json_and_deps_cpp_info():
'properties': {'pkg_config_aliases': ['pkg_alias1',
'pkg_alias2'],
'pkg_config_name': 'pkg_other_name'},
'requires': None,
'dependencies': None,
'resdirs': ['/path '
'with '
'spaces/.conan2/p/d15a235e212166d9/p/res'],
Expand Down Expand Up @@ -569,7 +586,7 @@ def package_info(self):
hello_pkg_ref = 'hello/0.1#18d5440ae45afc4c36139a160ac071c7'
pkg_pkg_ref = 'pkg/0.2#926714b5fb0a994f47ec37e071eba1da'
hello_cpp_info = pkg_cpp_info = None
for n in nodes:
for _, n in nodes.items():
ref = n["ref"]
if ref == hello_pkg_ref:
assert n['binary'] == "Build"
Expand Down
4 changes: 2 additions & 2 deletions conans/test/integration/command/export_pkg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def test_export_pkg_json(self):
# Wrong folders
client.run("export-pkg . --format=json", redirect_stdout="file.json")
graph = json.loads(client.load("file.json"))
assert "pkg/0.1" in graph["graph"]["nodes"][0]["ref"]
assert "pkg/0.1" in graph["graph"]["nodes"]["0"]["ref"]

def test_export_pkg_no_ref(self):
client = TestClient()
Expand Down Expand Up @@ -403,7 +403,7 @@ def package_info(self):
hello_pkg_ref = 'hello/0.1#18d5440ae45afc4c36139a160ac071c7'
pkg_pkg_ref = 'pkg/0.2#926714b5fb0a994f47ec37e071eba1da'
hello_cpp_info = pkg_cpp_info = None
for n in nodes:
for _, n in nodes.items():
ref = n["ref"]
if ref == hello_pkg_ref:
assert n['binary'] is None # The exported package has no binary status
Expand Down
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
44 changes: 22 additions & 22 deletions conans/test/integration/command/info/test_info_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def test_basic():
client.run(f"export . --user=user --channel=testing")
client.run(f"graph info --requires=package/0.1.0@user/testing --format=json")
nodes = json.loads(client.stdout)["nodes"]
assert client.cache_folder in nodes[1]["recipe_folder"]
assert os.path.basename(nodes[1]["recipe_folder"]).strip() == EXPORT_FOLDER
assert nodes[1]["source_folder"] is None
assert nodes[1]["build_folder"] is None
assert nodes[1]["package_folder"] is None
assert client.cache_folder in nodes["1"]["recipe_folder"]
assert os.path.basename(nodes["1"]["recipe_folder"]).strip() == EXPORT_FOLDER
assert nodes["1"]["source_folder"] is None
assert nodes["1"]["build_folder"] is None
assert nodes["1"]["package_folder"] is None


def test_build_id():
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_deps_basic(client_deps):
found_ref = False
assert len(nodes["nodes"]) == 3

for node in nodes["nodes"]:
for _, node in nodes["nodes"].items():
if node["ref"] == "conanfile":
assert node["source_folder"] is None
else:
Expand All @@ -103,22 +103,22 @@ def test_deps_specific_information(client_deps):
client_deps.run("graph info . --package-filter package/* --format=json")
nodes = json.loads(client_deps.stdout)["nodes"]
assert len(nodes) == 1
assert "package/0.1.0@user/testing" in nodes[0]["ref"]
assert nodes[0]["source_folder"] is None
assert nodes[0]["build_folder"] is None
assert nodes[0]["package_folder"] is None
assert "package/0.1.0@user/testing" in nodes["2"]["ref"]
assert nodes["2"]["source_folder"] is None
assert nodes["2"]["build_folder"] is None
assert nodes["2"]["package_folder"] is None

client_deps.run("graph info . --package-filter package* --format=json")
nodes = json.loads(client_deps.stdout)["nodes"]
assert len(nodes) == 2
assert "package2/0.2.0@user/testing" in nodes[0]["ref"]
assert nodes[0]["source_folder"] is None
assert nodes[0]["build_folder"] is None
assert nodes[0]["package_folder"] is None
assert "package/0.1.0@user/testing" in nodes[1]["ref"]
assert nodes[1]["source_folder"] is None
assert nodes[1]["build_folder"] is None
assert nodes[1]["package_folder"] is None
assert "package2/0.2.0@user/testing" in nodes["1"]["ref"]
assert nodes["1"]["source_folder"] is None
assert nodes["1"]["build_folder"] is None
assert nodes["1"]["package_folder"] is None
assert "package/0.1.0@user/testing" in nodes["2"]["ref"]
assert nodes["2"]["source_folder"] is None
assert nodes["2"]["build_folder"] is None
assert nodes["2"]["package_folder"] is None


def test_single_field():
Expand All @@ -128,10 +128,10 @@ def test_single_field():
client.run(f"graph info --requires package/0.1.0@user/testing --format=json")
nodes = json.loads(client.stdout)["nodes"]
assert len(nodes) == 2
assert "package/0.1.0@user/testing" in nodes[1]["ref"]
assert nodes[1]["source_folder"] is None
assert nodes[1]["build_folder"] is None
assert nodes[1]["package_folder"] is None
assert "package/0.1.0@user/testing" in nodes["1"]["ref"]
assert nodes["1"]["source_folder"] is None
assert nodes["1"]["build_folder"] is None
assert nodes["1"]["package_folder"] is None


def test_direct_conanfile():
Expand Down
4 changes: 2 additions & 2 deletions conans/test/integration/command/install/install_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def package_info(self):
hello_pkg_ref = 'hello/0.1' # no revision available
pkg_pkg_ref = 'pkg/0.2#926714b5fb0a994f47ec37e071eba1da'
hello_cpp_info = pkg_cpp_info = None
for n in nodes:
for _, n in nodes.items():
ref = n["ref"]
if ref == hello_pkg_ref:
assert n['binary'] is None
Expand All @@ -438,7 +438,7 @@ def package_info(self):
assert n['binary'] == "Cache"
pkg_cpp_info = n['cpp_info']

hello = nodes[0]
hello = nodes["0"]
assert hello["ref"] == hello_pkg_ref
assert hello["recipe_folder"] == client.current_folder
assert hello["build_folder"] == client.current_folder
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
Loading