Skip to content

Commit eaba5d7

Browse files
committed
fs: temporary implement get_file
While waiting for fsspec/filesystem_spec#1191
1 parent 4517567 commit eaba5d7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/scmrepo/fs.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import posixpath
44
from typing import TYPE_CHECKING, Any, BinaryIO, Callable, Dict, Optional, Tuple
55

6+
from fsspec.callbacks import _DEFAULT_CALLBACK
67
from fsspec.spec import AbstractFileSystem
8+
from fsspec.utils import isfilelike
79

810
if TYPE_CHECKING:
911
from io import BytesIO
@@ -242,3 +244,32 @@ def ls(self, path, detail=True, **kwargs):
242244
return paths
243245

244246
return [self.info(_path) for _path in paths]
247+
248+
def get_file(
249+
self, rpath, lpath, callback=_DEFAULT_CALLBACK, outfile=None, **kwargs
250+
):
251+
# NOTE: temporary workaround while waiting for
252+
# https://github.com/fsspec/filesystem_spec/pull/1191
253+
254+
if isfilelike(lpath):
255+
outfile = lpath
256+
elif self.isdir(rpath):
257+
os.makedirs(lpath, exist_ok=True)
258+
return None
259+
260+
with self.open(rpath, "rb", **kwargs) as f1:
261+
if outfile is None:
262+
outfile = open(lpath, "wb")
263+
264+
try:
265+
callback.set_size(getattr(f1, "size", None))
266+
data = True
267+
while data:
268+
data = f1.read(self.blocksize)
269+
segment_len = outfile.write(data)
270+
if segment_len is None:
271+
segment_len = len(data)
272+
callback.relative_update(segment_len)
273+
finally:
274+
if not isfilelike(lpath):
275+
outfile.close()

0 commit comments

Comments
 (0)