Beispiel #1
0
    def testPreferXZoverUNLZMA(self):
        # CNY-3231
        # Make sure if both xz and unlzma are present, that we prefer xz
        workDir = tempfile.mkdtemp(prefix="utiltest-")
        oldPath = os.getenv('PATH')
        xzPath = os.path.join(workDir, "xz")
        unlzmaPath = os.path.join(workDir, "unlzma")
        dumbFilePath = os.path.join(workDir, "some-file")
        scriptContents = "#!/bin/bash\n\n/bin/cat"
        file(xzPath, "w").write(scriptContents)
        file(unlzmaPath, "w").write(scriptContents)
        data = "Feed dog to cat"
        file(dumbFilePath, "w").write(data)
        os.chmod(xzPath, 0755)
        os.chmod(unlzmaPath, 0755)

        try:
            os.environ['PATH'] = workDir
            decompressor = util.LZMAFile(file(dumbFilePath))
            self.assertEqual(decompressor.read(), data)
            decompressor.close()
            # Make sure we prefer xz over unlzma
            self.assertEqual(decompressor.executable, xzPath)
            # But if xz is not available, we can use unlzma
            os.unlink(xzPath)
            decompressor = util.LZMAFile(file(dumbFilePath))
            self.assertEqual(decompressor.read(), data)
            decompressor.close()
            self.assertEqual(decompressor.executable, unlzmaPath)
        finally:
            os.environ['PATH'] = oldPath
            util.rmtree(workDir)
Beispiel #2
0
    def testLZMAFile(self):
        # CNY-3564 - test for short reads
        xzbin = "/usr/bin/xz"
        if not os.path.exists(xzbin):
            raise testhelp.SkipTestException(
                "Skipping test, %s not found" % xzbin)
        fobj = tempfile.TemporaryFile()
        p = subprocess.Popen([xzbin, "-zc"],
            stdin=subprocess.PIPE, stdout=fobj)
        data = "0" * 1024
        for i in range(1024):
            p.stdin.write(data)
        p.stdin.close()
        p.wait()
        fobj.flush()

        # Make sure something did get written
        self.assertTrue(fobj.tell() > 0)
        fobj.seek(0)

        b = util.LZMAFile(fobj)
        # Read a large amount of data, hopefully larger than the pipe buffer
        limit = 128000
        buf = b.read(limit)
        self.assertEqual(len(buf), limit)
        self.assertEqual(list(set(buf)), ["0"])
Beispiel #3
0
def UncompressedRpmPayload(fileIn):
    """
    Given a (seekable) file object containing an RPM package, return
    a file-like object that can be used for streaming uncompressed content
    """
    fileIn.seek(0)
    header = readHeader(fileIn)
    # check to make sure that this is a cpio archive (though most rpms
    # are cpio).  If the tag does not exist, assume it's cpio
    if PAYLOADFORMAT in header:
        if header[PAYLOADFORMAT] != 'cpio':
            raise UnknownPayloadFormat(header[PAYLOADFORMAT])

    # check to see how the payload is compressed.  Again, if the tag
    # does not exist, check if it is gzip, if not it is uncompressed
    if PAYLOADCOMPRESSOR in header:
        compression = header[PAYLOADCOMPRESSOR]
    else:
        b = fileIn.read(4096)
        if len(b) > 2 and b[0] == '\x1f' and b[1] == '\x8b':
            compression = 'gzip'
        else:
            compression = 'uncompressed'
    # rewind the file to let seekToData do its job
    fileIn.seek(0)
    seekToData(fileIn)

    if compression == 'gzip':
        uncompressed = util.GzipFile(fileobj=fileIn, mode="r")
        #uncompressed._new_member = False
    elif compression == 'bzip2':
        uncompressed = util.BZ2File(fileIn)
    elif compression in ['lzma', 'xz']:
        uncompressed = util.LZMAFile(fileIn)
    elif compression == 'uncompressed':
        uncompressed = fileIn
    else:
        raise UnknownCompressionType(compression)

    return uncompressed