Skip to content
Draft
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
35 changes: 35 additions & 0 deletions conan/api/model/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def load_graph(graphfile, graph_recipes=None, graph_binaries=None, context=None)
try:
base_context = context.split("-")[0] if context else None
graph = json.loads(load(graphfile))
if "order_by" in graph: # This is a "build-order" json, not a graph
return MultiPackagesList._define_build_order(graph, graph_binaries)

# Check if input json is a graph file
if "graph" not in graph:
raise ConanException(
Expand Down Expand Up @@ -191,6 +194,38 @@ def _define_graph(graph, graph_recipes=None, graph_binaries=None, context=None):
cache_list.add_pref(pref, node["info"])
return pkglist

@staticmethod
def _define_build_order(build_order, graph_binaries=None):
pkglist = MultiPackagesList()
cache_list = PackagesList()
if graph_binaries is None:
binaries = ["*"]
else:
binaries = [b.lower() for b in graph_binaries or []]

pkglist.lists["Local Cache"] = cache_list
order_by = build_order.get("order_by")
if order_by not in ("configuration", "recipe"):
raise ConanException(f"Invalid order_by value: {order_by}")

for level in build_order["order"]:
for node in level:
ref = node["ref"]
ref = RecipeReference.loads(ref)
pnodes = [node] if order_by == "configuration" else \
[p for lev in node["packages"] for p in lev]
for pnode in pnodes:
pref = PkgReference(ref, pnode["package_id"], pnode["prev"])
binary = pnode["binary"]
if binary in (BINARY_SKIP, BINARY_INVALID, BINARY_MISSING):
continue
binary = binary.lower()
if any(b == "*" or b == binary for b in binaries):
cache_list.add_ref(ref) # Binary listed forces recipe listed
cache_list.add_pref(pref, pnode["info"])

return pkglist


class PackagesList:
""" A collection of recipes, revisions and packages."""
Expand Down
39 changes: 39 additions & 0 deletions test/integration/command/list/test_combined_pkglist_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,42 @@ def test_graph_list(self):
tc.run("list --graph=graph_context.json --graph-context=build-only --format=json",
assert_error=True)
assert "Note that the graph file should not be filtered" in tc.out


class TestBuildOrderToPkgList:
@pytest.mark.parametrize("order_by", ["recipe", "configuration"])
def test_build_order_to_list(self, order_by):
c = TestClient(light=True)
c.save({"zlib/conanfile.py": GenConanfile("zlib", "1.0"),
"app/conanfile.py": GenConanfile("app", "1.0").with_requires("zlib/1.0")})
c.run("export zlib")
c.run("export app")

c.run("graph build-order --requires=app/1.0 "
f"--order-by={order_by} --build=missing --format=json", redirect_stdout="bo.json")
c.run("list --graph=bo.json --format=json")
pkglist = json.loads(c.stdout)
pkgs = pkglist["Local Cache"]
zlib_pkgs = pkgs["zlib/1.0"]["revisions"]["c570d63921c5f2070567da4bf64ff261"]["packages"]
assert zlib_pkgs == {"da39a3ee5e6b4b0d3255bfef95601890afd80709": {"info": {}}}
app_pkgs = pkgs["app/1.0"]["revisions"]["0fa1ff1b90576bb782600e56df642e19"]["packages"]
assert app_pkgs == {"594ed0eb2e9dfcc60607438924c35871514e6c2a":
{"info": {"requires": ["zlib/1.Y.Z"]}}}

@pytest.mark.parametrize("order_by", ["recipe", "configuration"])
def test_build_order_to_list_only_build(self, order_by):
c = TestClient(light=True)
c.save({"zlib/conanfile.py": GenConanfile("zlib", "1.0"),
"app/conanfile.py": GenConanfile("app", "1.0").with_requires("zlib/1.0")})
c.run("create zlib")
c.run("export app")

c.run("graph build-order --requires=app/1.0 "
f"--order-by={order_by} --build=missing --format=json", redirect_stdout="bo.json")
c.run("list --graph=bo.json --graph-binaries=build --format=json")
pkglist = json.loads(c.stdout)
pkgs = pkglist["Local Cache"]
assert "zlib/1.0" not in pkgs
app_pkgs = pkgs["app/1.0"]["revisions"]["0fa1ff1b90576bb782600e56df642e19"]["packages"]
assert app_pkgs == {"594ed0eb2e9dfcc60607438924c35871514e6c2a":
{"info": {"requires": ["zlib/1.Y.Z"]}}}
Loading