Repository: conan-io/conan
Pull request #14169 (2023-06-26 17:15:08)

trim_conandata()
Changelog: Feature: New ``trim_conandata()`` function to make things explicit, not depend on hook.
Docs: https://github.com/conan-io/docs/pull/XXXX





Patch:
diff --git a/conan/tools/cmake/cmake.py b/conan/tools/cmake/cmake.py
index d52a8ecb4..6dd77ac82 100644
--- a/conan/tools/cmake/cmake.py
+++ b/conan/tools/cmake/cmake.py
@@ -147,6 +147,10 @@ class CMake(object):
         arg_list = ["--install", build_folder, build_config, "--prefix", pkg_folder]
         if component:
             arg_list.extend(["--component", component])
+
+        do_strip = self._conanfile.conf.get("tools.cmake:install_strip", check_type=bool)
+        if do_strip:
+            arg_list.append("--strip")
         arg_list = " ".join(filter(None, arg_list))
         command = "%s %s" % (self._cmake_program, arg_list)
         self._conanfile.output.info("CMake command: %s" % command)
diff --git a/conan/tools/files/__init__.py b/conan/tools/files/__init__.py
index f47cd2bef..a69713675 100644
--- a/conan/tools/files/__init__.py
+++ b/conan/tools/files/__init__.py
@@ -7,4 +7,4 @@ from conan.tools.files.cpp_package import CppPackage
 from conan.tools.files.packager import AutoPackager
 from conan.tools.files.symlinks import symlinks
 from conan.tools.files.copy_pattern import copy
-from conan.tools.files.conandata import update_conandata
+from conan.tools.files.conandata import update_conandata, trim_conandata
diff --git a/conan/tools/files/conandata.py b/conan/tools/files/conandata.py
index 43bd7a12d..9e6228826 100644
--- a/conan/tools/files/conandata.py
+++ b/conan/tools/files/conandata.py
@@ -31,3 +31,31 @@ def update_conandata(conanfile, data):
     recursive_dict_update(conandata, data)
     new_content = yaml.safe_dump(conandata)
     save(path, new_content)
+
+
+def trim_conandata(conanfile):
+    """
+    Tool to modify the ``conandata.yml`` once it is exported, to limit it to the current version
+    only
+    """
+    if not hasattr(conanfile, "export_folder") or conanfile.export_folder is None:
+        raise ConanException("The 'trim_conandata()' can only be used in the 'export()' method")
+    path = os.path.join(conanfile.export_folder, "conandata.yml")
+    if not os.path.exists(path):
+        raise ConanException("conandata.yml file doesn't exist")
+
+    conandata = load(path)
+    conandata = yaml.safe_load(conandata)
+
+    version = str(conanfile.version)
+    result = {}
+    for k, v in conandata.items():
+        if not isinstance(v, dict):
+            result[k] = v
+            continue  # to allow user extra conandata, common to all versions
+        version_data = v.get(version)
+        if version_data is not None:
+            result[k] = {version: version_data}
+
+    new_conandata_yml = yaml.safe_dump(result, default_flow_style=False)
+    save(path, new_conandata_yml)
diff --git a/conans/client/build/autotools_environment.py b/conans/client/build/autotools_environment.py
index f06e81d08..808e3fbc7 100644
--- a/conans/client/build/autotools_environment.py
+++ b/conans/client/build/autotools_environment.py
@@ -34,6 +34,8 @@ class AutoToolsBuildEnvironment(object):
         """
         FIXME: include_rpath_flags CONAN 2.0 to default True? Could break many packages in center
         """
+        conanfile.output.warning(f"**** The 'AutoToolsBuildEnvironment' helper is deprecated. "
+                                 "Please update your code and remove it. ****")
         self._conanfile = conanfile
         self._win_bash = win_bash
         self._include_rpath_flags = include_rpath_flags
diff --git a/conans/client/build/cmake.py b/conans/client/build/cmake.py
index d9a668b37..0a1c83e67 100644
--- a/conans/client/build/cmake.py
+++ b/conans/client/build/cmake.py
@@ -55,6 +55,8 @@ class CMake(object):
                 conanfile.output.warning(wrong_helper_msg)
             else:
                 raise ConanException(wrong_helper_msg)
