SolarisPackageDirBundle.py revision 2276
#!/usr/bin/python
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
#
import os
import pkg.misc as misc
from pkg.sysvpkg import SolarisPackage
from pkg.cpiofile import CpioFile
from pkg.actions import *
from pkg.actions.attribute import AttributeAction
from pkg.actions.legacy import LegacyAction
class SolarisPackageDirBundle(object):
hollow_attr = "pkg.send.convert.sunw-pkg-hollow"
def __init__(self, filename, data=True, targetpaths=()):
self.pkg = SolarisPackage(filename)
self.pkgname = self.pkg.pkginfo["PKG"]
self.filename = filename
self.data = data
# map the path name to the SVR4 class it belongs to and
# maintain a set of pre/post install/remove and class action
# scripts this package uses.
self.class_actions_dir = {}
self.class_action_names = set()
self.scripts = set()
self.hollow = self.pkg.pkginfo.get("SUNW_PKG_HOLLOW",
"").lower() == "true"
# A list of pkg.action.AttributeActions with pkginfo
# attributes for items that don't map to pkg(5) equivalents
self.pkginfo_actions = self.get_pkginfo_actions(self.pkg.pkginfo)
def __iter__(self):
faspac = []
if "faspac" in self.pkg.pkginfo:
faspac = self.pkg.pkginfo["faspac"]
# Want to access the manifest as a dict.
pkgmap = {}
for p in self.pkg.manifest:
pkgmap[p.pathname] = p
self.class_actions_dir[p.pathname] = p.klass
self.class_action_names.add(p.klass)
for act in self.pkginfo_actions:
yield act
if not self.data:
for p in self.pkg.manifest:
act = self.action(p, None)
if act:
yield act
return
def j(path):
return os.path.join(self.pkg.basedir, path)
faspac_contents = set()
for klass in faspac:
fpath = os.path.join(self.filename, "archive", klass)
# We accept either bz2 or 7zip'd files
for x in [".bz2", ".7z"]:
if os.path.exists(fpath + x):
cf = CpioFile.open(fpath + x)
break
for ci in cf:
faspac_contents.add(j(ci.name))
act = self.action(pkgmap[j(ci.name)],
ci.extractfile())
if act:
yield act
# Remove BASEDIR from the path. The extra work is because if
# BASEDIR is not empty (non-"/"), then we probably need to strip
# an extra slash from the beginning of the path, but if BASEDIR
# is "" ("/" in the pkginfo file), then we don't need to do
# anything extra.
def r(path, type):
if type == "i":
return path
p = path[len(self.pkg.basedir):]
if p[0] == "/":
p = p[1:]
return p
for p in self.pkg.manifest:
# Just do the files that remain. Only regular file
# types end up compressed; so skip them and only them.
# Files with special characters in their names may not
# end up in the faspac archive, so we still need to emit
# the ones that aren't.
if p.type in "fev" and p.klass in faspac and \
p.pathname in faspac_contents:
continue
# These are the only valid file types in SysV packages
if p.type in "fevbcdxpls":
act = self.action(p, os.path.join(self.filename,
"reloc", r(p.pathname, p.type)))
if act:
yield act
elif p.type == "i":
a = self.action(p, os.path.join(self.filename,
"install", r(p.pathname, p.type)))
if a:
yield a
def action(self, mapline, data):
preserve_dict = {
"renameold": "renameold",
"renamenew": "renamenew",
"preserve": "true",
"svmpreserve": "true"
}
act = None
# If any one of the mode, owner, or group is "?", then we're
# clearly not capable of delivering the object correctly, so
# ignore it.
if mapline.type in "fevdx" and (mapline.mode == "?" or
mapline.owner == "?" or mapline.group == "?"):
return None
if mapline.type in "f":
act = file.FileAction(data, mode=mapline.mode,
owner=mapline.owner, group=mapline.group,
path=mapline.pathname,
timestamp=misc.time_to_timestamp(int(mapline.modtime)))
if mapline.klass in preserve_dict:
act.attrs["preserve"] = preserve_dict[mapline.klass]
elif mapline.type in "ev":
# for editable files, map klass onto IPS names; if match
# fails, make sure we at least preserve file
preserve=preserve_dict.get(mapline.klass, "true")
act = file.FileAction(data, mode=mapline.mode,
owner=mapline.owner, group=mapline.group,
path=mapline.pathname, preserve=preserve,
timestamp=misc.time_to_timestamp(int(mapline.modtime)))
elif mapline.type in "dx":
act = directory.DirectoryAction(mode=mapline.mode,
owner=mapline.owner, group=mapline.group,
path=mapline.pathname)
elif mapline.type == "s":
act = link.LinkAction(path=mapline.pathname,
target=mapline.target)
elif mapline.type == "l":
act = hardlink.HardLinkAction(path=mapline.pathname,
target=mapline.target)
elif mapline.type == "i" and mapline.pathname == "copyright":
act = license.LicenseAction(data,
license="%s.copyright" % self.pkgname,
path=mapline.pathname)
elif mapline.type == "i":
if mapline.pathname not in ["depend", "pkginfo"]:
# check to see if we've seen this script
# before
script = mapline.pathname
if script.startswith("i.") and \
script.replace("i.", "", 1) \
in self.class_action_names:
pass
elif script.startswith("r.") and \
script.replace("r.", "", 1) in \
self.class_action_names:
pass
else:
self.scripts.add(script)
return None
else:
act = unknown.UnknownAction(path=mapline.pathname)
if self.hollow and act:
act.attrs["pkg.send.convert.sunw-pkg-hollow"] = "true"
return act
def get_pkginfo_actions(self, pkginfo):
"""Creates a list of pkg.action.AttributeActions corresponding
to pkginfo fields that aren't directly mapped to pkg(5)
equivalents."""
# these keys get converted to a legacy action
legacy_keys = [
"arch",
"category",
"name",
"desc",
"hotline",
"pkg",
"vendor",
"version"
]
# parameters defined in pkginfo(4) that we always ignore.
# by default, we also ignore SUNW_*
ignored_keys = [
"pstamp",
"pkginst",
"maxinst",
"classes",
"basedir",
"intonly",
"istates",
"order",
"rstates",
"ulimit",
# XXX pkg.sysvpkg adds this, ignoring for now.
"pkg.plat",
]
ignored_keys.extend(legacy_keys)
actions = []
for key in pkginfo:
if not pkginfo[key]:
continue
name = key.lower()
if name in ignored_keys or "SUNW_" in key:
continue
name = "pkg.send.convert.%s" % name
name = name.replace("_", "-")
actions.append(AttributeAction(name=name,
value=pkginfo[key]))
legacy_attrs = {}
for key in pkginfo:
name = key.lower()
if name in legacy_keys:
name = name.replace("_", "-")
legacy_attrs[name] = pkginfo[key]
actions.append(LegacyAction(data=None, **legacy_attrs))
if "DESC" in pkginfo:
actions.append(AttributeAction(name="pkg.description",
value=pkginfo["DESC"]))
if "NAME" in pkginfo:
actions.append(AttributeAction(name="pkg.summary",
value=pkginfo["NAME"]))
if self.hollow:
for act in actions:
act.attrs[self.hollow_attr] = "true"
return actions
def test(filename):
if os.path.isfile(os.path.join(filename, "pkginfo")) and \
os.path.isfile(os.path.join(filename, "pkgmap")):
return True
return False