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

[feature] Add hooks for validate method #17856

Merged
merged 15 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions conan/internal/conan_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self, conan_api):
"""
global_conf = conan_api.config.global_conf
self.global_conf = global_conf
self.conan_api = conan_api
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked with @AbrilRBS about this. It looks better than changing more methods because we need to forward the hook manager to run_validate_package_id

cache_folder = conan_api.home_folder
self.cache_folder = cache_folder
self.cache = PkgCache(self.cache_folder, global_conf)
Expand Down
5 changes: 3 additions & 2 deletions conans/client/graph/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ def _should_migrate_file(file_path):

class BinaryCompatibility:

def __init__(self, compatibility_plugin_folder):
def __init__(self, compatibility_plugin_folder, hook_manager):
compatibility_file = os.path.join(compatibility_plugin_folder, "compatibility.py")
if not os.path.exists(compatibility_file):
raise ConanException("The 'compatibility.py' plugin file doesn't exist. If you want "
"to disable it, edit its contents instead of removing it")
mod, _ = load_python_file(compatibility_file)
self._compatibility = mod.compatibility
self._hook_manager = hook_manager

def compatibles(self, conanfile):
compat_infos = []
Expand Down Expand Up @@ -143,7 +144,7 @@ def compatibles(self, conanfile):
conanfile.settings = c.settings
conanfile.settings_target = c.settings_target
conanfile.options = c.options
run_validate_package_id(conanfile)
run_validate_package_id(conanfile, self._hook_manager)
pid = c.package_id()
if pid not in result and not c.invalid:
result[pid] = c
Expand Down
10 changes: 7 additions & 3 deletions conans/client/graph/compute_pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from conan.internal.model.info import ConanInfo, RequirementsInfo, RequirementInfo, PythonRequiresInfo


def compute_package_id(node, modes, config_version):
def compute_package_id(node, modes, config_version, hook_manager):
"""
Compute the binary package ID of this node
"""
Expand Down Expand Up @@ -50,7 +50,7 @@ def compute_package_id(node, modes, config_version):
config_version=config_version.copy() if config_version else None)
conanfile.original_info = conanfile.info.clone()

run_validate_package_id(conanfile)
run_validate_package_id(conanfile, hook_manager)

if conanfile.info.settings_target:
# settings_target has beed added to conan package via package_id api
Expand All @@ -60,24 +60,28 @@ def compute_package_id(node, modes, config_version):
node.package_id = info.package_id()


def run_validate_package_id(conanfile):
def run_validate_package_id(conanfile, hook_manager):
# IMPORTANT: This validation code must run before calling info.package_id(), to mark "invalid"
if hasattr(conanfile, "validate_build"):
with conanfile_exception_formatter(conanfile, "validate_build"):
hook_manager.execute("pre_validate_build", conanfile=conanfile)
with conanfile_remove_attr(conanfile, ['cpp_info'], "validate_build"):
try:
conanfile.validate_build()
except ConanInvalidConfiguration as e:
# This 'cant_build' will be ignored if we don't have to build the node.
conanfile.info.cant_build = str(e)
hook_manager.execute("post_validate_build", conanfile=conanfile)

if hasattr(conanfile, "validate"):
with conanfile_exception_formatter(conanfile, "validate"):
hook_manager.execute("pre_validate", conanfile=conanfile)
with conanfile_remove_attr(conanfile, ['cpp_info'], "validate"):
try:
conanfile.validate()
except ConanInvalidConfiguration as e:
conanfile.info.invalid = str(e)
hook_manager.execute("post_validate", conanfile=conanfile)

# Once we are done, call package_id() to narrow and change possible values
if hasattr(conanfile, "package_id"):
Expand Down
7 changes: 4 additions & 3 deletions conans/client/graph/graph_binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ def __init__(self, conan_app, global_conf):
self._home_folder = conan_app.cache_folder
self._global_conf = global_conf
self._remote_manager = conan_app.remote_manager
self._hook_manager = conan_app.conan_api.config.hook_manager
# These are the nodes with pref (not including PREV) that have been evaluated
self._evaluated = {} # {pref: [nodes]}
compat_folder = HomePaths(conan_app.cache_folder).compatibility_plugin_path
self._compatibility = BinaryCompatibility(compat_folder)
self._compatibility = BinaryCompatibility(compat_folder, self._hook_manager)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to execute the validate() pre/post hooks for all combinations of binary compatibility?
Can I have some examples of potential usages of the hook, and what does it mean to execute it for every binary compatibility hypothesis?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still an open question

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think so, I could remove it and add in the future, in case having a case.

For now, we only have the CI case to be covered.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then lets remove it. It is better to be minimalistic first, then add things when real use cases are requesting it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed on the commit 57878a1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd still like to see a more real use case for the feature, I haven't fully understood if it makes sense to be in the compatibility or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only real case listed right now is related to the CI: When the CI needs to skip a build, being checked in the pre_validate, but we do not want to change the recipe, by adding a new rule to raise ConanInvalidConfiguration.

About the compatibility I do not see a real usage right now, because it's related to binary compatibility mechanism, sounds be something unrelated to the validate stage.

unknown_mode = global_conf.get("core.package_id:default_unknown_mode", default="semver_mode")
non_embed = global_conf.get("core.package_id:default_non_embed_mode", default="minor_mode")
# recipe_revision_mode already takes into account the package_id
Expand Down Expand Up @@ -397,7 +398,7 @@ def _config_version(self):
return RequirementsInfo(result)

def _evaluate_package_id(self, node, config_version):
compute_package_id(node, self._modes, config_version=config_version)
compute_package_id(node, self._modes, config_version, self._hook_manager)

# TODO: layout() execution don't need to be evaluated at GraphBuilder time.
# it could even be delayed until installation time, but if we got enough info here for
Expand Down Expand Up @@ -458,7 +459,7 @@ def _evaluate_single(n):
if node.path is not None:
if node.path.endswith(".py"):
# For .py we keep evaluating the package_id, validate(), etc
compute_package_id(node, self._modes, config_version=config_version)
compute_package_id(node, self._modes, config_version, self._hook_manager)
# To support the ``[layout]`` in conanfile.txt
if hasattr(node.conanfile, "layout"):
with conanfile_exception_formatter(node.conanfile, "layout"):
Expand Down
2 changes: 2 additions & 0 deletions conans/client/hook_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from conan.errors import ConanException

valid_hook_methods = ["pre_export", "post_export",
"pre_validate_build", "post_validate_build",
"pre_validate", "post_validate",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can cover the real-world use cases with just 1 of these hooks, I'd go for that, do the minimal changes to satisfy the need.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, we have pre_validate requested in CCI as a real case, so the CI could skip the build for those cases when the glibc version is too old but the project requires a newer version (e.g. Qt), without injecting changes in the validate() which is really consumed by users.

For other cases, @jcar87 can tell us more about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre_validate as a minimum
I think post_validate is useful too, in case ConanInvalidConfiguration is raised in the implementation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@memsharded I'll drop the hook for validate_build.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed validate_build hook on the commit a71eaaa

"pre_source", "post_source",
"pre_generate", "post_generate",
"pre_build", "post_build", "post_build_fail",
Expand Down
35 changes: 35 additions & 0 deletions test/integration/extensions/hooks/hook_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import textwrap
import pytest

from conan.test.assets.genconanfile import GenConanfile
from conan.test.utils.tools import TestClient
Expand All @@ -21,6 +22,18 @@ def post_export(conanfile):
assert conanfile.export_sources_folder, "export_sources_folder not defined"
assert conanfile.recipe_folder, "recipe_folder not defined"

def pre_validate_build(conanfile):
conanfile.output.info("Hello")

def post_validate_build(conanfile):
conanfile.output.info("Hello")

def pre_validate(conanfile):
conanfile.output.info("Hello")

def post_validate(conanfile):
conanfile.output.info("Hello")

def pre_source(conanfile):
conanfile.output.info("Hello")
assert conanfile.source_folder, "source_folder not defined"
Expand Down Expand Up @@ -111,6 +124,8 @@ def test_complete_hook(self):
assert f"pkg/0.1: {hook_msg} post_package(): Hello" in c.out
assert f"pkg/0.1: {hook_msg} pre_package_info(): Hello" in c.out
assert f"pkg/0.1: {hook_msg} post_package_info(): Hello" in c.out
assert "pre_validate_build()" not in c.out
assert "post_validate_build()" not in c.out

def test_import_hook(self):
""" Test that a hook can import another random python file
Expand Down Expand Up @@ -176,3 +191,23 @@ def build(self):
assert "conanfile.py: [HOOK - my_hook/hook_my_hook.py] post_build_fail(): Hello" in c.out
assert "ERROR: conanfile.py: Error in build() method, line 5" in c.out

@pytest.mark.parametrize("method", ["validate_build", "validate"])
def test_validate_hook(self, method):
""" The validate_build and validate hooks are executed only if the method is declared
in the recipe.
"""
c = TestClient()
hook_path = os.path.join(c.paths.hooks_path, "testing", "hook_complete.py")
save(hook_path, complete_hook)

conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
def {method}(self):
pass
""".format(method=method))
c.save({"conanfile.py": conanfile})

c.run("create . --name=foo --version=0.1.0")
assert f"foo/0.1.0: [HOOK - testing/hook_complete.py] pre_{method}(): Hello" in c.out
assert f"foo/0.1.0: [HOOK - testing/hook_complete.py] post_{method}(): Hello" in c.out