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

trim_conandata() #14169

Merged
merged 1 commit into from
Jul 11, 2023
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
2 changes: 1 addition & 1 deletion conan/tools/files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
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
28 changes: 28 additions & 0 deletions conan/tools/files/conandata.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,31 @@ def recursive_dict_update(d, u):
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)
memsharded marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 50 additions & 0 deletions conans/test/integration/conanfile/conan_data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,53 @@ def export(self):
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