Skip to content

Commit

Permalink
improve download unzip output (#13937)
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded authored May 22, 2023
1 parent 2a17403 commit 00c87b0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
6 changes: 3 additions & 3 deletions conans/client/downloaders/caching_file_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SourcesCachingDownloader:
def __init__(self, conanfile):
helpers = getattr(conanfile, "_conan_helpers")
self._global_conf = helpers.global_conf
self._file_downloader = FileDownloader(helpers.requester)
self._file_downloader = FileDownloader(helpers.requester, scope=conanfile.display_name)
self._cache = helpers.cache
self._output = conanfile.output
self._conanfile = conanfile
Expand Down Expand Up @@ -152,11 +152,11 @@ def _download_from_urls(self, urls, file_path, retry, retry_wait, verify_ssl, au
class ConanInternalCacheDownloader:
""" This is used for the download of Conan packages from server, not for sources/backup sources
"""
def __init__(self, requester, config):
def __init__(self, requester, config, scope=None):
self._download_cache = config.get("core.download:download_cache")
if self._download_cache and not os.path.isabs(self._download_cache):
raise ConanException("core.download:download_cache must be an absolute path")
self._file_downloader = FileDownloader(requester)
self._file_downloader = FileDownloader(requester, scope=scope)

def download(self, url, file_path, auth, verify_ssl, retry, retry_wait):
if not self._download_cache:
Expand Down
10 changes: 6 additions & 4 deletions conans/client/downloaders/file_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

class FileDownloader:

def __init__(self, requester):
self._output = ConanOutput()
def __init__(self, requester, scope=None):
self._output = ConanOutput(scope=scope)
self._requester = requester

def download(self, url, file_path, retry=2, retry_wait=0, verify_ssl=True, auth=None,
Expand Down Expand Up @@ -102,9 +102,11 @@ def get_total_length():

try:
total_length = get_total_length()
if total_length > 100000:
if total_length > 1000000: # 10 Mb
from conan.tools.files.files import _human_size
hs = _human_size(total_length)
action = "Downloading" if range_start == 0 else "Continuing download of"
description = f"${action} {os.path.basename(file_path)} from {url}"
description = f"{action} {hs} {os.path.basename(file_path)}"
self._output.info(description)

chunk_size = 1024 * 100
Expand Down
15 changes: 10 additions & 5 deletions conans/client/remote_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from conan.api.output import ConanOutput
from conan.internal.cache.conan_reference_layout import METADATA
from conan.tools.files.files import _human_size
from conans.client.cache.remote_registry import Remote
from conans.client.pkg_sign import PkgSignaturesPlugin
from conans.errors import ConanConnectionError, ConanException, NotFoundException, \
Expand Down Expand Up @@ -75,7 +76,7 @@ def get_recipe(self, ref, remote):
tgz_file = zipped_files.pop(EXPORT_TGZ_NAME, None)

if tgz_file:
uncompress_file(tgz_file, export_folder)
uncompress_file(tgz_file, export_folder, scope=str(ref))
mkdir(export_folder)
for file_name, file_path in zipped_files.items(): # copy CONANFILE
shutil.move(file_path, os.path.join(export_folder, file_name))
Expand All @@ -94,7 +95,7 @@ def get_recipe_sources(self, ref, layout, remote):
return

tgz_file = zipped_files[EXPORT_SOURCES_TGZ_NAME]
uncompress_file(tgz_file, export_sources_folder)
uncompress_file(tgz_file, export_sources_folder, scope=str(ref))

def get_package(self, conanfile, pref, remote):
conanfile.output.info("Retrieving package %s from remote '%s' " % (pref.package_id,
Expand Down Expand Up @@ -123,7 +124,7 @@ def _get_package(self, layout, pref, remote, scoped_output):

tgz_file = zipped_files.pop(PACKAGE_TGZ_NAME, None)
package_folder = layout.package()
uncompress_file(tgz_file, package_folder)
uncompress_file(tgz_file, package_folder, scope=str(pref.ref))
mkdir(package_folder) # Just in case it doesn't exist, because uncompress did nothing
for file_name, file_path in zipped_files.items(): # copy CONANINFO and CONANMANIFEST
shutil.move(file_path, os.path.join(package_folder, file_name))
Expand Down Expand Up @@ -216,9 +217,13 @@ def _call_remote(self, remote, method, *args, **kwargs):
raise ConanException(exc, remote=remote)


def uncompress_file(src_path, dest_folder):
def uncompress_file(src_path, dest_folder, scope=None):
try:
ConanOutput().info("Decompressing %s" % os.path.basename(src_path))
filesize = os.path.getsize(src_path)
big_file = filesize > 1000000 # 10 Mb
if big_file:
hs = _human_size(filesize)
ConanOutput(scope=scope).info(f"Decompressing {hs} {os.path.basename(src_path)}")
with open(src_path, mode='rb') as file_handler:
tar_extract(file_handler, dest_folder)
except Exception as e:
Expand Down
8 changes: 4 additions & 4 deletions conans/client/rest/rest_client_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_recipe_sources(self, ref, dest_folder):

# If we didn't indicated reference, server got the latest, use absolute now, it's safer
urls = {fn: self.router.recipe_file(ref, fn) for fn in files}
self._download_and_save_files(urls, dest_folder, files)
self._download_and_save_files(urls, dest_folder, files, scope=str(ref))
ret = {fn: os.path.join(dest_folder, fn) for fn in files}
return ret

Expand All @@ -74,7 +74,7 @@ def get_package(self, pref, dest_folder):
files = [f for f in files if any(f.startswith(m) for m in accepted_files)]
# If we didn't indicated reference, server got the latest, use absolute now, it's safer
urls = {fn: self.router.package_file(pref, fn) for fn in files}
self._download_and_save_files(urls, dest_folder, files)
self._download_and_save_files(urls, dest_folder, files, scope=str(pref.ref))
ret = {fn: os.path.join(dest_folder, fn) for fn in files}
return ret

Expand Down Expand Up @@ -137,12 +137,12 @@ def _upload_files(self, files, urls):
raise ConanException("Execute upload again to retry upload the failed files: %s"
% ", ".join(failed))

def _download_and_save_files(self, urls, dest_folder, files, parallel=False):
def _download_and_save_files(self, urls, dest_folder, files, parallel=False, scope=None):
# Take advantage of filenames ordering, so that conan_package.tgz and conan_export.tgz
# can be < conanfile, conaninfo, and sent always the last, so smaller files go first
retry = self._config.get("core.download:retry", check_type=int, default=2)
retry_wait = self._config.get("core.download:retry_wait", check_type=int, default=0)
downloader = ConanInternalCacheDownloader(self.requester, self._config)
downloader = ConanInternalCacheDownloader(self.requester, self._config, scope=scope)
threads = []

for filename in sorted(files, reverse=True):
Expand Down

0 comments on commit 00c87b0

Please sign in to comment.