+        conanfile.output.warning(f"**** The 'from conans import CMake' helper is deprecated. "
+                                 "Please update your code and remove it. ****")
         self._append_vcvars = append_vcvars
         self._conanfile = conanfile
         self._settings = conanfile.settings
diff --git a/conans/client/build/meson.py b/conans/client/build/meson.py
index 8edf5f71e..d12625475 100644
--- a/conans/client/build/meson.py
+++ b/conans/client/build/meson.py
@@ -26,6 +26,8 @@ class Meson(object):
                Possible values: ninja,vs,vs2010,vs2015,vs2017,xcode
         :param build_type: Overrides default build type comming from settings
         """
+        conanfile.output.warning(f"**** The 'from conans import Meson' helper is deprecated. "
+                                 "Please update your code and remove it. ****")
         self._conanfile = conanfile
         self._settings = conanfile.settings
         self._append_vcvars = append_vcvars
diff --git a/conans/client/build/msbuild.py b/conans/client/build/msbuild.py
index df8b7bee4..70a62a1b9 100644
--- a/conans/client/build/msbuild.py
+++ b/conans/client/build/msbuild.py
@@ -36,6 +36,8 @@ class MSBuild(object):
             self._output = self._conanfile.output
             self.build_env = VisualStudioBuildEnvironment(self._conanfile,
                                                           with_build_type_flags=False)
+            conanfile.output.warning(f"**** The 'from conans import MSBuild' helper is deprecated. "
+                                     "Please update your code and remove it. ****")
         else:  # backwards compatible with build_sln_command
             self._settings = conanfile
             self.build_env = None
diff --git a/conans/client/build/visual_environment.py b/conans/client/build/visual_environment.py
index eb597d184..2be8fae52 100644
--- a/conans/client/build/visual_environment.py
+++ b/conans/client/build/visual_environment.py
@@ -25,6 +25,8 @@ class VisualStudioBuildEnvironment(object):
         """
         :param conanfile: ConanFile instance
         """
+        conanfile.output.warning(f"**** The 'VisualStudioBuildEnvironment' helper is deprecated. "
+                                 "Please update your code and remove it. ****")
         self._with_build_type_flags = with_build_type_flags
 
         self._conanfile = conanfile
diff --git a/conans/client/generators/__init__.py b/conans/client/generators/__init__.py
index 3413e98d0..82a777361 100644
--- a/conans/client/generators/__init__.py
+++ b/conans/client/generators/__init__.py
@@ -117,7 +117,7 @@ class GeneratorManager(object):
         elif generator_name == "MesonDeps":
             from conan.tools.meson import MesonDeps
             return MesonDeps
-        elif generator_name in ("MSBuildDeps", "msbuild"):
+        elif generator_name == "MSBuildDeps":
             from conan.tools.microsoft import MSBuildDeps
             return MSBuildDeps
         elif generator_name == "VCVars":
@@ -168,17 +168,6 @@ class GeneratorManager(object):
         for generator_name in set(conanfile.generators):
             generator_class = self._new_generator(generator_name, output)
             if generator_class:
-                if generator_name == "msbuild":
-                    msg = (
-                        "\n*****************************************************************\n"
-                        "******************************************************************\n"
-                        "'msbuild' has been deprecated and moved.\n"
-                        "It will be removed in next Conan release.\n"
-                        "Use 'MSBuildDeps' method instead.\n"
-                        "********************************************************************\n"
-                        "********************************************************************\n")
-                    from conans.client.output import Color
-                    output.writeln(msg, front=Color.BRIGHT_RED)
                 try:
                     generator = generator_class(conanfile)
                     output.highlight("Generator '{}' calling 'generate()'".format(generator_name))
@@ -207,6 +196,8 @@ class GeneratorManager(object):
             try:
                 generator.output_path = old_gen_folder
                 content = generator.content
+                conanfile.output.warning(f"**** The '{generator_name}' generator is deprecated. "
+                                         "Please update your code and remove it. ****")
                 if isinstance(content, dict):
                     if generator.filename:
                         output.warn("Generator %s is multifile. Property 'filename' not used"


Test patch:
diff --git a/conans/test/functional/toolchains/microsoft/test_msbuilddeps.py b/conans/test/functional/toolchains/microsoft/test_msbuilddeps.py
index f36d3bede..0fddd995e 100644
--- a/conans/test/functional/toolchains/microsoft/test_msbuilddeps.py
+++ b/conans/test/functional/toolchains/microsoft/test_msbuilddeps.py
@@ -496,7 +496,7 @@ class MSBuildGeneratorTest(unittest.TestCase):
         client = TestClient()
         client.save({"conanfile.py": GenConanfile()})
         client.run("create . mypkg/0.1@")
-        client.run("install mypkg/0.1@ -g msbuild -s build_type=None", assert_error=True)
+        client.run("install mypkg/0.1@ -g MSBuildDeps -s build_type=None", assert_error=True)
         self.assertIn("The 'msbuild' generator requires a 'build_type' setting value", client.out)
 
     def test_custom_configuration(self):
@@ -575,7 +575,7 @@ class MSBuildGeneratorTest(unittest.TestCase):
             class HelloConan(ConanFile):
                 settings = "os", "build_type", "compiler", "arch"
                 requires = "pkgb/1.0@", "pkga/1.0"
-                generators = "msbuild"
+                generators = "MSBuildDeps"
                 def build(self):
                     msbuild = MSBuild(self)
                     msbuild.build("MyProject.sln")
@@ -593,7 +593,6 @@ class MSBuildGeneratorTest(unittest.TestCase):
 
         client.save(files, clean_first=True)
         client.run("install .")
-        self.assertIn("'msbuild' has been deprecated and moved.", client.out)
         client.run("build .")
         self.assertNotIn("warning MSB4011", client.out)
 
@@ -665,7 +664,7 @@ def test_exclude_code_analysis(pattern, exclude_a, exclude_b):
         class HelloConan(ConanFile):
             settings = "os", "build_type", "compiler", "arch"
             requires = "pkgb/1.0@", "pkga/1.0"
-            generators = "msbuild"
+            generators = "MSBuildDeps"
             def build(self):
                 msbuild = MSBuild(self)
                 msbuild.build("MyProject.sln")
diff --git a/conans/test/functional/toolchains/test_basic.py b/conans/test/functional/toolchains/test_basic.py
index f1b50afbc..c798735fa 100644
--- a/conans/test/functional/toolchains/test_basic.py
+++ b/conans/test/functional/toolchains/test_basic.py
@@ -143,7 +143,7 @@ class BasicTest(unittest.TestCase):
                 name = "Pkg"
                 version = "0.1"
                 settings = "os", "compiler", "arch", "build_type"
-                generators = "msbuild"
+                generators = "MSBuildDeps"
 
                 def generate(self):
                     tc = MSBuildToolchain(self)
diff --git a/conans/test/integration/conanfile/conan_data_test.py b/conans/test/integration/conanfile/conan_data_test.py
index ff154c15c..47d773a25 100644
--- a/conans/test/integration/conanfile/conan_data_test.py
+++ b/conans/test/integration/conanfile/conan_data_test.py
@@ -331,3 +331,53 @@ class TestConanDataUpdate:
         c.save({"conanfile.py": conanfile})
         c.run("export .")  # It doesn't fail
         assert "pkg/0.1: Calling export()" in c.out
+
+
+def test_conandata_trim():
+    """ test the explict trim_conandata() helper
+    """
+    c = TestClient()
+    conanfile = textwrap.dedent("""
+        from conan import ConanFile
+        from conan.tools.files import trim_conandata
+
+        class Pkg(ConanFile):
+            name = "pkg"
+            def export(self):
+                trim_conandata(self)
+        """)
+    conandata_yml = textwrap.dedent("""\
+        sources:
+          "1.0":
+            url: "url1"
+            sha256: "sha1"
+        patches:
+          "1.0":
+            - patch_file: "patches/some_patch"
+              base_path: "source_subfolder"
+        something: else
+          """)
+    c.save({"conanfile.py": conanfile,
+            "conandata.yml": conandata_yml})
+    c.run("export . 1.0@")
+    assert "pkg/1.0: Exported revision: 70612e15e4fc9af1123fe11731ac214f" in c.out
+    conandata_yml2 = textwrap.dedent("""\
+        sources:
+         "1.0":
+           url: "url1"
+           sha256: "sha1"
+         "1.1":
+           url: "url2"
+           sha256: "sha2"
+        patches:
+         "1.1":
+           - patch_file: "patches/some_patch2"
+             base_path: "source_subfolder"
+         "1.0":
+           - patch_file: "patches/some_patch"
+             base_path: "source_subfolder"
+        something: else
+        """)
+    c.save({"conandata.yml": conandata_yml2})
+    c.run("export . 1.0@")
+    assert "pkg/1.0: Exported revision: 70612e15e4fc9af1123fe11731ac214f" in c.out
diff --git a/conans/test/integration/generators/generators_test.py b/conans/test/integration/generators/generators_test.py
index c938eca70..7d38c0067 100644
--- a/conans/test/integration/generators/generators_test.py
+++ b/conans/test/integration/generators/generators_test.py
@@ -156,21 +156,21 @@ qmake
                 settings = "os", "compiler", "arch", "build_type"
                 def configure(self):
                     if self.settings.os == "Windows":
-                        self.generators = ["msbuild"]
+                        self.generators = ["MSBuildDeps"]
                 """)
         client = TestClient()
         client.save({"conanfile.py": conanfile})
 
         client.run('install . -s os=Windows -s compiler="Visual Studio" -s compiler.version=15'
                    ' -s compiler.runtime=MD')
