Mercurial > hg > sworduploader
view sworduploader.py @ 3:c1918aa337c4
v0.2 small bug fixes
| author | Marco Fabiani <marco.fabiani@eecs.qmul.ac.uk> |
|---|---|
| date | Wed, 28 Mar 2012 17:48:59 +0100 |
| parents | c72d6b5d58bc |
| children | 96d62e78ac9f |
line wrap: on
line source
#!usr/bin/env/ python """ SWORD2 DSpace bulk uploader - v0.2 A python script to submit large numbers of files to a SWORD2-compatible repository, specifically DSpace 1.8x. Built on the SWORD2 python client library: https://bitbucket.org/beno/python-sword2/overview Dependencies: - python 2.X - sword2 library: https://bitbucket.org/beno/python-sword2/src ----------------------------------- Copyright 2012 Marco Fabiani Copyright 2012 Queen Mary, University of London ----------------------------------- """ 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. 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("--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: http://c4dm.eecs.qmul.ac.uk/smdmrd-test/swordv2/servicedocument") 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: # Server error print "Server unreachable!" break if c.sd != None: connected = True else: attempts-=1 print "Incorrect username and/or password" 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,os.path.basename(data)) myzip.close() elif os.path.isdir(data): # This is a directory, zip all the files and maintain the structure, but start from the base only... dataname = os.path.basename(os.path.normpath(data)) zipf = dataname+".zip" myzip = zipfile.ZipFile(zipf, "w") # get the directory structure 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)) 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" print "Uploading files..." 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() if temp: os.remove(zipf) else: # Failed to connect to SWORDv2 Server print "Couldn't connect to the server." if attempts == 0: print "Invalid credentials entered 3 times." except KeyboardInterrupt: print "------------------------" print "\nSubmission aborted by user."
