Changeset 37:a9637d8e2ea0 for src/mlx


Ignore:
Timestamp:
03/04/12 08:58:27 (13 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

The MLXMANIFEST is generated when creating the distribution and files to be removed are really removed.

Location:
src/mlx
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/gui/update.py

    r36 r37  
    157157            self._sudoCondition.notify()
    158158       
    159     def setTotalSize(self, numToModifyAndNew, totalSize, numToRemove):
     159    def setTotalSize(self, numToModifyAndNew, totalSize, numToRemove,
     160                     numToRemoveLocal):
    160161        """Called when the downloading of the files has started."""
    161162        self._numToModifyAndNew = numToModifyAndNew
     
    164165        self._downloaded = 0
    165166        self._numToRemove = numToRemove
     167        self._numToRemoveLocal = numToRemoveLocal
    166168        self._numRemoved = 0
    167169
    168170        self._totalProgress = self._totalSize + \
    169                               (self._numToModifyAndNew + self._numToRemove) * \
     171                              (self._numToModifyAndNew + \
     172                               self._numToRemove + self._numToRemoveLocal) * \
    170173                              Updater.REMOVE2BYTES
    171         self._waitAfterFinish = self._totalProgress > 0
     174        self._waitAfterFinish = self._totalSize > 0 or self._numToRemove > 0
    172175
    173176    def _startDownload(self):
     
    233236        """Called when the update has been done."""
    234237        gobject.idle_add(self._done)
    235         self._restart = self._totalProgress>0
     238        self._restart = self._waitAfterFinish
    236239
    237240    def _done(self):
  • src/mlx/update.py

    r36 r37  
    1111import socket
    1212import subprocess
     13import hashlib
    1314
    1415if os.name=="nt":
     
    1819
    1920manifestName = "MLXMANIFEST"
     21toremoveName = "toremove"
    2022
    2123#------------------------------------------------------------------------------
     
    4345    def addFile(self, path, size, sum):
    4446        """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               
    4770    def readFrom(self, file):
    4871        """Read a manifest from the given file object."""
     
    7497
    7598        removed = [path for path in self._files if path not in other._files]
    76         removed.sort(reverse = True)
    7799       
    78100        return (modifiedAndNew, removed)
     
    102124        assert False
    103125
    104     def setTotalSize(self, numToModifyAndNew, totalSize, numToRemove):
     126    def setTotalSize(self, numToModifyAndNew, totalSize, numToRemove,
     127                     numToRemoveLocal):
    105128        """Called when starting the downloading of the files."""
    106129        self._send(["setTotalSize", str(numToModifyAndNew), str(totalSize),
    107                     str(numToRemove)])
     130                    str(numToRemove), str(numToRemoveLocal)])
    108131
    109132    def setDownloaded(self, downloaded):
     
    190213#------------------------------------------------------------------------------
    191214
     215def 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
    192226def createLocalPath(directory, path):
    193227    """Create a local path from the given manifest path."""
     
    199233#------------------------------------------------------------------------------
    200234
     235def 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
     249def 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
    201268def updateFiles(directory, updateURL, listener,
    202                 manifest, modifiedAndNew, removed):
     269                manifest, modifiedAndNew, removed, localRemoved):
    203270    """Update the files according to the given information."""
    204271    totalSize = 0
     
    206273        totalSize += size
    207274
    208     listener.setTotalSize(len(modifiedAndNew), totalSize, len(removed))
     275    listener.setTotalSize(len(modifiedAndNew), totalSize,
     276                          len(removed), len(localRemoved))
    209277
    210278    downloaded = 0
    211279    fin = None
     280    toremoveDir = None
     281
    212282    try:       
    213283        updateURL += "/" + os.name
     
    233303                fin.close()
    234304                fin = None
    235 
     305               
    236306        listener.startRenaming()
    237307        count = 0
     
    241311            downloadedFile = targetFile + "." + sum
    242312            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)
    251314            os.rename(downloadedFile, targetFile)
    252315            count += 1
     
    255318        listener.startRemoving()
    256319        count = 0
     320        removed += localRemoved
     321        removed.sort(reverse = True)
    257322        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)
    261327            try:
    262                 os.rmdir(pathDirectory)
     328                os.removedirs(removeDirectory)
    263329            except:
    264330                pass
     331
     332            count += 1
    265333            listener.removed(path, count)
    266334
     
    316384            elif command=="setTotalSize":
    317385                listener.setTotalSize(int(words[1]), long(words[2]),
    318                                       int(words[3]))
     386                                      int(words[3]), int(words[4]))
    319387            elif command=="setDownloaded":
    320388                listener.setDownloaded(long(words[1]))
     
    417485
    418486    (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:
    421490        listener.done()
    422491        return
     
    428497    else:
    429498        updateFiles(directory, updateURL, listener, updateManifest,
    430                     modifiedAndNew, removed)
     499                    modifiedAndNew, removed, localRemoved)
    431500
    432501#------------------------------------------------------------------------------
     
    458527
    459528    (modifiedAndNew, removed) = manifest.compare(updateManifest)
     529    localRemoved = getToremoveFiles(directory)
    460530
    461531    updateFiles(directory, config.updateURL,
    462532                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
     537def 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.