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][json] Same json output for conan graph info, create, install and export-pkg #13967

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 6 additions & 11 deletions conan/cli/commands/create.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import json
import os
import shutil

from conan.api.output import ConanOutput, cli_out_write
from conan.api.output import ConanOutput
from conan.cli.args import add_lockfile_args, add_common_install_arguments
from conan.cli.command import conan_command, OnceArgument
from conan.cli.commands.export import common_args_export
from conan.cli.args import add_lockfile_args, add_common_install_arguments
from conan.cli.formatters.graph import format_graph_json
from conan.cli.printers import print_profiles
from conan.cli.printers.graph import print_graph_packages, print_graph_basic
from conan.errors import ConanException
from conans.util.files import mkdir


def json_create(deps_graph):
if deps_graph is None:
return
cli_out_write(json.dumps({"graph": deps_graph.serialize()}, indent=4))


@conan_command(group="Creator", formatters={"json": json_create})
@conan_command(group="Creator", formatters={"json": format_graph_json})
def create(conan_api, parser, *args):
"""
Create a package.
Expand Down Expand Up @@ -102,7 +96,8 @@ def create(conan_api, parser, *args):
clean=args.lockfile_clean)

conan_api.lockfile.save_lockfile(lockfile, args.lockfile_out, cwd)
return deps_graph
return {"graph": deps_graph,
"conan_api": conan_api}
franramirez688 marked this conversation as resolved.
Show resolved Hide resolved


def _check_tested_reference_matches(deps_graph, tested_ref, out):
Expand Down
17 changes: 7 additions & 10 deletions conan/cli/commands/install.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import json
import os

from conan.api.output import ConanOutput, cli_out_write
from conan.api.output import ConanOutput
from conan.cli import make_abs_path
from conan.cli.args import common_graph_args, validate_common_graph_args
from conan.cli.command import conan_command
from conan.cli import make_abs_path
from conan.cli.formatters.graph import format_graph_json
from conan.cli.printers import print_profiles
from conan.cli.printers.graph import print_graph_packages, print_graph_basic


def json_install(info):
deps_graph = info
cli_out_write(json.dumps({"graph": deps_graph.serialize()}, indent=4))
franramirez688 marked this conversation as resolved.
Show resolved Hide resolved


@conan_command(group="Consumer", formatters={"json": json_install})
@conan_command(group="Consumer", formatters={"json": format_graph_json})
def install(conan_api, parser, *args):
"""
Install the requirements specified in a recipe (conanfile.py or conanfile.txt).
Expand Down Expand Up @@ -95,4 +90,6 @@ def install(conan_api, parser, *args):
lockfile = conan_api.lockfile.update_lockfile(lockfile, deps_graph, args.lockfile_packages,
clean=args.lockfile_clean)
conan_api.lockfile.save_lockfile(lockfile, args.lockfile_out, cwd)
return deps_graph
return {"graph": deps_graph,
"conan_api": conan_api}

6 changes: 3 additions & 3 deletions conan/cli/formatters/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ def format_graph_dot(result):

def format_graph_json(result):
graph = result["graph"]
field_filter = result["field_filter"]
package_filter = result["package_filter"]
field_filter = result.get("field_filter")
package_filter = result.get("package_filter")
serial = graph.serialize()
serial = filter_graph(serial, package_filter, field_filter)
serial = filter_graph(serial, package_filter=package_filter, field_filter=field_filter)
json_result = json.dumps(serial, indent=4)
cli_out_write(json_result)
if graph.error:
Expand Down
2 changes: 1 addition & 1 deletion conan/cli/formatters/graph/graph_info_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from conan.api.output import ConanOutput


def filter_graph(graph, package_filter, field_filter=None):
def filter_graph(graph, package_filter=None, field_filter=None):
if package_filter is not None:
graph["nodes"] = {id_: n for id_, n in graph["nodes"].items()
if any(fnmatch.fnmatch(n["ref"] or "", p) for p in package_filter)}
Expand Down
4 changes: 2 additions & 2 deletions conans/test/functional/command/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def build(self):
client.save({"conanfile.py": conanfile_scope_env}, clean_first=True)
client.run("build conanfile.py --build=missing")

assert hellopkg_result["graph"]["nodes"]["1"]["package_folder"] in client.out
assert hellotools_result["graph"]["nodes"]["1"]["package_folder"] in client.out
assert hellopkg_result["nodes"]["1"]["package_folder"] in client.out
assert hellotools_result["nodes"]["1"]["package_folder"] in client.out


