Mercurial > hg > sworduploader
comparison sworduploader.py @ 15:e24aea2d14a4
- v0.6: use server.cfg, maintain dir structure with dir upload
author | Marco Fabiani <marco.fabiani@eecs.qmul.ac.uk> |
---|---|
date | Tue, 29 May 2012 11:55:26 +0100 |
parents | 273fc80d9623 |
children | 8b9e7f2f80e2 |
comparison
equal
deleted
inserted
replaced
14:273fc80d9623 | 15:e24aea2d14a4 |
---|---|
1 #!usr/bin/env/ python | 1 #!usr/bin/env/ python |
2 | 2 |
3 """ | 3 """ |
4 | 4 |
5 SWORD2 DSpace bulk uploader - v0.5 | 5 SWORD2 DSpace bulk uploader - v0.6 |
6 | 6 |
7 A python script to submit large numbers of files to a SWORD2-compatible repository, specifically DSpace 1.8x. | 7 A python script to submit large numbers of files to a SWORD2-compatible repository, specifically DSpace 1.8x. |
8 Built on the SWORD2 python client library: https://bitbucket.org/beno/python-sword2/overview with modifications. | 8 Built on the SWORD2 python client library: https://github.com/swordapp/python-client-sword2. |
9 | 9 |
10 Dependencies: | 10 Dependencies: |
11 | 11 |
12 - python 2.X | 12 - python 2.X |
13 | 13 |
14 - sword2 library: https://github.com/swordapp/python-client-sword2 | 14 - sword2 library: https://github.com/swordapp/python-client-sword2 |
15 | |
16 ----------------------------------- | |
17 Updates log: | |
18 | |
19 v0.6: - now uploading a directory will also maintain the path structure | |
20 - introduced a file where to specify the server (server.cfg) | |
21 v0.5: changed the default server to C4DM live server | |
15 | 22 |
16 ----------------------------------- | 23 ----------------------------------- |
17 Centre for Digital Music, Queen Mary, University of London | 24 Centre for Digital Music, Queen Mary, University of London |
18 Copyright (c) 2012 Marco Fabiani | 25 Copyright (c) 2012 Marco Fabiani |
19 | 26 |
51 parser.add_argument("--username", dest="user_name", type=str,nargs=1, help="DSpace username.") | 58 parser.add_argument("--username", dest="user_name", type=str,nargs=1, help="DSpace username.") |
52 parser.add_argument("--title", dest="title", type=str,nargs=1, help="Title (ignored for METS packages).") | 59 parser.add_argument("--title", dest="title", type=str,nargs=1, help="Title (ignored for METS packages).") |
53 parser.add_argument("--author", dest="author", type=str,nargs="+", help="Author(s) (ignored for METS packages). Accepts multiple entries in the format \"Surname, Name\"") | 60 parser.add_argument("--author", dest="author", type=str,nargs="+", help="Author(s) (ignored for METS packages). Accepts multiple entries in the format \"Surname, Name\"") |
54 parser.add_argument("--date", dest="date", type=str,nargs=1, help="Date of creation (string) (ignored for METS packages).") | 61 parser.add_argument("--date", dest="date", type=str,nargs=1, help="Date of creation (string) (ignored for METS packages).") |
55 parser.add_argument("--zip", action="store_true",dest="zip",default=False, help="If \"data\" is a directory, compress it and post it as a single file. The zip file will be saved along with the individual files.") | 62 parser.add_argument("--zip", action="store_true",dest="zip",default=False, help="If \"data\" is a directory, compress it and post it as a single file. The zip file will be saved along with the individual files.") |
56 parser.add_argument("--servicedoc", dest="sd", type=str,nargs=1, help="Url of the SWORDv2 service document (default: http://c4dm.eecs.qmul.ac.uk/smdmrd-test/swordv2/servicedocument") | 63 parser.add_argument("--servicedoc", dest="sd", type=str,nargs=1, help="Url of the SWORDv2 service document (default: use server.cfg if available, otherwise http://c4dm.eecs.qmul.ac.uk/rdr/swordv2/servicedocument") |
57 | 64 |
58 args = parser.parse_args() | 65 args = parser.parse_args() |
59 data = args.data[0] | 66 data = args.data[0] |
60 if args.zip: | 67 if args.zip: |
61 storezip = True | 68 storezip = True |
62 else: | 69 else: |
63 storezip = False | 70 storezip = False |
64 | 71 |
65 if args.sd == None: | 72 if args.sd == None: |
66 sd = "http://c4dm.eecs.qmul.ac.uk/rdr/swordv2/servicedocument" | 73 try: |
74 f = open("server.cfg", "r") | |
75 sd = f.readline() | |
76 print "server.cfg: ", sd | |
77 except: | |
78 sd = "http://c4dm.eecs.qmul.ac.uk/rdr/swordv2/servicedocument" | |
67 else: | 79 else: |
68 sd = args.sd[0] | 80 sd = args.sd[0] |
69 | 81 |
70 | 82 |
71 try: | 83 try: |
190 if entry is None: | 202 if entry is None: |
191 entry = Entry(dcterms_title=(os.path.basename(data))) | 203 entry = Entry(dcterms_title=(os.path.basename(data))) |
192 creation_receipt = c.create(col_iri = collection.href, metadata_entry = entry, in_progress=True) | 204 creation_receipt = c.create(col_iri = collection.href, metadata_entry = entry, in_progress=True) |
193 | 205 |
194 # Add the files | 206 # Add the files |
207 # Get the longest common path in order to send the correct filename to keep the structure | |
208 common = os.path.commonprefix(fileslist) | |
195 for f in fileslist: | 209 for f in fileslist: |
196 print "Uploading file ",os.path.basename(f) | 210 filename = os.path.relpath(f,common) |
211 print "Uploading file ", filename | |
197 payload = open(f,"rb") | 212 payload = open(f,"rb") |
198 deposit_receipt = c.add_file_to_resource(edit_media_iri = creation_receipt.edit_media, | 213 deposit_receipt = c.add_file_to_resource(edit_media_iri = creation_receipt.edit_media, |
199 payload = payload, | 214 payload = payload, |
200 filename = os.path.basename(f), | 215 filename = filename, |
201 mimetype = 'application/zip', | 216 mimetype = 'application/zip', |
202 packaging = 'http://purl.org/net/sword/package/Binary') | 217 packaging = 'http://purl.org/net/sword/package/Binary') |
203 payload.close() | 218 payload.close() |
204 except HTTPResponseError: | 219 except HTTPResponseError: |
205 print "Bad request" | 220 print "Bad request" |