Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #69 from sernst/file-synchronization
Browse files Browse the repository at this point in the history
File Synchronization
  • Loading branch information
sernst authored Jan 30, 2020
2 parents 17db5d0 + 64f6a9d commit 783b73c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
50 changes: 19 additions & 31 deletions cauldron/cli/sync/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,21 @@ def get_progress(complete_count: int = 0) -> typing.Tuple[int, str]:
return 0, ''

progress_value = int(100 * complete_count / chunk_count)
display = '({}%)'.format('{}'.format(progress_value).zfill(3))
display = '({}%)'.format('{}'.format(progress_value).ljust(3))
return progress_value, display

progress_display = get_progress(0)[-1]
callback(response.notify(
kind='SYNC',
code='STARTED',
message='{} "{}"'.format(progress_display, relative_path),
data=dict(
progress=0,
file_path=file_path,
relative_path=relative_path
)
progress=0,
file_path=file_path,
relative_path=relative_path,
))

offset = 0
for index, chunk in enumerate(chunks):
for index, (chunk, length) in enumerate(chunks):
response = send_chunk(
chunk=chunk,
index=index,
Expand All @@ -107,40 +105,30 @@ def get_progress(complete_count: int = 0) -> typing.Tuple[int, str]:
file_kind=file_kind,
remote_connection=remote_connection,
sync_time=sync_time,
location=location
location=location,
)
offset += len(chunk)
offset += length

if response.failed:
return response

progress, progress_display = get_progress(index + 1)
if chunk_count > 1 and (index + 1) < chunk_count:
if chunk_count > 1:
callback(response.notify(
kind='SYNC',
code='PROGRESS',
message='{} "{}"'.format(progress_display, relative_path),
data=dict(
progress=0.01 * progress,
chunk_count=chunk_count,
file_path=file_path,
relative_path=relative_path
)
code='PROGRESS' if (index + 1) < chunk_count else 'DONE',
message='{} -> {} {} "{}"'.format(
'+{:,.0f}B'.format(length),
'{:,.0f}B'.format(offset),
progress_display,
relative_path
),
progress=0.01 * progress,
chunk_count=chunk_count,
file_path=file_path,
relative_path=relative_path
))

progress, progress_display = get_progress(chunk_count)
callback(response.notify(
kind='SYNC',
code='DONE',
message='{} "{}"'.format(progress_display, relative_path),
data=dict(
chunk_count=chunk_count,
progress=progress,
file_path=file_path,
relative_path=relative_path
)
))

return response


Expand Down
5 changes: 3 additions & 2 deletions cauldron/cli/sync/sync_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import math
import os
import zlib
import typing

from cauldron import writer

Expand Down Expand Up @@ -68,7 +69,7 @@ def get_file_chunk_count(
def read_file_chunks(
file_path: str,
chunk_size: int = DEFAULT_CHUNK_SIZE
) -> bytes:
) -> typing.Tuple[str, int]:
"""
Reads the specified file in chunks and returns a generator where
each returned chunk is a compressed base64 encoded string for sync
Expand All @@ -89,7 +90,7 @@ def read_file_chunks(
for chunk_index in range(chunk_count):
source = fp.read(chunk_size)
chunk = pack_chunk(source)
yield chunk
yield chunk, len(source)


def write_file_chunk(
Expand Down
4 changes: 2 additions & 2 deletions cauldron/test/cli/sync/test_sync_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_reading_chunks(self):

path = os.path.realpath(__file__)
out = self.get_temp_path('test_reading_chunks', 'test.py')
for chunk in sync.io.read_file_chunks(path, 100):
for chunk, length in sync.io.read_file_chunks(path, 100):
sync.io.write_file_chunk(out, chunk)

with open(path) as f:
Expand All @@ -33,5 +33,5 @@ def test_reading_no_such_file(self):
"""Should abort reading chunks if no such file exists."""

fake_path = '{}.fake-file'.format(__file__)
chunks = [c for c in sync.io.read_file_chunks(fake_path)]
chunks = [c for c, length in sync.io.read_file_chunks(fake_path)]
self.assertEqual(0, len(chunks))

0 comments on commit 783b73c

Please sign in to comment.