Skip to content

Commit

Permalink
trim_conandata() (#14169)
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded authored Jul 11, 2023
1 parent 4fc5da8 commit b486f60
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
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)
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

0 comments on commit b486f60

Please sign in to comment.