Ejemplo 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
Ejemplo n.º 2
0
    for h in ihashes:
        if not h.targetLink is None:
            attr = zipfile.ZipInfo()
            attr.filename = h.name
            attr.create_system = 3
            # symlink magic
            attr.external_attr = 2716663808L
            zfile.writestr(attr, h.targetLink)
        else:
            zfile.write(os.path.join(directory, h.name), h.name)

    zfile.close()
    print "%s created" % packagepath

    fpe = get_file(packagepath)
    pe = PackageElement()
    pe.fileHash = fpe.fileHash
    pe.name = package + ".zip"
    pe.size = fpe.size

    update.packages.append(pe)

    manifestpath = os.path.join(options.output,
                                package.replace("-full", "-manifest.xml.bz2"))

    manifestfp = BZ2File(manifestpath, "w")
    manifestfp.write(update.render(pretty=True))
    manifestfp.close()

    print "%s created" % (manifestpath)