-        self.assertIn("conanfile.py: Generator 'msbuild' calling 'generate()'", client.out)
+        self.assertIn("conanfile.py: Generator 'MSBuildDeps' calling 'generate()'", client.out)
         client.run("install . -s os=Linux -s compiler=gcc -s compiler.version=5.2 '"
                    "'-s compiler.libcxx=libstdc++")
         self.assertNotIn("msbuild", client.out)
         # create
         client.run('create . pkg/0.1@ -s os=Windows -s compiler="Visual Studio" '
                    '-s compiler.version=15 -s compiler.runtime=MD')
-        self.assertIn("pkg/0.1: Generator 'msbuild' calling 'generate()'", client.out)
+        self.assertIn("pkg/0.1: Generator 'MSBuildDeps' calling 'generate()'", client.out)
         client.run("create . pkg/0.1@ -s os=Linux -s compiler=gcc -s compiler.version=5.2 "
                    "-s compiler.libcxx=libstdc++")
         self.assertNotIn("msbuild", client.out)
@@ -178,4 +178,4 @@ qmake
         # Test that command line generators append
         client.run('install . -s os=Windows -s compiler="Visual Studio" -s compiler.version=15'
                    ' -s compiler.runtime=MD -g cmake')
-        self.assertIn("conanfile.py: Generator 'msbuild' calling 'generate()'", client.out)
+        self.assertIn("conanfile.py: Generator 'MSBuildDeps' calling 'generate()'", client.out)
diff --git a/conans/test/unittests/tools/cmake/test_cmake_install.py b/conans/test/unittests/tools/cmake/test_cmake_install.py
index 273c38835..8fd6d45c4 100644
--- a/conans/test/unittests/tools/cmake/test_cmake_install.py
+++ b/conans/test/unittests/tools/cmake/test_cmake_install.py
@@ -31,9 +31,38 @@ def test_run_install_component():
     conanfile.settings = settings
     conanfile.folders.set_base_package(temp_folder())
 
-    # Choose generator to match generic setttings
+    # Choose generator to match generic settings
     write_cmake_presets(conanfile, "toolchain", "Visual Studio 14 2015", {})
     cmake = CMake(conanfile)
     cmake.install(component="foo")
 
     assert "--component foo" in conanfile.command
+
+
+def test_run_install_strip():
+    """
+    Testing that the install/strip rule is called
+    Issue related: https://github.com/conan-io/conan/issues/14166
+    """
+
+    settings = Settings.loads(get_default_settings_yml())
+    settings.os = "Linux"
+    settings.arch = "x86_64"
+    settings.build_type = "Release"
+    settings.compiler = "gcc"
+    settings.compiler.version = "11"
+
+    conanfile = ConanFileMock()
+
+    conanfile.conf = Conf()
+    conanfile.conf["tools.cmake:install_strip"] = True
+
+    conanfile.folders.generators = "."
+    conanfile.folders.set_base_generators(temp_folder())
+    conanfile.settings = settings
+    conanfile.folders.set_base_package(temp_folder())
+
+    write_cmake_presets(conanfile, "toolchain", "Unix Makefiles", {})
+    cmake = CMake(conanfile)
+    cmake.install()
+    assert "--strip" in conanfile.command