def write(self, key, value): key = safetype(key) value_json = JSONEncoder(ensure_ascii=False, sort_keys=True).encode(value) value_bytes = egest(value_json) value_hash = egest(checksum(value_bytes)) key_path = self.root.join(key) with ensure_file(key_path, 'wb') as f: f.write(value_bytes) f.write(b"\n") f.write(value_hash) f.write(b"\n")
def test_egest(): assert isinstance(egest('abc'), rawtype) assert egest('abc') == b'abc' assert isinstance(egest(b'abc'), rawtype) assert egest(b'abc') == b'abc' assert isinstance(egest(u'abc'), rawtype) assert egest(u'abc') == b'abc' with pytest.raises(TypeError): assert egest(5)
def test_farmfs_ignore(vol, capsys): farm_ignore = Path('.farmignore', vol) with farm_ignore.open("wb") as fifd: fifd.write(egest(u"a\n\u03B1\n")) for name in [u'a', u'b', u'\u03B1', u'\u03B2']: p = Path(name, vol) with p.open("w") as fd: fd.write("hi") r = farmfs_ui(['status'], vol) captured = capsys.readouterr() assert r == 0 assert captured.out == u".farmignore\nb\n\u03B2\n" assert captured.err == ""
def readraw(self, key): key = safetype(key) try: with self.root.join(key).open('rb') as f: obj_bytes = f.readline().strip() obj_bytes_checksum = checksum(obj_bytes).encode('utf-8') key_checksum = f.readline().strip() if obj_bytes_checksum != key_checksum: raise ValueError("Checksum mismatch for key %s. Expected %s, calculated %s" % (key, key_checksum, obj_bytes_checksum)) obj_str = egest(obj_bytes) return obj_str except IOError as e: if e.errno == NoSuchFile or e.errno == IsDirectory: return None else: raise e
def test_farmfs_ignore(tmp_path, capsys): root = Path(str(tmp_path)) r1 = farmfs_ui(['mkfs'], root) captured = capsys.readouterr() assert r1 == 0 farm_ignore = Path('.farmignore', root) with farm_ignore.open("wb") as fifd: fifd.write(egest(u"a\n\u03B1\n")) for name in [u'a', u'b', u'\u03B1', u'\u03B2']: p = Path(name, root) with p.open("w") as fd: fd.write("hi") r2 = farmfs_ui(['status'], root) captured = capsys.readouterr() assert r2 == 0 assert captured.out == u".farmignore\nb\n\u03B2\n" assert captured.err == ""
def test_egest_ingest(): tst_str = u'abc' b = egest(tst_str) s = ingest(b) assert tst_str == s
def test_ingest_egest(): byte_str = b'I\xc3\xb1t\xc3\xabrn\xc3\xa2ti\xc3\xb4n\xc3\xa0li\xc5\xbe\xc3\xa6ti\xc3\xb8n\n' s = ingest(byte_str) b = egest(s) assert byte_str == b