Esempio n. 1
0
def create_delta(data):
    oBundle = data[0]
    tBundle = data[1]
    oElement = data[0].updateElement
    tElement = data[1].updateElement

    print "Diffing %s->%s" % (oElement.targetVersion, tElement.targetVersion)
    oldmap = oElement.get_filemap()
    targetmap = tElement.get_filemap()

    print "Unpacking old version"
    oBundle.unpack()

    newUpdate = Update()
    newUpdate.targetVersion = tElement.targetVersion
    newUpdate.platform = tElement.platform
    newUpdate.version = 4

    zipname = os.path.splitext(os.path.basename(
        tBundle.zipfilePath))[0].replace("-full", "")
    newUpdateFileName = "%s-delta-from-%s.zip" % (zipname,
                                                  oElement.targetVersion)

    zf = zipfile.ZipFile(os.path.join(output_path, newUpdateFileName), "w")

    diffdir = tempfile.mkdtemp()

    filestodiff = []
    filestoadd = []

    # first iterate all the files and
    # find the ones that needs to be
    # diffed, the ones that needs to be installd
    # and the ones that just needs to be documented
    for tpath in targetmap.keys():
        tfile = targetmap[tpath]
        if tpath in oldmap and tfile.targetLink is None:
            ofile = oldmap[tpath]
            if not ofile == tfile:
                filestodiff.append((tfile, ofile, diffdir, oBundle, tBundle))
        else:
            filestoadd.append(tfile)

        tfile.package = newUpdateFileName.replace(".zip", "")
        newUpdate.manifest.append(tfile)

    # run bsdiff in parallel, we can't add files to the
    # zip in parallel because it's not thread safe
    print "Going to run delta on %d files" % len(filestodiff)
    pool = Pool(4)
    newUpdate.patches = pool.map(run_bsdiff, filestodiff)

    # now add the bsdiff fragments to the zip file
    for pe in newUpdate.patches:
        zf.write(os.path.join(diffdir, pe.name + ".bsdiff"),
                 pe.name + ".bsdiff", zipfile.ZIP_STORED)
        pe.package = newUpdateFileName.replace(".zip", "")

    # and the rest of the files
    for fa in filestoadd:
        zf.write(os.path.join(tBundle.directory, fa.name), fa.name,
                 zipfile.ZIP_DEFLATED)
        fa.package = newUpdateFileName.replace(".zip", "")
        newUpdate.install.append(fa)

    oBundle.cleanup()
    zf.close()
    print "%s created!" % newUpdateFileName

    package = PackageElement()
    package.fileHash = sha1fromfile(
        os.path.join(output_path, newUpdateFileName))
    package.name = newUpdateFileName.replace(".zip", "")
    package.size = sizefromfile(os.path.join(output_path, newUpdateFileName))

    newUpdate.packages.append(package)

    updateManifestPath = newUpdateFileName.replace(".zip", "-manifest.xml.bz2")
    bz = bz2.BZ2File(os.path.join(output_path, updateManifestPath), "w")
    bz.write(newUpdate.render(pretty=True))
    bz.close()

    print "%s created!" % updateManifestPath

    return newUpdate
Esempio n. 2
0
    for h in hashes:
        h.name = h.name.replace(directory, "")
        if h.name == options.mainbinary:
            h.is_main_binary = "true"

        h.package = package
        if excludere is None or excludere.match(h.name) is None:
            ihashes.append(h)
        else:
            print "Excluding %s" % h.name

    update = Update()
    update.install = ihashes
    update.manifest = ihashes
    update.version = 4
    update.targetVersion = options.version
    update.platform = options.platform

    print "Creating package..."

    try:
        os.makedirs(options.output)
    except:
        pass

    packagepath = os.path.join(options.output, package + ".zip")

    zfile = zipfile.ZipFile(packagepath, "w", zipfile.ZIP_DEFLATED)
    for h in ihashes:
        if not h.targetLink is None:
            attr = zipfile.ZipInfo()