Skip to content

Commit f142188

Browse files
authored
Merge pull request #2875 from casperdcl/stream-progress
Stream progress
2 parents f91aa06 + c622287 commit f142188

File tree

4 files changed

+41
-75
lines changed

4 files changed

+41
-75
lines changed

dvc/remote/gdrive/__init__.py renamed to dvc/remote/gdrive.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from funcy import retry, compose, decorator, wrap_with
99
from funcy.py3 import cat
1010

11-
from dvc.remote.gdrive.utils import TrackFileReadProgress, FOLDER_MIME_TYPE
11+
from dvc.progress import Tqdm
1212
from dvc.scheme import Schemes
1313
from dvc.path_info import CloudURLInfo
1414
from dvc.remote.base import RemoteBASE
@@ -17,6 +17,7 @@
1717
from dvc.utils import tmp_fname
1818

1919
logger = logging.getLogger(__name__)
20+
FOLDER_MIME_TYPE = "application/vnd.google-apps.folder"
2021

2122

2223
class GDriveRetriableError(DvcException):
@@ -92,30 +93,34 @@ def gdrive_upload_file(
9293
item = self.drive.CreateFile(
9394
{"title": args["title"], "parents": [{"id": args["parent_id"]}]}
9495
)
95-
self.upload_file(item, no_progress_bar, from_file, progress_name)
96-
return item
9796

98-
def upload_file(self, item, no_progress_bar, from_file, progress_name):
99-
with open(from_file, "rb") as opened_file:
100-
if not no_progress_bar:
101-
opened_file = TrackFileReadProgress(progress_name, opened_file)
102-
# PyDrive doesn't like content property setting for empty files
103-
# https://github.com/gsuitedevs/PyDrive/issues/121
104-
if os.stat(from_file).st_size:
105-
item.content = opened_file
106-
item.Upload()
97+
with open(from_file, "rb") as fobj:
98+
total = os.path.getsize(from_file)
99+
with Tqdm.wrapattr(
100+
fobj,
101+
"read",
102+
desc=progress_name,
103+
total=total,
104+
disable=no_progress_bar,
105+
) as wrapped:
106+
# PyDrive doesn't like content property setting for empty files
107+
# https://github.com/gsuitedevs/PyDrive/issues/121
108+
if total:
109+
item.content = wrapped
110+
item.Upload()
111+
return item
107112

108113
@gdrive_retry
109114
def gdrive_download_file(
110115
self, file_id, to_file, progress_name, no_progress_bar
111116
):
112-
from dvc.progress import Tqdm
113-
114117
gdrive_file = self.drive.CreateFile({"id": file_id})
118+
bar_format = (
119+
"Donwloading {desc:{ncols_desc}.{ncols_desc}}... "
120+
+ Tqdm.format_sizeof(int(gdrive_file["fileSize"]), "B", 1024)
121+
)
115122
with Tqdm(
116-
desc=progress_name,
117-
total=int(gdrive_file["fileSize"]),
118-
disable=no_progress_bar,
123+
bar_format=bar_format, desc=progress_name, disable=no_progress_bar
119124
):
120125
gdrive_file.GetContentFile(to_file)
121126

dvc/remote/gdrive/utils.py

-25
This file was deleted.

dvc/remote/gs.py

+18-32
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,15 @@ def _upload_to_bucket(
5858
no_progress_bar=True,
5959
):
6060
blob = bucket.blob(to_info.path, chunk_size=chunk_size)
61-
with Tqdm(
62-
desc=name or to_info.path,
63-
total=os.path.getsize(from_file),
64-
bytes=True,
65-
disable=no_progress_bar,
66-
) as pbar:
67-
with io.open(from_file, mode="rb") as fobj:
68-
raw_read = fobj.read
69-
70-
def read(size=chunk_size):
71-
res = raw_read(size)
72-
if res:
73-
pbar.update(len(res))
74-
return res
75-
76-
fobj.read = read
77-
blob.upload_from_file(fobj)
61+
with io.open(from_file, mode="rb") as fobj:
62+
with Tqdm.wrapattr(
63+
fobj,
64+
"read",
65+
desc=name or to_info.path,
66+
total=os.path.getsize(from_file),
67+
disable=no_progress_bar,
68+
) as wrapped:
69+
blob.upload_from_file(wrapped)
7870

7971

8072
class RemoteGS(RemoteBASE):
@@ -191,21 +183,15 @@ def _upload(self, from_file, to_info, name=None, no_progress_bar=True):
191183
def _download(self, from_info, to_file, name=None, no_progress_bar=True):
192184
bucket = self.gs.bucket(from_info.bucket)
193185
blob = bucket.get_blob(from_info.path)
194-
with Tqdm(
195-
desc=name or from_info.path,
196-
total=blob.size,
197-
bytes=True,
198-
disable=no_progress_bar,
199-
) as pbar:
200-
with io.open(to_file, mode="wb") as fobj:
201-
raw_write = fobj.write
202-
203-
def write(byte_string):
204-
raw_write(byte_string)
205-
pbar.update(len(byte_string))
206-
207-
fobj.write = write
208-
blob.download_to_file(fobj)
186+
with io.open(to_file, mode="wb") as fobj:
187+
with Tqdm.wrapattr(
188+
fobj,
189+
"write",
190+
desc=name or from_info.path,
191+
total=blob.size,
192+
disable=no_progress_bar,
193+
) as wrapped:
194+
blob.download_to_file(wrapped)
209195

210196
def _generate_download_url(self, path_info, expires=3600):
211197
expiration = timedelta(seconds=int(expires))

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def run(self):
7777
"funcy>=1.14",
7878
"pathspec>=0.6.0",
7979
"shortuuid>=0.5.0",
80-
"tqdm>=4.38.0,<5",
80+
"tqdm>=4.40.0,<5",
8181
"packaging>=19.0",
8282
"win-unicode-console>=0.5; sys_platform == 'win32'",
8383
"pywin32>=225; sys_platform == 'win32'",

0 commit comments

Comments
 (0)