Skip to content

Commit

Permalink
demo: add class and documentation for demo (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
remileduc committed Sep 15, 2017
1 parent dcce3e3 commit 90aa482
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
92 changes: 92 additions & 0 deletions examples/create_archive.py
@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2017 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Script to create an archive object from 0 into Invenio.
This script can be copy / paste in the invenio shell: run ``invenio shell``
to access it.
"""

import uuid
from os import makedirs

from invenio_db import db
from invenio_files_rest.models import Bucket, Location
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
from invenio_records_files.api import Record, RecordsBuckets
from invenio_sipstore.api import RecordSIP
from invenio_sipstore.models import SIPMetadataType
from six import BytesIO

from invenio_archivematica.tasks import oais_start_transfer

# init
try:
makedirs('/modules/archive/')
except:
pass
loc = Location(
name='archive', # this should go in SIPSTORE_ARCHIVER_LOCATION_NAME
uri='/modules/archive/',
default=True
)
db.session.add(loc)
db.session.commit()

# first we create a metadata type with a schema used by the following record
mtype = SIPMetadataType(title='Invenio JSON test',
name='invenio-json-test',
format='json',
schema='https://zenodo.org/schemas/deposits/records/record-v1.0.0.json')
db.session.add(mtype)
db.session.commit()

# create record, it needs to use the same schema as the one in the metadata type
recid = uuid.uuid4()
pid = PersistentIdentifier.create('recid', '1501', object_type='rec',
object_uuid=recid,
status=PIDStatus.REGISTERED)
record = Record.create({'$schema': 'https://zenodo.org/schemas/deposits/records/record-v1.0.0.json',
'_deposit': {'status': 'draft'},
'title': 'demo'},
recid)
record.commit()
db.session.commit()

# put a file in the record
stream = BytesIO(b'head crab\n')
b = Bucket.create()
RecordsBuckets.create(bucket=b, record=record.model)
db.session.commit()
record.files['crab.txt'] = stream
record.files.dumps()
record.commit()
db.session.commit()

# create the archive
sip = RecordSIP.create(pid, record, True, user_id=1, agent={'demo': 'archivematica'})
db.session.commit()

# archive it
oais_start_transfer(sip.sip.id, 'test-demo-archivematica')
37 changes: 37 additions & 0 deletions invenio_archivematica/factories.py
Expand Up @@ -121,6 +121,43 @@ def transfer_rsync(uuid, config):
return ret


def transfer_demo(uuid, config):
"""Transfer the files contained in the sip to the destination.
Very similar to the rsync transfer. However, because of time, I use the
VERY UNSECURE sshpass package for rsync authentication.
DO NOT USE IN PROD!!!
:param str uuid: the id of the sip containing files to transfer
:param dict config: here config must be a dict with the following keys:
- user - the SSH user
- password_file - a path where the password is stored
- remote - the URL or IP of the remote
- remote_path - where to store files on the remote
- args - the args for rsync
"""
# we retrieve the archive and the SIP associated
sip = SIP.get_sip(uuid)
ark = Archive.get_from_sip(uuid)

# we export it to the temp folder
archiver = BaseArchiver(sip)
archiver.write_all_files()

# we rsync it to the remote
src_path = archiver._get_fullpath('')
dest_path = join(config['remote_path'], ark.accession_id)
dest_path = '{}:{}'.format(config['remote'], dest_path)
ssh_command = 'sshpass -f {filename} ssh -l {user}'.format(
filename=config['password_file'],
user=config['user'])
return call(['rsync',
config['args'],
'--rsh={}'.format(ssh_command),
src_path,
dest_path])


def is_archivable_default(sip):
"""Tell if the given sip should be archived or not.
Expand Down

0 comments on commit 90aa482

Please sign in to comment.