This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from ukaea/feature/add-metadata-to-archive-files/
- Loading branch information
Showing
16 changed files
with
429 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 43 additions & 10 deletions
53
ditto_web_api/DittoWebApi/src/services/internal/archiver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,55 @@ | ||
from DittoWebApi.src.utils.system_helper import current_time_in_utc | ||
|
||
|
||
class Archiver: | ||
def __init__(self, file_system_helper, logger): | ||
def __init__(self, file_read_write_helper, file_system_helper, logger): | ||
self._logger = logger | ||
self._file_system_helper = file_system_helper | ||
self._file_read_write_helper = file_read_write_helper | ||
|
||
def write_archive(self, archive_file_path, file_summary): | ||
try: | ||
content = {} | ||
new_archive_file = self._file_system_helper.create_and_open_file_for_writing(archive_file_path) | ||
|
||
def write_archive(self, file_path, file_summary): | ||
content = "test" | ||
self._file_system_helper.create_file(file_path, content) | ||
self._logger.debug(f"Archive file created: {file_path}") | ||
self._archive_file_summary(file_summary, content) | ||
|
||
def update_archive(self, file_path, file_summary): | ||
content = "test test" | ||
self._file_read_write_helper.write_json_to_file(new_archive_file, content) | ||
except Exception as exception: | ||
self._logger.error(f"Exception found here: {exception}") | ||
raise | ||
finally: | ||
self._file_system_helper.close_file(new_archive_file) | ||
self._logger.debug(f"Archive file created: {archive_file_path}") | ||
|
||
def update_archive(self, archive_file_path, file_summary): | ||
try: | ||
archived_file = self._file_system_helper.open_file(file_path) | ||
archived_file.write(content) | ||
archived_file = self._file_system_helper.open_file_for_reading_and_writing(archive_file_path) | ||
content = self._file_read_write_helper.read_file_as_json(archived_file) | ||
self._file_read_write_helper.clear_file(archived_file) | ||
|
||
self._archive_file_summary(file_summary, content) | ||
|
||
self._file_read_write_helper.write_json_to_file(archived_file, content) | ||
|
||
except Exception as exception: | ||
self._logger.error(f"Exception found: {exception}") | ||
raise | ||
finally: | ||
self._file_system_helper.close_file(archived_file) | ||
self._logger.debug(f"Archive file updated: {file_path}") | ||
self._logger.debug(f"Archive file updated: {archive_file_path}") | ||
|
||
def _archive_file(self, file, time_of_transfer, type_of_transfer): | ||
size = self._file_system_helper.file_size(file.abs_path) | ||
return {"file": file.file_name, | ||
"size": size, | ||
"latest update": time_of_transfer, | ||
"type of transfer": type_of_transfer} | ||
|
||
def _archive_file_summary(self, file_summary, content): | ||
time_of_transfer = str(current_time_in_utc()) | ||
for file in file_summary.new_files: | ||
content[file.file_name] = self._archive_file(file, time_of_transfer, "new upload") | ||
|
||
for file in file_summary.updated_files: | ||
content[file.file_name] = self._archive_file(file, time_of_transfer, "file update") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
ditto_web_api/DittoWebApi/src/utils/file_read_write_helper.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import json | ||
|
||
|
||
class FileReadWriteHelper: | ||
@staticmethod | ||
def write_json_to_file(open_file, json_content): | ||
json.dump(json_content, open_file) | ||
|
||
@staticmethod | ||
def read_file_as_json(open_file): | ||
return json.load(open_file) | ||
|
||
@staticmethod | ||
def clear_file(open_file): | ||
open_file.seek(0) | ||
open_file.truncate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import time | ||
from datetime import datetime | ||
|
||
|
||
def current_time(): | ||
return time.time() | ||
|
||
|
||
def current_time_in_utc(): | ||
time_stamp = current_time() | ||
return datetime.utcfromtimestamp(time_stamp) |
115 changes: 115 additions & 0 deletions
115
ditto_web_api/DittoWebApi/tests/services/archiver_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# pylint: disable=W0201 | ||
import logging | ||
import unittest | ||
import pytest | ||
import mock | ||
|
||
from DittoWebApi.src.utils.file_system.files_system_helpers import FileSystemHelper | ||
from DittoWebApi.src.models.file_storage_summary import FilesStorageSummary | ||
from DittoWebApi.src.models.file_information import FileInformation | ||
from DittoWebApi.src.services.internal.archiver import Archiver | ||
from DittoWebApi.src.utils.file_read_write_helper import FileReadWriteHelper | ||
|
||
|
||
class TestArchive(unittest.TestCase): | ||
@pytest.fixture(autouse=True) | ||
def setup(self): | ||
self._file_system_helper = mock.create_autospec(FileSystemHelper) | ||
self._logger = mock.create_autospec(logging.Logger) | ||
self._file_read_write_helper = mock.create_autospec(FileReadWriteHelper) | ||
self.test_archiver = Archiver(self._file_read_write_helper, self._file_system_helper, self._logger) | ||
|
||
self.mock_open_file = mock.Mock() | ||
self._file_system_helper.create_and_open_file_for_writing.return_value = self.mock_open_file | ||
self._file_system_helper.open_file_for_reading_and_writing.return_value = self.mock_open_file | ||
|
||
self.mock_file_1 = mock.create_autospec(FileInformation) | ||
self.mock_file_2 = mock.create_autospec(FileInformation) | ||
self.mock_file_1.file_name = "file_1.txt" | ||
self.mock_file_2.file_name = "file_2.txt" | ||
|
||
self.mock_file_summary = mock.create_autospec(FilesStorageSummary) | ||
|
||
self._file_system_helper.file_size.side_effect = [100, 50] | ||
|
||
@mock.patch('DittoWebApi.src.utils.system_helper.time.time', return_value=12345) | ||
def test_write_archive_creates_archive_file(self, time): | ||
self.mock_file_summary.new_files = [self.mock_file_1] | ||
self.mock_file_summary.updated_files = [self.mock_file_2] | ||
# Act | ||
self.test_archiver.write_archive("some_file_path", self.mock_file_summary) | ||
# Assert | ||
self._logger.debug.assert_called_with("Archive file created: some_file_path") | ||
self._file_read_write_helper.write_json_to_file.assert_called_once_with( | ||
self.mock_open_file, {self.mock_file_1.file_name: { | ||
'file': self.mock_file_1.file_name, | ||
'size': 100, | ||
'latest update': '1970-01-01 03:25:45', | ||
'type of transfer': 'new upload'}, | ||
self.mock_file_2.file_name: { | ||
'file': self.mock_file_2.file_name, | ||
'size': 50, | ||
'latest update': '1970-01-01 03:25:45', | ||
'type of transfer': 'file update'}} | ||
) | ||
|
||
@mock.patch('DittoWebApi.src.utils.system_helper.time.time', return_value=12345) | ||
def test_update_archive_updates_an_archive_file(self, time): | ||
self.mock_file_summary.new_files = [self.mock_file_1] | ||
self.mock_file_summary.updated_files = [self.mock_file_2] | ||
# Arrange | ||
self._file_read_write_helper.read_file_as_json.return_value = { | ||
self.mock_file_2.file_name: { | ||
'file': self.mock_file_2.file_name, | ||
'size': 50, | ||
'latest update': '1970-01-01 03:25:45', | ||
'type of transfer': 'file update'} | ||
} | ||
# Act | ||
self.test_archiver.update_archive("some_file_path", self.mock_file_summary) | ||
# Assert | ||
self._logger.debug.assert_called_with("Archive file updated: some_file_path") | ||
self._file_read_write_helper.write_json_to_file.assert_called_once_with( | ||
self.mock_open_file, { | ||
self.mock_file_2.file_name: { | ||
'file': self.mock_file_2.file_name, | ||
'size': 50, | ||
'latest update': '1970-01-01 03:25:45', | ||
'type of transfer': 'file update'}, | ||
self.mock_file_1.file_name: { | ||
'file': self.mock_file_1.file_name, | ||
'size': 100, | ||
'latest update': '1970-01-01 03:25:45', | ||
'type of transfer': 'new upload'} | ||
}) | ||
|
||
@mock.patch('DittoWebApi.src.utils.system_helper.time.time', return_value=12345) | ||
def test_update_archive_updates_an_archive_file_when_no_new_files(self, time): | ||
self.mock_file_summary.new_files = [] | ||
self.mock_file_summary.updated_files = [self.mock_file_1, self.mock_file_2] | ||
# Arrange | ||
self._file_read_write_helper.read_file_as_json.return_value = { | ||
self.mock_file_2.file_name: { | ||
'file': self.mock_file_2.file_name, | ||
'size': 50, | ||
'latest update': '1970-01-01 03:25:45', | ||
'type of transfer': 'new upload'} | ||
} | ||
# Act | ||
self.test_archiver.update_archive("some_file_path", self.mock_file_summary) | ||
# Assert | ||
self._logger.debug.assert_called_with("Archive file updated: some_file_path") | ||
self._file_read_write_helper.write_json_to_file.assert_called_once_with( | ||
self.mock_open_file, { | ||
self.mock_file_1.file_name: { | ||
'file': self.mock_file_1.file_name, | ||
'size': 100, | ||
'latest update': '1970-01-01 03:25:45', | ||
'type of transfer': 'file update'}, | ||
self.mock_file_2.file_name: { | ||
'file': self.mock_file_2.file_name, | ||
'size': 50, | ||
'latest update': '1970-01-01 03:25:45', | ||
'type of transfer': 'file update'} | ||
|
||
}) |
Oops, something went wrong.