diff options
Diffstat (limited to 'test/util.py')
-rw-r--r-- | test/util.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/test/util.py b/test/util.py new file mode 100644 index 0000000..76ac49e --- /dev/null +++ b/test/util.py @@ -0,0 +1,58 @@ + +import hashlib +import numpy +import os + + +def check_same_md5sum(fa, fb): + return (fa.md5sum() == fb.md5sum()) + + +class File(): + def __init__(self, path, size = 0, content = "random", perm = 0o664): + if not content in ["zero", "random"]: + raise ValueError("invalid type: {}".format(content)) + self.path = path + self.size = size + self.content = content + self.perm = perm + + def __str__(self): + return self.path + + def make(self): + d = os.path.dirname(self.path) + if d: + os.makedirs(d, exist_ok = True) + if self.content == "zero": + self.make_content_zero() + elif self.content == "random": + self.make_content_random() + else: + raise ValueError("invalud content type: {}".format(self.content)) + os.chmod(self.path, self.perm) + return self + + def make_content_zero(self): + with open(self.path, "wb") as f: + f.seek(self.size, 0) + + def make_content_random(self): + with open(self.path, "wb") as f: + f.write(numpy.random.bytes(self.size)) + + def cleanup(self): + os.remove(self.path) + tmp = os.path.dirname(self.path) + while tmp and not tmp in [".", "/"]: + if len(os.listdir(tmp)) == 0: + os.rmdir(tmp) + tmp = os.path.dirname(tmp) + + def md5sum(self): + m = hashlib.md5() + with open(self.path, 'rb') as f: + for chunk in iter(lambda: f.read(4096 * m.block_size), b''): + m.update(chunk) + return m.hexdigest() + |