Skip to content

Commit

Permalink
Fix/save restore portable (conan-io#15253)
Browse files Browse the repository at this point in the history
* make save-restore portable

* msg
  • Loading branch information
memsharded authored Dec 12, 2023
1 parent 1c43d5f commit 7aa0b4b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion conan/api/subapi/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,23 @@ def save(self, package_list, tgz_path):
for ref, ref_bundle in package_list.refs().items():
ref_layout = app.cache.recipe_layout(ref)
recipe_folder = os.path.relpath(ref_layout.base_folder, cache_folder)
recipe_folder = recipe_folder.replace("\\", "/") # make win paths portable
ref_bundle["recipe_folder"] = recipe_folder
out.info(f"Saving {ref}: {recipe_folder}")
tgz.add(os.path.join(cache_folder, recipe_folder), recipe_folder, recursive=True)
for pref, pref_bundle in package_list.prefs(ref, ref_bundle).items():
pref_layout = app.cache.pkg_layout(pref)
pkg_folder = pref_layout.package()
folder = os.path.relpath(pkg_folder, cache_folder)
folder = folder.replace("\\", "/") # make win paths portable
pref_bundle["package_folder"] = folder
out.info(f"Saving {pref}: {folder}")
tgz.add(os.path.join(cache_folder, folder), folder, recursive=True)
if os.path.exists(pref_layout.metadata()):
metadata_folder = os.path.relpath(pref_layout.metadata(), cache_folder)
metadata_folder = metadata_folder.replace("\\", "/") # make paths portable
pref_bundle["metadata_folder"] = metadata_folder
out.info(f"Saving {pref} metadata: {folder}")
out.info(f"Saving {pref} metadata: {metadata_folder}")
tgz.add(os.path.join(cache_folder, metadata_folder), metadata_folder,
recursive=True)
serialized = json.dumps(package_list.serialize(), indent=2)
Expand All @@ -159,6 +162,7 @@ def restore(self, path):
recipe_layout = cache.get_or_create_ref_layout(ref)
recipe_folder = ref_bundle["recipe_folder"]
rel_path = os.path.relpath(recipe_layout.base_folder, cache.cache_folder)
rel_path = rel_path.replace("\\", "/")
assert rel_path == recipe_folder, f"{rel_path}!={recipe_folder}"
out.info(f"Restore: {ref} in {recipe_folder}")
for pref, pref_bundle in package_list.prefs(ref, ref_bundle).items():
Expand Down
11 changes: 11 additions & 0 deletions conans/test/integration/command_v2/test_cache_save_restore.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import shutil
import tarfile

from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.tools import TestClient
Expand All @@ -18,6 +19,16 @@ def test_cache_save_restore():
assert os.path.exists(cache_path)
_validate_restore(cache_path)

# Lets test that the pkglist does not contain windows backslash paths to make it portable
with open(cache_path, mode='rb') as file_handler:
the_tar = tarfile.open(fileobj=file_handler)
fileobj = the_tar.extractfile("pkglist.json")
pkglist = fileobj.read()
the_tar.close()

package_list = json.loads(pkglist)
assert "\\" not in package_list


def test_cache_save_downloaded_restore():
""" what happens if we save packages downloaded from server, not
Expand Down

0 comments on commit 7aa0b4b

Please sign in to comment.