def test_build_with_deps_env_info():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def system_requirements(self):
redirect_stdout="graph.json")
graph = json.loads(client.load("graph.json"))
assert {"apt-get": {"install": ["pkg1", "pkg2"], "missing": []}} == \
graph["graph"]["nodes"]["0"]["system_requires"]
graph["nodes"]["0"]["system_requires"]

# plain report, do not check
client.run("graph info . -c tools.system.package_manager:tool=apt-get "
Expand Down
37 changes: 18 additions & 19 deletions conans/test/integration/command/create_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,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["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 @@ -362,22 +362,21 @@ def test_create_format_json():
The result should be something like:

{
'graph': {
'nodes': [
{'ref': '', # consumer
'recipe': 'Virtual',
....
},
{'ref': 'hello/0.1#18d5440ae45afc4c36139a160ac071c7',
'dependencies': {'1': {'ref': 'hello/0.1', 'visible': 'True', ...}},
....
},
{'ref': 'pkg/0.2#44a1a27ac2ea1fbcf434a05c4d57388d',
....
}
],
'root': {'0': 'None'}
}
'nodes': [
{'ref': '', # consumer
'recipe': 'Virtual',
....
},
{'ref': 'hello/0.1#18d5440ae45afc4c36139a160ac071c7',
'dependencies': {'1': {'ref': 'hello/0.1', 'visible': 'True', ...}},
....
},
{'ref': 'pkg/0.2#44a1a27ac2ea1fbcf434a05c4d57388d',
....
}
],
'root': {'0': 'None'}
}
}
"""
client = TestClient()
Expand Down Expand Up @@ -430,7 +429,7 @@ class MyTest(ConanFile):
"host": profile_host, "build": profile_build}, clean_first=True)
client.run("create . -f json -pr:h host -pr:b build")
info = json.loads(client.stdout)
nodes = info["graph"]['nodes']
nodes = info['nodes']
consumer_ref = 'conanfile'
hello_pkg_ref = 'hello/0.1#18d5440ae45afc4c36139a160ac071c7'
pkg_pkg_ref = 'pkg/0.2#db78b8d06a78af5c3ac56706f133098d'
Expand Down Expand Up @@ -582,7 +581,7 @@ def package_info(self):
.with_require("pkg/0.2")}, clean_first=True)
client.run("create . -f json")
info = json.loads(client.stdout)
nodes = info["graph"]["nodes"]
nodes = info["nodes"]
hello_pkg_ref = 'hello/0.1#18d5440ae45afc4c36139a160ac071c7'
pkg_pkg_ref = 'pkg/0.2#926714b5fb0a994f47ec37e071eba1da'
hello_cpp_info = pkg_cpp_info = None
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["nodes"]["0"]["ref"]

def test_export_pkg_no_ref(self):
client = TestClient()
Expand Down Expand Up @@ -399,7 +399,7 @@ def package_info(self):
.with_require("pkg/0.2")}, clean_first=True)
client.run("export-pkg . -f json")
info = json.loads(client.stdout)
nodes = info["graph"]["nodes"]
nodes = info["nodes"]
hello_pkg_ref = 'hello/0.1#18d5440ae45afc4c36139a160ac071c7'
pkg_pkg_ref = 'pkg/0.2#926714b5fb0a994f47ec37e071eba1da'
hello_cpp_info = pkg_cpp_info = None
Expand Down
2 changes: 1 addition & 1 deletion conans/test/integration/command/install/install_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def package_info(self):
.with_require("pkg/0.2")}, clean_first=True)
client.run("install . -f json")
info = json.loads(client.stdout)
nodes = info["graph"]["nodes"]
nodes = info["nodes"]
hello_pkg_ref = 'hello/0.1' # no revision available
pkg_pkg_ref = 'pkg/0.2#926714b5fb0a994f47ec37e071eba1da'
hello_cpp_info = pkg_cpp_info = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def test_install(client):
client.run("install --requires=dep/0.1 -c tools.build:download_source=True --format=json")
assert "RUNNING SOURCE" in client.out
graph = json.loads(client.stdout)
zlib = graph["graph"]["nodes"]["1"]
zlib = graph["nodes"]["1"]
assert os.path.exists(zlib["source_folder"])
client.run("install --requires=dep/0.1 -c tools.build:download_source=True --format=json")
assert "RUNNING SOURCE" not in client.out
graph = json.loads(client.stdout)
zlib = graph["graph"]["nodes"]["1"]
zlib = graph["nodes"]["1"]
assert os.path.exists(zlib["source_folder"])


Expand Down