view sworduploader.py @ 0:71b6185352a5

Initial commit
author Marco Fabiani <marco.fabiani@eecs.qmul.ac.uk>
date Tue, 27 Mar 2012 17:55:00 +0100
parents
children 2ceacc9bb602
line wrap: on
line source
#!usr/bin/env/ python
# dspaceuploader.py
# Copyright: Marco Fabiani, Centre for Digital Music, Queen Mary University of London
# License: XXXXXX

import argparse, getpass, zipfile,os,sys
from sword2 import *

# Parse arguments
parser = argparse.ArgumentParser(description="Bulk upload to DSpace using SWORDv2.",epilog="If the submission is created successfully, it will remain open to be completed with the necessary metadata and licenses, using the DSpace web interface. The submission can be found in the \"My Account -> Submissions\" section of the user's area.")
parser.add_argument("data", type=str, nargs=1,
                   help="Accepts: METSDSpaceSIP packages, zip files, directories, single files")
parser.add_argument("--username", dest="user_name", type=str,nargs=1, help="DSpace username.")
parser.add_argument("--title", dest="title", type=str,nargs=1, help="Title (ignored for METS packages).")
parser.add_argument("--author", dest="author", type=str,nargs="+", help="Author(s) (ignored for METS packages). Accepts multiple entries in the format \"Surname, Name\"")
parser.add_argument("--date", dest="date", type=str,nargs=1, help="Date of creation (string) (ignored for METS packages).")
parser.add_argument("--servicedoc", dest="dspaceurl", type=str,nargs=1, help="Url of the SWORDv2 service document (default: c4dm).")

args = parser.parse_args()
data = args.data[0]
if args.dspaceurl == None:
	dspaceurl = "http://c4dm.eecs.qmul.ac.uk/smdmrd-test/swordv2/servicedocument"
else:
	dspaceurl = args.dspaceurl[0]


try:
	# Connect to SWORD server
	attempts = 3 #  Number of attempts left to connect to server
	connected = False
	while attempts>0 and not connected:
		print "Connecting to SWORD server. Remaining attempts: ", attempts
		# Try to login, get service document
		# Get username and password
		if args.user_name == None:
			user_name = raw_input("DSpace Username: ")
		else:
			user_name = args.user_name[0]
			print "DSpace Username: ",user_name
		user_pass = getpass.getpass("DSpace password:")
		# Connect to the server
		c = Connection(dspaceurl, user_name=user_name, user_pass=user_pass,keep_history=False)
		# Get service document
		try:
			c.get_service_document()
		except: # Could be Forbidden if the exception was raised
			attempts-=1
			print "Incorrect username and/or password"
		if c.sd != None:
			connected = True
			
	if connected:
		# List available collections
		print "------------------------"
		print "Welcome to the ",c.workspaces[0][0], "repository"
		print "Available Collections: "
		numColl = len(c.workspaces[0][1])
		for ctr in range(numColl):
			coll = c.workspaces[0][1][ctr]
			print ctr+1,":",coll.title
		# Select a collection to deposit into
		sel = -1
		while (sel<=0 or sel>numColl):
			sel = input("Select a Collection to submit your files into: ")
		collection = c.workspaces[0][1][sel-1]
		print "Selected Collection: ",collection.title
		
		# Create a submission: build the zip files
		temp = True # delete the zip file at the end of the upload	
		if zipfile.is_zipfile(data):
			zipf = data
			temp = False
		elif os.path.isfile(data): # This is a single file
			dataname = os.path.basename(data)
			zipf = os.path.splitext(dataname)[0]+".zip"
			myzip = zipfile.ZipFile(zipf, "w")
			myzip.write(data)
			myzip.close()
		elif os.path.isdir(data): # This is a directory, zip all the files and maintain the structure!
			dataname = os.path.basename(os.path.normpath(data))
			zipf = dataname+".zip"
			myzip = zipfile.ZipFile(zipf, "w")
			# get the directory structure
			for root, dirs, files in os.walk(data):
				for name in files:
					myzip.write(os.path.join(root,name))
			myzip.close()
		else:
			print "Couldn't find the data."
			sys.exit()
		
		#Check if this is a METSDSpaceSIP: see if there is a mets.xml file in the zip
		myzip = zipfile.ZipFile(zipf)
		if "mets.xml" in myzip.namelist():
			packaging = "http://purl.org/net/sword/package/METSDSpaceSIP"
			type = "METS"
		else:
			packaging = "http://purl.org/net/sword/package/SimpleZip"
			type = "SimpleZip"
		
		print "------------------------"
		print "This is a ",type," submission"
		myzip.close()

		payload = open(zipf, "rb")
		try:
			receipt_dep = c.create(col_iri = collection.href,
			payload = payload,
			filename = zipf,
			mimetype = "application/zip",
			packaging = packaging)
			print type, " submission successful."
			if type == "SimpleZip":
				# If some of the additional arguments for author, title, date etc. have been specified, update the metadata
				if (args.title != None) or (args.author != None) or (args.date != None):
					entry = Entry()	
					print "------------------------"
					print "Updating with additional metadata"
					if  args.title != None:
						entry.add_fields(dcterms_title = args.title[0])
					if args.author != None:
						for creator in args.author:
							entry.add_fields(dcterms_creator=creator)
					if args.date != None:
						entry.add_fields(dcterms_created = args.date[0])
					try:
						receipt_update = c.update(dr = receipt_dep , metadata_entry = entry, in_progress = True) # in_progress is True: we don't want to close the submission
						print "Additional metadata updated successfully."
					except:
						print "Server error"
			print "------------------------"
			print "You will find this submission in the \"Submissions\" list in your DSpace account. To complete/edit it with metadata and licenses, click on the title and then on \"Resume\"."			
		except:
			print "Error! Couldn't submit the file!"
			if type == "METS": # Just guessing: not sure this is the problem...
				print "To submit a METS package, the collection MUST have a workflow!"
		payload.close()
		
	else: # Failed to connect to SWORDv2 Server
		print "Couldn't connect to the server."
		if attempts == 0:
			print "Invalid credentials entered 3 times."
	if temp:
		os.remove(zipf)
	
except KeyboardInterrupt:
	print "------------------------"
	print "\nSubmission aborted by user."