Ejemplo n.º 1
0
    def save_bagit_metadata(self, filesinfo=None, overwrite=False):
        """Generate and save the BagIt metadata information as SIPMetadata."""
        if not filesinfo:
            filesinfo = self.get_all_files()
        bagit_schema = current_jsonschemas.path_to_url(
            current_app.config['SIPSTORE_DEFAULT_BAGIT_JSONSCHEMA'])
        bagit_metadata = {
            'files': filesinfo,
            '$schema': bagit_schema,
        }
        # Validate the BagIt metadata with JSONSchema
        schema_path = current_jsonschemas.url_to_path(bagit_schema)
        schema = current_jsonschemas.get_schema(schema_path)
        validate(bagit_metadata, schema)

        # Create the BagIt schema object
        bagit_type = self._get_bagit_metadata_type()
        content = json.dumps(bagit_metadata)
        with db.session.begin_nested():
            obj = SIPMetadata.query.get((self.sip.id, bagit_type.id))
            if obj:
                if overwrite:
                    obj.content = content
                else:
                    raise Exception(
                        'Attempting to save BagIt metadata on top of existing'
                        ' one. Use the "overwrite" flag to force.')
            else:
                obj = SIPMetadata(sip_id=self.sip.id,
                                  type_id=bagit_type.id,
                                  content=content)
                db.session.add(obj)
Ejemplo n.º 2
0
    def attach_metadata(self, type, metadata):
        """Add metadata to the SIP.

        :param str type: the type of metadata (a valid
            :py:class:`invenio_sipstore.models.SIPMetadataType` name)
        :param str metadata: the metadata to attach.
        :returns: the created SIPMetadata
        :rtype: :py:class:`invenio_sipstore.models.SIPMetadata`
        """
        mtype = SIPMetadataType.get_from_name(type)
        sm = SIPMetadata(sip_id=self.id, type=mtype, content=metadata)
        db.session.add(sm)
        return sm
Ejemplo n.º 3
0
def test_sip_metadata_model(db):
    """Test the SIPMetadata model."""
    sip1 = SIP.create()
    mtype = SIPMetadataType(title='JSON Test', name='json-test',
                            format='json', schema='url')
    db.session.add(mtype)
    metadata1 = '{"title": "great book"}'
    sipmetadata = SIPMetadata(sip_id=sip1.id, content=metadata1,
                              type=mtype)
    db.session.add(sipmetadata)
    db.session.commit()
    assert SIP.query.count() == 1
    assert SIPMetadataType.query.count() == 1
    assert SIPMetadata.query.count() == 1
    sipmetadata = SIPMetadata.query.one()
    assert sipmetadata.content == metadata1
    assert sipmetadata.type.format == 'json'
    assert sipmetadata.sip.id == sip1.id
Ejemplo n.º 4
0
def test_transfer_rsync(app, db, location):
    """Test factories.transfer_rsync function."""
    # config
    app.config['SIPSTORE_ARCHIVER_DIRECTORY_BUILDER'] = \
        'helpers:archive_directory_builder'
    app.config['SIPSTORE_ARCHIVER_METADATA_TYPES'] = ['test']
    # SIP
    sip = SIP.create()
    # SIPMetadataType
    mtype = SIPMetadataType(title='Test', name='test', format='json')
    db.session.add(mtype)
    # SIPMetadata
    mcontent = {'title': 'title', 'author': 'me'}
    meth = SIPMetadata(sip=sip, type=mtype, content=json.dumps(mcontent))
    db.session.add(meth)
    # SIPFile
    f = FileInstance.create()
    fcontent = b'weighted companion cube\n'
    f.set_contents(BytesIO(fcontent), default_location=location.uri)
    sfile = SIPFile(sip=sip, file=f, filepath='portal.txt')
    db.session.add(sfile)
    db.session.commit()

    # EXPORT
    folder = path.join(location.uri, 'lulz')
    params = {
        'server': '',
        'user': '',
        'destination': folder,
        'args': '-az'
    }
    factories.transfer_rsync(sip.id, params)

    # TEST
    assert not path.exists(path.join(location.uri, 'test'))
    assert path.isdir(folder)
    assert path.isdir(path.join(folder, 'files'))
    assert path.isfile(path.join(folder, 'files', 'portal.txt'))
    assert path.isdir(path.join(folder, 'metadata'))
    assert path.isfile(path.join(folder, 'metadata', 'test.json'))
    with open(path.join(folder, 'files', 'portal.txt'), 'rb') as fp:
        assert fp.read() == fcontent
    with open(path.join(folder, 'metadata', 'test.json'), 'r') as fp:
        assert json.loads(fp.read()) == mcontent