src/modules/client/history.py
author Danek Duvall <danek.duvall@sun.com>
Tue, 11 Nov 2008 15:17:25 -0800
changeset 696 6bbfd2dece6f
parent 593 f4305c5f2602
child 732 9ea802fef2fb
permissions -rw-r--r--
4371 user-agent string needs to be different for different client front-ends
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     1
#!/usr/bin/python2.4
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     2
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     3
# CDDL HEADER START
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     4
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     5
# The contents of this file are subject to the terms of the
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     6
# Common Development and Distribution License (the "License").
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     7
# You may not use this file except in compliance with the License.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     8
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
     9
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    10
# or http://www.opensolaris.org/os/licensing.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    11
# See the License for the specific language governing permissions
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    12
# and limitations under the License.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    13
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    14
# When distributing Covered Code, include this CDDL HEADER in each
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    15
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    16
# If applicable, add the following below this CDDL HEADER, with the
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    17
# fields enclosed by brackets "[]" replaced with your own identifying
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    18
# information: Portions Copyright [yyyy] [name of copyright owner]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    19
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    20
# CDDL HEADER END
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    21
#
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    22
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    23
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    24
# Use is subject to license terms.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    25
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    26
import errno
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    27
import os
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    28
import shutil
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    29
import sys
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    30
import xml.dom.minidom as xmini
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    31
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    32
import pkg
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    33
import pkg.misc as misc
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    34
import pkg.portable as portable
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    35
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    36
# Constants for the (outcome, reason) combination for operation result.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    37
# Indicates that the operation succeeded.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    38
RESULT_SUCCEEDED = ["Succeeded"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    39
# Indicates that the user canceled the operation.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    40
RESULT_CANCELED = ["Canceled"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    41
# Indicates that the operation had no work to perform or didn't need to make
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    42
# any changes to the image.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    43
RESULT_NOTHING_TO_DO = ["Nothing to do"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    44
# Indicates that the operation failed for an unknown reason.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    45
RESULT_FAILED_UNKNOWN = ["Failed", "Unknown"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    46
# Indicates that the operation failed due to package constraints or because of
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    47
# a restriction enforced by the client (e.g. SUNWipkg out of date).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    48
RESULT_FAILED_CONSTRAINED = ["Failed", "Constrained"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    49
# Indicates that the user or client provided bad information which resulted in
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    50
# operation failure.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    51
RESULT_FAILED_BAD_REQUEST = ["Failed", "Bad Request"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    52
# Indicates that a search operation failed.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    53
RESULT_FAILED_SEARCH = ["Failed", "Search"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    54
# Indicates that there was a problem writing a file or a permissions error.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    55
RESULT_FAILED_STORAGE = ["Failed", "Storage"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    56
# Indicates that a transport error caused the operation to fail.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    57
RESULT_FAILED_TRANSPORT = ["Failed", "Transport"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    58
556
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
    59
# Operations that are discarded, not saved, when recorded by history.
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
    60
DISCARDED_OPERATIONS = ["contents", "info", "list"]
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
    61
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    62
class __HistoryException(Exception):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    63
        """Private base exception class for all History exceptions."""
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    64
        def __init__(self, *args):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    65
                Exception.__init__(self, *args)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    66
                self.data = args[0]
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    67
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    68
        def __str__(self):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    69
                return str(self.data)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    70
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    71
class HistoryLoadException(__HistoryException):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    72
        """Used to indicate that an unexpected error occurred while loading
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    73
        History operation information.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    74
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    75
        The first argument should be an exception object related to the
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    76
        error encountered.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    77
        """
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    78
        pass
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    79
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    80
class HistoryStoreException(__HistoryException):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    81
        """Used to indicate that an unexpected error occurred while storing
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    82
        History operation information.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    83
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    84
        The first argument should be an exception object related to the
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    85
        error encountered.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    86
        """
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    87
        pass
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    88
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    89
class HistoryPurgeException(__HistoryException):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    90
        """Used to indicate that an unexpected error occurred while purging
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    91
        History operation information.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    92
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    93
        The first argument should be an exception object related to the
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    94
        error encountered.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    95
        """
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    96
        pass
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
    97
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    98
class _HistoryOperation(object):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
    99
        """A _HistoryOperation object is a representation of data about an
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   100
        operation that a pkg(5) client has performed.  This class is private
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   101
        and not intended for use by classes other than History.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   102
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   103
        This class provides an abstraction layer between the stack of
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   104
        operations that History manages should these values need to be
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   105
        manipulated as they are set or retrieved.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   106
        """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   107
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   108
        def __setattr__(self, name, value):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   109
                if name not in ("result", "errors"):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   110
                        # Force all other attribute values to be a string
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   111
                        # to avoid issues with minidom.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   112
                        value = str(value)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   113
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   114
                return object.__setattr__(self, name, value)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   115
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   116
        def __str__(self):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   117
                return """\
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   118
Operation Name: %s
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   119
Operation Result: %s
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   120
Operation Start Time: %s
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   121
Operation End Time: %s
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   122
Operation Start State:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   123
%s
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   124
Operation End State:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   125
%s
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   126
Operation User: %s (%s)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   127
""" % (self.name, self.result, self.start_time, self.end_time,
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   128
    self.start_state, self.end_state, self.username, self.userid)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   129
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   130
        # All "time" values should be in UTC, using ISO 8601 as the format.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   131
        # Name of the operation performed (e.g. install, image-update, etc.).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   132
        name = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   133
        # When the operation started.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   134
        start_time = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   135
        # When the operation ended.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   136
        end_time = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   137
        # The starting state of the operation (e.g. image plan pre-evaluation).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   138
        start_state = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   139
        # The ending state of the operation (e.g. image plan post-evaluation).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   140
        end_state = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   141
        # Errors encountered during an operation.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   142
        errors = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   143
        # username of the user that performed the operation.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   144
        username = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   145
        # id of the user that performed the operation.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   146
        userid = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   147
        # The result of the operation (must be a list indicating (outcome,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   148
        # reason)).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   149
        result = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   150
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   151
        def __init__(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   152
                self.errors = []
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   153
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   154
class History(object):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   155
        """A History object is a representation of data about a pkg(5) client
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   156
        and about operations that the client is executing or has executed.  It
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   157
        uses the _HistoryOperation class to represent the data about an
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   158
        operation.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   159
        """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   160
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   161
        # The directory where the history directory can be found (or
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   162
        # created if it doesn't exist).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   163
        root_dir = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   164
        # The name of the client (e.g. pkg, packagemanager, etc.)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   165
        client_name = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   166
        # The version of the client (e.g. 093ca22da67c).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   167
        client_version = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   168
        # How the client was invoked (e.g. 'pkg install -n foo').
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   169
        client_args = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   170
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   171
        # A stack where operation data will actually be stored.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   172
        __operations = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   173
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   174
        # These attributes exist to fake access to the operations stack.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   175
        operation_name = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   176
        operation_username = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   177
        operation_userid = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   178
        operation_start_time = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   179
        operation_end_time = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   180
        operation_start_state = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   181
        operation_end_state = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   182
        operation_errors = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   183
        operation_result = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   184
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   185
        def __getattribute__(self, name):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   186
                if name == "client_args":
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   187
                        return object.__getattribute__(self, name)[:]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   188
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   189
                if not name.startswith("operation_"):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   190
                        return object.__getattribute__(self, name)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   191
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   192
                ops = object.__getattribute__(self, "_History__operations")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   193
                if not ops:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   194
                        return None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   195
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   196
                return getattr(ops[-1]["operation"], name[len("operation_"):])
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   197
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   198
        def __setattr__(self, name, value):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   199
                if name == "client_args":
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   200
                        raise AttributeError("'history' object attribute '%s' "
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   201
                            "is read-only." % name)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   202
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   203
                if not name.startswith("operation_"):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   204
                        return object.__setattr__(self, name, value)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   205
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   206
                ops = object.__getattribute__(self, "_History__operations")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   207
                if name == "operation_name":
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   208
                        if not ops:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   209
                                ops = []
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   210
                                object.__setattr__(self,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   211
                                    "_History__operations", ops)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   212
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   213
                        ops.append({
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   214
                            "pathname": None,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   215
                            "operation": _HistoryOperation()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   216
                        })
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   217
                elif not ops:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   218
                        raise AttributeError("'history' object attribute '%s' "
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   219
                            "cannot be set before 'operation_name'." % name)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   220
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   221
                op = ops[-1]["operation"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   222
                setattr(op, name[len("operation_"):], value)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   223
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   224
                # Access to the class attributes is done through object instead
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   225
                # of just referencing self to avoid any of the special logic in
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   226
                # place interfering with logic here.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   227
                if name == "operation_name":
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   228
                        # Mark the operation as having started and record
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   229
                        # other, relevant information.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   230
                        op.start_time = misc.time_to_timestamp(None)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   231
                        op.username = portable.get_username()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   232
                        op.userid = portable.get_userid()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   233
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   234
                        ca = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   235
                        if sys.argv[0]:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   236
                                ca = [sys.argv[0]]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   237
                        else:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   238
                                # Fallback for clients that provide no value.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   239
                                ca = [self.client_name]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   240
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   241
                        ca.extend(sys.argv[1:])
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   242
                        object.__setattr__(self, "client_args", ca)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   243
                        object.__setattr__(self, "client_version", pkg.VERSION)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   244
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   245
                elif name == "operation_result":
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   246
                        # Record when the operation ended.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   247
                        op.end_time = misc.time_to_timestamp(None)
556
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   248
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   249
                        # Some operations shouldn't be saved -- they're merely
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   250
                        # included in the stack for completeness or to support
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   251
                        # client functionality.
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   252
                        if op.name not in DISCARDED_OPERATIONS:
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   253
                                # Write current history and last operation to a
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   254
                                # file.
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   255
                                self.__save()
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   256
1c3526ca7b9e 2022 client should provide operational intent to server
Shawn Walker <shawn.walker@sun.com>
parents: 539
diff changeset
   257
                        # Discard it now that it is no longer needed.
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   258
                        del ops[-1]
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   259
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   260
        def __init__(self, root_dir=".", filename=None):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   261
                """'root_dir' should be the path of the directory where the
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   262
                history directory can be found (or created if it doesn't
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   263
                exist).  'filename' should be the name of an XML file
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   264
                containing serialized history information to load.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   265
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   266
                # Since this is a read-only attribute normally, we have to
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   267
                # bypass our setattr override by calling object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   268
                object.__setattr__(self, "client_args", [])
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   269
696
6bbfd2dece6f 4371 user-agent string needs to be different for different client front-ends
Danek Duvall <danek.duvall@sun.com>
parents: 593
diff changeset
   270
                # Initialize client_name to what the client thinks it is.  This
6bbfd2dece6f 4371 user-agent string needs to be different for different client front-ends
Danek Duvall <danek.duvall@sun.com>
parents: 593
diff changeset
   271
                # will be overridden if we load history entries off disk.
6bbfd2dece6f 4371 user-agent string needs to be different for different client front-ends
Danek Duvall <danek.duvall@sun.com>
parents: 593
diff changeset
   272
                self.client_name = pkg.client.global_settings.client_name
6bbfd2dece6f 4371 user-agent string needs to be different for different client front-ends
Danek Duvall <danek.duvall@sun.com>
parents: 593
diff changeset
   273
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   274
                self.root_dir = root_dir
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   275
                if filename:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   276
                        self.__load(filename)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   277
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   278
        def __str__(self):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   279
                ops = self.__operations
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   280
                return "\n".join([str(op["operation"]) for op in ops])
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   281
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   282
        @property
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   283
        def path(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   284
                """The directory where history files will be written to or
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   285
                read from.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   286
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   287
                return os.path.join(self.root_dir, "history")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   288
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   289
        @property
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   290
        def pathname(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   291
                """Returns the pathname that the history information was read
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   292
                from or will attempted to be written to.  Returns None if no
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   293
                operation has started yet or if no operation has been loaded.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   294
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   295
                if not self.operation_start_time:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   296
                        return None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   297
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   298
                ops = self.__operations
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   299
                pathname = ops[-1]["pathname"]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   300
                if not pathname:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   301
                        return os.path.join(self.path,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   302
                            "%s-01.xml" % ops[-1]["operation"].start_time)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   303
                return pathname
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   304
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   305
        def clear(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   306
                """Discards all information related to the current history
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   307
                object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   308
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   309
                self.client_name = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   310
                self.client_version = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   311
                object.__setattr__(self, "client_args", [])
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   312
                self.__operations = []
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   313
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   314
        def __load_client_data(self, node):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   315
                """Internal function to load the client data from the given XML
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   316
                'node' object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   317
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   318
                self.client_name = node.getAttribute("name")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   319
                self.client_version = node.getAttribute("version")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   320
                try:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   321
                        args = node.getElementsByTagName("args")[0]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   322
                except IndexError:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   323
                        # There might not be any.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   324
                        pass
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   325
                else:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   326
                        ca = object.__getattribute__(self, "client_args")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   327
                        for cnode in args.getElementsByTagName("arg"):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   328
                                ca.append(cnode.childNodes[0].wholeText)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   329
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   330
        @staticmethod
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   331
        def __load_operation_data(node):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   332
                """Internal function to load the operation data from the given
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   333
                XML 'node' object and return a _HistoryOperation object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   334
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   335
                op = _HistoryOperation()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   336
                op.name = node.getAttribute("name")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   337
                op.start_time = node.getAttribute("start_time")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   338
                op.end_time = node.getAttribute("end_time")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   339
                op.username = node.getAttribute("username")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   340
                op.userid = node.getAttribute("userid")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   341
                op.result = node.getAttribute("result").split(", ")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   342
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   343
                state = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   344
                try:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   345
                        state = node.getElementsByTagName("start_state")[0]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   346
                except IndexError:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   347
                        # The element might not exist.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   348
                        pass
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   349
                else:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   350
                        op.start_state = state.childNodes[0].wholeText
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   351
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   352
                try:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   353
                        state = node.getElementsByTagName("end_state")[0]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   354
                except IndexError:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   355
                        # The element might not exist.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   356
                        pass
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   357
                else:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   358
                        op.end_state = state.childNodes[0].wholeText
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   359
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   360
                errors = None
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   361
                try:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   362
                        errors = node.getElementsByTagName("errors")[0]
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   363
                except IndexError:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   364
                        # The element might not exist.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   365
                        pass
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   366
                else:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   367
                        for cnode in errors.getElementsByTagName("error"):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   368
                                op.errors.append(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   369
                                    cnode.childNodes[0].wholeText)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   370
                return op
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   371
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   372
        def __load(self, filename):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   373
                """Loads the history from a file located in self.path/history/
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   374
                {filename}.  The file should contain a serialized history
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   375
                object in XML format.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   376
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   377
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   378
                # Ensure all previous information is discarded.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   379
                self.clear()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   380
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   381
                try:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   382
                        pathname = os.path.join(self.path, filename)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   383
                        d = xmini.parse(pathname)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   384
                        root = d.documentElement
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   385
                        for cnode in root.childNodes:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   386
                                if cnode.nodeName == "client":
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   387
                                        self.__load_client_data(cnode)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   388
                                elif cnode.nodeName == "operation":
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   389
                                        # Operations load differently due to
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   390
                                        # the stack.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   391
                                        self.__operations.append({
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   392
                                            "pathname": pathname,
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   393
                                            "operation":
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   394
                                                self.__load_operation_data(
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   395
                                                cnode)
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   396
                                            })
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   397
                except KeyboardInterrupt:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   398
                        raise
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   399
                except Exception, e:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   400
                        raise HistoryLoadException(e)
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   401
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   402
        def __serialize_client_data(self, d):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   403
                """Internal function used to serialize current client data
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   404
                using the supplied 'd' (xml.dom.minidom) object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   405
                """
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   406
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   407
                assert self.client_name is not None
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   408
                assert self.client_version is not None
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   409
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   410
                root = d.documentElement
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   411
                client = d.createElement("client")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   412
                client.setAttribute("name", self.client_name)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   413
                client.setAttribute("version", self.client_version)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   414
                root.appendChild(client)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   415
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   416
                if self.client_args:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   417
                        args = d.createElement("args")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   418
                        client.appendChild(args)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   419
                        for entry in self.client_args:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   420
                                arg = d.createElement("arg")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   421
                                args.appendChild(arg)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   422
                                arg.appendChild(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   423
                                    d.createCDATASection(str(entry)))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   424
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   425
        def __serialize_operation_data(self, d):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   426
                """Internal function used to serialize current operation data
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   427
                using the supplied 'd' (xml.dom.minidom) object.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   428
                """
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   429
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   430
                if self.operation_userid is None:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   431
                        raise HistoryStoreException("Unable to determine the "
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   432
                            "id of the user that performed the current "
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   433
                            "operation; unable to store history information.")
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   434
                elif self.operation_username is None:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   435
                        raise HistoryStoreException("Unable to determine the "
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   436
                            "username of the user that performed the current "
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   437
                            "operation; unable to store history information.")
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   438
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   439
                root = d.documentElement
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   440
                op = d.createElement("operation")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   441
                op.setAttribute("name", self.operation_name)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   442
                # Must explictly convert values to a string due to minidom bug
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   443
                # that causes a fatal whenever using types other than str.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   444
                op.setAttribute("username", str(self.operation_username))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   445
                op.setAttribute("userid", str(self.operation_userid))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   446
                op.setAttribute("result", ", ".join(self.operation_result))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   447
                op.setAttribute("start_time", self.operation_start_time)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   448
                op.setAttribute("end_time", self.operation_end_time)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   449
                root.appendChild(op)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   450
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   451
                if self.operation_start_state:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   452
                        state = d.createElement("start_state")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   453
                        op.appendChild(state)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   454
                        state.appendChild(d.createCDATASection(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   455
                            str(self.operation_start_state)))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   456
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   457
                if self.operation_end_state:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   458
                        state = d.createElement("end_state")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   459
                        op.appendChild(state)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   460
                        state.appendChild(d.createCDATASection(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   461
                            str(self.operation_end_state)))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   462
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   463
                if self.operation_errors:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   464
                        errors = d.createElement("errors")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   465
                        op.appendChild(errors)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   466
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   467
                        for entry in self.operation_errors:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   468
                                error = d.createElement("error")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   469
                                errors.appendChild(error)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   470
                                error.appendChild(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   471
                                    d.createCDATASection(str(entry)))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   472
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   473
        def __save(self):
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   474
                """Serializes the current history information and writes it to
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   475
                a file in self.path/{operation_start_time}-{sequence}.xml.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   476
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   477
                d = xmini.Document()
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   478
                d.appendChild(d.createElement("history"))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   479
                self.__serialize_client_data(d)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   480
                self.__serialize_operation_data(d)
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   481
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   482
                if not os.path.exists(self.path):
593
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   483
                        try:
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   484
                                os.makedirs(self.path, mode=0755)
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   485
                        except EnvironmentError, e:
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   486
                                if e.errno not in (errno.EROFS,
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   487
                                    errno.EACCES):
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   488
                                        # Ignore read-only file system and
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   489
                                        # access errors as it isn't critical
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   490
                                        # to the image that this data is
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   491
                                        # written.
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   492
                                        raise HistoryStoreException(
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   493
                                            e)
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   494
                                # Return, since without the directory, the rest
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   495
                                # of this will fail.
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   496
                                return
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   497
                        except KeyboardInterrupt:
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   498
                                raise
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   499
                        except Exception, e:
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   500
                                raise HistoryStoreException(e)
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   501
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   502
                # Repeatedly attempt to write the history (only if it's because
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   503
                # the file already exists).  This is necessary due to multiple
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   504
                # operations possibly occuring within the same second (but not
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   505
                # microsecond).
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   506
                pathname = self.pathname
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   507
                for i in range(1, 100):
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   508
                        try:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   509
                                f = os.fdopen(os.open(pathname,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   510
                                    os.O_CREAT|os.O_EXCL|os.O_WRONLY), "w")
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   511
                                d.writexml(f,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   512
                                    encoding=sys.getdefaultencoding())
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   513
                                f.close()
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   514
                                return
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   515
                        except EnvironmentError, e:
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   516
                                if e.errno == errno.EEXIST:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   517
                                        name, ext = os.path.splitext(
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   518
                                            os.path.basename(pathname))
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   519
                                        name = name.split("-", 1)[0]
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   520
                                        # Pick the next name in our sequence
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   521
                                        # and try again.
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   522
                                        pathname = os.path.join(self.path,
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   523
                                            "%s-%02d%s" % (name, i + 1, ext))
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   524
                                        continue
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   525
                                elif e.errno not in (errno.EROFS,
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   526
                                    errno.EACCES):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   527
                                        # Ignore read-only file system and
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   528
                                        # access errors as it isn't critical
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   529
                                        # to the image that this data is
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   530
                                        # written.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   531
                                        raise HistoryStoreException(
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   532
                                            e)
593
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   533
                                # For all other failures, return, and avoid any
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   534
                                # further attempts.
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   535
                                return
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   536
                        except KeyboardInterrupt:
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   537
                                raise
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   538
                        except Exception, e:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   539
                                raise HistoryStoreException(e)
593
f4305c5f2602 3823 no perms to create /var/pkg/history results in stack trace, even with -n
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 569
diff changeset
   540
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   541
        def purge(self):
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   542
                """Removes all history information by deleting the directory
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   543
                indicated by the value self.path and then creates a new history
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   544
                entry to record that this purge occurred.
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   545
                """
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   546
                self.operation_name = "purge-history"
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   547
                try:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   548
                        shutil.rmtree(self.path)
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   549
                except KeyboardInterrupt:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   550
                        raise
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   551
                except Exception, e:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   552
                        raise HistoryPurgeException(e)
539
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   553
                else:
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   554
                        self.operation_result = RESULT_SUCCEEDED
7486304966c5 1449 pkg requires operational history
Shawn Walker <shawn.walker@sun.com>
parents:
diff changeset
   555
569
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   556
        def abort(self, result):
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   557
                """Intended to be used by the client during top-level error
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   558
                handling to indicate that an unrecoverable error occurred
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   559
                during the current operation(s).  This allows History to end
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   560
                all of the current operations properly and handle any possible
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   561
                errors that might be encountered in History itself.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   562
                """
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   563
                try:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   564
                        # Ensure that all operations in the current stack are
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   565
                        # ended properly.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   566
                        while self.operation_name:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   567
                                self.operation_result = result
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   568
                except HistoryStoreException:
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   569
                        # Ignore storage errors as it's likely that whatever
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   570
                        # caused the client to abort() also caused the storage
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   571
                        # of the history information to fail.
48f7e9d2b1ac 3715 permission or read-only filesystem errors cause history traceback
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 556
diff changeset
   572
                        return