comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:71b6185352a5
1 #!usr/bin/env/ python
2 # dspaceuploader.py
3 # Copyright: Marco Fabiani, Centre for Digital Music, Queen Mary University of London
4 # License: XXXXXX
5
6 import argparse, getpass, zipfile,os,sys
7 from sword2 import *
8
9 # Parse arguments
10 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.")
11 parser.add_argument("data", type=str, nargs=1,
12 help="Accepts: METSDSpaceSIP packages, zip files, directories, single files")
13 parser.add_argument("--username", dest="user_name", type=str,nargs=1, help="DSpace username.")
14 parser.add_argument("--title", dest="title", type=str,nargs=1, help="Title (ignored for METS packages).")
15 parser.add_argument("--author", dest="author", type=str,nargs="+", help="Author(s) (ignored for METS packages). Accepts multiple entries in the format \"Surname, Name\"")
16 parser.add_argument("--date", dest="date", type=str,nargs=1, help="Date of creation (string) (ignored for METS packages).")
17 parser.add_argument("--servicedoc", dest="dspaceurl", type=str,nargs=1, help="Url of the SWORDv2 service document (default: c4dm).")
18
19 args = parser.parse_args()
20 data = args.data[0]
21 if args.dspaceurl == None:
22 dspaceurl = "http://c4dm.eecs.qmul.ac.uk/smdmrd-test/swordv2/servicedocument"
23 else:
24 dspaceurl = args.dspaceurl[0]
25
26
27 try:
28 # Connect to SWORD server
29 attempts = 3 # Number of attempts left to connect to server
30 connected = False
31 while attempts>0 and not connected:
32 print "Connecting to SWORD server. Remaining attempts: ", attempts
33 # Try to login, get service document
34 # Get username and password
35 if args.user_name == None:
36 user_name = raw_input("DSpace Username: ")
37 else:
38 user_name = args.user_name[0]
39 print "DSpace Username: ",user_name
40 user_pass = getpass.getpass("DSpace password:")
41 # Connect to the server
42 c = Connection(dspaceurl, user_name=user_name, user_pass=user_pass,keep_history=False)
43 # Get service document
44 try:
45 c.get_service_document()
46 except: # Could be Forbidden if the exception was raised
47 attempts-=1
48 print "Incorrect username and/or password"
49 if c.sd != None:
50 connected = True
51
52 if connected:
53 # List available collections
54 print "------------------------"
55 print "Welcome to the ",c.workspaces[0][0], "repository"
56 print "Available Collections: "
57 numColl = len(c.workspaces[0][1])
58 for ctr in range(numColl):
59 coll = c.workspaces[0][1][ctr]
60 print ctr+1,":",coll.title
61 # Select a collection to deposit into
62 sel = -1
63 while (sel<=0 or sel>numColl):
64 sel = input("Select a Collection to submit your files into: ")
65 collection = c.workspaces[0][1][sel-1]
66 print "Selected Collection: ",collection.title
67
68 # Create a submission: build the zip files
69 temp = True # delete the zip file at the end of the upload
70 if zipfile.is_zipfile(data):
71 zipf = data
72 temp = False
73 elif os.path.isfile(data): # This is a single file
74 dataname = os.path.basename(data)
75 zipf = os.path.splitext(dataname)[0]+".zip"
76 myzip = zipfile.ZipFile(zipf, "w")
77 myzip.write(data)
78 myzip.close()
79 elif os.path.isdir(data): # This is a directory, zip all the files and maintain the structure!
80 dataname = os.path.basename(os.path.normpath(data))
81 zipf = dataname+".zip"
82 myzip = zipfile.ZipFile(zipf, "w")
83 # get the directory structure
84 for root, dirs, files in os.walk(data):
85 for name in files:
86 myzip.write(os.path.join(root,name))
87 myzip.close()
88 else:
89 print "Couldn't find the data."
90 sys.exit()
91
92 #Check if this is a METSDSpaceSIP: see if there is a mets.xml file in the zip
93 myzip = zipfile.ZipFile(zipf)
94 if "mets.xml" in myzip.namelist():
95 packaging = "http://purl.org/net/sword/package/METSDSpaceSIP"
96 type = "METS"
97 else:
98 packaging = "http://purl.org/net/sword/package/SimpleZip"
99 type = "SimpleZip"
100
101 print "------------------------"
102 print "This is a ",type," submission"
103 myzip.close()
104
105 payload = open(zipf, "rb")
106 try:
107 receipt_dep = c.create(col_iri = collection.href,
108 payload = payload,
109 filename = zipf,
110 mimetype = "application/zip",
111 packaging = packaging)
112 print type, " submission successful."
113 if type == "SimpleZip":
114 # If some of the additional arguments for author, title, date etc. have been specified, update the metadata
115 if (args.title != None) or (args.author != None) or (args.date != None):
116 entry = Entry()
117 print "------------------------"
118 print "Updating with additional metadata"
119 if args.title != None:
120 entry.add_fields(dcterms_title = args.title[0])
121 if args.author != None:
122 for creator in args.author:
123 entry.add_fields(dcterms_creator=creator)
124 if args.date != None:
125 entry.add_fields(dcterms_created = args.date[0])
126 try:
127 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
128 print "Additional metadata updated successfully."
129 except:
130 print "Server error"
131 print "------------------------"
132 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\"."
133 except:
134 print "Error! Couldn't submit the file!"
135 if type == "METS": # Just guessing: not sure this is the problem...
136 print "To submit a METS package, the collection MUST have a workflow!"
137 payload.close()
138
139 else: # Failed to connect to SWORDv2 Server
140 print "Couldn't connect to the server."
141 if attempts == 0:
142 print "Invalid credentials entered 3 times."
143 if temp:
144 os.remove(zipf)
145
146 except KeyboardInterrupt:
147 print "------------------------"
148 print "\nSubmission aborted by user."
149