manifest.py revision 72
39N/A#!/usr/bin/python
39N/A#
39N/A# CDDL HEADER START
39N/A#
39N/A# The contents of this file are subject to the terms of the
39N/A# Common Development and Distribution License (the "License").
39N/A# You may not use this file except in compliance with the License.
39N/A#
39N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
39N/A# or http://www.opensolaris.org/os/licensing.
39N/A# See the License for the specific language governing permissions
39N/A# and limitations under the License.
39N/A#
39N/A# When distributing Covered Code, include this CDDL HEADER in each
39N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
39N/A# If applicable, add the following below this CDDL HEADER, with the
39N/A# fields enclosed by brackets "[]" replaced with your own identifying
39N/A# information: Portions Copyright [yyyy] [name of copyright owner]
39N/A#
39N/A# CDDL HEADER END
39N/A#
39N/A# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
39N/A# Use is subject to license terms.
39N/A
39N/Aimport bisect
39N/Aimport os
39N/Aimport re
39N/Aimport sha
39N/Aimport shutil
39N/Aimport time
39N/Aimport urllib
39N/A
51N/Aimport pkg.actions as actions
39N/Aimport pkg.fmri as fmri
39N/Aimport pkg.package as package
51N/Aimport pkg.client.retrieve as retrieve
39N/A
39N/A# The type member is used for the ordering of actions.
39N/AACTION_DIR = 10
39N/AACTION_FILE = 20
39N/AACTION_LINK = 50
39N/AACTION_HARDLINK = 55
39N/AACTION_DEVICE = 100
39N/AACTION_USER = 200
39N/AACTION_GROUP = 210
39N/AACTION_SERVICE = 300
39N/AACTION_RESTART = 310
48N/AACTION_DEPEND = 400
48N/A
48N/ADEPEND_REQUIRE = 0
48N/ADEPEND_OPTIONAL = 1
48N/ADEPEND_INCORPORATE =10
48N/A
48N/Adepend_str = { DEPEND_REQUIRE : "require",
48N/A DEPEND_OPTIONAL : "optional",
48N/A DEPEND_INCORPORATE : "incorporate"
48N/A}
39N/A
39N/Aclass Manifest(object):
39N/A """A Manifest is the representation of the actions composing a specific
39N/A package version on both the client and the repository. Both purposes
39N/A utilize the same storage format.
39N/A
39N/A The serialized structure of a manifest is an unordered list of package
39N/A attributes, followed by an unordered list of actions (such as files to
39N/A install).
39N/A
39N/A The special action, "set", represents an attribute setting.
39N/A
39N/A The reserved attribute, "fmri", represents the package and version
39N/A described by this manifest. It is available as a string via the
39N/A attributes dictionary, and as an FMRI object from the fmri member.
39N/A
39N/A The list of manifest-wide reserved attributes is
39N/A
39N/A base_directory Default base directory, for non-user images.
39N/A fmri Package FMRI.
39N/A isa Package is intended for a list of ISAs.
39N/A licenses Package contains software available under a list
39N/A of license terms.
39N/A platform Package is intended for a list of platforms.
39N/A relocatable Suitable for User Image.
39N/A
39N/A All non-prefixed attributes are reserved to the framework. Third
39N/A parties may prefix their attributes with a reversed domain name, domain
39N/A name, or stock symbol. An example might be
39N/A
39N/A com.example,supported
39N/A
39N/A as an indicator that a specific package version is supported by the
39N/A vendor, example.com.
39N/A
48N/A manifest.null is provided as the null manifest. Differences against the
48N/A null manifest result in the complete set of attributes and actions of
48N/A the non-null manifest, meaning that all operations can be viewed as
48N/A tranitions between the manifest being installed and the manifest already
48N/A present in the image (which may be the null manifest).
59N/A """
48N/A
39N/A def __init__(self):
39N/A self.fmri = None
39N/A
39N/A self.actions = []
39N/A self.attributes = {}
39N/A return
39N/A
39N/A def __str__(self):
39N/A r = ""
39N/A
39N/A if self.fmri != None:
39N/A r = r + "set fmri = %s\n" % self.fmri
39N/A
39N/A for att in sorted(self.attributes.keys()):
39N/A r = r + "set %s = %s\n" % (att, self.attributes[att])
39N/A
39N/A for act in self.actions:
39N/A r = r + "%s\n" % act
39N/A
39N/A return r
39N/A
72N/A def difference(self, origin):
72N/A """Return a list of action pairs representing origin and
72N/A destination actions."""
72N/A # XXX Do we need to find some way to assert that the keys are
72N/A # all unique?
59N/A
72N/A sdict = dict(
72N/A ((a.name, a.attrs.get(a.key_attr, id(a))), a)
72N/A for a in self.actions
72N/A )
72N/A odict = dict(
72N/A ((a.name, a.attrs.get(a.key_attr, id(a))), a)
72N/A for a in origin.actions
72N/A )
59N/A
72N/A sset = set(sdict.keys())
72N/A oset = set(odict.keys())
59N/A
72N/A added = [(None, sdict[i]) for i in sset - oset]
72N/A removed = [(odict[i], None) for i in oset - sset]
72N/A changed = [
72N/A (odict[i], sdict[i])
72N/A for i in oset & sset
72N/A if odict[i].different(sdict[i])
72N/A ]
59N/A
72N/A # XXX Do changed actions need to be sorted at all? This is
72N/A # likely to be the largest list, so we might save significant
72N/A # time by not sorting. Should we sort above? Insert into a
72N/A # sorted list?
59N/A
72N/A # singlesort = lambda x: x[0] or x[1]
72N/A addsort = lambda x: x[1]
72N/A remsort = lambda x: x[0]
72N/A removed.sort(key = remsort)
72N/A added.sort(key = addsort)
72N/A changed.sort(key = addsort)
59N/A
72N/A return removed + added + changed
59N/A
46N/A def display_differences(self, other):
48N/A """Output expects that self is newer than other. Use of sets
48N/A requires that we convert the action objects into some marshalled
48N/A form, otherwise set member identities are derived from the
48N/A object pointers, rather than the contents."""
48N/A
72N/A l = self.difference(other)
48N/A
72N/A for src, dest in l:
72N/A if not src:
72N/A print "+", dest
72N/A elif not dest:
72N/A print "-", src
46N/A else:
72N/A print "%s -> %s" % (src, dest)
46N/A
39N/A def set_fmri(self, fmri):
39N/A self.fmri = fmri
39N/A
51N/A @staticmethod
51N/A def make_opener(fmri, action):
51N/A def opener():
51N/A return retrieve.get_datastream(fmri, action.hash)
51N/A return opener
51N/A
39N/A def set_content(self, str):
39N/A """str is the text representation of the manifest"""
39N/A
72N/A # So we could build up here the type/key_attr dictionaries like
72N/A # sdict and odict in difference() above, and have that be our
72N/A # main datastore, rather than the simple list we have now. If
72N/A # we do that here, we can even assert that the "same" action
72N/A # can't be in a manifest twice. (The problem of having the same
72N/A # action more than once in packages that can be installed
72N/A # together has to be solved somewhere else, though.)
39N/A for l in str.splitlines():
51N/A if re.match("^\s*(#.*)?$", l):
50N/A continue
51N/A
51N/A try:
51N/A action = actions.fromstr(l)
51N/A except KeyError:
51N/A raise SyntaxError, \
51N/A "unknown action '%s'" % l.split()[0]
51N/A
51N/A if hasattr(action, "hash"):
51N/A action.data = \
51N/A self.make_opener(self.fmri, action)
51N/A
72N/A if not self.actions:
51N/A self.actions.append(action)
39N/A else:
51N/A bisect.insort(self.actions, action)
39N/A
39N/A return
39N/A
48N/Anull = Manifest()
48N/A
39N/Aif __name__ == "__main__":
46N/A m1 = Manifest()
39N/A
39N/A x = """\
51N/Aset com.sun,test=true
59N/Adepend type=require fmri=pkg:/library/libc
51N/Afile fff555fff mode=0555 owner=sch group=staff path=/usr/bin/i386/sort isa=i386
39N/A"""
46N/A m1.set_content(x)
46N/A
46N/A print m1
46N/A
46N/A m2 = Manifest()
39N/A
46N/A y = """\
72N/Aset com.sun,test=false
51N/Aset com.sun,data=true
59N/Adepend type=require fmri=pkg:/library/libc
51N/Afile fff555ff9 mode=0555 owner=sch group=staff path=/usr/bin/i386/sort isa=i386
51N/Afile eeeaaaeee mode=0555 owner=sch group=staff path=/usr/bin/amd64/sort isa=amd64
51N/A
51N/Afile ff555fff mode=0555 owner=root group=bin path=/kernel/drv/foo isa=i386
51N/Afile ff555ffe mode=0555 owner=root group=bin path=/kernel/drv/amd64/foo isa=amd64
51N/Afile ff555ffd mode=0644 owner=root group=bin path=/kernel/drv/foo.conf
46N/A"""
46N/A
46N/A m2.set_content(y)
46N/A
46N/A print m2
46N/A
46N/A m2.display_differences(m1)
48N/A
48N/A print null
48N/A
48N/A m2.display_differences(null)
72N/A
72N/A print
72N/A m2.difference(m1)
72N/A
72N/A m3 = Manifest()
72N/A t3 = """\
72N/Adir mode=0755 owner=root group=sys path=/bin
72N/Afile 00000000 mode=0644 owner=root group=sys path=/bin/change
72N/Afile 00000001 mode=0644 owner=root group=sys path=/bin/nochange
72N/Afile 00000002 mode=0644 owner=root group=sys path=/bin/toberemoved
72N/Alink path=/bin/change-link target=change
72N/Alink path=/bin/nochange-link target=nochange
72N/Alink path=/bin/change-target target=target1
72N/Alink path=/bin/change-type target=random
72N/A"""
72N/A m3.set_content(t3)
72N/A
72N/A m4 = Manifest()
72N/A t4 = """\
72N/Adir mode=0755 owner=root group=sys path=/bin
72N/Afile 0000000f mode=0644 owner=root group=sys path=/bin/change
72N/Afile 00000001 mode=0644 owner=root group=sys path=/bin/nochange
72N/Afile 00000003 mode=0644 owner=root group=sys path=/bin/wasadded
72N/Alink path=/bin/change-link target=change
72N/Alink path=/bin/nochange-link target=nochange
72N/Alink path=/bin/change-target target=target2
72N/Adir mode=0755 owner=root group=sys path=/bin/change-type
72N/A"""
72N/A m4.set_content(t4)
72N/A
72N/A print "\n" + 50 * "=" + "\n"
72N/A m4.difference(m3)
72N/A print "\n" + 50 * "=" + "\n"
72N/A m4.difference(null)