changeset 8:ff51b8204ad4

If using zip files, the zip is stored in DSpace as well Small changes (in_progress explicitly)
author Marco Fabiani <marco.fabiani@eecs.qmul.ac.uk>
date Tue, 03 Apr 2012 16:22:18 +0100
parents 9d9d5a1b1d3c
children 394b4d094767
files sworduploader.py
diffstat 1 files changed, 31 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/sworduploader.py	Mon Apr 02 16:01:06 2012 +0100
+++ b/sworduploader.py	Tue Apr 03 16:22:18 2012 +0100
@@ -27,7 +27,7 @@
 parser.add_argument("data", type=str, nargs=1,
                    help="Accepts: METSDSpaceSIP packages, zip files, directories, single files. NOTE: METSDSpaceSIP packages are only accepted by Collections with a workflow!")
 parser.add_argument("--username", dest="user_name", type=str,nargs=1, help="DSpace username.")
-parser.add_argument("--zip", action="store_true",dest="zip",default=False, help="If \"data\" is a directory, send it as a single zip archive to preserve its structure.")
+parser.add_argument("--zip", action="store_true",dest="zip",default=False, help="If \"data\" is a directory, send it as a single zip archive to preserve its structure. The zip file will be saved along with the individual files.")
 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).")
@@ -35,6 +35,8 @@
 
 args = parser.parse_args()
 data = args.data[0]
+storezip = True
+
 if args.dspaceurl == None:
 	dspaceurl = "http://c4dm.eecs.qmul.ac.uk/smdmrd-test/swordv2/servicedocument"
 else:
@@ -100,8 +102,9 @@
 				print "Creating a zip archive for submission..."
 				for root, dirs, files in os.walk(data):
 					for name in files:
-						myzip.write(os.path.join(root,name),
-							os.path.relpath(os.path.join(root,name),data))
+						if not name.startswith('.'): # Do not upload hidden files, OSX/linux
+							myzip.write(os.path.join(root,name),
+								os.path.relpath(os.path.join(root,name),data))
 				fileslist.append(zipf)
 				myzip.close()
 				packaging = "http://purl.org/net/sword/package/SimpleZip"
@@ -118,6 +121,7 @@
 			if "mets.xml" in myzip.namelist(): #This is a METS package
 				packaging = "http://purl.org/net/sword/package/METSDSpaceSIP"
 				type = "METS"
+				in_progress = False
 			else: #THis is a simple zip file
 				packaging = "http://purl.org/net/sword/package/SimpleZip"
 				type = "SimpleZip"
@@ -145,14 +149,14 @@
 		else:
 			entry = None
 		# Select what to do
-		if (type is "single file") or (type is "multiple files"):
+		if (type is "single file") or (type is "multiple files"): # Use the single file upload procedure
 			try:
 				# Create the metadata entry with ATOM
 				print "------------------------"
 				print "Creating the item..."
 				if entry is None:
 					entry = Entry(dcterms_title=(os.path.basename(data)))
-				creation_receipt = c.create(col_iri = collection.href, metadata_entry = entry)
+				creation_receipt = c.create(col_iri = collection.href, metadata_entry = entry, in_progress=True)
 				
 				# Add the files
 				for f in fileslist:
@@ -167,14 +171,19 @@
 			except HTTPResponseError:
 				print "Bad request"
 		else:
-			# PUT the data
+			# Send the zip file and let the ingester do its job
 			payload = open(fileslist[0], "rb")
+			if type == "SimpleZip":
+				in_progress = True
+			else:
+				in_progress = False
 			try:
 				deposit_receipt = c.create(col_iri = collection.href,
 					payload = payload,
 					filename = fileslist[0],
 					mimetype = "application/zip",
-					packaging = packaging)
+					packaging = packaging,
+					in_progress = in_progress)
 				print type, " submission successful."
 			except:
 				print "Error! Couldn't submit the file!"						
@@ -182,15 +191,28 @@
 					print "To submit a METS package, the collection MUST have a workflow!"
 			payload.close()
 			
-			# If some of the additional arguments for author, title, date etc. have been specified, update the metadata
+			# If some of the additional arguments for author, title, date etc. have been specified, update the metadata (only SimpleZip)
 			if type == "SimpleZip":
 				if entry is None:
 					entry = Entry(dcterms_title=(os.path.basename(fileslist[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
+					update_receipt = c.update(dr = deposit_receipt , metadata_entry = entry, in_progress = True) # in_progress is True: we don't want to close the submission
 					print "Metadata update successfull."
 				except:
 					print "Server error"
+				# If we want to store the zip file along with the individual files (Only SimpleZip)
+				if storezip:
+					try:
+						payload = open(fileslist[0],"rb")
+						zipdeposit_receipt = c.add_file_to_resource(edit_media_iri = deposit_receipt.edit_media,
+							payload = payload,
+							filename = os.path.basename(fileslist[0]).replace(" ","_"),
+							mimetype = 'application/zip',
+							packaging = 'http://purl.org/net/sword/package/Binary')
+						payload.close()
+						print "Zip file successfully added to the bitstreams."
+					except:
+						print "Server error: could not add the zip file to the resources"
 			if temp:
 				os.remove(fileslist[0])