Skip to content

Commit

Permalink
Merge branch 'master' of ssh://vcs.r3c.de:63022/till-software/labinform
Browse files Browse the repository at this point in the history
  • Loading branch information
tillbiskup committed Jun 20, 2019
2 parents cf924b0 + 401639a commit be48378
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
16 changes: 14 additions & 2 deletions labinform/datasafe/datasafe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""

import os
import hashlib
import shutil


class Error(Exception):
Expand Down Expand Up @@ -253,7 +255,6 @@ def generate(self, experiment="", sample_id=""):
dir_path = os.path.join(self.path, path_for_loi)
if not self.has_dir(dir_path):
self.add_directory(dir_path)
#print(dir_path)
loi_complete = loi_basic + path_for_loi
return loi_complete

Expand All @@ -271,7 +272,10 @@ def push(self, data="", loi=""):
unique identifier providing a directory path
"""
pass
target_path = self.loi_to_path(loi)
print(target_path)
shutil.copy(data, target_path)


def pull(self, loi=""):
"""Retrieve data from the datasafe.
Expand Down Expand Up @@ -312,6 +316,14 @@ def index(self, loi=""):
"""
return dict()

def make_checksum_for_path(self, path=""):
md5 = hashlib.md5()
with open(path, "rb") as f:
for line in f.readlines():
md5.update(line)
checksum = md5.hexdigest()
return checksum

def checksum(self, loi=""):
"""Create a cryptographic hash (MD5) for a file in the datasafe.
Expand Down
57 changes: 57 additions & 0 deletions tests/datasafe/test_datasafe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_has_push_method(self):
self.assertTrue(hasattr(self.datasafe, 'push'))
self.assertTrue(callable(self.datasafe.push))

@unittest.skip
def test_call_push_with_parameters(self):
self.datasafe.push("", "42.1001/ds/cwepr/sa42/01/data/raw")

Expand Down Expand Up @@ -174,6 +175,19 @@ def test_has_dir_returns_bool(self):
hasdir = self.datasafe.has_dir("")
self.assertEqual(bool, type(hasdir))

def test_has_make_checksum_for_path_method(self):
self.assertTrue(hasattr(self.datasafe, 'make_checksum_for_path'))
self.assertTrue(callable(self.datasafe.make_checksum_for_path))

@unittest.skip
def test_call_make_checksum_for_path_with_parameters(self):
self.datasafe.make_checksum_for_path("")

@unittest.skip
def test_make_checksum_for_path_returns_str(self):
checksum = self.datasafe.make_checksum_for_path("")
self.assertEqual(str, type(checksum))


class TestEmptyDir(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -335,3 +349,46 @@ def test_find_highest(self):
highest = self.datasafe.find_highest(self.target_directory[:-1])
self.assertEqual(1, highest)


class TestChecksumPath(unittest.TestCase):
def setUp(self):
self.datasafe = datasafe.Datasafe()
top_level_directory = os.path.dirname(os.path.dirname(os.path.dirname(
os.path.dirname(__file__))))
self.target_directory = top_level_directory + "/datasafe-test/cwepr/sa571/1"
self.target_file = self.target_directory + "/data/raw/Manifest.yaml"

def test_correct_checksum_for_yaml(self):
md5 = self.datasafe.make_checksum_for_path(self.target_file)
self.assertEqual("48aa739357f70bd7694fcf0ebc3a2e24", md5)


class TestChecksum(unittest.TestCase):
pass


class TestPush(unittest.TestCase):
def setUp(self):
self.datasafe = datasafe.Datasafe()
top_level_directory = os.path.dirname(os.path.dirname(os.path.dirname(
os.path.dirname(__file__))))
self.common_directory = top_level_directory + "/datasafe-test"
self.target_directory = top_level_directory + "/datasafe-test/ds"
print(self.target_directory)
if not os.path.exists(self.target_directory):
os.makedirs(self.target_directory)
self.datasafe.set_path(self.target_directory)
self.target_file = self.common_directory + "/cwepr/sa571/1/data/raw/Manifest.yaml"

def tearDown(self):
shutil.rmtree(self.target_directory)

def test_push(self):
loi = self.datasafe.generate(experiment="cwepr", sample_id="sa571")
self.assertEqual(True, self.datasafe.has_dir(self.datasafe.path + "/cwepr/sa571/1/data/raw"))
self.datasafe.push(self.target_file, loi)
final_path = self.datasafe.path + "/cwepr/sa571/1/data/raw/Manifest.yaml"
self.assertEqual(True, os.path.isfile(final_path))



0 comments on commit be48378

Please sign in to comment.