Mercurial > hg > sworduploader
comparison sworduploader.py @ 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 |
comparison
equal
deleted
inserted
replaced
7:9d9d5a1b1d3c | 8:ff51b8204ad4 |
---|---|
25 # Parse arguments | 25 # Parse arguments |
26 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.") | 26 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.") |
27 parser.add_argument("data", type=str, nargs=1, | 27 parser.add_argument("data", type=str, nargs=1, |
28 help="Accepts: METSDSpaceSIP packages, zip files, directories, single files. NOTE: METSDSpaceSIP packages are only accepted by Collections with a workflow!") | 28 help="Accepts: METSDSpaceSIP packages, zip files, directories, single files. NOTE: METSDSpaceSIP packages are only accepted by Collections with a workflow!") |
29 parser.add_argument("--username", dest="user_name", type=str,nargs=1, help="DSpace username.") | 29 parser.add_argument("--username", dest="user_name", type=str,nargs=1, help="DSpace username.") |
30 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.") | 30 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.") |
31 parser.add_argument("--title", dest="title", type=str,nargs=1, help="Title (ignored for METS packages).") | 31 parser.add_argument("--title", dest="title", type=str,nargs=1, help="Title (ignored for METS packages).") |
32 parser.add_argument("--author", dest="author", type=str,nargs="+", help="Author(s) (ignored for METS packages). Accepts multiple entries in the format \"Surname, Name\"") | 32 parser.add_argument("--author", dest="author", type=str,nargs="+", help="Author(s) (ignored for METS packages). Accepts multiple entries in the format \"Surname, Name\"") |
33 parser.add_argument("--date", dest="date", type=str,nargs=1, help="Date of creation (string) (ignored for METS packages).") | 33 parser.add_argument("--date", dest="date", type=str,nargs=1, help="Date of creation (string) (ignored for METS packages).") |
34 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") | 34 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") |
35 | 35 |
36 args = parser.parse_args() | 36 args = parser.parse_args() |
37 data = args.data[0] | 37 data = args.data[0] |
38 storezip = True | |
39 | |
38 if args.dspaceurl == None: | 40 if args.dspaceurl == None: |
39 dspaceurl = "http://c4dm.eecs.qmul.ac.uk/smdmrd-test/swordv2/servicedocument" | 41 dspaceurl = "http://c4dm.eecs.qmul.ac.uk/smdmrd-test/swordv2/servicedocument" |
40 else: | 42 else: |
41 dspaceurl = args.dspaceurl[0] | 43 dspaceurl = args.dspaceurl[0] |
42 | 44 |
98 myzip = zipfile.ZipFile(zipf, "w") | 100 myzip = zipfile.ZipFile(zipf, "w") |
99 # get the directory structure | 101 # get the directory structure |
100 print "Creating a zip archive for submission..." | 102 print "Creating a zip archive for submission..." |
101 for root, dirs, files in os.walk(data): | 103 for root, dirs, files in os.walk(data): |
102 for name in files: | 104 for name in files: |
103 myzip.write(os.path.join(root,name), | 105 if not name.startswith('.'): # Do not upload hidden files, OSX/linux |
104 os.path.relpath(os.path.join(root,name),data)) | 106 myzip.write(os.path.join(root,name), |
107 os.path.relpath(os.path.join(root,name),data)) | |
105 fileslist.append(zipf) | 108 fileslist.append(zipf) |
106 myzip.close() | 109 myzip.close() |
107 packaging = "http://purl.org/net/sword/package/SimpleZip" | 110 packaging = "http://purl.org/net/sword/package/SimpleZip" |
108 type = "SimpleZip" | 111 type = "SimpleZip" |
109 temp = True | 112 temp = True |
116 fileslist.append(data) | 119 fileslist.append(data) |
117 myzip = zipfile.ZipFile(data) | 120 myzip = zipfile.ZipFile(data) |
118 if "mets.xml" in myzip.namelist(): #This is a METS package | 121 if "mets.xml" in myzip.namelist(): #This is a METS package |
119 packaging = "http://purl.org/net/sword/package/METSDSpaceSIP" | 122 packaging = "http://purl.org/net/sword/package/METSDSpaceSIP" |
120 type = "METS" | 123 type = "METS" |
124 in_progress = False | |
121 else: #THis is a simple zip file | 125 else: #THis is a simple zip file |
122 packaging = "http://purl.org/net/sword/package/SimpleZip" | 126 packaging = "http://purl.org/net/sword/package/SimpleZip" |
123 type = "SimpleZip" | 127 type = "SimpleZip" |
124 myzip.close() | 128 myzip.close() |
125 elif os.path.isfile(data): # This is a single file | 129 elif os.path.isfile(data): # This is a single file |
143 if args.date != None: | 147 if args.date != None: |
144 entry.add_fields(dcterms_created = args.date[0]) | 148 entry.add_fields(dcterms_created = args.date[0]) |
145 else: | 149 else: |
146 entry = None | 150 entry = None |
147 # Select what to do | 151 # Select what to do |
148 if (type is "single file") or (type is "multiple files"): | 152 if (type is "single file") or (type is "multiple files"): # Use the single file upload procedure |
149 try: | 153 try: |
150 # Create the metadata entry with ATOM | 154 # Create the metadata entry with ATOM |
151 print "------------------------" | 155 print "------------------------" |
152 print "Creating the item..." | 156 print "Creating the item..." |
153 if entry is None: | 157 if entry is None: |
154 entry = Entry(dcterms_title=(os.path.basename(data))) | 158 entry = Entry(dcterms_title=(os.path.basename(data))) |
155 creation_receipt = c.create(col_iri = collection.href, metadata_entry = entry) | 159 creation_receipt = c.create(col_iri = collection.href, metadata_entry = entry, in_progress=True) |
156 | 160 |
157 # Add the files | 161 # Add the files |
158 for f in fileslist: | 162 for f in fileslist: |
159 print "Uploading file ",os.path.basename(f) | 163 print "Uploading file ",os.path.basename(f) |
160 payload = open(f,"rb") | 164 payload = open(f,"rb") |
165 packaging = 'http://purl.org/net/sword/package/Binary') | 169 packaging = 'http://purl.org/net/sword/package/Binary') |
166 payload.close() | 170 payload.close() |
167 except HTTPResponseError: | 171 except HTTPResponseError: |
168 print "Bad request" | 172 print "Bad request" |
169 else: | 173 else: |
170 # PUT the data | 174 # Send the zip file and let the ingester do its job |
171 payload = open(fileslist[0], "rb") | 175 payload = open(fileslist[0], "rb") |
176 if type == "SimpleZip": | |
177 in_progress = True | |
178 else: | |
179 in_progress = False | |
172 try: | 180 try: |
173 deposit_receipt = c.create(col_iri = collection.href, | 181 deposit_receipt = c.create(col_iri = collection.href, |
174 payload = payload, | 182 payload = payload, |
175 filename = fileslist[0], | 183 filename = fileslist[0], |
176 mimetype = "application/zip", | 184 mimetype = "application/zip", |
177 packaging = packaging) | 185 packaging = packaging, |
186 in_progress = in_progress) | |
178 print type, " submission successful." | 187 print type, " submission successful." |
179 except: | 188 except: |
180 print "Error! Couldn't submit the file!" | 189 print "Error! Couldn't submit the file!" |
181 if type == "METS": # Just guessing: not sure this is the problem... | 190 if type == "METS": # Just guessing: not sure this is the problem... |
182 print "To submit a METS package, the collection MUST have a workflow!" | 191 print "To submit a METS package, the collection MUST have a workflow!" |
183 payload.close() | 192 payload.close() |
184 | 193 |
185 # If some of the additional arguments for author, title, date etc. have been specified, update the metadata | 194 # If some of the additional arguments for author, title, date etc. have been specified, update the metadata (only SimpleZip) |
186 if type == "SimpleZip": | 195 if type == "SimpleZip": |
187 if entry is None: | 196 if entry is None: |
188 entry = Entry(dcterms_title=(os.path.basename(fileslist[0]))) | 197 entry = Entry(dcterms_title=(os.path.basename(fileslist[0]))) |
189 try: | 198 try: |
190 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 | 199 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 |
191 print "Metadata update successfull." | 200 print "Metadata update successfull." |
192 except: | 201 except: |
193 print "Server error" | 202 print "Server error" |
203 # If we want to store the zip file along with the individual files (Only SimpleZip) | |
204 if storezip: | |
205 try: | |
206 payload = open(fileslist[0],"rb") | |
207 zipdeposit_receipt = c.add_file_to_resource(edit_media_iri = deposit_receipt.edit_media, | |
208 payload = payload, | |
209 filename = os.path.basename(fileslist[0]).replace(" ","_"), | |
210 mimetype = 'application/zip', | |
211 packaging = 'http://purl.org/net/sword/package/Binary') | |
212 payload.close() | |
213 print "Zip file successfully added to the bitstreams." | |
214 except: | |
215 print "Server error: could not add the zip file to the resources" | |
194 if temp: | 216 if temp: |
195 os.remove(fileslist[0]) | 217 os.remove(fileslist[0]) |
196 | 218 |
197 print "------------------------" | 219 print "------------------------" |
198 print "You will find the 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\"." | 220 print "You will find the 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\"." |