#!/usr/bin/python2.4
# -*- mode: python -*-
#
# Copyright (c) 2004-2005 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any waranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#

import gzip
import os
import sys

import conary
import rpmhelper
from lib import util

def usage():
    print "Usage: rpm2cpio <rpmpackage> | cpio -ivumd"
    return 1
            
def main(argv):
    if len(argv) > 2 or len(argv) < 2:
        return usage()
    try:
        r = file(argv[1], 'r')
    except:
        return usage()

    # 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
    h = rpmhelper.readHeader(r)
    if h.has_key(rpmhelper.PAYLOADFORMAT):
        if h[rpmhelper.PAYLOADFORMAT] != 'cpio':
            print >> sys.stderr, 'payload is not in cpio format'
            sys.exit(1)

    # check to see how the payload is compressed.  Again, if the tag
    # does not exist, assume that it's gzip.
    if h.has_key(rpmhelper.PAYLOADCOMPRESSOR):
        compression = h[rpmhelper.PAYLOADCOMPRESSOR]
        if compression == 'gzip':
            decompressor = lambda fobj: gzip.GzipFile(fileobj=fobj)
        elif compression == 'bzip2':
            decompressor = lambda fobj: util.BZ2File(fobj)
        else:
            print >> sys.stderr, 'compression type', compression, 'unknown'
            sys.exit(1)
    else:
        decompressor = lambda fobj: gzip.GzipFile(fileobj=fobj)

    # rewind the file to let seekToData do its job
    r.seek(0)
    rpmhelper.seekToData(r)
    uncompressed = decompressor(r)
    while 1:
	buf = uncompressed.read(4096)
	if not buf:
	    break
	sys.stdout.write(buf)
    
                
if __name__ == "__main__":
    sys.exit(main(sys.argv))
