Skip to content

Commit

Permalink
Merge pull request #131 from MichaelAquilina/total_size
Browse files Browse the repository at this point in the history
Total size
  • Loading branch information
MichaelAquilina committed Jun 29, 2018
2 parents 47902c2 + db835ff commit eed050a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
4 changes: 4 additions & 0 deletions s4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,7 @@ def main(arguments):

def entry_point():
main(sys.argv[1:])


if __name__ == "__main__":
entry_point()
34 changes: 34 additions & 0 deletions s4/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,61 @@ def get_sync_state(index_local, real_local, remote):

class SyncClient(object):
def get_client_name(self):
"""
Return a human readable name for the client.
"""
raise NotImplementedError()

def get_uri(self, key=""):
"""
Get the full Unique Resource Identifier given a key.
"""
raise NotImplementedError()

def lock(self, timeout=10):
"""
Lock the client storage so that any other external access to the client is prevented.
"""
raise NotImplementedError()

def unlock(self):
"""
Unlock the client storage for other external access. Will do nothing if nothing
was locked in the first place.
"""
raise NotImplementedError()

def put(self, key, sync_object):
"""
Put the given SyncObject on the clients storage with the given key.
"""
raise NotImplementedError()

def get(self, key):
"""
Get a SyncObject reference for the given key. Returns None if it does
not exist on the client's storage.
"""
raise NotImplementedError()

def delete(self, key):
"""
Delete the given key from client storage.
"""
raise NotImplementedError()

def get_size(self, key):
"""
Get the size of the file on the given client.
"""
raise NotImplementedError()

def get_local_keys(self):
"""
Get *all* files that exists on the clients local storage. This means that
keys outside the index could be returned if they are new. Also keys in the
index could not be listed if the file has been deleted.
"""
raise NotImplementedError()

def get_real_local_timestamp(self, key):
Expand Down
7 changes: 7 additions & 0 deletions s4/clients/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ def set_index_local_timestamp(self, key, timestamp):
self.index[key] = {}
self.index[key]["local_timestamp"] = timestamp

def get_size(self, key):
path = self.get_uri(key)
if os.path.exists(path):
return os.stat(path).st_size
else:
return 0

def get_remote_timestamp(self, key):
return self.index.get(key, {}).get("remote_timestamp")

Expand Down
9 changes: 8 additions & 1 deletion s4/commands/ls_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ def run(self):
sort_by = self.args.sort_by.lower()
descending = self.args.descending

keys = set(client_1.index) | set(client_2.index)
keys = set(client_1.get_index_keys()) | set(client_2.get_index_keys())

total_size = 0

data = []
for key in sorted(keys):
self.logger.debug("Processing %s", key)
entry_1 = client_1.index.get(key, {})
entry_2 = client_2.index.get(key, {})

Expand All @@ -47,8 +50,12 @@ def run(self):
else None,
)
)
size = client_1.get_size(key)
self.logger.debug("%s size: %s", key, size)
total_size += size

headers = ["key", "local", "s3"]
data = sorted(data, reverse=descending, key=lambda x: x[headers.index(sort_by)])

print(tabulate(data, headers=headers))
print("Total Size: {:.2f}Mb".format(total_size / (1024 * 1024)))
10 changes: 10 additions & 0 deletions tests/clients/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def test_unlock(self):
with pytest.raises(NotImplementedError):
client.unlock()

def test_get_size(self):
client = SyncClient()
with pytest.raises(NotImplementedError):
client.get_size("test")

def test_get_client_name(self):
client = SyncClient()
with pytest.raises(NotImplementedError):
Expand Down Expand Up @@ -110,6 +115,11 @@ def test_get_remote_timestamp(self):
with pytest.raises(NotImplementedError):
client.get_remote_timestamp("something")

def test_set_remote_timestamp(self):
client = SyncClient()
with pytest.raises(NotImplementedError):
client.set_remote_timestamp("test", None)

def test_get_all_remote_timestamps(self):
client = SyncClient()
with pytest.raises(NotImplementedError):
Expand Down
7 changes: 7 additions & 0 deletions tests/clients/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def test_lock(self, local_client):
local_client.lock(timeout=0.01)
local_client2.unlock()

def test_get_size_not_exists(self, local_client):
assert local_client.get_size("idontexist") == 0

def test_get_size_exists(self, local_client):
utils.write_local(local_client.get_uri("foo"), "test data")
assert local_client.get_size("foo") == 9

def test_put_callback(self, local_client):
mock_callback = mock.MagicMock()

Expand Down
6 changes: 5 additions & 1 deletion tests/commands/test_ls_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def test_correct_output_empty(self, s3_client, local_client, capsys):
out, err = capsys.readouterr()

assert err == ""
assert out == ("key local s3\n" "----- ------- ----\n")
assert out == (
"key local s3\n" "----- ------- ----\n" "Total Size: 0.00Mb\n"
)

def test_correct_output_nonempty(self, s3_client, local_client, capsys):
config = {
Expand Down Expand Up @@ -94,6 +96,7 @@ def test_correct_output_nonempty(self, s3_client, local_client, capsys):
"honey 2016-11-10 18:40:00 2016-12-12 08:30:00\n"
"lemon 2017-02-02 08:30:00\n"
"milk 2016-12-12 08:30:00 1989-10-23 11:30:00\n"
"Total Size: 0.00Mb\n"
)

def test_show_all(self, s3_client, local_client, capsys):
Expand Down Expand Up @@ -137,4 +140,5 @@ def test_show_all(self, s3_client, local_client, capsys):
"-------- ------------------- -------------------\n"
"cheese 2017-02-02 08:30:00 2017-12-12 08:30:00\n"
"crackers <deleted>\n"
"Total Size: 0.00Mb\n"
)

0 comments on commit eed050a

Please sign in to comment.