Changeset 37:a9637d8e2ea0 for src/mlx
- Timestamp:
- 03/04/12 08:58:27 (13 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- src/mlx
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mlx/gui/update.py
r36 r37 157 157 self._sudoCondition.notify() 158 158 159 def setTotalSize(self, numToModifyAndNew, totalSize, numToRemove): 159 def setTotalSize(self, numToModifyAndNew, totalSize, numToRemove, 160 numToRemoveLocal): 160 161 """Called when the downloading of the files has started.""" 161 162 self._numToModifyAndNew = numToModifyAndNew … … 164 165 self._downloaded = 0 165 166 self._numToRemove = numToRemove 167 self._numToRemoveLocal = numToRemoveLocal 166 168 self._numRemoved = 0 167 169 168 170 self._totalProgress = self._totalSize + \ 169 (self._numToModifyAndNew + self._numToRemove) * \ 171 (self._numToModifyAndNew + \ 172 self._numToRemove + self._numToRemoveLocal) * \ 170 173 Updater.REMOVE2BYTES 171 self._waitAfterFinish = self._total Progress> 0174 self._waitAfterFinish = self._totalSize > 0 or self._numToRemove > 0 172 175 173 176 def _startDownload(self): … … 233 236 """Called when the update has been done.""" 234 237 gobject.idle_add(self._done) 235 self._restart = self._ totalProgress>0238 self._restart = self._waitAfterFinish 236 239 237 240 def _done(self): -
src/mlx/update.py
r36 r37 11 11 import socket 12 12 import subprocess 13 import hashlib 13 14 14 15 if os.name=="nt": … … 18 19 19 20 manifestName = "MLXMANIFEST" 21 toremoveName = "toremove" 20 22 21 23 #------------------------------------------------------------------------------ … … 43 45 def addFile(self, path, size, sum): 44 46 """Add a file to the manifest.""" 45 self._files[path] = ( size, sum) 46 47 self._files[path] = (size, sum) 48 49 def addFiles(self, baseDirectory, subdirectory): 50 """Add the files in the given directory and subdirectories of it to the 51 manifest.""" 52 directory = baseDirectory 53 for d in subdirectory: directory = os.path.join(directory, d) 54 55 for entry in os.listdir(directory): 56 fullPath = os.path.join(directory, entry) 57 if os.path.isfile(fullPath): 58 size = os.stat(fullPath).st_size 59 sum = hashlib.md5() 60 with open(fullPath, "rb") as f: 61 while True: 62 data = f.read(4096) 63 if data: sum.update(data) 64 else: break 65 self.addFile("/".join(subdirectory + [entry]), size, 66 sum.hexdigest()) 67 elif os.path.isdir(fullPath): 68 self.addFiles(baseDirectory, subdirectory + [entry]) 69 47 70 def readFrom(self, file): 48 71 """Read a manifest from the given file object.""" … … 74 97 75 98 removed = [path for path in self._files if path not in other._files] 76 removed.sort(reverse = True)77 99 78 100 return (modifiedAndNew, removed) … … 102 124 assert False 103 125 104 def setTotalSize(self, numToModifyAndNew, totalSize, numToRemove): 126 def setTotalSize(self, numToModifyAndNew, totalSize, numToRemove, 127 numToRemoveLocal): 105 128 """Called when starting the downloading of the files.""" 106 129 self._send(["setTotalSize", str(numToModifyAndNew), str(totalSize), 107 str(numToRemove) ])130 str(numToRemove), str(numToRemoveLocal)]) 108 131 109 132 def setDownloaded(self, downloaded): … … 190 213 #------------------------------------------------------------------------------ 191 214 215 def getToremoveFiles(directory): 216 """Add the files to remove from the toremove directory.""" 217 toremove = [] 218 toremoveDirectory = os.path.join(directory, toremoveName) 219 if os.path.isdir(toremoveDirectory): 220 for entry in os.listdir(toremoveDirectory): 221 toremove.append(os.path.join(toremoveName, entry)) 222 return toremove 223 224 #------------------------------------------------------------------------------ 225 192 226 def createLocalPath(directory, path): 193 227 """Create a local path from the given manifest path.""" … … 199 233 #------------------------------------------------------------------------------ 200 234 235 def getToremoveDir(toremoveDir, directory): 236 """Get the path of the directory that will contain the files that are to be 237 removed.""" 238 if toremoveDir is None: 239 toremoveDir = os.path.join(directory, toremoveName) 240 try: 241 os.mkdir(toremoveDir) 242 except: 243 pass 244 245 return toremoveDir 246 247 #------------------------------------------------------------------------------ 248 249 def removeFile(toremoveDir, directory, path): 250 """Remove the file at the given path or store it in a temporary directory. 251 252 If the removal of the file fails, it will be stored in a temporary 253 directory. This is useful for files thay may be open and cannot be removed 254 right away.""" 255 try: 256 os.remove(path) 257 except: 258 try: 259 sum = hashlib.md5() 260 sum.update(path) 261 toremoveDir = getToremoveDir(toremoveDir, directory) 262 os.rename(path, os.path.join(toremoveDir, sum.hexdigest())) 263 except Exception, e: 264 print "Cannot remove file " + path + ": " + str(e) 265 266 #------------------------------------------------------------------------------ 267 201 268 def updateFiles(directory, updateURL, listener, 202 manifest, modifiedAndNew, removed ):269 manifest, modifiedAndNew, removed, localRemoved): 203 270 """Update the files according to the given information.""" 204 271 totalSize = 0 … … 206 273 totalSize += size 207 274 208 listener.setTotalSize(len(modifiedAndNew), totalSize, len(removed)) 275 listener.setTotalSize(len(modifiedAndNew), totalSize, 276 len(removed), len(localRemoved)) 209 277 210 278 downloaded = 0 211 279 fin = None 280 toremoveDir = None 281 212 282 try: 213 283 updateURL += "/" + os.name … … 233 303 fin.close() 234 304 fin = None 235 305 236 306 listener.startRenaming() 237 307 count = 0 … … 241 311 downloadedFile = targetFile + "." + sum 242 312 if os.name=="nt" and os.path.isfile(targetFile): 243 try: 244 os.remove(targetFile) 245 except: 246 try: 247 os.remove(targetFile + ".tmp") 248 except: 249 pass 250 os.rename(targetFile, targetFile + ".tmp") 313 removeFile(toremoveDir, directory, targetFile) 251 314 os.rename(downloadedFile, targetFile) 252 315 count += 1 … … 255 318 listener.startRemoving() 256 319 count = 0 320 removed += localRemoved 321 removed.sort(reverse = True) 257 322 for path in removed: 258 os.remove(path) 259 count += 1 260 pathDirectory = os.path.dirname(path) 323 removePath = createLocalPath(directory, path) 324 removeFile(toremoveDir, directory, removePath) 325 326 removeDirectory = os.path.dirname(removePath) 261 327 try: 262 os.r mdir(pathDirectory)328 os.removedirs(removeDirectory) 263 329 except: 264 330 pass 331 332 count += 1 265 333 listener.removed(path, count) 266 334 … … 316 384 elif command=="setTotalSize": 317 385 listener.setTotalSize(int(words[1]), long(words[2]), 318 int(words[3]) )386 int(words[3]), int(words[4])) 319 387 elif command=="setDownloaded": 320 388 listener.setDownloaded(long(words[1])) … … 417 485 418 486 (manifest, updateManifest, modifiedAndNew, removed) = result 419 420 if not modifiedAndNew and not removed: 487 localRemoved = getToremoveFiles(directory) 488 489 if not modifiedAndNew and not removed and not localRemoved: 421 490 listener.done() 422 491 return … … 428 497 else: 429 498 updateFiles(directory, updateURL, listener, updateManifest, 430 modifiedAndNew, removed )499 modifiedAndNew, removed, localRemoved) 431 500 432 501 #------------------------------------------------------------------------------ … … 458 527 459 528 (modifiedAndNew, removed) = manifest.compare(updateManifest) 529 localRemoved = getToremoveFiles(directory) 460 530 461 531 updateFiles(directory, config.updateURL, 462 532 ClientListener(clientSocket), 463 updateManifest, modifiedAndNew, removed) 464 465 #------------------------------------------------------------------------------ 466 467 if __name__ == "__main__": 468 manifest1 = Manifest() 469 manifest1.addFile("file1.exe", 3242, "40398754589435345934") 470 manifest1.addFile("dir/file2.zip", 45645, "347893245873456987") 471 manifest1.addFile("dir/file3.txt", 123, "3432434534534534") 472 473 with open("manifest1", "wt") as f: 474 manifest1.writeInto(f) 475 476 manifest2 = Manifest() 477 manifest2.addFile("file1.exe", 4353, "390734659834756349876") 478 manifest2.addFile("dir/file2.zip", 45645, "347893245873456987") 479 manifest2.addFile("dir/file4.log", 2390, "56546546546546") 480 481 with open("manifest2", "wt") as f: 482 manifest2.writeInto(f) 483 484 manifest1 = Manifest() 485 with open("manifest1", "rt") as f: 486 manifest1.readFrom(f) 487 488 manifest2 = Manifest() 489 with open("manifest2", "rt") as f: 490 manifest2.readFrom(f) 491 492 (modifiedAndNew, removed) = manifest1.compare(manifest2) 493 494 for (path, size, sum) in modifiedAndNew: 495 print "modified or new:", path, size, sum 496 497 for path in removed: 498 print "removed:", path 499 500 #------------------------------------------------------------------------------ 533 updateManifest, modifiedAndNew, removed, localRemoved) 534 535 #------------------------------------------------------------------------------ 536 537 def buildManifest(directory): 538 """Build a manifest from the contents of the given directory, into the 539 given directory.""" 540 541 manifestPath = os.path.join(directory, manifestName) 542 try: 543 os.remove(manifestPath) 544 except: 545 pass 546 547 manifest = Manifest() 548 manifest.addFiles(directory, []) 549 with open(manifestPath, "wt") as f: 550 manifest.writeInto(f) 551 552 #------------------------------------------------------------------------------ 553 554 # if __name__ == "__main__": 555 # manifest1 = Manifest() 556 # manifest1.addFile("file1.exe", 3242, "40398754589435345934") 557 # manifest1.addFile("dir/file2.zip", 45645, "347893245873456987") 558 # manifest1.addFile("dir/file3.txt", 123, "3432434534534534") 559 560 # with open("manifest1", "wt") as f: 561 # manifest1.writeInto(f) 562 563 # manifest2 = Manifest() 564 # manifest2.addFile("file1.exe", 4353, "390734659834756349876") 565 # manifest2.addFile("dir/file2.zip", 45645, "347893245873456987") 566 # manifest2.addFile("dir/file4.log", 2390, "56546546546546") 567 568 # with open("manifest2", "wt") as f: 569 # manifest2.writeInto(f) 570 571 # manifest1 = Manifest() 572 # with open("manifest1", "rt") as f: 573 # manifest1.readFrom(f) 574 575 # manifest2 = Manifest() 576 # with open("manifest2", "rt") as f: 577 # manifest2.readFrom(f) 578 579 # (modifiedAndNew, removed) = manifest1.compare(manifest2) 580 581 # for (path, size, sum) in modifiedAndNew: 582 # print "modified or new:", path, size, sum 583 584 # for path in removed: 585 # print "removed:", path 586 587 #------------------------------------------------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.