manifest.py revision 302
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#
227N/A# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
39N/A# Use is subject to license terms.
39N/A
39N/Aimport bisect
39N/Aimport os
144N/Aimport cPickle
203N/Afrom itertools import groupby, chain
39N/A
51N/Aimport pkg.actions as actions
51N/Aimport pkg.client.retrieve as retrieve
111N/Aimport pkg.client.filter as filter
296N/Afrom pkg.actions.attribute import AttributeAction
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
205N/A The serialized structure of a manifest is an unordered list of actions.
39N/A
205N/A The special action, "set", represents a package attribute.
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 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):
104N/A self.img = None
39N/A self.fmri = None
39N/A
301N/A self.size = 0
301N/A
39N/A self.actions = []
39N/A
39N/A def __str__(self):
39N/A r = ""
39N/A
39N/A if self.fmri != None:
296N/A r = r + "set name=fmri value=%s\n" % self.fmri
39N/A
227N/A for act in sorted(self.actions):
39N/A r = r + "%s\n" % act
39N/A
39N/A return r
39N/A
72N/A def difference(self, origin):
203N/A """Return three lists of action pairs representing origin and
203N/A destination actions. The first list contains the pairs
203N/A representing additions, the second list contains the pairs
203N/A representing updates, and the third list contains the pairs
203N/A represnting removals. All three lists are in the order in which
203N/A they should be executed."""
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]
237N/A # XXX for now, we force license actions to always be
237N/A # different to insure that existing license files for
237N/A # new versions are always installed
72N/A changed = [
72N/A (odict[i], sdict[i])
72N/A for i in oset & sset
237N/A if odict[i].different(sdict[i]) or i[0] == "license"
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]
202N/A removed.sort(key = remsort, reverse = True)
72N/A added.sort(key = addsort)
72N/A changed.sort(key = addsort)
59N/A
202N/A return (added, changed, removed)
59N/A
203N/A def combined_difference(self, origin):
203N/A """Where difference() returns three lists, combined_difference()
203N/A returns a single list of the concatenation of th three."""
203N/A return list(chain(*self.difference(origin)))
203N/A
181N/A def humanized_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)
203N/A out = ""
48N/A
203N/A for src, dest in chain(*l):
72N/A if not src:
181N/A out += "+ %s\n" % str(dest)
72N/A elif not dest:
181N/A out += "- %s\n" + str(src)
46N/A else:
181N/A out += "%s -> %s\n" % (src, dest)
237N/A return out
46N/A
111N/A def filter(self, filters):
111N/A """Filter out actions from the manifest based on filters."""
111N/A
111N/A self.actions = [
111N/A a
111N/A for a in self.actions
111N/A if filter.apply_filters(a, filters)
111N/A ]
111N/A
111N/A def duplicates(self):
111N/A """Find actions in the manifest which are duplicates (i.e.,
111N/A represent the same object) but which are not identical (i.e.,
111N/A have all the same attributes)."""
111N/A
111N/A def fun(a):
111N/A """Return a key on which actions can be sorted."""
111N/A return a.name, a.attrs.get(a.key_attr, id(a))
111N/A
113N/A alldups = []
111N/A for k, g in groupby(sorted(self.actions, key = fun), fun):
113N/A glist = list(g)
113N/A dups = set()
113N/A for i in range(len(glist) - 1):
113N/A if glist[i].different(glist[i + 1]):
113N/A dups.add(glist[i])
113N/A dups.add(glist[i + 1])
113N/A if dups:
113N/A alldups.append((k, dups))
113N/A return alldups
111N/A
104N/A def set_fmri(self, img, fmri):
104N/A self.img = img
39N/A self.fmri = fmri
39N/A
51N/A @staticmethod
104N/A def make_opener(img, fmri, action):
51N/A def opener():
104N/A return retrieve.get_datastream(img, 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"""
301N/A assert self.actions == []
301N/A
301N/A self.size = 0
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():
146N/A l = l.lstrip()
146N/A if not l or l[0] == "#":
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
123N/A if action.attrs.has_key("path"):
123N/A np = action.attrs["path"].lstrip(os.path.sep)
123N/A action.attrs["path"] = np
123N/A
51N/A if hasattr(action, "hash"):
51N/A action.data = \
104N/A self.make_opener(self.img, self.fmri, action)
51N/A
301N/A self.size += int(action.attrs.get("pkg.size", "0"))
301N/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
144N/A def search_dict(self):
144N/A """Return the dictionary used for searching."""
144N/A action_dict = {}
144N/A for a in self.actions:
144N/A for k, v in a.generate_indices().iteritems():
302N/A # The value might be a list if an indexed action
302N/A # attribute is multivalued, such as driver
302N/A # aliases.
302N/A t = (a.name, a.attrs.get(a.key_attr))
144N/A if isinstance(v, list):
302N/A if k in action_dict:
302N/A action_dict[k].update(
302N/A dict((i, t) for i in v))
302N/A else:
302N/A action_dict[k] = \
302N/A dict((i, t) for i in v)
302N/A else:
302N/A # XXX if there's more than one k,v pair
302N/A # in the manifest, only one will get
302N/A # recorded. basename,gmake is one
302N/A # example.
144N/A if k in action_dict:
302N/A action_dict[k][v] = t
144N/A else:
302N/A action_dict[k] = { v: t }
144N/A return action_dict
144N/A
144N/A def pickle(self, file):
144N/A """Pickle the indices of the manifest's actions to the 'file'.
144N/A """
144N/A
144N/A cPickle.dump(self.search_dict(), file,
144N/A protocol = cPickle.HIGHEST_PROTOCOL)
144N/A
205N/A def get(self, key, default):
257N/A try:
257N/A return self[key]
257N/A except KeyError:
205N/A return default
205N/A
205N/A def __getitem__(self, key):
257N/A """Return the value for the package attribute 'key'. If
257N/A multiple attributes exist, return the first. Raises KeyError if
257N/A the attribute is not found."""
257N/A try:
257N/A values = [
257N/A a.attrs["value"]
257N/A for a in self.actions
257N/A if a.name == "set" and a.attrs["name"] == key
257N/A ]
257N/A except KeyError:
257N/A # This hides the fact that we had busted attribute
257N/A # actions in the manifest, but that's probably not so
257N/A # bad.
257N/A raise KeyError, key
205N/A
205N/A if values:
205N/A return values[0]
257N/A
257N/A raise KeyError, key
205N/A
296N/A def __setitem__(self, key, value):
296N/A """Set the value for the package attribute 'key' to 'value'."""
296N/A for a in self.actions:
296N/A if a.name == "set" and a.attrs["name"] == key:
296N/A a.attrs["value"] = value
296N/A return
296N/A
296N/A new_attr = AttributeAction(None, name=key, value=value)
296N/A self.actions.append(new_attr)
296N/A
205N/A def __contains__(self, key):
205N/A for a in self.actions:
205N/A if a.name == "set" and a.attrs["name"] == key:
205N/A return True
205N/A return False
205N/A
48N/Anull = Manifest()