Skip to content

Commit

Permalink
Return Popen handle if requested
Browse files Browse the repository at this point in the history
Allows easy interruption, potentially answers Johannes11833#20
  • Loading branch information
kunaltyagi authored Oct 25, 2023
1 parent 20a6f88 commit 61bbfbc
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions rclone_python/rclone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from functools import wraps
from shutil import which
from subprocess import Popen
from typing import Optional, Union, List, Dict, Callable

from rclone_python import utils
Expand Down Expand Up @@ -127,7 +128,8 @@ def copy(
show_progress=True,
listener: Callable[[Dict], None] = None,
args=None,
):
wait_to_finish:bool = True,
) -> Optional[Popen]:
"""
Copies a file or a directory from a src path to a destination path.
:param in_path: The source path to use. Specify the remote with 'remote_name:path_on_remote'
Expand All @@ -136,6 +138,7 @@ def copy(
:param show_progress: If true, show a progressbar.
:param listener: An event-listener that is called with every update of rclone.
:param args: List of additional arguments/ flags.
:param wait_to_finish: A boolean flag to choose between sync (default) and async operation. Returns a handle for async mode
"""
if args is None:
args = []
Expand All @@ -149,6 +152,7 @@ def copy(
show_progress=show_progress,
listener=listener,
args=args,
wait_to_finish=wait_to_finish,
)


Expand All @@ -159,7 +163,8 @@ def copyto(
show_progress=True,
listener: Callable[[Dict], None] = None,
args=None,
):
wait_to_finish:bool = True,
) -> Optional[Popen]:
"""
Copies a file or a directory from a src path to a destination path and is typically used when renaming a file is necessary.
:param in_path: The source path to use. Specify the remote with 'remote_name:path_on_remote'
Expand All @@ -168,6 +173,7 @@ def copyto(
:param show_progress: If true, show a progressbar.
:param listener: An event-listener that is called with every update of rclone.
:param args: List of additional arguments/ flags.
:param wait_to_finish: A boolean flag to choose between sync (default) and async operation. Returns a handle for async mode
"""
if args is None:
args = []
Expand All @@ -181,6 +187,7 @@ def copyto(
show_progress=show_progress,
listener=listener,
args=args,
wait_to_finish=wait_to_finish,
)


Expand All @@ -191,7 +198,8 @@ def move(
show_progress=True,
listener: Callable[[Dict], None] = None,
args=None,
):
wait_to_finish:bool = True,
) -> Optional[Popen]:
"""
Moves a file or a directory from a src path to a destination path.
:param in_path: The source path to use. Specify the remote with 'remote_name:path_on_remote'
Expand All @@ -200,6 +208,7 @@ def move(
:param show_progress: If true, show a progressbar.
:param listener: An event-listener that is called with every update of rclone.
:param args: List of additional arguments/ flags.
:param wait_to_finish: A boolean flag to choose between sync (default) and async operation. Returns a handle for async mode
"""
if args is None:
args = []
Expand All @@ -213,6 +222,7 @@ def move(
show_progress=show_progress,
listener=listener,
args=args,
wait_to_finish=wait_to_finish,
)


Expand All @@ -223,7 +233,8 @@ def moveto(
show_progress=True,
listener: Callable[[Dict], None] = None,
args=None,
):
wait_to_finish:bool = True,
) -> Optional[Popen]:
"""
Moves a file or a directory from a src path to a destination path and is typically used when renaming is necessary.
:param in_path: The source path to use. Specify the remote with 'remote_name:path_on_remote'
Expand All @@ -232,6 +243,7 @@ def moveto(
:param show_progress: If true, show a progressbar.
:param listener: An event-listener that is called with every update of rclone.
:param args: List of additional arguments/ flags.
:param wait_to_finish: A boolean flag to choose between sync (default) and async operation. Returns a handle for async mode
"""
if args is None:
args = []
Expand All @@ -245,6 +257,7 @@ def moveto(
show_progress=show_progress,
listener=listener,
args=args,
wait_to_finish=wait_to_finish,
)


Expand All @@ -254,14 +267,16 @@ def sync(
show_progress=True,
listener: Callable[[Dict], None] = None,
args=None,
):
wait_to_finish:bool = True,
) -> Optional[Popen]:
"""
Sync the source to the destination, changing the destination only. Doesn't transfer files that are identical on source and destination, testing by size and modification time or MD5SUM.
:param in_path: The source path to use. Specify the remote with 'remote_name:path_on_remote'
:param out_path: The destination path to use. Specify the remote with 'remote_name:path_on_remote'
:param show_progress: If true, show a progressbar.
:param listener: An event-listener that is called with every update of rclone.
:param args: List of additional arguments/ flags.
:param wait_to_finish: A boolean flag to choose between sync (default) and async operation. Returns a handle for async mode
"""
if args is None:
args = []
Expand All @@ -274,6 +289,7 @@ def sync(
show_progress=show_progress,
listener=listener,
args=args,
wait_to_finish=wait_to_finish,
)


Expand Down Expand Up @@ -566,7 +582,8 @@ def _rclone_transfer_operation(
show_progress=True,
listener: Callable[[Dict], None] = None,
args=None,
):
wait_to_finish:bool = True,
) -> Optional[Popen]:
"""Executes the rclone transfer operation (e.g. copyto, move, ...) and displays the progress of every individual file.
Args:
Expand All @@ -578,6 +595,10 @@ def _rclone_transfer_operation(
show_progress (bool, optional): If true, show a progressbar.
listener (Callable[[Dict], None], optional): An event-listener that is called with every update of rclone.
args: List of additional arguments/ flags.
wait_to_finish (bool): A flag to choose between sync (default) and async operation
Returns:
handle to the subprocess or None depending on wait_to_finish
"""
if args is None:
args = []
Expand All @@ -601,6 +622,8 @@ def _rclone_transfer_operation(
process = utils.rclone_progress(
command, prog_title, listener=listener, show_progress=show_progress, debug=DEBUG
)
if not wait_to_finish:
return process

if process.wait() == 0:
logging.info("Cloud upload completed.")
Expand Down

0 comments on commit 61bbfbc

Please sign in to comment.