manifest.py revision 181
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 sha
39N/Aimport shutil
39N/Aimport time
39N/Aimport urllib
144N/Aimport cPickle
111N/Afrom itertools import groupby
39N/A
51N/Aimport pkg.actions as actions
39N/Aimport pkg.fmri as fmri
51N/Aimport pkg.client.retrieve as retrieve
111N/Aimport pkg.client.filter as filter
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):
104N/A self.img = None
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
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)
181N/A out = ""
48N/A
72N/A for src, dest in 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)
181N/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"""
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
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():
144N/A if isinstance(v, list):
144N/A if k in action_dict:
144N/A action_dict[k].update(
144N/A dict((i, True) for i in v))
144N/A else:
144N/A action_dict[k] = \
144N/A dict((i, True) for i in v)
144N/A else:
144N/A if k in action_dict:
144N/A action_dict[k][v] = True
144N/A else:
144N/A action_dict[k] = { v: True }
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
48N/Anull = Manifest()