Source code for egaia.egaia_bag

import os
import shutil
import json

import bagit

import egaia_config
import egaia_root
import egaia_tag
import egaia_docx
import utils


term_description = egaia_config.getConfig('terms', 'DCTERMS.description')
term_title = egaia_config.getConfig('terms', 'DCTERMS.title')
term_identifier = egaia_config.getConfig('terms', 'DCTERMS.identifier')
term_type = egaia_config.getConfig('terms', 'DCTERMS.type')


[docs]def newBag(description='None provided', title='None provided'): """Check if a bag already exists in the current path, and if not, create a new one.""" root = egaia_root.get_root() if root: print "Bag already exists at %s!" % root return uuid = egaia_tag.mkuuid() baginfo = { 'Source-Organization': egaia_config.getConfig('archive', 'organization'), 'Organization-Address': egaia_config.getConfig('archive', 'archive_url'), 'Contact-Name': egaia_config.getConfig('archive', 'user'), 'Contact-Email': egaia_config.getConfig('archive', 'email'), 'External-Description': description, 'External-Identifier': uuid, 'Title': title } docinfo = { term_description: description, term_identifier: uuid, term_title: title, term_type: 'Collection' } print "Creating new bag at %s." % os.getcwd() bag = bagit.make_bag(os.getcwd(), baginfo) bag.save() # Now add to the docx file updateDocx(docinfo)
[docs]def updateDocx(docinfo): """Update the collection docx file""" language = egaia_config.getConfig('archive', 'language') docx_file = os.path.join(egaia_root.get_root(), 'metadata-%s.docx' % language) if os.path.exists(docx_file): print "File %s already exists." % docx_file print "Delete it first to rebuild from bag-info.txt." return egaia_docx.writeDocx(docx_file, write=json.dumps(docinfo), force=True)
[docs]def updateBag(): """Update bag manifests""" root = egaia_root.get_root() bag = bagit.Bag(root) # FIXME: This should NOT recalculate everything. We should ONLY update # the hashes for modified files. # https://github.com/ahankinson/pybagit/blob/master/pybagit/multichecksum.py bag.save(manifests=True)
[docs]def validateBag(): """Validate a bag""" root = egaia_root.get_root() bag = bagit.Bag(root) try: bag.validate(fast=True) except bagit.BagValidationError as e: print e return False return True
[docs]def loadBag(): """Load bag-info.txt and return a dictionary of its contents.""" root = egaia_root.get_root() if not root: exit('No root found here') bag = bagit.Bag(root) return bag.info
[docs]def updateFindingAid(): """Rebuild the docx finding aid based on bag-info.txt.""" try: docinfo = loadBag() except: print "Could not load info from bag-info.txt!" return info = { term_description: docinfo['External-Description'], term_identifier: docinfo['External-Identifier'], term_title: docinfo['Title'], term_type: 'Collection' } updateDocx(info) return
[docs]def init(description=None, title=None, new=True): """Wrap the newBag function in the bagit utility, and create an empty csv file for user metadata entry.""" while not title: title = utils.rlinput('Collection title: ', prefill='') while not description: description = utils.rlinput('Collection description: ', prefill='') # create a new bag newBag(description=description, title=title) return
def _cli(args): """egaia bag Create or update the BagIt information for a new or existing collection. Usage: egaia bag --help egaia bag --init [ --description=DESCRIPTION ] [ --title=TITLE ] egaia bag --validate egaia bag --update-finding-aid egaia bag --update """ # check if a bag exists if not egaia_root.get_root(): init(description=args['--description'], title=args['--title']) elif args['--validate']: print validateBag() elif args['--update-finding-aid']: updateFindingAid() else: updateBag()