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

Sketch to show that populating download cache on conan upload is possible #15223

Draft
wants to merge 4 commits into
base: release/2.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions conan/cli/commands/upload.py
@@ -1,3 +1,5 @@
import os

from conan.api.conan_api import ConanAPI
from conan.api.model import ListPattern, MultiPackagesList
from conan.api.output import ConanOutput
Expand Down
1 change: 1 addition & 0 deletions conan/internal/conan_app.py
Expand Up @@ -38,6 +38,7 @@ def __init__(self, requester, cmd_wrapper, global_conf, cache):

class ConanApp(object):
def __init__(self, cache_folder, global_conf):
self.global_conf = global_conf

self.cache_folder = cache_folder
self.cache = ClientCache(self.cache_folder, global_conf)
Expand Down
38 changes: 37 additions & 1 deletion conans/client/cmd/uploader.py
Expand Up @@ -4,12 +4,14 @@

from conan.internal.conan_app import ConanApp
from conan.api.output import ConanOutput
from conans.client.downloaders.download_cache import DownloadCache
from conans.client.source import retrieve_exports_sources
from conans.errors import ConanException, NotFoundException
from conans.paths import (CONAN_MANIFEST, CONANFILE, EXPORT_SOURCES_TGZ_NAME,
EXPORT_TGZ_NAME, PACKAGE_TGZ_NAME, CONANINFO)
from conans.util.files import (clean_dirty, is_dirty, gather_files,
gzopen_without_timestamps, set_dirty_context_manager, mkdir)
gzopen_without_timestamps, set_dirty_context_manager, mkdir,
remove_if_dirty)

UPLOAD_POLICY_FORCE = "force-upload"
UPLOAD_POLICY_SKIP = "skip-upload"
Expand Down Expand Up @@ -232,6 +234,9 @@ def upload_recipe(self, ref, bundle, remote):

duration = time.time() - t1
self._output.debug(f"Upload {ref} in {duration} time")
for cache_file in cache_files.items():
self._local_cache_recipe_file(ref, cache_file, remote)

return ref

def upload_package(self, pref, prev_bundle, remote):
Expand All @@ -244,6 +249,37 @@ def upload_package(self, pref, prev_bundle, remote):
self._app.remote_manager.upload_package(pref, cache_files, remote)
duration = time.time() - t1
self._output.debug(f"Upload {pref} in {duration} time")
for cache_file in cache_files.items():
self._local_cache_package_file(pref, cache_file, remote)

def _local_cache_recipe_file(self, ref, cache_file, remote):
approved_files = ["conanfile.py", "conan_export.tgz", "conanmanifest.txt"]
filename, path = cache_file
if filename not in approved_files:
return
_download_cache = self._app.global_conf.get("core.download:download_cache")
url = self._app.remote_manager._auth_manager._get_rest_client(remote)._get_api().router.recipe_file(ref, filename)
self._cache_local_upload_url(url, path)

def _local_cache_package_file(self, pref, cache_file, remote):
approved_files = ["conaninfo.txt", "conan_package.tgz", "conanmanifest.txt"]
RubenRBS marked this conversation as resolved.
Show resolved Hide resolved
filename, path = cache_file
if filename not in approved_files:
return
url = self._app.remote_manager._auth_manager._get_rest_client(remote)._get_api().router.package_file(pref, filename)
self._cache_local_upload_url(url, path)

def _cache_local_upload_url(self, url, path):
_download_cache = self._app.global_conf.get("core.download:download_cache")
if _download_cache and os.path.isabs(_download_cache):
download_cache = DownloadCache(_download_cache)
cached_path, h = download_cache.cached_path(url)
with download_cache.lock(h):
remove_if_dirty(cached_path)
with set_dirty_context_manager(cached_path):
shutil.copy2(path, cached_path)
self._output.info(
f"Caching {path} to local cache '{cached_path}' - was uploaded to {url}")


def compress_files(files, name, dest_dir, compresslevel=None, ref=None):
Expand Down
7 changes: 6 additions & 1 deletion conans/test/integration/command/upload/upload_test.py
Expand Up @@ -14,7 +14,8 @@
from conans.paths import EXPORT_SOURCES_TGZ_NAME, PACKAGE_TGZ_NAME
from conans.test.utils.tools import NO_SETTINGS_PACKAGE_ID, TestClient, TestServer, \
TurboTestClient, GenConanfile, TestRequester, TestingResponse
from conans.util.files import gzopen_without_timestamps, is_dirty, save, set_dirty
from conans.test.utils.test_files import temp_folder
from conans.util.files import gzopen_without_timestamps, is_dirty, save, set_dirty, load

conanfile = """from conan import ConanFile
from conan.tools.files import copy
Expand Down Expand Up @@ -231,10 +232,14 @@ def test_upload_unmodified_package(self):

client.save({"conanfile.py": conanfile,
"hello.cpp": ""})
download_cache = temp_folder()
client.save_home({"global.conf": f"core.download:download_cache={download_cache}"})
ref = RecipeReference.loads("hello0/1.2.1@frodo/stable")
client.run("create . --user=frodo --channel=stable")
client.run("upload hello0/1.2.1@frodo/stable -r default")

assert len(os.listdir(os.path.join(download_cache, "c"))) == 5

client2 = TestClient(servers=client.servers, inputs=["admin", "password"])
client2.save({"conanfile.py": conanfile,
"hello.cpp": ""})
Expand Down