comparison sworduploader.py @ 9:394b4d094767

Give better names to zip file Remove square brakets from file names
author Marco Fabiani <marco.fabiani@eecs.qmul.ac.uk>
date Wed, 04 Apr 2012 17:59:06 +0100
parents ff51b8204ad4
children af2a645f63a2
comparison
equal deleted inserted replaced
8:ff51b8204ad4 9:394b4d094767
94 temp = False # Delete temp files 94 temp = False # Delete temp files
95 # If folder 95 # If folder
96 if os.path.isdir(data): 96 if os.path.isdir(data):
97 if args.zip: # If zip option, zip all the files and maintain the structure, but start from the base only... 97 if args.zip: # If zip option, zip all the files and maintain the structure, but start from the base only...
98 dataname = os.path.basename(os.path.normpath(data)) 98 dataname = os.path.basename(os.path.normpath(data))
99 zipf = dataname+".zip" 99 if args.title != None:
100 zipf = args.title[0].replace(" ","_")+".zip"
101 else:
102 zipf = dataname.replace(" ","_")+".zip"
100 myzip = zipfile.ZipFile(zipf, "w") 103 myzip = zipfile.ZipFile(zipf, "w")
101 # get the directory structure 104 # get the directory structure
102 print "Creating a zip archive for submission..." 105 print "Creating a zip archive for submission..."
103 for root, dirs, files in os.walk(data): 106 for root, dirs, files in os.walk(data):
104 for name in files: 107 for name in files:
105 if not name.startswith('.'): # Do not upload hidden files, OSX/linux 108 if not name.startswith('.'): # Do not upload hidden files, OSX/linux
106 myzip.write(os.path.join(root,name), 109 myzip.write(os.path.join(root,name),
107 os.path.relpath(os.path.join(root,name),data)) 110 os.path.relpath(os.path.join(root,name),data).replace(" ","_").replace("[","(").replace("]",")")) # Remove spaces and square brakets
108 fileslist.append(zipf) 111 fileslist.append(zipf)
109 myzip.close() 112 myzip.close()
110 packaging = "http://purl.org/net/sword/package/SimpleZip" 113 packaging = "http://purl.org/net/sword/package/SimpleZip"
111 type = "SimpleZip" 114 type = "SimpleZip"
112 temp = True 115 temp = True
113 else: #create a list of files to upload 116 else: #create a list of files to upload
114 for root, dirs, files in os.walk(data): 117 for root, dirs, files in os.walk(data):
115 for name in files: 118 for name in files:
116 fileslist.append(os.path.join(root,name)) 119 if not name.startswith('.'):
120 fileslist.append(os.path.join(root,name))
117 type = "multiple files" 121 type = "multiple files"
118 elif zipfile.is_zipfile(data): #This is a zip file 122 elif zipfile.is_zipfile(data): #This is a zip file
119 fileslist.append(data) 123 fileslist.append(data)
120 myzip = zipfile.ZipFile(data) 124 myzip = zipfile.ZipFile(data)
121 if "mets.xml" in myzip.namelist(): #This is a METS package 125 if "mets.xml" in myzip.namelist(): #This is a METS package
173 else: 177 else:
174 # Send the zip file and let the ingester do its job 178 # Send the zip file and let the ingester do its job
175 payload = open(fileslist[0], "rb") 179 payload = open(fileslist[0], "rb")
176 if type == "SimpleZip": 180 if type == "SimpleZip":
177 in_progress = True 181 in_progress = True
182 # FIXME: we don't want to write silly things in dc.description!
178 else: 183 else:
179 in_progress = False 184 in_progress = False
180 try: 185 try:
181 deposit_receipt = c.create(col_iri = collection.href, 186 deposit_receipt = c.create(col_iri = collection.href,
182 payload = payload, 187 payload = payload,