comparison 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
comparison
equal deleted inserted replaced
2:c72d6b5d58bc 3:c1918aa337c4
1 #!usr/bin/env/ python 1 #!usr/bin/env/ python
2 2
3 """ SWORD2 DSpace bulk uploader - v0.1 3 """ SWORD2 DSpace bulk uploader - v0.2
4 4
5 A python script to submit large numbers of files to a SWORD2-compatible repository, specifically DSpace 1.8x. 5 A python script to submit large numbers of files to a SWORD2-compatible repository, specifically DSpace 1.8x.
6 Built on the SWORD2 python client library: https://bitbucket.org/beno/python-sword2/overview 6 Built on the SWORD2 python client library: https://bitbucket.org/beno/python-sword2/overview
7 7
8 Dependencies: 8 Dependencies:
52 user_name = args.user_name[0] 52 user_name = args.user_name[0]
53 print "DSpace Username: ",user_name 53 print "DSpace Username: ",user_name
54 user_pass = getpass.getpass("DSpace password:") 54 user_pass = getpass.getpass("DSpace password:")
55 # Connect to the server 55 # Connect to the server
56 c = Connection(dspaceurl, user_name=user_name, user_pass=user_pass,keep_history=False) 56 c = Connection(dspaceurl, user_name=user_name, user_pass=user_pass,keep_history=False)
57
57 # Get service document 58 # Get service document
58 try: 59 try:
59 c.get_service_document() 60 c.get_service_document()
60 except: # Could be Forbidden if the exception was raised 61 except: # Server error
62 print "Server unreachable!"
63 break
64 if c.sd != None:
65 connected = True
66 else:
61 attempts-=1 67 attempts-=1
62 print "Incorrect username and/or password" 68 print "Incorrect username and/or password"
63 if c.sd != None: 69
64 connected = True
65 70
66 if connected: 71 if connected:
67 # List available collections 72 # List available collections
68 print "------------------------" 73 print "------------------------"
69 print "Welcome to the ",c.workspaces[0][0], "repository" 74 print "Welcome to the ",c.workspaces[0][0], "repository"
86 temp = False 91 temp = False
87 elif os.path.isfile(data): # This is a single file 92 elif os.path.isfile(data): # This is a single file
88 dataname = os.path.basename(data) 93 dataname = os.path.basename(data)
89 zipf = os.path.splitext(dataname)[0]+".zip" 94 zipf = os.path.splitext(dataname)[0]+".zip"
90 myzip = zipfile.ZipFile(zipf, "w") 95 myzip = zipfile.ZipFile(zipf, "w")
91 myzip.write(data) 96 myzip.write(data,os.path.basename(data))
92 myzip.close() 97 myzip.close()
93 elif os.path.isdir(data): # This is a directory, zip all the files and maintain the structure! 98 elif os.path.isdir(data): # This is a directory, zip all the files and maintain the structure, but start from the base only...
94 dataname = os.path.basename(os.path.normpath(data)) 99 dataname = os.path.basename(os.path.normpath(data))
95 zipf = dataname+".zip" 100 zipf = dataname+".zip"
96 myzip = zipfile.ZipFile(zipf, "w") 101 myzip = zipfile.ZipFile(zipf, "w")
97 # get the directory structure 102 # get the directory structure
103 print "Creating a zip archive for submission..."
98 for root, dirs, files in os.walk(data): 104 for root, dirs, files in os.walk(data):
99 for name in files: 105 for name in files:
100 myzip.write(os.path.join(root,name)) 106 myzip.write(os.path.join(root,name),
107 os.path.relpath(os.path.join(root,name),data))
101 myzip.close() 108 myzip.close()
102 else: 109 else:
103 print "Couldn't find the data." 110 print "Couldn't find the data."
104 sys.exit() 111 sys.exit()
105 112
112 packaging = "http://purl.org/net/sword/package/SimpleZip" 119 packaging = "http://purl.org/net/sword/package/SimpleZip"
113 type = "SimpleZip" 120 type = "SimpleZip"
114 121
115 print "------------------------" 122 print "------------------------"
116 print "This is a ",type," submission" 123 print "This is a ",type," submission"
124 print "Uploading files..."
117 myzip.close() 125 myzip.close()
118 126
119 payload = open(zipf, "rb") 127 payload = open(zipf, "rb")
120 try: 128 try:
121 receipt_dep = c.create(col_iri = collection.href, 129 receipt_dep = c.create(col_iri = collection.href,
147 except: 155 except:
148 print "Error! Couldn't submit the file!" 156 print "Error! Couldn't submit the file!"
149 if type == "METS": # Just guessing: not sure this is the problem... 157 if type == "METS": # Just guessing: not sure this is the problem...
150 print "To submit a METS package, the collection MUST have a workflow!" 158 print "To submit a METS package, the collection MUST have a workflow!"
151 payload.close() 159 payload.close()
160 if temp:
161 os.remove(zipf)
152 162
153 else: # Failed to connect to SWORDv2 Server 163 else: # Failed to connect to SWORDv2 Server
154 print "Couldn't connect to the server." 164 print "Couldn't connect to the server."
155 if attempts == 0: 165 if attempts == 0:
156 print "Invalid credentials entered 3 times." 166 print "Invalid credentials entered 3 times."
157 if temp:
158 os.remove(zipf)
159 167
160 except KeyboardInterrupt: 168 except KeyboardInterrupt:
161 print "------------------------" 169 print "------------------------"
162 print "\nSubmission aborted by user." 170 print "\nSubmission aborted by user."
163