def test_write_filelike(self): src = os.path.join(self.workdir, b'f1') open(src, 'w').write(b'content') dst = os.path.join(self.workdir, b'f2') write_filelike(open(src, 'rb'), dst) assert open(dst, 'rb').read() == b'content' write_filelike(b'different', dst) assert open(dst, 'rb').read() == b'different'
def test_write_filelike(self, tmpdir): src = tmpdir / "f1" src.write('content') dst = str(tmpdir / "f2") write_filelike(open(str(src), 'r'), dst) assert open(dst, 'r').read() == 'content' write_filelike(b'different', dst) assert open(dst, 'r').read() == 'different'
def store_representation(self, src_path, repr_path, repr_key=''): """Store a representation for a source under a representation key. `repr_key` can be a string or some file-like object already opened for reading. Sources are only stored really if they do not exist already. A source is considered to be already stored, if both, the contents of the file given in `src_path` and the contents of an already stored file are equal. Representations and their respective files are created if they do not exist already or *overwritten* otherwise. A representation is considered to exist already, if a representation with the same `repr_key` as passed in is already stored. Returns a bucket key. """ src_num = self.get_stored_source_num(src_path) if src_num is None: # create new source src_num = self.get_current_source_num() + 1 shutil.copy2( src_path, os.path.join(self.srcdir, 'source_%s' % src_num)) self.set_current_source_num(src_num) os.makedirs(os.path.join(self.keysdir, str(src_num))) repr_num = self.get_stored_repr_num(src_num, repr_key) if repr_num is None: # store new key repr_num = self.get_current_repr_num(src_num) + 1 self.set_current_repr_num(src_num, repr_num) key_path = os.path.join( self.keysdir, str(src_num), '%s.key' % repr_num) write_filelike(repr_key, key_path) # store/update representation repr_dir = os.path.join( self.resultdir, str(src_num), str(repr_num)) if os.path.exists(repr_dir): shutil.rmtree(repr_dir) # remove any old representation os.makedirs(repr_dir) shutil.copy2(repr_path, repr_dir) return '%s_%s' % (src_num, repr_num)
def store_representation(self, src_path, repr_path, repr_key=''): """Store a representation for a source under a representation key. `repr_key` can be a string or some file-like object already opened for reading. Sources are only stored really if they do not exist already. A source is considered to be already stored, if both, the contents of the file given in `src_path` and the contents of an already stored file are equal. Representations and their respective files are created if they do not exist already or *overwritten* otherwise. A representation is considered to exist already, if a representation with the same `repr_key` as passed in is already stored. Returns a bucket key. """ src_num = self.get_stored_source_num(src_path) if src_num is None: # create new source src_num = self.get_current_source_num() + 1 shutil.copy2(src_path, os.path.join(self.srcdir, 'source_%s' % src_num)) self.set_current_source_num(src_num) os.makedirs(os.path.join(self.keysdir, str(src_num))) repr_num = self.get_stored_repr_num(src_num, repr_key) if repr_num is None: # store new key repr_num = self.get_current_repr_num(src_num) + 1 self.set_current_repr_num(src_num, repr_num) key_path = os.path.join(self.keysdir, str(src_num), '%s.key' % repr_num) write_filelike(repr_key, key_path) # store/update representation repr_dir = os.path.join(self.resultdir, str(src_num), str(repr_num)) if os.path.exists(repr_dir): shutil.rmtree(repr_dir) # remove any old representation os.makedirs(repr_dir) shutil.copy2(repr_path, repr_dir) return '%s_%s' % (src_num, repr_num)