1516N/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#
3155N/A# Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
38N/A#
38N/A
38N/Aimport os
42N/Aimport stat
38N/Aimport tarfile
2456N/Aimport pkg.bundle
443N/Aimport pkg.misc as misc
49N/Afrom pkg.actions import *
49N/A
2456N/Aclass TarBundle(pkg.bundle.Bundle):
38N/A
3155N/A def __init__(self, filename, **kwargs):
50N/A # XXX This could be more intelligent. Or get user input. Or
50N/A # extend API to take FMRI.
2456N/A filename = os.path.normpath(filename)
2456N/A self.tf = tarfile.open(filename)
2456N/A self.filename = filename
38N/A self.pkgname = os.path.basename(filename)
2276N/A self.pkg = None
38N/A
38N/A def __del__(self):
38N/A self.tf.close()
38N/A
2456N/A def _walk_bundle(self):
38N/A for f in self.tf:
3155N/A yield f.name, (self.tf, f)
2456N/A
2456N/A def __iter__(self):
2456N/A for path, data in self._walk_bundle():
2456N/A yield self.action(*data)
49N/A
49N/A def action(self, tarfile, tarinfo):
49N/A if tarinfo.isreg():
3253N/A # false positive
3253N/A # file-builtin; pylint: disable=W1607
49N/A return file.FileAction(tarfile.extractfile(tarinfo),
443N/A mode=oct(stat.S_IMODE(tarinfo.mode)),
443N/A owner=tarinfo.uname, group=tarinfo.gname,
443N/A path=tarinfo.name,
443N/A timestamp=misc.time_to_timestamp(tarinfo.mtime))
49N/A elif tarinfo.isdir():
49N/A return directory.DirectoryAction(
443N/A mode=oct(stat.S_IMODE(tarinfo.mode)),
443N/A owner=tarinfo.uname, group=tarinfo.gname,
443N/A path=tarinfo.name)
1196N/A elif tarinfo.issym():
1196N/A return link.LinkAction(path=tarinfo.name,
1196N/A target=tarinfo.linkname)
1196N/A elif tarinfo.islnk():
1196N/A return hardlink.HardLinkAction(path=tarinfo.name,
1196N/A target=tarinfo.linkname)
1196N/A else:
1196N/A return unknown.UnknownAction(path=tarinfo.name)
38N/A
38N/Adef test(filename):
1944N/A return tarfile.is_tarfile(filename)