__init__.py revision 1846
#
# 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
# 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 2010 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
"""
package containing packaging action (file type) modules
This package contains modules describing packaging actions, or file types. The
actions are dynamically discovered, so that new modules can be placed in this
package directory and they'll just be picked up. The current package contents
can be seen in the section "PACKAGE CONTENTS", below.
This package has one data member: "types". This is a dictionary which maps the
action names to the classes that represent them.
This package also has one function: "fromstr", which creates an action instance
based on a str() representation of an action.
"""
import inspect
import os
# All modules in this package (all python files except __init__.py with their
# extensions stripped off).
__all__ = [
f[:-3]
]
# A dictionary of all the types in this package, mapping to the classes that
# define them.
types = {}
# Pull the class objects out of nvlist, keeping only those that are
# actually defined in this package.
classes = [
c[1]
for c in nvlist
]
# Clean up after ourselves
class ActionError(Exception):
"""Base exception class for Action errors."""
def __unicode__(self):
# To workaround python issues 6108 and 2517, this provides a
# a standard wrapper for this class' exceptions so that they
# have a chance of being stringified correctly.
raise NotImplementedError()
class UnknownActionError(ActionError):
return _("unknown action type '%(type)s' in package "
"'%(fmri)s' in action '%(action)s'") % {
return _("unknown action type '%(type)s' in action "
class MalformedActionError(ActionError):
return _("Malformed action in package '%(fmri)s' at "
"position: %(pos)d:\n %(action)s\n"
"marker": marker }
return _("Malformed action at position: %(pos)d:\n "
class ActionDataError(ActionError):
"""Used to indicate that a file-related error occuring during action
initialization."""
class InvalidActionError(ActionError):
"""Used to indicate that attributes provided were invalid, or required
attributes were missing for an action."""
return _("invalid action in package %(fmri)s: "
return _("invalid action, '%(action)s': %(error)s") % {
"""Used to indicate that one more action attributes were invalid."""
"""'act' is an Action (object or string).
'errors' is a list of tuples of the form (name, error) where
'name' is the action attribute name, and 'error' is a string
indicating what attribute is invalid and why.
'fmri' is an optional package FMRI (object or string)
indicating what package contained the actions with invalid
attributes."""
return _("The action '%(action)s' in package "
"'%(fmri)s' has invalid attribute(s):\n"
return _("The action '%(action)s' has invalid attribute(s):\n"
"act_errors": act_errors }
def attrsfromstr(string):
"""Create an attribute dict given a string w/ key=value pairs.
Raises MalformedActionError if the attributes have syntactic problems.
"""
"""Create an action instance based on a str() representation of an
action.
Raises UnknownActionError if the action type is unknown.
Raises MalformedActionError if the action has other syntactic problems.
"""
"was not provided.") % ka)
if ahash:
return action
"""Create an action instance based on a sequence of "key=value" strings.
This function also translates external representations of actions with
payloads (like file and license which can use NOHASH or file paths to
point to the payload) to an internal representation which sets the
data field of the action returned.
The "atype" parameter is the type of action to be built.
The "args" parameter is the sequence of "key=value" strings.
The "ahash" parameter is used to set the hash value for the action.
The "basedirs" parameter is the list of directories to look in to find
any payload for the action.
Raises MalformedActionError if the attribute strings are malformed.
"""
data = None
attrs = {}
try:
if v == '' or a == '':
raise MalformedActionError(
"attribute '%s'" % kv)
# This is by far the common case-- an attribute with
# a single value.
if a not in attrs:
attrs[a] = v
else:
else:
except ValueError:
# We're only here if the for: statement above throws a
# MalformedActionError. That can happen if split yields a
# single element, which is possible if e.g. an attribute lacks
# an =.
# keys called 'data' cause problems due to the named parameter being
# passed to the action constructor below. Check for these. Note that
# _fromstr also checks for this.
if "data" in attrs:
raise InvalidActionError(astr,
"%s action cannot have a 'data' attribute" % atype)
"'%s', was not provided.") % ka)
if ahash:
return action, local_path
"""Create an action instance based on a sequence of strings.
This function also translates external representations of actions with
payloads (like file and license which can use NOHASH or file paths to
point to the payload) to an internal representation which sets the
data field of the action returned.
In general, each string should be in the form of "key=value". The
exception is a payload for certain actions which should be the first
item in the sequence.
Raises MalformedActionError if the attribute strings are malformed.
"""
return action, None
return action, local_path
"""Sets the data field of an action using the information in the
payload and returns the actual path used to set the data.
The "payload" parameter is the representation of the data to assign to
the action's data field. It can either be NOHASH or a path to the file.
The "action" parameter is the action to modify.
The "basedirs" parameter contains the directories to examine to find
the payload in."""
if not payload:
return None
if payload == "NOHASH":
else:
if basedirs:
# look for file in specified dirs
for d in basedirs:
break
else:
return data