1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
"""Functions for copying resources *between* filesystem.
"""
from __future__ import print_function, unicode_literals
import typing
import warnings
from .errors import ResourceNotFound
from .opener import manage_fs
from .path import abspath, combine, frombase, normpath
from .tools import is_thread_safe
from .walk import Walker
if typing.TYPE_CHECKING:
from typing import Callable, Optional, Text, Union
from .base import FS
_OnCopy = Callable[[FS, Text, FS, Text], object]
def copy_fs(
src_fs, # type: Union[FS, Text]
dst_fs, # type: Union[FS, Text]
walker=None, # type: Optional[Walker]
on_copy=None, # type: Optional[_OnCopy]
workers=0, # type: int
preserve_time=False, # type: bool
):
# type: (...) -> None
"""Copy the contents of one filesystem to another.
Arguments:
src_fs (FS or str): Source filesystem (URL or instance).
dst_fs (FS or str): Destination filesystem (URL or instance).
walker (~fs.walk.Walker, optional): A walker object that will be
used to scan for files in ``src_fs``. Set this if you only want
to consider a sub-set of the resources in ``src_fs``.
on_copy (callable): A function callback called after a single file copy
is executed. Expected signature is ``(src_fs, src_path, dst_fs,
dst_path)``.
workers (int): Use `worker` threads to copy data, or ``0`` (default) for
a single-threaded copy.
preserve_time (bool): If `True`, try to preserve mtime of the
resources (defaults to `False`).
"""
return copy_fs_if(
src_fs, dst_fs, "always", walker, on_copy, workers, preserve_time=preserve_time
)
def copy_fs_if_newer(
src_fs, # type: Union[FS, Text]
dst_fs, # type: Union[FS, Text]
walker=None, # type: Optional[Walker]
on_copy=None, # type: Optional[_OnCopy]
workers=0, # type: int
preserve_time=False, # type: bool
):
# type: (...) -> None
"""Copy the contents of one filesystem to another, checking times.
.. deprecated:: 2.5.0
Use `~fs.copy.copy_fs_if` with ``condition="newer"`` instead.
"""
warnings.warn(
"copy_fs_if_newer is deprecated. Use copy_fs_if instead.", DeprecationWarning
)
return copy_fs_if(
src_fs, dst_fs, "newer", walker, on_copy, workers, preserve_time=preserve_time
)
def copy_fs_if(
src_fs, # type: Union[FS, Text]
dst_fs, # type: Union[FS, Text]
condition="always", # type: Text
walker=None, # type: Optional[Walker]
on_copy=None, # type: Optional[_OnCopy]
workers=0, # type: int
preserve_time=False, # type: bool
):
# type: (...) -> None
"""Copy the contents of one filesystem to another, depending on a condition.
Arguments:
src_fs (FS or str): Source filesystem (URL or instance).
dst_fs (FS or str): Destination filesystem (URL or instance).
condition (str): Name of the condition to check for each file.
walker (~fs.walk.Walker, optional): A walker object that will be
used to scan for files in ``src_fs``. Set this if you only want
to consider a sub-set of the resources in ``src_fs``.
on_copy (callable):A function callback called after a single file copy
is executed. Expected signature is ``(src_fs, src_path, dst_fs,
dst_path)``.
workers (int): Use ``worker`` threads to copy data, or ``0`` (default)
for a single-threaded copy.
preserve_time (bool): If `True`, try to preserve mtime of the
resources (defaults to `False`).
See Also:
`~fs.copy.copy_file_if` for the full list of supported values for the
``condition`` argument.
"""
return copy_dir_if(
src_fs,
"/",
dst_fs,
"/",
condition,
walker=walker,
on_copy=on_copy,
workers=workers,
preserve_time=preserve_time,
)
def copy_file(
src_fs, # type: Union[FS, Text]
src_path, # type: Text
dst_fs, # type: Union[FS, Text]
dst_path, # type: Text
preserve_time=False, # type: bool
):
# type: (...) -> None
"""Copy a file from one filesystem to another.
If the destination exists, and is a file, it will be first truncated.
Arguments:
src_fs (FS or str): Source filesystem (instance or URL).
src_path (str): Path to a file on the source filesystem.
dst_fs (FS or str): Destination filesystem (instance or URL).
dst_path (str): Path to a file on the destination filesystem.
preserve_time (bool): If `True`, try to preserve mtime of the
resource (defaults to `False`).
"""
copy_file_if(
src_fs, src_path, dst_fs, dst_path, "always", preserve_time=preserve_time
)
def copy_file_if_newer(
src_fs, # type: Union[FS, Text]
src_path, # type: Text
dst_fs, # type: Union[FS, Text]
dst_path, # type: Text
preserve_time=False, # type: bool
):
# type: (...) -> bool
"""Copy a file from one filesystem to another, checking times.
.. deprecated:: 2.5.0
Use `~fs.copy.copy_file_if` with ``condition="newer"`` instead.
"""
warnings.warn(
"copy_file_if_newer is deprecated. Use copy_file_if instead.",
DeprecationWarning,
)
return copy_file_if(
src_fs, src_path, dst_fs, dst_path, "newer", preserve_time=preserve_time
)
def copy_file_if(
src_fs, # type: Union[FS, Text]
src_path, # type: Text
dst_fs, # type: Union[FS, Text]
dst_path, # type: Text
condition, # type: Text
preserve_time=False, # type: bool
):
# type: (...) -> bool
"""Copy a file from one filesystem to another, depending on a condition.
Depending on the value of ``condition``, certain requirements must
be fulfilled for a file to be copied to ``dst_fs``. The following
values are supported:
``"always"``
The source file is always copied.
``"newer"``
The last modification time of the source file must be newer than that
of the destination file. If either file has no modification time, the
copy is performed always.
``"older"``
The last modification time of the source file must be older than that
of the destination file. If either file has no modification time, the
copy is performed always.
``"exists"``
The source file is only copied if a file of the same path already
exists in ``dst_fs``.
``"not_exists"``
The source file is only copied if no file of the same path already
exists in ``dst_fs``.
Arguments:
src_fs (FS or str): Source filesystem (instance or URL).
src_path (str): Path to a file on the source filesystem.
dst_fs (FS or str): Destination filesystem (instance or URL).
dst_path (str): Path to a file on the destination filesystem.
condition (str): Name of the condition to check for each file.
preserve_time (bool): If `True`, try to preserve mtime of the
resource (defaults to `False`).
Returns:
bool: `True` if the file copy was executed, `False` otherwise.
"""
with manage_fs(src_fs, writeable=False) as _src_fs:
with manage_fs(dst_fs, create=True) as _dst_fs:
do_copy = _copy_is_necessary(
_src_fs, src_path, _dst_fs, dst_path, condition
)
if do_copy:
copy_file_internal(
_src_fs,
src_path,
_dst_fs,
dst_path,
preserve_time=preserve_time,
lock=True,
)
return do_copy
def copy_file_internal(
src_fs, # type: FS
src_path, # type: Text
dst_fs, # type: FS
dst_path, # type: Text
preserve_time=False, # type: bool
lock=False, # type: bool
):
# type: (...) -> None
"""Copy a file at low level, without calling `manage_fs` or locking.
If the destination exists, and is a file, it will be first truncated.
This method exists to optimize copying in loops. In general you
should prefer `copy_file`.
Arguments:
src_fs (FS): Source filesystem.
src_path (str): Path to a file on the source filesystem.
dst_fs (FS): Destination filesystem.
dst_path (str): Path to a file on the destination filesystem.
preserve_time (bool): If `True`, try to preserve mtime of the
resource (defaults to `False`).
lock (bool): Lock both filesystems before copying.
"""
if src_fs is dst_fs:
# Same filesystem, so we can do a potentially optimized
# copy
src_fs.copy(src_path, dst_path, overwrite=True, preserve_time=preserve_time)
return
def _copy_locked():
if dst_fs.hassyspath(dst_path):
with dst_fs.openbin(dst_path, "w") as write_file:
src_fs.download(src_path, write_file)
else:
with src_fs.openbin(src_path) as read_file:
dst_fs.upload(dst_path, read_file)
if preserve_time:
copy_modified_time(src_fs, src_path, dst_fs, dst_path)
if lock:
with src_fs.lock(), dst_fs.lock():
_copy_locked()
else:
_copy_locked()
def copy_structure(
src_fs, # type: Union[FS, Text]
dst_fs, # type: Union[FS, Text]
walker=None, # type: Optional[Walker]
src_root="/", # type: Text
dst_root="/", # type: Text
):
# type: (...) -> None
"""Copy directories (but not files) from ``src_fs`` to ``dst_fs``.
Arguments:
src_fs (FS or str): Source filesystem (instance or URL).
dst_fs (FS or str): Destination filesystem (instance or URL).
walker (~fs.walk.Walker, optional): A walker object that will be
used to scan for files in ``src_fs``. Set this if you only
want to consider a sub-set of the resources in ``src_fs``.
src_root (str): Path of the base directory to consider as the root
of the tree structure to copy.
dst_root (str): Path to the target root of the tree structure.
"""
walker = walker or Walker()
with manage_fs(src_fs) as _src_fs:
with manage_fs(dst_fs, create=True) as _dst_fs:
with _src_fs.lock(), _dst_fs.lock():
_dst_fs.makedirs(dst_root, recreate=True)
for dir_path in walker.dirs(_src_fs, src_root):
_dst_fs.makedir(
combine(dst_root, frombase(src_root, dir_path)), recreate=True
)
def copy_dir(
src_fs, # type: Union[FS, Text]
src_path, # type: Text
dst_fs, # type: Union[FS, Text]
dst_path, # type: Text
walker=None, # type: Optional[Walker]
on_copy=None, # type: Optional[_OnCopy]
workers=0, # type: int
preserve_time=False, # type: bool
):
# type: (...) -> None
"""Copy a directory from one filesystem to another.
Arguments:
src_fs (FS or str): Source filesystem (instance or URL).
src_path (str): Path to a directory on the source filesystem.
dst_fs (FS or str): Destination filesystem (instance or URL).
dst_path (str): Path to a directory on the destination filesystem.
walker (~fs.walk.Walker, optional): A walker object that will be
used to scan for files in ``src_fs``. Set this if you only
want to consider a sub-set of the resources in ``src_fs``.
on_copy (callable, optional): A function callback called after
a single file copy is executed. Expected signature is
``(src_fs, src_path, dst_fs, dst_path)``.
workers (int): Use ``worker`` threads to copy data, or ``0`` (default) for
a single-threaded copy.
preserve_time (bool): If `True`, try to preserve mtime of the
resources (defaults to `False`).
"""
copy_dir_if(
src_fs,
src_path,
dst_fs,
dst_path,
"always",
walker,
on_copy,
workers,
preserve_time=preserve_time,
)
def copy_dir_if_newer(
src_fs, # type: Union[FS, Text]
src_path, # type: Text
dst_fs, # type: Union[FS, Text]
dst_path, # type: Text
walker=None, # type: Optional[Walker]
on_copy=None, # type: Optional[_OnCopy]
workers=0, # type: int
preserve_time=False, # type: bool
):
# type: (...) -> None
"""Copy a directory from one filesystem to another, checking times.
.. deprecated:: 2.5.0
Use `~fs.copy.copy_dir_if` with ``condition="newer"`` instead.
"""
warnings.warn(
"copy_dir_if_newer is deprecated. Use copy_dir_if instead.", DeprecationWarning
)
copy_dir_if(
src_fs,
src_path,
dst_fs,
dst_path,
"newer",
walker,
on_copy,
workers,
preserve_time=preserve_time,
)
def copy_dir_if(
src_fs, # type: Union[FS, Text]
src_path, # type: Text
dst_fs, # type: Union[FS, Text]
dst_path, # type: Text
condition, # type: Text
walker=None, # type: Optional[Walker]
on_copy=None, # type: Optional[_OnCopy]
workers=0, # type: int
preserve_time=False, # type: bool
):
# type: (...) -> None
"""Copy a directory from one filesystem to another, depending on a condition.
Arguments:
src_fs (FS or str): Source filesystem (instance or URL).
src_path (str): Path to a directory on the source filesystem.
dst_fs (FS or str): Destination filesystem (instance or URL).
dst_path (str): Path to a directory on the destination filesystem.
condition (str): Name of the condition to check for each file.
walker (~fs.walk.Walker, optional): A walker object that will be
used to scan for files in ``src_fs``. Set this if you only want
to consider a sub-set of the resources in ``src_fs``.
on_copy (callable):A function callback called after a single file copy
is executed. Expected signature is ``(src_fs, src_path, dst_fs,
dst_path)``.
workers (int): Use ``worker`` threads to copy data, or ``0`` (default) for
a single-threaded copy.
preserve_time (bool): If `True`, try to preserve mtime of the
resources (defaults to `False`).
See Also:
`~fs.copy.copy_file_if` for the full list of supported values for the
``condition`` argument.
"""
on_copy = on_copy or (lambda *args: None)
walker = walker or Walker()
_src_path = abspath(normpath(src_path))
_dst_path = abspath(normpath(dst_path))
from ._bulk import Copier
copy_structure(src_fs, dst_fs, walker, src_path, dst_path)
with manage_fs(src_fs, writeable=False) as _src_fs, manage_fs(
dst_fs, create=True
) as _dst_fs:
with _src_fs.lock(), _dst_fs.lock():
_thread_safe = is_thread_safe(_src_fs, _dst_fs)
with Copier(
num_workers=workers if _thread_safe else 0, preserve_time=preserve_time
) as copier:
for dir_path in walker.files(_src_fs, _src_path):
copy_path = combine(_dst_path, frombase(_src_path, dir_path))
if _copy_is_necessary(
_src_fs, dir_path, _dst_fs, copy_path, condition
):
copier.copy(_src_fs, dir_path, _dst_fs, copy_path)
on_copy(_src_fs, dir_path, _dst_fs, copy_path)
def _copy_is_necessary(
src_fs, # type: FS
src_path, # type: Text
dst_fs, # type: FS
dst_path, # type: Text
condition, # type: Text
):
# type: (...) -> bool
if condition == "always":
return True
elif condition == "newer":
try:
src_modified = src_fs.getmodified(src_path)
dst_modified = dst_fs.getmodified(dst_path)
except ResourceNotFound:
return True
else:
return (
src_modified is None
or dst_modified is None
or src_modified > dst_modified
)
elif condition == "older":
try:
src_modified = src_fs.getmodified(src_path)
dst_modified = dst_fs.getmodified(dst_path)
except ResourceNotFound:
return True
else:
return (
src_modified is None
or dst_modified is None
or src_modified < dst_modified
)
elif condition == "exists":
return dst_fs.exists(dst_path)
elif condition == "not_exists":
return not dst_fs.exists(dst_path)
else:
raise ValueError("{} is not a valid copy condition.".format(condition))
def copy_modified_time(
src_fs, # type: Union[FS, Text]
src_path, # type: Text
dst_fs, # type: Union[FS, Text]
dst_path, # type: Text
):
# type: (...) -> None
"""Copy modified time metadata from one file to another.
Arguments:
src_fs (FS or str): Source filesystem (instance or URL).
src_path (str): Path to a directory on the source filesystem.
dst_fs (FS or str): Destination filesystem (instance or URL).
dst_path (str): Path to a directory on the destination filesystem.
"""
namespaces = ("details",)
with manage_fs(src_fs, writeable=False) as _src_fs:
with manage_fs(dst_fs, create=True) as _dst_fs:
src_meta = _src_fs.getinfo(src_path, namespaces)
src_details = src_meta.raw.get("details", {})
dst_details = {}
for value in ("metadata_changed", "modified"):
if value in src_details:
dst_details[value] = src_details[value]
_dst_fs.setinfo(dst_path, {"details": dst_details})
|