Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Popen handle if requested #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 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,11 +138,12 @@ 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 = []

_rclone_transfer_operation(
return _rclone_transfer_operation(
in_path,
out_path,
ignore_existing=ignore_existing,
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,11 +173,12 @@ 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 = []

_rclone_transfer_operation(
return _rclone_transfer_operation(
in_path,
out_path,
ignore_existing=ignore_existing,
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,11 +208,12 @@ 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 = []

_rclone_transfer_operation(
return _rclone_transfer_operation(
in_path,
out_path,
ignore_existing=ignore_existing,
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,11 +243,12 @@ 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 = []

_rclone_transfer_operation(
return _rclone_transfer_operation(
in_path,
out_path,
ignore_existing=ignore_existing,
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,26 +267,29 @@ 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 = []

_rclone_transfer_operation(
return _rclone_transfer_operation(
src_path,
dest_path,
command="rclone sync",
command_descr="Syncing",
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,11 @@ 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 if `wait_on_finish`
None after waiting until operation is complete otherwise
"""
if args is None:
args = []
Expand All @@ -601,6 +623,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