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

Added Parameter to Remove Extraneous Files on Upload #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 39 additions & 1 deletion acd_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import acdcli
from acdcli.api import client
from acdcli.api.common import RequestError, is_valid_id
from acdcli.cache import db, format
from acdcli.cache import db, format, schema
from acdcli.utils import hashing, progress
from acdcli.utils.threading import QueuedLoader

Expand Down Expand Up @@ -658,6 +658,31 @@ def regex_helper(args: argparse.Namespace) -> 'List[re._pattern_type]':
return excl_re


def delete_extraneous(node, path, exclude_re, exclude_paths, delete_excluded):
if node == None or node.status == 'TRASH':
return

if not os.path.exists(path):
name = os.path.basename(path)

#determine whether the missing file matches any exclude pattern
path_excluded = (path in exclude_paths) or any(map(lambda x: re.match(x, name), exclude_re))

if delete_excluded or not path_excluded:
logger.info('Moving "%s" to trash as it has been removed locally.' % node.name)

r = acd_client.move_to_trash(node.id)
cache.insert_node(r)

return

if isinstance(node, schema.Folder):
for child in node.children:
child_path = os.path.join(path, child.name)

delete_extraneous(child, child_path, exclude_re, exclude_paths, delete_excluded)


@no_autores_trash_action
def upload_action(args: argparse.Namespace) -> int:
if not cache.get_node(args.parent):
Expand All @@ -666,6 +691,15 @@ def upload_action(args: argparse.Namespace) -> int:

excl_re = regex_helper(args)

if args.delete:
exclude_paths = [os.path.realpath(p) for p in args.exclude_path]

for path in (os.path.realpath(p) for p in args.path):
# get destination node on server
node = cache.resolve('/' + os.path.basename(path), cache.get_node(args.parent))[0]

delete_extraneous(node, path, excl_re, exclude_paths, args.delete_excluded)

jobs = []
ret_val = 0
for path in args.path:
Expand Down Expand Up @@ -1221,6 +1255,10 @@ def get_parser() -> tuple:
help='exclude duplicate files from upload')
upload_sp.add_argument('--remove-source-files', '-rsf', action='store_true',
help='remove local files on successful upload')
upload_sp.add_argument('--delete', action='store_true',
help='delete extraneous files from dest dirs')
upload_sp.add_argument('--delete-excluded', action='store_true',
help='also delete excluded files from dest dirs')
upload_sp.add_argument('path', nargs='+', help='a path to a local file or directory')
upload_sp.add_argument('parent', help='remote parent folder')
upload_sp.set_defaults(func=upload_action)
Expand Down
2 changes: 2 additions & 0 deletions docs/contributors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Contributors

Thanks to

- `sthm <https://github.com/sthm>`_ for extending the upload action

- `chrisidefix <https://github.com/chrisidefix>`_ for adding the find-md5 action and
forcing me to create a proper package and use PyPI

Expand Down