TarBundle.py revision 50
38N/A#!/usr/bin/python
38N/A#
38N/A# CDDL HEADER START
38N/A#
38N/A# The contents of this file are subject to the terms of the
38N/A# Common Development and Distribution License (the "License").
38N/A# You may not use this file except in compliance with the License.
38N/A#
38N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
38N/A# or http://www.opensolaris.org/os/licensing.
38N/A# See the License for the specific language governing permissions
38N/A# and limitations under the License.
38N/A#
38N/A# When distributing Covered Code, include this CDDL HEADER in each
38N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38N/A# If applicable, add the following below this CDDL HEADER, with the
38N/A# fields enclosed by brackets "[]" replaced with your own identifying
38N/A# information: Portions Copyright [yyyy] [name of copyright owner]
38N/A#
38N/A# CDDL HEADER END
38N/A#
38N/A
38N/A#
38N/A# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
38N/A# Use is subject to license terms.
38N/A#
38N/A
38N/Aimport os
42N/Aimport stat
38N/Aimport tarfile
38N/Aimport tempfile
38N/A
49N/Afrom pkg.actions import *
49N/A
38N/Aclass TarBundle(object):
38N/A
38N/A def __init__(self, filename):
38N/A self.tf = tarfile.open(filename)
50N/A # XXX This could be more intelligent. Or get user input. Or
50N/A # extend API to take FMRI.
38N/A self.pkgname = os.path.basename(filename)
38N/A
38N/A def __del__(self):
38N/A self.tf.close()
38N/A
38N/A def __iter__(self):
38N/A for f in self.tf:
49N/A yield self.action(self.tf, f)
49N/A
49N/A def action(self, tarfile, tarinfo):
49N/A if tarinfo.isreg():
49N/A return file.FileAction(tarfile.extractfile(tarinfo),
49N/A mode = oct(stat.S_IMODE(tarinfo.mode)),
49N/A owner = tarinfo.uname, group = tarinfo.gname,
49N/A path = tarinfo.name)
49N/A elif tarinfo.isdir():
49N/A return directory.DirectoryAction(
49N/A mode = oct(stat.S_IMODE(tarinfo.mode)),
49N/A owner = tarinfo.uname, group = tarinfo.gname,
49N/A path = tarinfo.name)
38N/A
38N/Adef test(filename):
38N/A try:
38N/A return tarfile.is_tarfile(filename)
38N/A except:
38N/A return False