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

Add --deploy to graph-info #12243

Merged
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
13 changes: 7 additions & 6 deletions conan/api/subapi/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def install_consumer(self, deps_graph, generators=None, source_folder=None, outp
if deploy:
base_folder = conanfile.folders.base_build
mkdir(base_folder)
_do_deploys(self.conan_api, deps_graph, deploy, base_folder)
do_deploys(self.conan_api, deps_graph, deploy, base_folder)

conanfile.generators = list(set(conanfile.generators).union(generators or []))
app = ConanApp(self.conan_api.cache_folder)
Expand Down Expand Up @@ -87,17 +87,16 @@ def _load(path):
raise ConanException(f"Cannot find deployer '{d}'")


def _do_deploys(conan_api, graph, deploy, deploy_folder):
def do_deploys(conan_api, graph, deploy, deploy_folder):
# Handle the deploys
cache = ClientCache(conan_api.cache_folder)
for d in deploy or []:
deployer = _find_deployer(d, cache.deployers_path)
# IMPORTANT: Use always kwargs to not break if it changes in the future
conanfile = graph.root.conanfile
deployer(conanfile=conanfile, output_folder=deploy_folder)
deployer(graph=graph, output_folder=deploy_folder)


def full_deploy(conanfile, output_folder):
def full_deploy(graph, output_folder):
"""
Deploys to output_folder + host/dep/0.1/Release/x86_64 subfolder
"""
Expand All @@ -106,6 +105,7 @@ def full_deploy(conanfile, output_folder):
import os
import shutil

conanfile = graph.root.conanfile
conanfile.output.info(f"Conan built-in full deployer to {output_folder}")
for dep in conanfile.dependencies.values():
if dep.package_folder is None:
Expand All @@ -123,7 +123,7 @@ def full_deploy(conanfile, output_folder):
dep.set_deploy_folder(new_folder)


def direct_deploy(conanfile, output_folder):
def direct_deploy(graph, output_folder):
"""
Deploys to output_folder a single package,
"""
Expand All @@ -132,6 +132,7 @@ def direct_deploy(conanfile, output_folder):
import os
import shutil

conanfile = graph.root.conanfile
conanfile.output.info(f"Conan built-in pkg deployer to {output_folder}")
# If the argument is --requires, the current conanfile is a virtual one with 1 single
# dependency, the "reference" package. If the argument is a local path, then all direct
Expand Down
7 changes: 6 additions & 1 deletion conan/cli/commands/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

from conan.api.output import ConanOutput, cli_out_write
from conan.api.subapi.install import do_deploys
from conan.cli.command import conan_command, COMMAND_GROUPS, conan_subcommand, \
Extender
from conan.cli.commands import make_abs_path
Expand Down Expand Up @@ -86,6 +87,8 @@ def graph_info(conan_api, parser, subparser, *args):
help="Show only the specified fields")
subparser.add_argument("--package-filter", nargs=1, action=Extender,
help='Print information only for packages that match the patterns')
subparser.add_argument("--deploy", action=Extender,
help='Deploy using the provided deployer to the output folder')
args = parser.parse_args(*args)

# parameter validation
Expand All @@ -102,6 +105,8 @@ def graph_info(conan_api, parser, subparser, *args):
print_graph_info(deps_graph, args.filter, args.package_filter)

save_lockfile_out(args, deps_graph, lockfile, os.getcwd())
if args.deploy:
base_folder = os.getcwd()
do_deploys(conan_api, deps_graph, args.deploy, base_folder)

return deps_graph, os.path.join(conan_api.cache_folder, "templates")

15 changes: 10 additions & 5 deletions conans/test/functional/command/test_install_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def test_install_deploy():
import os, shutil

# USE **KWARGS to be robust against changes
def deploy(conanfile, output_folder, **kwargs):
def deploy(graph, output_folder, **kwargs):
conanfile = graph.root.conanfile
for r, d in conanfile.dependencies.items():
new_folder = os.path.join(output_folder, d.ref.name)
shutil.copytree(d.package_folder, new_folder)
Expand Down Expand Up @@ -51,7 +52,8 @@ def test_copy_files_deploy():
deploy = textwrap.dedent("""
import os, shutil

def deploy(conanfile, output_folder, **kwargs):
def deploy(graph, output_folder, **kwargs):
conanfile = graph.root.conanfile
for r, d in conanfile.dependencies.items():
bindir = os.path.join(d.package_folder, "bin")
for f in os.listdir(bindir):
Expand All @@ -73,15 +75,18 @@ def test_multi_deploy():
"""
c = TestClient()
deploy1 = textwrap.dedent("""
def deploy(conanfile, output_folder, **kwargs):
def deploy(graph, output_folder, **kwargs):
conanfile = graph.root.conanfile
conanfile.output.info("deploy1!!")
""")
deploy2 = textwrap.dedent("""
def deploy(conanfile, output_folder, **kwargs):
def deploy(graph, output_folder, **kwargs):
conanfile = graph.root.conanfile
conanfile.output.info("sub/deploy2!!")
""")
deploy_cache = textwrap.dedent("""
def deploy(conanfile, output_folder, **kwargs):
def deploy(graph, output_folder, **kwargs):
conanfile = graph.root.conanfile
conanfile.output.info("deploy cache!!")
""")
save(os.path.join(c.cache_folder, "extensions", "deploy", "deploy_cache.py"), deploy_cache)
Expand Down
31 changes: 31 additions & 0 deletions conans/test/integration/command/info/info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,34 @@ def build_requirements(self):
client.run("graph info . " + args)
assert "AttributeError: 'HelloConan' object has no attribute 'tested_reference_str'"\
not in client.out


class TestDeployers:

def test_custom_deploy(self):
c = TestClient()
conanfile = GenConanfile("pkg", "0.1").with_class_attribute("license = 'MIT'")
c.save({"conanfile.py": conanfile})
c.run("create .")
collectlicenses = textwrap.dedent(r"""
from conan.tools.files import save

def deploy(graph, output_folder, **kwargs):
contents = []
conanfile = graph.root.conanfile
for pkg in graph.nodes:
d = pkg.conanfile
contents.append("LICENSE {}: {}!".format(d.ref, d.license))
contents = "\n".join(contents)
conanfile.output.info(contents)
save(conanfile, "licenses.txt", contents)
""")
c.save({"conanfile.py": GenConanfile().with_requires("pkg/0.1")
.with_class_attribute("license='GPL'"),
"collectlicenses.py": collectlicenses})
c.run("graph info . --deploy=collectlicenses")
assert "conanfile.py: LICENSE : GPL!" in c.out
assert "LICENSE pkg/0.1: MIT!" in c.out
contents = c.load("licenses.txt")
assert "LICENSE pkg/0.1: MIT!" in contents
assert "LICENSE : GPL!" in contents