Changeset c632b1c
- Timestamp:
- Sep 9, 2012, 6:43:39 AM (14 years ago)
- Branches:
- master, space-access, stable, stage
- Children:
- 71e71e3
- Parents:
- 7e45324
- git-author:
- Alex Dehnert <adehnert@…> (09/09/12 06:34:45)
- git-committer:
- Alex Dehnert <adehnert@…> (09/09/12 06:43:39)
- Location:
- asadb/groups
- Files:
-
- 2 edited
-
gather_constitutions.py (modified) (1 diff)
-
models.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
asadb/groups/gather_constitutions.py
r5f07d50 rc632b1c 13 13 import subprocess 14 14 15 import django.contrib.auth.models 15 16 import reversion 16 17 -
asadb/groups/models.py
r7e45324 rc632b1c 10 10 import re 11 11 import shutil 12 import urlparse 12 13 import urllib 14 import urllib2 13 15 14 16 import settings … … 136 138 def record_failure(self, msg): 137 139 now = datetime.datetime.now() 138 self.failure_date = now 140 if not self.failure_date: 141 self.failure_date = now 139 142 self.status_msg = msg 140 143 self.failure_reason = self.status_msg … … 156 159 old_success = (self.failure_date is None) 157 160 if url: 158 url_opener = urllib.FancyURLopener()159 now = datetime.datetime.now()160 161 161 # Fetch the file 162 error_msg = None 162 163 try: 163 tmp_path, headers = url_opener.retrieve(url) 164 new_mimetype = None 165 if url.startswith('/afs/') or url.startswith('/mit/'): 166 new_fp = open(url, 'rb') 167 else: 168 new_fp = urllib2.urlopen(url) 169 if new_fp.info().getheader('Content-Type'): 170 new_mimetype = new_fp.info().gettype() 171 172 new_data = new_fp.read() 173 new_fp.close() 174 except urllib2.HTTPError, e: 175 error_msg = "HTTPError: %s %s" % (e.code, e.msg) 176 except urllib2.URLError, e: 177 error_msg = "URLError: %s" % (e.reason) 164 178 except IOError: 165 self.record_failure("retrieval failed (IOError)") 166 success = False 167 return (success, self.status_msg, old_success, ) 168 169 # At this point, failures are our fault's, not the group's. 179 error_msg = "IOError" 180 except ValueError, e: 181 if e.args[0].startswith('unknown url type'): 182 error_msg = "unknown url type" 183 else: 184 raise 185 if error_msg: 186 self.record_failure(error_msg) 187 return (False, self.status_msg, old_success, ) 188 189 # At this point, failures are our fault, not the group's. 170 190 # We can let any errors bubble all the way up, rather than 171 191 # trying to catch and neatly record them … … 173 193 174 194 # Find a destination, and how to put it there 175 save_filename = self.compute_filename(tmp_path, headers, ) 176 dest_path = self.path_from_filename(self.dest_file) 177 if tmp_path == url: 178 mover = shutil.copyfile 179 else: 180 mover = shutil.move 195 old_path = self.path_from_filename(self.dest_file) 196 new_filename = self.compute_filename(url, new_mimetype, ) 181 197 182 198 # Process the update 183 if save_filename != self.dest_file: 184 if self.dest_file: os.remove(dest_path) 185 mover(tmp_path, self.path_from_filename(save_filename)) 186 self.dest_file = save_filename 199 if new_filename != self.dest_file: # new filename 200 if self.dest_file: 201 if os.path.exists(old_path): 202 os.remove(old_path) 203 else: 204 print "Warning: %s doesn't exist, but is referenced by dest_file" % (old_path, ) 205 self.dest_file = new_filename 206 new_path = self.path_from_filename(new_filename) 207 with open(new_path, 'wb') as fp: 208 fp.write(new_data) 187 209 self.record_success("new path", updated=True) 188 else: 189 if filecmp.cmp(tmp_path, dest_path, shallow=False, ): 210 else: # old filename 211 with open(old_path, 'rb') as old_fp: 212 old_data = old_fp.read() 213 if old_data == new_data: # unchanged 190 214 self.record_success("no change", updated=False) 191 else: 192 # changed193 mover(tmp_path, dest_path)215 else: # changed 216 with open(old_path, 'wb') as fp: 217 fp.write(new_data) 194 218 self.record_success("updated in place", updated=True) 195 219 … … 200 224 return (success, self.status_msg, old_success, ) 201 225 202 def compute_filename(self, tmp_path, headers,):226 def compute_filename(self, url, mimetype): 203 227 slug = self.group.slug() 204 228 known_ext = set([ … … 211 235 '.txt' 212 236 ]) 213 basename, fileext = os.path.splitext(tmp_path) 237 238 # This probably breaks on Windows. But that's probably true of 239 # everything... 240 path = urlparse.urlparse(url).path 241 basename, fileext = os.path.splitext(path) 242 214 243 if fileext: 215 244 ext = fileext 216 245 else: 217 if headers.getheader('Content-Type'):218 extensions = mimetypes.guess_all_extensions( headers.gettype())246 if mimetype: 247 extensions = mimetypes.guess_all_extensions(mimetype) 219 248 for extension in extensions: 220 249 if extension in known_ext:
Note: See TracChangeset
for help on using the changeset viewer.