Skip to content

Commit

Permalink
Restored prev FileProgress location and use with
Browse files Browse the repository at this point in the history
  • Loading branch information
perseoGI committed Dec 31, 2024
1 parent 82edcf8 commit 8fe92be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
27 changes: 6 additions & 21 deletions conan/tools/files/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import platform
import shutil
import subprocess
import io
from contextlib import contextmanager
from fnmatch import fnmatch
from shutil import which


from conan.api.output import TimedOutput
from conans.client.downloaders.caching_file_downloader import SourcesCachingDownloader
from conan.errors import ConanException
from conans.client.rest.file_uploader import FileProgress
from conans.util.files import rmdir as _internal_rmdir, human_size, check_with_algorithm_sum


Expand Down Expand Up @@ -289,7 +288,7 @@ def unzip(conanfile, filename, destination=".", keep_permissions=False, pattern=
if (filename.endswith(".tar.gz") or filename.endswith(".tgz") or
filename.endswith(".tbz2") or filename.endswith(".tar.bz2") or
filename.endswith(".tar")):
return untargz(filename, destination, pattern, strip_root, extract_filter, output)
return untargz(filename, destination, pattern, strip_root, extract_filter)
if filename.endswith(".gz"):
target_name = filename[:-3] if destination == "." else destination
target_dir = os.path.dirname(target_name)
Expand All @@ -300,12 +299,12 @@ def unzip(conanfile, filename, destination=".", keep_permissions=False, pattern=
shutil.copyfileobj(fin, fout)
return
if filename.endswith(".tar.xz") or filename.endswith(".txz"):
return untargz(filename, destination, pattern, strip_root, extract_filter, output)
return untargz(filename, destination, pattern, strip_root, extract_filter)

import zipfile
full_path = os.path.normpath(os.path.join(os.getcwd(), destination))

with zipfile.ZipFile(FileProgress(filename, msg="Unzipping", mode="r")) as z:
with FileProgress(filename, msg="Unzipping", mode="r") as file, zipfile.ZipFile(file) as z:
zip_info = z.infolist()
if pattern:
zip_info = [zi for zi in zip_info if fnmatch(zi.filename, pattern)]
Expand Down Expand Up @@ -351,10 +350,10 @@ def unzip(conanfile, filename, destination=".", keep_permissions=False, pattern=
output.error(f"Error extract {file_.filename}\n{str(e)}", error_type="exception")
output.writeln("")

def untargz(filename, destination=".", pattern=None, strip_root=False, extract_filter=None, output=None):
def untargz(filename, destination=".", pattern=None, strip_root=False, extract_filter=None):
# NOT EXPOSED at `conan.tools.files` but used in tests
import tarfile
with tarfile.TarFile.open(fileobj=FileProgress(filename, msg="Uncompressing"), mode='r:*') as tarredgzippedFile:
with FileProgress(filename, msg="Uncompressing") as fileobj, tarfile.TarFile.open(fileobj=fileobj, mode='r:*') as tarredgzippedFile:
f = getattr(tarfile, f"{extract_filter}_filter", None) if extract_filter else None
tarredgzippedFile.extraction_filter = f or (lambda member_, _: member_)
if not pattern and not strip_root:
Expand Down Expand Up @@ -532,17 +531,3 @@ def move_folder_contents(conanfile, src_folder, dst_folder):
os.rmdir(src_folder)
except OSError:
pass


class FileProgress(io.FileIO):
def __init__(self, path: str, msg: str = "Uploading", report_interval: float = 10, *args, **kwargs):
super().__init__(path, *args, **kwargs)
self._total_size = os.path.getsize(path)
self._filename = os.path.basename(path)
self._t = TimedOutput(interval=report_interval)
self.msg = msg

def read(self, size: int = -1) -> bytes:
current_percentage = int(self.tell() * 100.0 / self._total_size) if self._total_size != 0 else 0
self._t.info(f"{self.msg} {self._filename}: {current_percentage}%")
return super().read(size)
19 changes: 17 additions & 2 deletions conans/client/rest/file_uploader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import io
import os
import time

from conan.api.output import ConanOutput
from conan.tools.files.files import FileProgress
from conan.api.output import ConanOutput, TimedOutput
from conans.client.rest import response_to_str
from conan.internal.errors import InternalErrorException, RequestErrorException, AuthenticationException, \
ForbiddenException, NotFoundException
Expand Down Expand Up @@ -93,3 +94,17 @@ def _upload_file(self, url, abs_path, headers, auth, ref):
raise
except Exception as exc:
raise ConanException(exc)


class FileProgress(io.FileIO):
def __init__(self, path: str, msg: str = "Uploading", report_interval: float = 10, *args, **kwargs):
super().__init__(path, *args, **kwargs)
self._total_size = os.path.getsize(path)
self._filename = os.path.basename(path)
self._t = TimedOutput(interval=report_interval)
self.msg = msg

def read(self, size: int = -1) -> bytes:
current_percentage = int(self.tell() * 100.0 / self._total_size) if self._total_size != 0 else 0
self._t.info(f"{self.msg} {self._filename}: {current_percentage}%")
return super().read(size)

0 comments on commit 8fe92be

Please sign in to comment.