Skip to content

Commit 9d0acbc

Browse files
Add deletion-batch-size option to sync
* Add `deletion-batch-size` option to `sync` * Apply suggestions from code review Co-authored-by: carlevison <[email protected]> Co-authored-by: carlevison <[email protected]>
1 parent 2e1fb41 commit 9d0acbc

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

cloudinary_cli/modules/sync.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from cloudinary_cli.utils.json_utils import print_json
1313
from cloudinary_cli.utils.utils import logger, run_tasks_concurrently, get_user_action
1414

15-
DELETE_ASSETS_BATCH_SIZE = 30
15+
_DEFAULT_DELETION_BATCH_SIZE = 30
16+
_DEFAULT_CONCURRENT_WORKERS = 30
1617

1718

1819
@command("sync",
@@ -22,14 +23,17 @@
2223
@argument("cloudinary_folder")
2324
@option("--push", help="Push changes from your local folder to your Cloudinary folder.", is_flag=True)
2425
@option("--pull", help="Pull changes from your Cloudinary folder to your local folder.", is_flag=True)
25-
@option("-w", "--concurrent_workers", type=int, default=30, help="Specify number of concurrent network threads.")
26+
@option("-w", "--concurrent_workers", type=int, default=_DEFAULT_CONCURRENT_WORKERS,
27+
help="Specify the number of concurrent network threads.")
2628
@option("-F", "--force", is_flag=True, help="Skip confirmation when deleting files.")
2729
@option("-K", "--keep-unique", is_flag=True, help="Keep unique files in the destination folder.")
28-
def sync(local_folder, cloudinary_folder, push, pull, concurrent_workers, force, keep_unique):
30+
@option("-D", "--deletion-batch-size", type=int, default=_DEFAULT_DELETION_BATCH_SIZE,
31+
help="Specify the batch size for deleting remote assets.")
32+
def sync(local_folder, cloudinary_folder, push, pull, concurrent_workers, force, keep_unique, deletion_batch_size):
2933
if push == pull:
3034
raise Exception("Please use either the '--push' OR '--pull' options")
3135

32-
sync_dir = SyncDir(local_folder, cloudinary_folder, concurrent_workers, force, keep_unique)
36+
sync_dir = SyncDir(local_folder, cloudinary_folder, concurrent_workers, force, keep_unique, deletion_batch_size)
3337

3438
if push:
3539
sync_dir.push()
@@ -40,12 +44,13 @@ def sync(local_folder, cloudinary_folder, push, pull, concurrent_workers, force,
4044

4145

4246
class SyncDir:
43-
def __init__(self, local_dir, remote_dir, concurrent_workers, force, keep_deleted):
47+
def __init__(self, local_dir, remote_dir, concurrent_workers, force, keep_deleted, deletion_batch_size):
4448
self.local_dir = local_dir
4549
self.remote_dir = remote_dir
4650
self.concurrent_workers = concurrent_workers
4751
self.force = force
4852
self.keep_unique = keep_deleted
53+
self.deletion_batch_size = deletion_batch_size
4954

5055
self.verbose = logger.getEffectiveLevel() < logging.INFO
5156

@@ -125,10 +130,10 @@ def _handle_unique_remote_files(self):
125130

126131
logger.info("Deleting {} resources with type '{}' and resource_type '{}'".format(len(batch), *i))
127132
counter = 0
128-
while counter * DELETE_ASSETS_BATCH_SIZE < len(batch) and len(batch) > 0:
133+
while counter * self.deletion_batch_size < len(batch) and len(batch) > 0:
129134
counter += 1
130135
res = api.delete_resources(
131-
batch[(counter - 1) * DELETE_ASSETS_BATCH_SIZE:counter * DELETE_ASSETS_BATCH_SIZE], invalidate=True,
136+
batch[(counter - 1) * self.deletion_batch_size:counter * self.deletion_batch_size], invalidate=True,
132137
resource_type=i[1], type=i[0])
133138
num_deleted = reduce(lambda x, y: x + 1 if y == "deleted" else x, res['deleted'].values(), 0)
134139
if self.verbose:

cloudinary_cli/utils/api_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,22 @@ def asset_source(asset_details):
7575
"""
7676
Public ID of the transformable file (image/video) does not include file extension.
7777
78-
It needs to be added in order to download file properly (without creating derived asset).
78+
It needs to be added in order to download the file properly (without creating a derived asset).
7979
8080
Raw files are accessed using only public_id.
8181
82+
Fetched files are not altered as well.
83+
8284
:param asset_details: The details of the asset.
8385
:rtype asset_details: dict
8486
8587
:return:
8688
"""
8789
base_name = asset_details['public_id']
88-
return base_name + "." + asset_details['format'] if asset_details['resource_type'] != 'raw' else base_name
90+
if asset_details['resource_type'] == 'raw' or asset_details['type'] == 'fetch':
91+
return base_name
92+
93+
return base_name + '.' + asset_details['format']
8994

9095

9196
def handle_command(

0 commit comments

Comments
 (0)