src/modules/misc.py
author thejaswini.k@oracle.com
Tue, 23 Oct 2012 11:20:24 +0530
changeset 2820 8c33db61e6a6
parent 2764 7220dcd7755f
child 2826 cae308eb6426
permissions -rw-r--r--
7197510 pkgrecv error handling traces back 7197865 pkgrecv doesn't cleanup temporary files when failing due to space errors
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1516
8c950a3b4171 10485 move pkg(5) to Python 2.6
Rich Burridge <rich.burridge@sun.com>
parents: 1507
diff changeset
     1
#!/usr/bin/python
46
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
     2
#
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
     3
# CDDL HEADER START
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
     4
#
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
     5
# The contents of this file are subject to the terms of the
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
     6
# Common Development and Distribution License (the "License").
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
     7
# You may not use this file except in compliance with the License.
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
     8
#
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
     9
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    10
# or http://www.opensolaris.org/os/licensing.
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    11
# See the License for the specific language governing permissions
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    12
# and limitations under the License.
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    13
#
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    14
# When distributing Covered Code, include this CDDL HEADER in each
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    15
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    16
# If applicable, add the following below this CDDL HEADER, with the
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    17
# fields enclosed by brackets "[]" replaced with your own identifying
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    18
# information: Portions Copyright [yyyy] [name of copyright owner]
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    19
#
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    20
# CDDL HEADER END
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    21
#
86a10bea7bb6 commonize file/ prefix handling; sketch manifest difference algorithm
Stephen Hahn <sch@sun.com>
parents:
diff changeset
    22
2616
3c00fe4465d3 19148 PKG_STATE_* defines need a new home
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2580
diff changeset
    23
# Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
119
537d69114be4 Implement bundled file downloads using filelist
johansen <johansen@sun.com>
parents: 108
diff changeset
    24
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    25
"""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    26
Misc utility functions used by the packaging system.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    27
"""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    28
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    29
import OpenSSL.crypto as osc
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    30
import cStringIO
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
    31
import calendar
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    32
import collections
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
    33
import datetime
384
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
    34
import errno
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    35
import getopt
1516
8c950a3b4171 10485 move pkg(5) to Python 2.6
Rich Burridge <rich.burridge@sun.com>
parents: 1507
diff changeset
    36
import hashlib
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    37
import itertools
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
    38
import locale
119
537d69114be4 Implement bundled file downloads using filelist
johansen <johansen@sun.com>
parents: 108
diff changeset
    39
import os
386
5cd680466abe 2147 depot should check port instead of showing traceback if port binding fails
Shawn Walker <swalker@opensolaris.org>
parents: 384
diff changeset
    40
import platform
5cd680466abe 2147 depot should check port instead of showing traceback if port binding fails
Shawn Walker <swalker@opensolaris.org>
parents: 384
diff changeset
    41
import re
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    42
import resource
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
    43
import shutil
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
    44
import simplejson as json
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
    45
import socket
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
    46
import struct
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
    47
import sys
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    48
import threading
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
    49
import time
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    50
import traceback
145
08bee7fd13f6 Add versioning to the protocols
Danek Duvall <danek.duvall@sun.com>
parents: 119
diff changeset
    51
import urllib
08bee7fd13f6 Add versioning to the protocols
Danek Duvall <danek.duvall@sun.com>
parents: 119
diff changeset
    52
import urlparse
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
    53
import zlib
462
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
    54
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    55
from stat import S_IFMT, S_IMODE, S_IRGRP, S_IROTH, S_IRUSR, S_IRWXU, \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    56
    S_ISBLK, S_ISCHR, S_ISDIR, S_ISFIFO, S_ISLNK, S_ISREG, S_ISSOCK, \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    57
    S_IWUSR, S_IXGRP, S_IXOTH
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    58
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
    59
import pkg.client.api_errors as api_errors
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
    60
import pkg.portable as portable
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
    61
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    62
from pkg import VERSION
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    63
from pkg.client import global_settings
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    64
from pkg.client.debugvalues import DebugValues
319
39b8b5c538bf 1104 want custom user-agent string
Danek Duvall <danek.duvall@sun.com>
parents: 310
diff changeset
    65
from pkg.client.imagetypes import img_type_names, IMG_NONE
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    66
from pkg.pkggzip import PkgGzipFile
310
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
    67
2820
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
    68
# Default path where the temporary directories will be created.
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
    69
DEFAULT_TEMP_PATH = "/var/tmp"
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
    70
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
    71
# Minimum number of days to issue warning before a certificate expires
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
    72
MIN_WARN_DAYS = datetime.timedelta(days=30)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
    73
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
    74
# Constant string used across many modules as a property name.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
    75
SIGNATURE_POLICY = "signature-policy"
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
    76
2569
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    77
# Bug URI Constants (deprecated)
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    78
# Line too long; pylint: disable-msg=C0301
2126
7f6719cd083b 14316 please suggest the proper classification and product in the pkg error message
Shawn Walker <shawn.walker@oracle.com>
parents: 2112
diff changeset
    79
BUG_URI_CLI = "https://defect.opensolaris.org/bz/enter_bug.cgi?product=pkg&component=cli"
7f6719cd083b 14316 please suggest the proper classification and product in the pkg error message
Shawn Walker <shawn.walker@oracle.com>
parents: 2112
diff changeset
    80
BUG_URI_GUI = "https://defect.opensolaris.org/bz/enter_bug.cgi?product=pkg&component=gui"
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    81
# pylint: enable-msg=C0301
2126
7f6719cd083b 14316 please suggest the proper classification and product in the pkg error message
Shawn Walker <shawn.walker@oracle.com>
parents: 2112
diff changeset
    82
2569
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    83
# Traceback message.
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    84
def get_traceback_message():
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    85
        """This function returns the standard traceback message.  A function
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    86
        is necessary since the _() call must be done at runtime after locale
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    87
        setup."""
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    88
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    89
        return _("""\n
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    90
This is an internal error in pkg(5) version %(version)s.  Please log a
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    91
Service Request about this issue including the information above and this
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    92
message.""") % { "version": VERSION }
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    93
1023
73f73773b4c9 6358 Release Notes message after Update All needs to be updated
John Rice <john.rice@sun.com>
parents: 956
diff changeset
    94
def get_release_notes_url():
73f73773b4c9 6358 Release Notes message after Update All needs to be updated
John Rice <john.rice@sun.com>
parents: 956
diff changeset
    95
        """Return a release note URL pointing to the correct release notes
73f73773b4c9 6358 Release Notes message after Update All needs to be updated
John Rice <john.rice@sun.com>
parents: 956
diff changeset
    96
           for this version"""
73f73773b4c9 6358 Release Notes message after Update All needs to be updated
John Rice <john.rice@sun.com>
parents: 956
diff changeset
    97
73f73773b4c9 6358 Release Notes message after Update All needs to be updated
John Rice <john.rice@sun.com>
parents: 956
diff changeset
    98
        # TBD: replace with a call to api.info() that can return a "release"
73f73773b4c9 6358 Release Notes message after Update All needs to be updated
John Rice <john.rice@sun.com>
parents: 956
diff changeset
    99
        # attribute of form YYYYMM against the SUNWsolnm package
2566
32ef5a3b4c12 18917 pkg update's release note URL in need of an update
david.comay@oracle.com
parents: 2510
diff changeset
   100
        return "http://www.oracle.com/pls/topic/lookup?ctx=E23824&id=SERNS"
1023
73f73773b4c9 6358 Release Notes message after Update All needs to be updated
John Rice <john.rice@sun.com>
parents: 956
diff changeset
   101
443
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   102
def time_to_timestamp(t):
462
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   103
        """convert seconds since epoch to %Y%m%dT%H%M%SZ format"""
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   104
        # XXX optimize?; pylint: disable-msg=W0511
443
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   105
        return time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(t))
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   106
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   107
def timestamp_to_time(ts):
462
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   108
        """convert %Y%m%dT%H%M%SZ format to seconds since epoch"""
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   109
        # XXX optimize?; pylint: disable-msg=W0511
443
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   110
        return calendar.timegm(time.strptime(ts, "%Y%m%dT%H%M%SZ"))
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   111
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2126
diff changeset
   112
def timestamp_to_datetime(ts):
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2126
diff changeset
   113
        """convert %Y%m%dT%H%M%SZ format to a datetime object"""
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2126
diff changeset
   114
        return datetime.datetime.strptime(ts,"%Y%m%dT%H%M%SZ")
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2126
diff changeset
   115
462
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   116
def copyfile(src_path, dst_path):
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   117
        """copy a file, preserving attributes, ownership, etc. where possible"""
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   118
        fs = os.lstat(src_path)
462
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   119
        shutil.copy2(src_path, dst_path)
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   120
        try:
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   121
                portable.chown(dst_path, fs.st_uid, fs.st_gid)
462
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   122
        except OSError, e:
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   123
                if e.errno != errno.EPERM:
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   124
                        raise
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   125
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   126
def copytree(src, dst):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   127
        """Rewrite of shutil.copytree() that can handle special files such as
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   128
        FIFOs, sockets, and device nodes.  It re-creates all symlinks rather
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   129
        than copying the data behind them, and supports neither the 'symlinks'
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   130
        nor the 'ignore' keyword arguments of the shutil version.
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   131
        """
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   132
2756
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   133
        problem = None
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   134
        os.makedirs(dst, PKG_DIR_MODE)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   135
        src_stat = os.stat(src)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   136
        for name in sorted(os.listdir(src)):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   137
                s_path = os.path.join(src, name)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   138
                d_path = os.path.join(dst, name)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   139
                s = os.lstat(s_path)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   140
                if S_ISDIR(s.st_mode):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   141
                        copytree(s_path, d_path)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   142
                        os.chmod(d_path, S_IMODE(s.st_mode))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   143
                        os.chown(d_path, s.st_uid, s.st_gid)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   144
                        os.utime(d_path, (s.st_atime, s.st_mtime))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   145
                elif S_ISREG(s.st_mode):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   146
                        shutil.copyfile(s_path, d_path)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   147
                        os.chmod(d_path, S_IMODE(s.st_mode))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   148
                        os.chown(d_path, s.st_uid, s.st_gid)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   149
                        os.utime(d_path, (s.st_atime, s.st_mtime))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   150
                elif S_ISLNK(s.st_mode):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   151
                        os.symlink(os.readlink(s_path), d_path)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   152
                elif S_ISFIFO(s.st_mode):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   153
                        os.mkfifo(d_path, S_IMODE(s.st_mode))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   154
                        os.chown(d_path, s.st_uid, s.st_gid)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   155
                        os.utime(d_path, (s.st_atime, s.st_mtime))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   156
                elif S_ISSOCK(s.st_mode):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   157
                        sock = socket.socket(socket.AF_UNIX)
2756
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   158
                        # The s11 fcs version of python doesn't have os.mknod()
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   159
                        # but sock.bind has a path length limitation that we can
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   160
                        # hit when archiving the test suite.
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   161
                        # E1101 Module '%s' has no '%s' member
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   162
                        # pylint: disable-msg=E1101
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   163
                        if hasattr(os, "mknod"):
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   164
                                os.mknod(d_path, s.st_mode, s.st_dev)
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   165
                        else:
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   166
                                try:
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   167
                                        sock.bind(d_path)
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   168
                                        sock.close()
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   169
                                except sock.error, _e:
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   170
                                        # Store original exception so that the
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   171
                                        # real cause of failure can be raised if
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   172
                                        # this fails.
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   173
                                        problem = sys.exc_info()
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   174
                                        continue
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   175
                        os.chown(d_path, s.st_uid, s.st_gid)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   176
                        os.utime(d_path, (s.st_atime, s.st_mtime))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   177
                elif S_ISCHR(s.st_mode) or S_ISBLK(s.st_mode):
2696
455351680ab0 7140127 pkg update with zones takes too long (fix pylint)
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2693
diff changeset
   178
                        # the s11 fcs version of python doesn't have os.mknod()
455351680ab0 7140127 pkg update with zones takes too long (fix pylint)
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2693
diff changeset
   179
                        # E1101 Module '%s' has no '%s' member
455351680ab0 7140127 pkg update with zones takes too long (fix pylint)
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2693
diff changeset
   180
                        # pylint: disable-msg=E1101
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   181
                        if hasattr(os, "mknod"):
2620
12fff26f76ad 7133778 zones p2v failing due to pkg error
Danek Duvall <danek.duvall@oracle.com>
parents: 2617
diff changeset
   182
                                os.mknod(d_path, s.st_mode, s.st_dev)
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   183
                                os.chown(d_path, s.st_uid, s.st_gid)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   184
                                os.utime(d_path, (s.st_atime, s.st_mtime))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   185
                elif S_IFMT(s.st_mode) == 0xd000: # doors
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   186
                        pass
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   187
                elif S_IFMT(s.st_mode) == 0xe000: # event ports
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   188
                        pass
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   189
                else:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   190
                        print "unknown file type:", oct(S_IFMT(s.st_mode))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   191
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   192
        os.chmod(dst, S_IMODE(src_stat.st_mode))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   193
        os.chown(dst, src_stat.st_uid, src_stat.st_gid)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   194
        os.utime(dst, (src_stat.st_atime, src_stat.st_mtime))
2756
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   195
        if problem:
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   196
                raise problem[0], problem[1], problem[2]
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   197
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   198
def move(src, dst):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   199
        """Rewrite of shutil.move() that uses our copy of copytree()."""
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   200
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   201
        # If dst is a directory, then we try to move src into it.
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   202
        if os.path.isdir(dst):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   203
                dst = os.path.join(dst,
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   204
                    os.path.basename(src).rstrip(os.path.sep))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   205
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   206
        try:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   207
                os.rename(src, dst)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   208
        except EnvironmentError, e:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   209
                s = os.lstat(src)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   210
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   211
                if e.errno == errno.EXDEV:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   212
                        if S_ISDIR(s.st_mode):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   213
                                copytree(src, dst)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   214
                                shutil.rmtree(src)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   215
                        else:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   216
                                shutil.copyfile(src, dst)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   217
                                os.chmod(dst, S_IMODE(s.st_mode))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   218
                                os.chown(dst, s.st_uid, s.st_gid)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   219
                                os.utime(dst, (s.st_atime, s.st_mtime))
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   220
                                os.unlink(src)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   221
                elif e.errno == errno.EINVAL and S_ISDIR(s.st_mode):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   222
                        raise shutil.Error, "Cannot move a directory '%s' " \
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   223
                            "into itself '%s'." % (src, dst)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   224
                elif e.errno == errno.ENOTDIR and S_ISDIR(s.st_mode):
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   225
                        raise shutil.Error, "Destination path '%s' already " \
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   226
                            "exists" % dst
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   227
                else:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   228
                        raise
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   229
956
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   230
def expanddirs(dirs):
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   231
        """given a set of directories, return expanded set that includes
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   232
        all components"""
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   233
        out = set()
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   234
        for d in dirs:
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   235
                p = d
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   236
                while p != "":
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   237
                        out.add(p)
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   238
                        p = os.path.dirname(p)
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   239
        return out
462
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   240
310
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   241
def url_affix_trailing_slash(u):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   242
        """if 'u' donesn't have a trailing '/', append one."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   243
310
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   244
        if u[-1] != '/':
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   245
                u = u + '/'
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   246
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   247
        return u
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   248
696
6bbfd2dece6f 4371 user-agent string needs to be different for different client front-ends
Danek Duvall <danek.duvall@sun.com>
parents: 670
diff changeset
   249
_client_version = "pkg/%s (%s %s; %s %s; %%s; %%s)" % \
319
39b8b5c538bf 1104 want custom user-agent string
Danek Duvall <danek.duvall@sun.com>
parents: 310
diff changeset
   250
    (VERSION, portable.util.get_canonical_os_name(), platform.machine(),
39b8b5c538bf 1104 want custom user-agent string
Danek Duvall <danek.duvall@sun.com>
parents: 310
diff changeset
   251
    portable.util.get_os_release(), platform.version())
39b8b5c538bf 1104 want custom user-agent string
Danek Duvall <danek.duvall@sun.com>
parents: 310
diff changeset
   252
1191
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   253
def user_agent_str(img, client_name):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   254
        """Return a string that can use to identify the client."""
1191
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   255
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   256
        if not img or img.type is None:
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   257
                imgtype = IMG_NONE
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   258
        else:
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   259
                imgtype = img.type
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   260
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   261
        useragent = _client_version % (img_type_names[imgtype], client_name)
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   262
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   263
        return useragent
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   264
2617
062f7d8b7568 7116135 pkg requires first portion of hostname in publisher URI to have more than one character
Saurabh Vyas <saurabh.vyas@oracle.com>
parents: 2616
diff changeset
   265
# Valid hostname can be : HOSTNAME or IPv4 addr or IPV6 addr
062f7d8b7568 7116135 pkg requires first portion of hostname in publisher URI to have more than one character
Saurabh Vyas <saurabh.vyas@oracle.com>
parents: 2616
diff changeset
   266
_hostname_re = re.compile(r"""^(?:[a-zA-Z0-9\-]+[a-zA-Z0-9\-\.]*
062f7d8b7568 7116135 pkg requires first portion of hostname in publisher URI to have more than one character
Saurabh Vyas <saurabh.vyas@oracle.com>
parents: 2616
diff changeset
   267
                   |(?:\d{1,3}\.){3}\d{3}
062f7d8b7568 7116135 pkg requires first portion of hostname in publisher URI to have more than one character
Saurabh Vyas <saurabh.vyas@oracle.com>
parents: 2616
diff changeset
   268
                   |\[([a-fA-F0-9\.]*:){,7}[a-fA-F0-9\.]+\])$""", re.X)
062f7d8b7568 7116135 pkg requires first portion of hostname in publisher URI to have more than one character
Saurabh Vyas <saurabh.vyas@oracle.com>
parents: 2616
diff changeset
   269
2230
76f30e842e82 15750 depot bind address should be configurable
Shawn Walker <shawn.walker@oracle.com>
parents: 2219
diff changeset
   270
_invalid_host_chars = re.compile(".*[^a-zA-Z0-9\-\.:\[\]]+")
1895
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   271
_valid_proto = ["file", "http", "https"]
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   272
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   273
def valid_pub_prefix(prefix):
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   274
        """Verify that the publisher prefix only contains valid characters."""
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   275
1142
64d164b5bf04 9164 misc.valid_pub_prefix and misc.valid_pub_url should handle None
Shawn Walker <srw@sun.com>
parents: 1072
diff changeset
   276
        if not prefix:
64d164b5bf04 9164 misc.valid_pub_prefix and misc.valid_pub_url should handle None
Shawn Walker <srw@sun.com>
parents: 1072
diff changeset
   277
                return False
64d164b5bf04 9164 misc.valid_pub_prefix and misc.valid_pub_url should handle None
Shawn Walker <srw@sun.com>
parents: 1072
diff changeset
   278
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   279
        # This is a workaround for the the hostname_re being slow when
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   280
        # it comes to finding invalid characters in the prefix string.
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   281
        if _invalid_host_chars.match(prefix):
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   282
                # prefix bad chars
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   283
                return False
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   284
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   285
        if _hostname_re.match(prefix):
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   286
                return True
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   287
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   288
        return False
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   289
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   290
def valid_pub_url(url, proxy=False):
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   291
        """Verify that the publisher URL contains only valid characters.
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   292
        If 'proxy' is set to True, some checks are relaxed."""
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   293
1142
64d164b5bf04 9164 misc.valid_pub_prefix and misc.valid_pub_url should handle None
Shawn Walker <srw@sun.com>
parents: 1072
diff changeset
   294
        if not url:
64d164b5bf04 9164 misc.valid_pub_prefix and misc.valid_pub_url should handle None
Shawn Walker <srw@sun.com>
parents: 1072
diff changeset
   295
                return False
64d164b5bf04 9164 misc.valid_pub_prefix and misc.valid_pub_url should handle None
Shawn Walker <srw@sun.com>
parents: 1072
diff changeset
   296
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   297
        # First split the URL and check if the scheme is one we support
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   298
        o = urlparse.urlsplit(url)
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   299
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   300
        if not o[0] in _valid_proto:
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   301
                return False
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   302
1895
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   303
        if o[0] == "file":
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   304
                path = urlparse.urlparse(url, "file", allow_fragments=0)[2]
1895
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   305
                path = urllib.url2pathname(path)
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   306
                if not os.path.abspath(path):
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   307
                        return False
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   308
                # No further validation to be done.
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   309
                return True
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   310
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   311
        # Next verify that the network location is valid
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   312
        host = urllib.splitport(o[1])[0]
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   313
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   314
        if proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   315
                # We may have authentication details in the proxy URI, which
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   316
                # we must ignore when checking for hostname validity.
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   317
                host_parts = host.split("@")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   318
                if len(host_parts) == 2:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   319
                        host = host[1]
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   320
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   321
        if not host or _invalid_host_chars.match(host):
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   322
                return False
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   323
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   324
        if _hostname_re.match(host):
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   325
                return True
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   326
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   327
        return False
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   328
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   329
def gunzip_from_stream(gz, outfile):
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   330
        """Decompress a gzipped input stream into an output stream.
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   331
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   332
        The argument 'gz' is an input stream of a gzipped file and 'outfile'
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   333
        is is an output stream.  gunzip_from_stream() decompresses data from
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   334
        'gz' and writes it to 'outfile', and returns the hexadecimal SHA-1 sum
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   335
        of that data.
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   336
        """
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   337
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   338
        FHCRC = 2
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   339
        FEXTRA = 4
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   340
        FNAME = 8
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   341
        FCOMMENT = 16
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   343
        # Read the header
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   344
        magic = gz.read(2)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   345
        if magic != "\037\213":
621
6c144915eed1 551 IPS should handle socket errors
johansen <johansen@sun.com>
parents: 576
diff changeset
   346
                raise zlib.error, "Not a gzipped file"
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   347
        method = ord(gz.read(1))
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   348
        if method != 8:
621
6c144915eed1 551 IPS should handle socket errors
johansen <johansen@sun.com>
parents: 576
diff changeset
   349
                raise zlib.error, "Unknown compression method"
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   350
        flag = ord(gz.read(1))
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   351
        gz.read(6) # Discard modtime, extraflag, os
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   352
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   353
        # Discard an extra field
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   354
        if flag & FEXTRA:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   355
                xlen = ord(gz.read(1))
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   356
                xlen = xlen + 256 * ord(gz.read(1))
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   357
                gz.read(xlen)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   358
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   359
        # Discard a null-terminated filename
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   360
        if flag & FNAME:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   361
                while True:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   362
                        s = gz.read(1)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   363
                        if not s or s == "\000":
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   364
                                break
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   365
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   366
        # Discard a null-terminated comment
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   367
        if flag & FCOMMENT:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   368
                while True:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   369
                        s = gz.read(1)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   370
                        if not s or s == "\000":
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   371
                                break
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   372
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   373
        # Discard a 16-bit CRC
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   374
        if flag & FHCRC:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   375
                gz.read(2)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   376
1516
8c950a3b4171 10485 move pkg(5) to Python 2.6
Rich Burridge <rich.burridge@sun.com>
parents: 1507
diff changeset
   377
        shasum = hashlib.sha1()
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   378
        dcobj = zlib.decompressobj(-zlib.MAX_WBITS)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   379
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   380
        while True:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   381
                buf = gz.read(64 * 1024)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   382
                if buf == "":
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   383
                        ubuf = dcobj.flush()
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   384
                        shasum.update(ubuf)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   385
                        outfile.write(ubuf)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   386
                        break
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   387
                ubuf = dcobj.decompress(buf)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   388
                shasum.update(ubuf)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   389
                outfile.write(ubuf)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   390
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   391
        return shasum.hexdigest()
384
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   392
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   393
class PipeError(Exception):
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   394
        """ Pipe exception. """
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   395
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   396
        def __init__(self, args=None):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   397
                Exception.__init__(self)
1516
8c950a3b4171 10485 move pkg(5) to Python 2.6
Rich Burridge <rich.burridge@sun.com>
parents: 1507
diff changeset
   398
                self._args = args
384
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   399
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   400
def msg(*text):
429
6c9cbb6e6600 983 pkg search returns just one action per package/token-type combo
Brock Pytlik <bpytlik@sun.com>
parents: 409
diff changeset
   401
        """ Emit a message. """
384
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   402
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   403
        try:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   404
                print ' '.join([str(l) for l in text])
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   405
        except IOError, e:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   406
                if e.errno == errno.EPIPE:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   407
                        raise PipeError, e
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   408
                raise
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   409
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   410
def emsg(*text):
429
6c9cbb6e6600 983 pkg search returns just one action per package/token-type combo
Brock Pytlik <bpytlik@sun.com>
parents: 409
diff changeset
   411
        """ Emit a message to sys.stderr. """
384
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   412
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   413
        try:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   414
                print >> sys.stderr, ' '.join([str(l) for l in text])
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   415
        except IOError, e:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   416
                if e.errno == errno.EPIPE:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   417
                        raise PipeError, e
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   418
                raise
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   419
791
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   420
def setlocale(category, loc=None, printer=None):
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   421
        """Wraps locale.setlocale(), falling back to the C locale if the desired
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   422
        locale is broken or unavailable.  The 'printer' parameter should be a
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   423
        function which takes a string and displays it.  If 'None' (the default),
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   424
        setlocale() will print the message to stderr."""
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   425
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   426
        if printer is None:
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   427
                printer = emsg
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   428
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   429
        try:
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   430
                locale.setlocale(category, loc)
1318
a12f41da2189 8263 misc.setlocale should handle locale.getdefaultlocale failure
Tom Mueller <Tom.Mueller@sun.com>
parents: 1191
diff changeset
   431
                # Because of Python bug 813449, getdefaultlocale may fail
a12f41da2189 8263 misc.setlocale should handle locale.getdefaultlocale failure
Tom Mueller <Tom.Mueller@sun.com>
parents: 1191
diff changeset
   432
                # with a ValueError even if setlocale succeeds. So we call
1507
b956ea23d3a6 11735 pkg history data files should not be executable
Richard Lowe <richlowe@richlowe.net>
parents: 1505
diff changeset
   433
                # it here to prevent having this error raised if it is
1318
a12f41da2189 8263 misc.setlocale should handle locale.getdefaultlocale failure
Tom Mueller <Tom.Mueller@sun.com>
parents: 1191
diff changeset
   434
                # called later by other non-pkg(5) code.
a12f41da2189 8263 misc.setlocale should handle locale.getdefaultlocale failure
Tom Mueller <Tom.Mueller@sun.com>
parents: 1191
diff changeset
   435
                locale.getdefaultlocale()
a12f41da2189 8263 misc.setlocale should handle locale.getdefaultlocale failure
Tom Mueller <Tom.Mueller@sun.com>
parents: 1191
diff changeset
   436
        except (locale.Error, ValueError):
791
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   437
                try:
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   438
                        dl = " '%s.%s'" % locale.getdefaultlocale()
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   439
                except ValueError:
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   440
                        dl = ""
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   441
                printer("Unable to set locale%s; locale package may be broken "
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   442
                    "or\nnot installed.  Reverting to C locale." % dl)
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   443
                locale.setlocale(category, "C")
2197
f0a833446218 13054 solver could emit a directed error message for over-revved incorporated pkgs
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2135
diff changeset
   444
def N_(message):
f0a833446218 13054 solver could emit a directed error message for over-revved incorporated pkgs
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2135
diff changeset
   445
        """Return its argument; used to mark strings for localization when
f0a833446218 13054 solver could emit a directed error message for over-revved incorporated pkgs
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2135
diff changeset
   446
        their use is delayed by the program."""
f0a833446218 13054 solver could emit a directed error message for over-revved incorporated pkgs
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2135
diff changeset
   447
        return message
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
   448
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   449
def bytes_to_str(nbytes, fmt="%(num).2f %(unit)s"):
451
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   450
        """Returns a human-formatted string representing the number of bytes
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   451
        in the largest unit possible.
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   452
        
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   453
        If provided, 'fmt' should be a string which can be formatted
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   454
        with a dictionary containing a float 'num' and strings 'unit' and
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   455
        'shortunit'.  The default format prints, for example, '3.23 MB' """
451
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   456
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   457
        units = [
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   458
            (_("B"), _("B"), 2**10),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   459
            (_("kB"), _("k"), 2**20),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   460
            (_("MB"), _("M"), 2**30),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   461
            (_("GB"), _("G"), 2**40),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   462
            (_("TB"), _("T"), 2**50),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   463
            (_("PB"), _("P"), 2**60),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   464
            (_("EB"), _("E"), 2**70)
451
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   465
        ]
386
5cd680466abe 2147 depot should check port instead of showing traceback if port binding fails
Shawn Walker <swalker@opensolaris.org>
parents: 384
diff changeset
   466
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   467
        for uom, shortuom, limit in units:
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   468
                if uom != _("EB") and nbytes >= limit:
451
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   469
                        # Try the next largest unit of measure unless this is
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   470
                        # the largest or if the byte size is within the current
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   471
                        # unit of measure's range.
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   472
                        continue
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   473
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   474
                return fmt % {
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   475
                    "num": round(nbytes / float(limit / 2**10), 2),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   476
                    "unit": uom,
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   477
                    "shortunit": shortuom
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   478
                }
451
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   479
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
   480
def get_rel_path(request, uri, pub=None):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   481
        """Calculate the depth of the current request path relative to our
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   482
        base uri. path_info always ends with a '/' -- so ignore it when
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   483
        calculating depth."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   484
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
   485
        rpath = request.path_info
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
   486
        if pub:
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
   487
                rpath = rpath.replace("/%s/" % pub, "/")
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
   488
        depth = rpath.count("/") - 1
451
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   489
        return ("../" * depth) + uri
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   490
487
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   491
def get_pkg_otw_size(action):
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   492
        """Takes a file action and returns the over-the-wire size of
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   493
        a package as an integer.  The OTW size is the compressed size,
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   494
        pkg.csize.  If that value isn't available, it returns pkg.size.
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   495
        If pkg.size isn't available, return zero."""
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   496
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   497
        size = action.attrs.get("pkg.csize", 0)
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   498
        if size == 0:
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   499
                size = action.attrs.get("pkg.size", 0)
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   500
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   501
        return int(size)
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   502
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   503
def get_data_digest(data, length=None, return_content=False):
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   504
        """Returns a tuple of (SHA-1 hexdigest, content).
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   505
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   506
        'data' should be a file-like object or a pathname to a file.
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   507
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   508
        'length' should be an integer value representing the size of
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   509
        the contents of data in bytes.
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   510
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   511
        'return_content' is a boolean value indicating whether the
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   512
        second tuple value should contain the content of 'data' or
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   513
        if the content should be discarded during processing."""
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   514
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   515
        bufsz = 128 * 1024
1974
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   516
        closefobj = False
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   517
        if isinstance(data, basestring):
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   518
                f = file(data, "rb", bufsz)
1974
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   519
                closefobj = True
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   520
        else:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   521
                f = data
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   522
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   523
        if length is None:
1038
2514d758e462 7908 publishing api and tools should use os.stat not os.lstat for file size
Shawn Walker <srw@sun.com>
parents: 1035
diff changeset
   524
                length = os.stat(data).st_size
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   525
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   526
        # Read the data in chunks and compute the SHA1 hash as it comes in.  A
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   527
        # large read on some platforms (e.g. Windows XP) may fail.
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   528
        content = cStringIO.StringIO()
1516
8c950a3b4171 10485 move pkg(5) to Python 2.6
Rich Burridge <rich.burridge@sun.com>
parents: 1507
diff changeset
   529
        fhash = hashlib.sha1()
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   530
        while length > 0:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   531
                data = f.read(min(bufsz, length))
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   532
                if return_content:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   533
                        content.write(data)
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   534
                fhash.update(data)
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   535
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   536
                l = len(data)
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   537
                if l == 0:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   538
                        break
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   539
                length -= l
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   540
        content.reset()
1974
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   541
        if closefobj:
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   542
                f.close()
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   543
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   544
        return fhash.hexdigest(), content.read()
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   545
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   546
def compute_compressed_attrs(fname, file_path, data, size, compress_dir,
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   547
    bufsz=64*1024):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   548
        """Returns the size and hash of the compressed data.  If the file
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   549
        located at file_path doesn't exist or isn't gzipped, it creates a file
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   550
        in compress_dir named fname."""
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   551
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   552
        #
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   553
        # This check prevents compressing a file which is already compressed.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   554
        # This takes CPU load off the depot on large imports of mostly-the-same
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   555
        # stuff.  And in general it saves disk bandwidth, and on ZFS in
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   556
        # particular it saves us space in differential snapshots.  We also need
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   557
        # to check that the destination is in the same compression format as
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   558
        # the source, as we must have properly formed files for chash/csize
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   559
        # properties to work right.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   560
        #
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   561
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   562
        fileneeded = True
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   563
        if file_path:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   564
                if PkgGzipFile.test_is_pkggzipfile(file_path):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   565
                        fileneeded = False
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   566
                        opath = file_path
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   567
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   568
        if fileneeded:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   569
                opath = os.path.join(compress_dir, fname)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   570
                ofile = PkgGzipFile(opath, "wb")
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   571
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   572
                nbuf = size / bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   573
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   574
                for n in range(0, nbuf):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   575
                        l = n * bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   576
                        h = (n + 1) * bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   577
                        ofile.write(data[l:h])
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   578
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   579
                m = nbuf * bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   580
                ofile.write(data[m:])
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   581
                ofile.close()
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   582
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   583
        data = None
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   584
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   585
        # Now that the file has been compressed, determine its
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   586
        # size.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   587
        fs = os.stat(opath)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   588
        csize = str(fs.st_size)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   589
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   590
        # Compute the SHA hash of the compressed file.  In order for this to
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   591
        # work correctly, we have to use the PkgGzipFile class.  It omits
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   592
        # filename and timestamp information from the gzip header, allowing us
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   593
        # to generate deterministic hashes for different files with identical
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   594
        # content.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   595
        cfile = open(opath, "rb")
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   596
        chash = hashlib.sha1()
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   597
        while True:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   598
                cdata = cfile.read(bufsz)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   599
                if cdata == "":
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   600
                        break
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   601
                chash.update(cdata)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   602
        cfile.close()
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   603
        return csize, chash
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   604
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   605
class ProcFS(object):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   606
        """This class is used as an interface to procfs."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   607
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   608
        _ctype_formats = {
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   609
            # This dictionary maps basic c types into python format characters
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   610
            # that can be used with struct.unpack().  The format of this
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   611
            # dictionary is:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   612
            #    <ctype>: (<repeat count>, <format char>)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   613
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   614
            # basic c types (repeat count should always be 1)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   615
            # char[] is used to encode character arrays
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   616
            "char":        (1,  "c"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   617
            "char[]":      (1,  "s"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   618
            "int":         (1,  "i"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   619
            "long":        (1,  "l"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   620
            "uintptr_t":   (1,  "I"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   621
            "ushort_t":    (1,  "H"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   622
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   623
            # other simple types (repeat count should always be 1)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   624
            "ctid_t":      (1,  "i"), # ctid_t -> id_t -> int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   625
            "dev_t":       (1,  "L"), # dev_t -> ulong_t
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   626
            "gid_t":       (1,  "I"), # gid_t -> uid_t -> uint_t
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   627
            "pid_t":       (1,  "i"), # pid_t -> int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   628
            "poolid_t":    (1,  "i"), # poolid_t -> id_t -> int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   629
            "projid_t":    (1,  "i"), # projid_t -> id_t -> int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   630
            "size_t":      (1,  "L"), # size_t -> ulong_t
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   631
            "taskid_t":    (1,  "i"), # taskid_t -> id_t -> int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   632
            "time_t":      (1,  "l"), # time_t -> long
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   633
            "uid_t":       (1,  "I"), # uid_t -> uint_t
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   634
            "zoneid_t":    (1,  "i"), # zoneid_t -> id_t -> int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   635
            "id_t":        (1,  "i"), # id_t -> int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   636
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   637
            # structures must be represented as character arrays
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   638
            "timestruc_t": (8,  "s"), # sizeof (timestruc_t) = 8
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   639
        }
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   640
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   641
        _timestruct_desc = [
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   642
            # this list describes a timestruc_t structure
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   643
            # the entry format is (<ctype>, <repeat count>, <name>)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   644
            ("time_t", 1, "tv_sec"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   645
            ("long",   1, "tv_nsec"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   646
        ]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   647
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   648
        _psinfo_desc = [
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   649
            # this list describes a psinfo_t structure
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   650
            # the entry format is: (<ctype>, <repeat count>, <name>)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   651
            ("int",         1,  "pr_flag"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   652
            ("int",         1,  "pr_nlwp"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   653
            ("pid_t",       1,  "pr_pid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   654
            ("pid_t",       1,  "pr_ppid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   655
            ("pid_t",       1,  "pr_pgid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   656
            ("pid_t",       1,  "pr_sid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   657
            ("uid_t",       1,  "pr_uid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   658
            ("uid_t",       1,  "pr_euid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   659
            ("gid_t",       1,  "pr_gid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   660
            ("gid_t",       1,  "pr_egid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   661
            ("uintptr_t",   1,  "pr_addr"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   662
            ("size_t",      1,  "pr_size"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   663
            ("size_t",      1,  "pr_rssize"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   664
            ("size_t",      1,  "pr_pad1"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   665
            ("dev_t",       1,  "pr_ttydev"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   666
            ("ushort_t",    1,  "pr_pctcpu"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   667
            ("ushort_t",    1,  "pr_pctmem"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   668
            ("timestruc_t", 1,  "pr_start"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   669
            ("timestruc_t", 1,  "pr_time"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   670
            ("timestruc_t", 1,  "pr_ctime"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   671
            ("char[]",      16, "pr_fname"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   672
            ("char[]",      80, "pr_psargs"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   673
            ("int",         1,  "pr_wstat"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   674
            ("int",         1,  "pr_argc"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   675
            ("uintptr_t",   1,  "pr_argv"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   676
            ("uintptr_t",   1,  "pr_envp"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   677
            ("char",        1,  "pr_dmodel"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   678
            ("char[]",      3,  "pr_pad2"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   679
            ("taskid_t",    1,  "pr_taskid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   680
            ("projid_t",    1,  "pr_projid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   681
            ("int",         1,  "pr_nzomb"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   682
            ("poolid_t",    1,  "pr_poolid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   683
            ("zoneid_t",    1,  "pr_zoneid"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   684
            ("id_t",        1,  "pr_contract"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   685
            ("int",         1,  "pr_filler"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   686
        ]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   687
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   688
        _struct_descriptions = {
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   689
            # this list contains all the known structure description lists
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   690
            # the entry format is: <structure name>: \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   691
            #    [ <description>, <format string>, <namedtuple> ]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   692
            #
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   693
            # Note that <format string> and <namedtuple> should be assigned
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   694
            # None in this table, and then they will get pre-populated
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   695
            # automatically when this class is instantiated
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   696
            #
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   697
            "psinfo_t":    [_psinfo_desc, None, None],
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   698
            "timestruc_t": [_timestruct_desc, None, None],
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   699
        }
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   700
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   701
        # fill in <format string> and <namedtuple> in _struct_descriptions
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   702
        for struct_name, v in _struct_descriptions.iteritems():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   703
                desc = v[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   704
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   705
                # update _struct_descriptions with a format string
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   706
                v[1] = ""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   707
                for ctype, count1, name in desc:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   708
                        count2, fmt_char = _ctype_formats[ctype]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   709
                        v[1] = v[1] + str(count1 * count2) + fmt_char
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   710
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   711
                # update _struct_descriptions with a named tuple
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   712
                v[2] = collections.namedtuple(struct_name,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   713
                    [ i[2] for i in desc ])
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   714
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   715
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   716
        def _struct_unpack(data, name):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   717
                """Unpack 'data' using struct.unpack().  'name' is the name of
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   718
                the data we're unpacking and is used to lookup a description
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   719
                of the data (which in turn is used to build a format string to
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   720
                decode the data)."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   721
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   722
                # lookup the description of the data to unpack
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   723
                desc, fmt, nt = ProcFS._struct_descriptions[name]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   724
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   725
                # unpack the data into a list
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   726
                rv = list(struct.unpack(fmt, data))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   727
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   728
                # check for any nested data that needs unpacking
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   729
                for index, v in enumerate(desc):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   730
                        ctype = v[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   731
                        if ctype not in ProcFS._struct_descriptions:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   732
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   733
                        rv[index] = ProcFS._struct_unpack(rv[index], ctype)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   734
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   735
                # return the data in a named tuple
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   736
                return nt(*rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   737
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   738
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   739
        def psinfo():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   740
                """Read the psinfo file and return its contents."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   741
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   742
                # This works only on Solaris, in 32-bit mode.  It may not work
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   743
                # on older or newer versions than 5.11.  Ideally, we would use
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   744
                # libproc, or check sbrk(0), but this is expedient.  In most
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   745
                # cases (there's a small chance the file will decode, but
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   746
                # incorrectly), failure will raise an exception, and we'll
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   747
                # fail safe.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   748
                psinfo_size = 232
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   749
                try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   750
                        psinfo_data = file("/proc/self/psinfo").read(
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   751
                            psinfo_size)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   752
                # Catch "Exception"; pylint: disable-msg=W0703
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   753
                except Exception:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   754
                        return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   755
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   756
                # make sure we got the expected amount of data, otherwise
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   757
                # unpacking it will fail.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   758
                if len(psinfo_data) != psinfo_size:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   759
                        return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   760
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   761
                return ProcFS._struct_unpack(psinfo_data, "psinfo_t")
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   762
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   763
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   764
def __getvmusage():
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   765
        """Return the amount of virtual memory in bytes currently in use."""
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   766
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   767
        psinfo = ProcFS.psinfo()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   768
        if psinfo is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   769
                return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   770
        return psinfo.pr_size * 1024
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   771
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   772
def _prstart():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   773
        """Return the process start time expressed as a floating point number
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   774
        in seconds since the epoch, in UTC."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   775
        psinfo = ProcFS.psinfo()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   776
        if psinfo is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   777
                return 0.0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   778
        return psinfo.pr_start.tv_sec + (float(psinfo.pr_start.tv_nsec) / 1e9)
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   779
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   780
def out_of_memory():
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   781
        """Return an out of memory message, for use in a MemoryError handler."""
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   782
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   783
        # figure out how much memory we're using (note that we could run out
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   784
        # of memory while doing this, so check for that.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   785
        vsz = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   786
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   787
                vmusage = __getvmusage()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   788
                if vmusage is not None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   789
                        vsz = bytes_to_str(vmusage, fmt="%(num).0f%(unit)s")
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   790
        except (MemoryError, EnvironmentError), __e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   791
                if isinstance(__e, EnvironmentError) and \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   792
                    __e.errno != errno.ENOMEM:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   793
                        raise
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   794
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   795
        if vsz is not None:
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   796
                error = """\
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   797
There is not enough memory to complete the requested operation.  At least
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   798
%(vsz)s of virtual memory was in use by this command before it ran out of memory.
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   799
You must add more memory (swap or physical) or allow the system to access more
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   800
existing memory, or quit other programs that may be consuming memory, and try
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   801
the operation again."""
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   802
        else:
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   803
                error = """\
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   804
There is not enough memory to complete the requested operation.  You must
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   805
add more memory (swap or physical) or allow the system to access more existing
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   806
memory, or quit other programs that may be consuming memory, and try the
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   807
operation again."""
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   808
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   809
        return _(error) % locals()
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   810
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   811
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   812
# EmptyI for argument defaults
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   813
EmptyI = tuple()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   814
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   815
# ImmutableDict for argument defaults
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   816
class ImmutableDict(dict):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   817
        # Missing docstring; pylint: disable-msg=C0111
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   818
        # Unused argument; pylint: disable-msg=W0613
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   819
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   820
        def __init__(self, default=EmptyI):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   821
                dict.__init__(self, default)
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   822
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   823
        def __setitem__(self, item, value):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   824
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   825
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   826
        def __delitem__(self, item, value):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   827
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   828
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   829
        def pop(self, item, default=None):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   830
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   831
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   832
        def popitem(self):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   833
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   834
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   835
        def setdefault(self, item, default=None):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   836
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   837
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   838
        def update(self, d):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   839
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   840
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   841
        def copy(self):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   842
                return ImmutableDict()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   843
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   844
        def clear(self):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   845
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   846
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   847
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   848
        def __oops():
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   849
                raise TypeError, "Item assignment to ImmutableDict"
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   850
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   851
# A way to have a dictionary be a property
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   852
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   853
class DictProperty(object):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   854
        # Missing docstring; pylint: disable-msg=C0111
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   855
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   856
        class __InternalProxy(object):
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   857
                def __init__(self, obj, fget, fset, fdel, iteritems, keys,
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   858
                    values, iterator, fgetdefault, fsetdefault, update, pop):
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   859
                        self.__obj = obj
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   860
                        self.__fget = fget
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   861
                        self.__fset = fset
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   862
                        self.__fdel = fdel
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   863
                        self.__iteritems = iteritems
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   864
                        self.__keys = keys
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   865
                        self.__values = values
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   866
                        self.__iter = iterator
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   867
                        self.__fgetdefault = fgetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   868
                        self.__fsetdefault = fsetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   869
                        self.__update = update
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   870
                        self.__pop = pop
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   871
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   872
                def __getitem__(self, key):
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   873
                        if self.__fget is None:
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   874
                                raise AttributeError, "unreadable attribute"
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   875
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   876
                        return self.__fget(self.__obj, key)
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   877
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   878
                def __setitem__(self, key, value):
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   879
                        if self.__fset is None:
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   880
                                raise AttributeError, "can't set attribute"
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   881
                        self.__fset(self.__obj, key, value)
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   882
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   883
                def __delitem__(self, key):
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   884
                        if self.__fdel is None:
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   885
                                raise AttributeError, "can't delete attribute"
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   886
                        self.__fdel(self.__obj, key)
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   887
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   888
                def iteritems(self):
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   889
                        if self.__iteritems is None:
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   890
                                raise AttributeError, "can't iterate over items"
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   891
                        return self.__iteritems(self.__obj)
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   892
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   893
                def keys(self):
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   894
                        if self.__keys is None:
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   895
                                raise AttributeError, "can't iterate over keys"
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   896
                        return self.__keys(self.__obj)
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   897
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   898
                def values(self):
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   899
                        if self.__values is None:
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
   900
                                raise AttributeError, "can't iterate over " \
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
   901
                                    "values"
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   902
                        return self.__values(self.__obj)
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   903
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   904
                def get(self, key, default=None):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   905
                        if self.__fgetdefault is None:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   906
                                raise AttributeError, "can't use get"
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   907
                        return self.__fgetdefault(self.__obj, key, default)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   908
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   909
                def setdefault(self, key, default=None):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   910
                        if self.__fsetdefault is None:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   911
                                raise AttributeError, "can't use setdefault"
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   912
                        return self.__fsetdefault(self.__obj, key, default)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   913
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   914
                def update(self, d):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   915
                        if self.__update is None:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   916
                                raise AttributeError, "can't use update"
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   917
                        return self.__update(self.__obj, d)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   918
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   919
                def pop(self, d, default):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   920
                        if self.__pop is None:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   921
                                raise AttributeError, "can't use pop"
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   922
                        return self.__pop(self.__obj, d, default)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   923
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   924
                def __iter__(self):
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   925
                        if self.__iter is None:
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   926
                                raise AttributeError, "can't iterate"
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   927
                        return self.__iter(self.__obj)
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   928
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
   929
        def __init__(self, fget=None, fset=None, fdel=None, iteritems=None,
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   930
            keys=None, values=None, iterator=None, doc=None, fgetdefault=None,
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   931
            fsetdefault=None, update=None, pop=None):
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   932
                self.__fget = fget
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   933
                self.__fset = fset
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   934
                self.__fdel = fdel
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   935
                self.__iteritems = iteritems
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   936
                self.__doc__ = doc
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   937
                self.__keys = keys
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   938
                self.__values = values
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   939
                self.__iter = iterator
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   940
                self.__fgetdefault = fgetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   941
                self.__fsetdefault = fsetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   942
                self.__update = update
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   943
                self.__pop = pop
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   944
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   945
        def __get__(self, obj, objtype=None):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   946
                # Unused argument; pylint: disable-msg=W0613
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   947
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   948
                if obj is None:
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   949
                        return self
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
   950
                return self.__InternalProxy(obj, self.__fget, self.__fset,
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   951
                    self.__fdel, self.__iteritems, self.__keys, self.__values,
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   952
                    self.__iter, self.__fgetdefault, self.__fsetdefault,
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   953
                    self.__update, self.__pop)
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   954
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
   955
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   956
def build_cert(path, uri=None, pub=None):
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   957
        """Take the file given in path, open it, and use it to create
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   958
        an X509 certificate object.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   959
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   960
        'uri' is an optional value indicating the uri associated with or that
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   961
        requires the certificate for access.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   962
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   963
        'pub' is an optional string value containing the name (prefix) of a
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   964
        related publisher."""
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   965
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   966
        try:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   967
                cf = file(path, "rb")
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   968
                certdata = cf.read()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   969
                cf.close()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   970
        except EnvironmentError, e:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   971
                if e.errno == errno.ENOENT:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   972
                        raise api_errors.NoSuchCertificate(path, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   973
                            publisher=pub)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   974
                if e.errno == errno.EACCES:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   975
                        raise api_errors.PermissionsException(e.filename)
1431
62b6033670e4 10416 server catalog v1 support desired
Shawn Walker <srw@sun.com>
parents: 1352
diff changeset
   976
                if e.errno == errno.EROFS:
62b6033670e4 10416 server catalog v1 support desired
Shawn Walker <srw@sun.com>
parents: 1352
diff changeset
   977
                        raise api_errors.ReadOnlyFileSystemException(e.filename)
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   978
                raise
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   979
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   980
        try:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   981
                return osc.load_certificate(osc.FILETYPE_PEM, certdata)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   982
        except osc.Error, e:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   983
                # OpenSSL.crypto.Error
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   984
                raise api_errors.InvalidCertificate(path, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   985
                    publisher=pub)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   986
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   987
def validate_ssl_cert(ssl_cert, prefix=None, uri=None):
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   988
        """Validates the indicated certificate and returns a pyOpenSSL object
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   989
        representing it if it is valid."""
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   990
        cert = build_cert(ssl_cert, uri=uri, pub=prefix)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   991
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   992
        if cert.has_expired():
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   993
                raise api_errors.ExpiredCertificate(ssl_cert, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   994
                    publisher=prefix)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   995
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   996
        now = datetime.datetime.utcnow()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   997
        nb = cert.get_notBefore()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   998
        t = time.strptime(nb, "%Y%m%d%H%M%SZ")
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   999
        nbdt = datetime.datetime.utcfromtimestamp(
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1000
            calendar.timegm(t))
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1001
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1002
        # PyOpenSSL's has_expired() doesn't validate the notBefore
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1003
        # time on the certificate.  Don't ask me why.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1004
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1005
        if nbdt > now:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1006
                raise api_errors.NotYetValidCertificate(ssl_cert, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1007
                    publisher=prefix)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1008
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1009
        na = cert.get_notAfter()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1010
        t = time.strptime(na, "%Y%m%d%H%M%SZ")
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1011
        nadt = datetime.datetime.utcfromtimestamp(
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1012
            calendar.timegm(t))
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1013
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1014
        diff = nadt - now
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1015
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1016
        if diff <= MIN_WARN_DAYS:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1017
                raise api_errors.ExpiringCertificate(ssl_cert, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1018
                    publisher=prefix, days=diff.days)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1019
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1020
        return cert
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1021
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1022
# Used for the conversion of the signature value between hex and binary.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1023
char_list = "0123456789abcdef"
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1024
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1025
def binary_to_hex(s):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1026
        """Converts a string of bytes to a hexadecimal representation.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1027
        """
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1028
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1029
        res = ""
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1030
        for p in s:
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1031
                p = ord(p)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1032
                a = char_list[p % 16]
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1033
                p = p/16
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1034
                b = char_list[p % 16]
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1035
                res += b + a
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1036
        return res
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1037
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1038
def hex_to_binary(s):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1039
        """Converts a string of hex digits to the binary representation.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1040
        """
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1041
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1042
        res = ""
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1043
        for i in range(0, len(s), 2):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1044
                res += chr(char_list.find(s[i]) * 16 +
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1045
                    char_list.find(s[i+1]))
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1046
        return res
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1047
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1048
def config_temp_root():
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1049
        """Examine the environment.  If the environment has set TMPDIR, TEMP,
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1050
        or TMP, return None.  This tells tempfile to use the environment
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1051
        settings when creating temporary files/directories.  Otherwise,
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1052
        return a path that the caller should pass to tempfile instead."""
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1053
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1054
        # In Python's tempfile module, the default temp directory
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1055
        # includes some paths that are suboptimal for holding large numbers
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1056
        # of files.  If the user hasn't set TMPDIR, TEMP, or TMP in the
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1057
        # environment, override the default directory for creating a tempfile.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1058
        tmp_envs = [ "TMPDIR", "TEMP", "TMP" ]
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1059
        for ev in tmp_envs:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1060
                env_val = os.getenv(ev)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1061
                if env_val:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1062
                        return None
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1063
2820
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1064
        return DEFAULT_TEMP_PATH
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1065
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1066
def get_temp_root_path():
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1067
        """Return the directory path where the temporary directories or 
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1068
        files should be created. If the environment has set TMPDIR
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1069
        or TEMP or TMP then return the corresponding value else return the
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1070
        default value."""
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1071
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1072
        temp_env = [ "TMPDIR", "TEMP", "TMP" ]
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1073
        for env in temp_env:
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1074
                env_val = os.getenv(env)
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1075
                if env_val:
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1076
                        return env_val
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1077
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1078
        return DEFAULT_TEMP_PATH 
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1079
2219
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1080
def parse_uri(uri, cwd=None):
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1081
        """Parse the repository location provided and attempt to transform it
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1082
        into a valid repository URI.
2219
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1083
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1084
        'cwd' is the working directory to use to turn paths into an absolute
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1085
        path.  If not provided, the current working directory is used.
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1086
        """
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1087
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1088
        if uri.find("://") == -1 and not uri.startswith("file:/"):
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1089
                # Convert the file path to a URI.
2219
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1090
                if not cwd:
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1091
                        uri = os.path.abspath(uri)
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1092
                elif not os.path.isabs(uri):
2430
a1937ef0a955 18325 image-create should accept simple paths for sources
Shawn Walker <shawn.walker@oracle.com>
parents: 2407
diff changeset
  1093
                        uri = os.path.normpath(os.path.join(cwd, uri))
2219
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1094
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1095
                uri = urlparse.urlunparse(("file", "",
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1096
                    urllib.pathname2url(uri), "", "", ""))
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1097
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1098
        scheme, netloc, path, params, query, fragment = \
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1099
            urlparse.urlparse(uri, "file", allow_fragments=0)
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1100
        scheme = scheme.lower()
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1101
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1102
        if scheme == "file":
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1103
                # During urlunparsing below, ensure that the path starts with
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1104
                # only one '/' character, if any are present.
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1105
                if path.startswith("/"):
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1106
                        path = "/" + path.lstrip("/")
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1107
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1108
        # Rebuild the URI with the sanitized components.
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1109
        return urlparse.urlunparse((scheme, netloc, path, params,
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1110
            query, fragment))
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1111
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1112
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1113
def makedirs(pathname):
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1114
        """Create a directory at the specified location if it does not
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1115
        already exist (including any parent directories) re-raising any
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1116
        unexpected exceptions as ApiExceptions.
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1117
        """
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1118
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1119
        try:
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1120
                os.makedirs(pathname, PKG_DIR_MODE)
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1121
        except EnvironmentError, e:
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1122
                if e.filename == pathname and (e.errno == errno.EEXIST or
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1123
                    os.path.exists(e.filename)):
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1124
                        return
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1125
                elif e.errno == errno.EACCES:
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1126
                        raise api_errors.PermissionsException(
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1127
                            e.filename)
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1128
                elif e.errno == errno.EROFS:
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1129
                        raise api_errors.ReadOnlyFileSystemException(
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1130
                            e.filename)
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1131
                elif e.errno != errno.EEXIST or e.filename != pathname:
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1132
                        raise
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1133
2043
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1134
class DummyLock(object):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1135
        """This has the same external interface as threading.Lock,
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1136
        but performs no locking.  This is a placeholder object for situations
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1137
        where we want to be able to do locking, but don't always need a
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1138
        lock object present.  The object has a held value, that is used
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1139
        for _is_owned.  This is informational and doesn't actually
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1140
        provide mutual exclusion in any way whatsoever."""
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1141
        # Missing docstring; pylint: disable-msg=C0111
2043
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1142
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1143
        def __init__(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1144
                self.held = False
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1145
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1146
        def acquire(self, blocking=1):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1147
                # Unused argument; pylint: disable-msg=W0613
2043
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1148
                self.held = True
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1149
                return True
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1150
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1151
        def release(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1152
                self.held = False
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1153
                return
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1154
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1155
        def _is_owned(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1156
                return self.held
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1157
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1158
        @property
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1159
        def locked(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1160
                return self.held
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1161
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1162
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1163
class Singleton(type):
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1164
        """Set __metaclass__ to Singleton to create a singleton.
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1165
        See http://en.wikipedia.org/wiki/Singleton_pattern """
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1166
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1167
        def __init__(mcs, name, bases, dictionary):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1168
                super(Singleton, mcs).__init__(name, bases, dictionary)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1169
                mcs.instance = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1170
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1171
        def __call__(mcs, *args, **kw):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1172
                if mcs.instance is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1173
                        mcs.instance = super(Singleton, mcs).__call__(*args,
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1174
                            **kw)
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1175
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1176
                return mcs.instance
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1177
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1178
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
  1179
EmptyDict = ImmutableDict()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
  1180
1026
d4aa3ac69dc0 7878 token_byte_offset file not correctly sorted
Brock Pytlik <bpytlik@sun.com>
parents: 1023
diff changeset
  1181
# Setting the python file buffer size to 128k gives substantial performance
d4aa3ac69dc0 7878 token_byte_offset file not correctly sorted
Brock Pytlik <bpytlik@sun.com>
parents: 1023
diff changeset
  1182
# gains on certain files.
d4aa3ac69dc0 7878 token_byte_offset file not correctly sorted
Brock Pytlik <bpytlik@sun.com>
parents: 1023
diff changeset
  1183
PKG_FILE_BUFSIZ = 128 * 1024
1507
b956ea23d3a6 11735 pkg history data files should not be executable
Richard Lowe <richlowe@richlowe.net>
parents: 1505
diff changeset
  1184
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
  1185
PKG_FILE_MODE = S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
  1186
PKG_DIR_MODE = (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
  1187
PKG_RO_FILE_MODE = S_IRUSR | S_IRGRP | S_IROTH
2112
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1188
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1189
def relpath(path, start="."):
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1190
        """Version of relpath to workaround python bug:
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1191
            http://bugs.python.org/issue5117
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1192
        """
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1193
        if path and start and start == "/" and path[0] == "/":
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1194
                return path.lstrip("/")
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1195
        return os.path.relpath(path, start=start)
2310
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1196
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1197
def recursive_chown_dir(d, uid, gid):
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1198
        """Change the ownership of all files under directory d to uid:gid."""
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1199
        for dirpath, dirnames, filenames in os.walk(d):
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1200
                for name in dirnames:
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1201
                        path = os.path.join(dirpath, name)
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1202
                        portable.chown(path, uid, gid)
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1203
                for name in filenames:
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1204
                        path = os.path.join(dirpath, name)
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1205
                        portable.chown(path, uid, gid)
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1206
def opts_parse(op, api_inst, args, table, pargs_limit, usage_cb):
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1207
        """Generic table-based options parsing function.  Returns a tuple
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1208
        consisting of a dictionary of parsed options and the remaining
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1209
        unparsed options.
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1210
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1211
        'op' is the operation being performed.
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1212
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1213
        'api_inst' is an image api object that is passed to options handling
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1214
        callbacks (passed in via 'table').
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1215
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1216
        'args' is the arguments that should be parsed.
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1217
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1218
        'table' is a list of options and callbacks.Each entry is either a
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1219
        a tuple or a callback function.
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1220
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1221
        tuples in 'table' specify allowable options and have the following
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1222
        format:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1223
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1224
                (<short opt>, <long opt>, <key>, <default value>)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1225
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1226
        An example of a short opt is "f", which maps to a "-f" option.  An
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1227
        example of a long opt is "foo", which maps to a "--foo" option.  Key
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1228
        is the value of this option in the parsed option dictionary.  The
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1229
        default value not only represents the default value assigned to the
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1230
        option, but it also implicitly determines how the option is parsed.  If
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1231
        the default value is True or False, the option doesn't take any
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1232
        arguments, can only be specified once, and if specified it inverts the
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1233
        default value.  If the default value is 0, the option doesn't take any
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1234
        arguments, can be specified multiple times, and if specified its value
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1235
        will be the number of times it was seen.  If the default value is
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1236
        None, the option requires an argument, can only be specified once, and
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1237
        if specified its value will be its argument string.  If the default
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1238
        value is an empty list, the option requires an argument, may be
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1239
        specified multiple times, and if specified its value will be a list
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1240
        with all the specified argument values.
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1241
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1242
        callbacks in 'table' specify callback functions that are invoked after
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1243
        all options have been parsed.  Callback functions must have the
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1244
        following signature:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1245
                callback(api_inst, opts, opts_new)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1246
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1247
        The opts parameter is a dictionary containing all the raw, parsed
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1248
        options.  Callbacks should never update the contents of this
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1249
        dictionary.  The opts_new parameter is a dictionary which is initially
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1250
        a copy of the opts dictionary.  This is the dictionary that will be
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1251
        returned to the caller of opts_parse().  If a callback function wants
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1252
        to update the arguments dictionary that will be returned to the
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1253
        caller, they should make all their updates to the opts_new dictionary.
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1254
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1255
        'pargs_limit' specified how to handle extra arguments not parsed by
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1256
        getops.  A value of -1 indicates that we allow an unlimited number of
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1257
        extra arguments.  A value of 0 or greater indicates the number of
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1258
        allowed additional unparsed options.
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1259
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1260
        'usage_cb' is a function pointer that should display usage information
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1261
        and will be invoked if invalid arguments are detected."""
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1262
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1263
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1264
        assert type(table) == list
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1265
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1266
        # return dictionary
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1267
        rv = dict()
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1268
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1269
        # option string passed to getopt
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1270
        opts_s_str = ""
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1271
        # long options list passed to getopt
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1272
        opts_l_list = list()
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1273
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1274
        # dict to map options returned by getopt to keys
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1275
        opts_keys = dict()
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1276
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1277
        # sanity checking to make sure each option is unique
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1278
        opts_s_set = set()
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1279
        opts_l_set = set()
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1280
        opts_seen = dict()
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1281
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1282
        # callbacks to invoke after processing options
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1283
        callbacks = []
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1284
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1285
        # process each option entry
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1286
        for entry in table:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1287
                # check for a callback
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1288
                if type(entry) != tuple:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1289
                        callbacks.append(entry)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1290
                        continue
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1291
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1292
                # decode the table entry
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1293
                # s: a short option, ex: -f
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1294
                # l: a long option, ex: --foo
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1295
                # k: the key value for the options dictionary
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1296
                # v: the default value
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1297
                (s, l, k, v) = entry
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1298
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1299
                # make sure an option was specified
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1300
                assert s or l
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1301
                # sanity check the default value
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1302
                assert (v == None) or (v == []) or \
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1303
                    (type(v) == bool) or (type(v) == int)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1304
                # make sure each key is unique
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1305
                assert k not in rv
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1306
                # initialize the default return dictionary entry.
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1307
                rv[k] = v
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1308
                if l:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1309
                        # make sure each option is unique
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1310
                        assert set([l]) not in opts_l_set
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1311
                        opts_l_set |= set([l])
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1312
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1313
                        if type(v) == bool:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1314
                                v = not v
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1315
                                opts_l_list.append("%s" % l)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1316
                        elif type(v) == int:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1317
                                opts_l_list.append("%s" % l)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1318
                        else:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1319
                                opts_l_list.append("%s=" % l)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1320
                        opts_keys["--%s" % l] = k
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1321
                if s:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1322
                        # make sure each option is unique
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1323
                        assert set([s]) not in opts_s_set
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1324
                        opts_s_set |= set([s])
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1325
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1326
                        if type(v) == bool:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1327
                                v = not v
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1328
                                opts_s_str += "%s" % s
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1329
                        elif type(v) == int:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1330
                                opts_s_str += "%s" % s
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1331
                        else:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1332
                                opts_s_str += "%s:" % s
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1333
                        opts_keys["-%s" % s] = k
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1334
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1335
        # parse options
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1336
        try:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1337
                opts, pargs = getopt.getopt(args, opts_s_str, opts_l_list)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1338
        except getopt.GetoptError, e:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1339
                usage_cb(_("illegal option -- %s") % e.opt, cmd=op)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1340
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1341
        if (pargs_limit >= 0) and (pargs_limit < len(pargs)):
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1342
                usage_cb(_("illegal argument -- %s") % pargs[pargs_limit],
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1343
                    cmd=op)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1344
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1345
        # update options dictionary with the specified options
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1346
        for opt, arg in opts:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1347
                k = opts_keys[opt]
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1348
                v = rv[k]
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1349
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1350
                # check for duplicate options
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1351
                if k in opts_seen and (type(v) != list and type(v) != int):
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1352
                        if opt == opts_seen[k]:
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1353
                                usage_cb(_("option '%s' repeated") % opt,
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1354
                                    cmd=op)
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1355
                        usage_cb(_("'%s' and '%s' have the same meaning") %
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1356
                            (opts_seen[k], opt), cmd=op)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1357
                opts_seen[k] = opt
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1358
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1359
                # update the return dict value
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1360
                if type(v) == bool:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1361
                        rv[k] = not rv[k]
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1362
                elif type(v) == list:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1363
                        rv[k].append(arg)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1364
                elif type(v) == int:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1365
                        rv[k] += 1
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1366
                else:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1367
                        rv[k] = arg
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1368
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1369
        # invoke callbacks (cast to set() to eliminate dups)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1370
        rv_updated = rv.copy()
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1371
        for cb in set(callbacks):
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1372
                cb(op, api_inst, rv, rv_updated)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1373
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1374
        return (rv_updated, pargs)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1375
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1376
def api_cmdpath():
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1377
        """Returns the path to the executable that is invoking the api client
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1378
        interfaces."""
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1379
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1380
        cmdpath = None
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1381
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1382
        if global_settings.client_args[0]:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1383
                cmdpath = os.path.realpath(os.path.join(sys.path[0],
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1384
                    os.path.basename(global_settings.client_args[0])))
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1385
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1386
        if "PKG_CMDPATH" in os.environ:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1387
                cmdpath = os.environ["PKG_CMDPATH"]
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1388
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1389
        if DebugValues.get_value("simulate_cmdpath"):
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1390
                cmdpath = DebugValues.get_value("simulate_cmdpath")
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1391
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1392
        return cmdpath
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1393
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1394
def api_pkgcmd():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1395
        """When running a pkg(1) command from within a packaging module, try
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1396
        to use the same pkg(1) path as our current invocation.  If we're
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1397
        running pkg(1) from some other command (like the gui updater) then
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1398
        assume that pkg(1) is in the default path."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1399
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1400
        pkg_bin = "pkg"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1401
        cmdpath = api_cmdpath()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1402
        if cmdpath and os.path.basename(cmdpath) == "pkg":
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1403
                try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1404
                        # check if the currently running pkg command
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1405
                        # exists and is accessible.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1406
                        os.stat(cmdpath)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1407
                        pkg_bin = cmdpath
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1408
                except OSError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1409
                        pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1410
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1411
        pkg_cmd = [pkg_bin]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1412
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1413
        # propagate debug options
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1414
        for k, v in DebugValues.iteritems():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1415
                pkg_cmd.append("-D")
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1416
                pkg_cmd.append("%s=%s" % (k, v))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1417
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1418
        return pkg_cmd
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1419
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1420
def liveroot():
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1421
        """Return path to the current live root image, i.e. the image
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1422
        that we are running from."""
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1423
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1424
        live_root = DebugValues.get_value("simulate_live_root")
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1425
        if not live_root and "PKG_LIVE_ROOT" in os.environ:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1426
                live_root = os.environ["PKG_LIVE_ROOT"]
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1427
        if not live_root:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1428
                live_root = "/"
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1429
        return live_root
2407
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1430
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1431
def spaceavail(path):
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1432
        """Find out how much space is available at the specified path if
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1433
        it exists; return -1 if path doesn't exist"""
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1434
        try:
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1435
                res = os.statvfs(path)
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1436
                return res.f_frsize * res.f_bavail
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1437
        except OSError:
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1438
                return -1
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1439
2446
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1440
def get_dir_size(path):
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1441
        """Return the size (in bytes) of a directory and all of its contents."""
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1442
        try:
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1443
                return sum(
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1444
                    os.path.getsize(os.path.join(d, fname))
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1445
                    for d, dnames, fnames in os.walk(path)
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1446
                    for fname in fnames
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1447
                )
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1448
        except EnvironmentError, e:
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1449
                # Access to protected member; pylint: disable-msg=W0212
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1450
                raise api_errors._convert_error(e)
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1451
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1452
def get_listing(desired_field_order, field_data, field_values, out_format,
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1453
    def_fmt, omit_headers, escape_output=True):
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1454
        """Returns a string containing a listing defined by provided values
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1455
        in the specified output format.
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1456
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1457
        'desired_field_order' is the list of the fields to show in the order
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1458
        they should be output left to right.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1459
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1460
        'field_data' is a dictionary of lists of the form:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1461
          {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1462
            field_name1: {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1463
              [(output formats), field header, initial field value]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1464
            },
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1465
            field_nameN: {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1466
              [(output formats), field header, initial field value]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1467
            }
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1468
          }
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1469
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1470
        'field_values' is a generator or list of dictionaries of the form:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1471
          {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1472
            field_name1: field_value,
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1473
            field_nameN: field_value
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1474
          }
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1475
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1476
        'out_format' is the format to use for output.  Currently 'default',
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1477
        'tsv', 'json', and 'json-formatted' are supported.  The first is
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1478
        intended for columnar, human-readable output, and the others for
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1479
        parsable output.
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1480
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1481
        'def_fmt' is the default Python formatting string to use for the
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1482
        'default' human-readable output.  It must match the fields defined
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1483
        in 'field_data'.
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1484
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1485
        'omit_headers' is a boolean specifying whether headers should be
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1486
        included in the listing.  (If applicable to the specified output
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1487
        format.)
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1488
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1489
        'escape_output' is an optional boolean indicating whether shell
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1490
        metacharacters or embedded control sequences should be escaped
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1491
        before display.  (If applicable to the specified output format.)
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1492
        """
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1493
        # Missing docstring; pylint: disable-msg=C0111
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1494
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1495
        # Custom sort function for preserving field ordering
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1496
        def sort_fields(one, two):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1497
                return desired_field_order.index(get_header(one)) - \
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1498
                    desired_field_order.index(get_header(two))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1499
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1500
        # Functions for manipulating field_data records
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1501
        def filter_default(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1502
                return "default" in record[0]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1503
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1504
        def filter_tsv(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1505
                return "tsv" in record[0]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1506
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1507
        def get_header(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1508
                return record[1]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1509
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1510
        def get_value(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1511
                return record[2]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1512
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1513
        def quote_value(val):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1514
                if out_format == "tsv":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1515
                        # Expand tabs if tsv output requested.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1516
                        val = val.replace("\t", " " * 8)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1517
                nval = val
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1518
                # Escape bourne shell metacharacters.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1519
                for c in ("\\", " ", "\t", "\n", "'", "`", ";", "&", "(", ")",
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1520
                    "|", "^", "<", ">"):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1521
                        nval = nval.replace(c, "\\" + c)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1522
                return nval
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1523
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1524
        def set_value(entry):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1525
                val = entry[1]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1526
                multi_value = False
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1527
                if isinstance(val, (list, tuple, set, frozenset)):
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1528
                        multi_value = True
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1529
                elif val == "":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1530
                        entry[0][2] = '""'
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1531
                        return
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1532
                elif val is None:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1533
                        entry[0][2] = ''
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1534
                        return
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1535
                else:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1536
                        val = [val]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1537
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1538
                nval = []
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1539
                for v in val:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1540
                        if v == "":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1541
                                # Indicate empty string value using "".
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1542
                                nval.append('""')
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1543
                        elif v is None:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1544
                                # Indicate no value using empty string.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1545
                                nval.append('')
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1546
                        elif escape_output:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1547
                                # Otherwise, escape the value to be displayed.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1548
                                nval.append(quote_value(str(v)))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1549
                        else:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1550
                                # Caller requested value not be escaped.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1551
                                nval.append(str(v))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1552
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1553
                val = " ".join(nval)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1554
                nval = None
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1555
                if multi_value:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1556
                        val = "(%s)" % val
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1557
                entry[0][2] = val
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1558
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1559
        if out_format == "default":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1560
                # Create a formatting string for the default output
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1561
                # format.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1562
                fmt = def_fmt
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1563
                filter_func = filter_default
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1564
        elif out_format == "tsv":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1565
                # Create a formatting string for the tsv output
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1566
                # format.
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1567
                num_fields = sum(
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1568
                    1 for k in field_data
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1569
                    if filter_tsv(field_data[k])
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1570
                )
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1571
                fmt = "\t".join('%s' for x in xrange(num_fields))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1572
                filter_func = filter_tsv
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1573
        elif out_format == "json" or out_format == "json-formatted":
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1574
                args = { "sort_keys": True }
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1575
                if out_format == "json-formatted":
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1576
                        args["indent"] = 2
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1577
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1578
                # 'json' formats always include any extra fields returned;
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1579
                # any explicitly named fields are only included if 'json'
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1580
                # is explicitly listed.
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1581
                def fmt_val(v):
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1582
                        if isinstance(v, basestring):
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1583
                                return v
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1584
                        if isinstance(v, (list, tuple, set, frozenset)):
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1585
                                return [fmt_val(e) for e in v]
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1586
                        if isinstance(v, dict):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1587
                                for k, e in v.iteritems():
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1588
                                        v[k] = fmt_val(e)
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1589
                                return v
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1590
                        return str(v)
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1591
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1592
                output = json.dumps([
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1593
                    dict(
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1594
                        (k, fmt_val(entry[k]))
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1595
                        for k in entry
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1596
                        if k not in field_data or "json" in field_data[k][0]
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1597
                    )
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1598
                    for entry in field_values
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1599
                ], **args)
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1600
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1601
                if out_format == "json-formatted":
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1602
                        # Include a trailing newline for readability.
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1603
                        return output + "\n"
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1604
                return output
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1605
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1606
        # Extract the list of headers from the field_data dictionary.  Ensure
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1607
        # they are extracted in the desired order by using the custom sort
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1608
        # function.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1609
        hdrs = map(get_header, sorted(filter(filter_func, field_data.values()),
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1610
            sort_fields))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1611
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1612
        # Output a header if desired.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1613
        output = ""
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1614
        if not omit_headers:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1615
                output += fmt % tuple(hdrs)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1616
                output += "\n"
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1617
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1618
        for entry in field_values:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1619
                map(set_value, (
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1620
                    (field_data[f], v)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1621
                    for f, v in entry.iteritems()
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1622
                    if f in field_data
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1623
                ))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1624
                values = map(get_value, sorted(filter(filter_func,
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1625
                    field_data.values()), sort_fields))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1626
                output += fmt % tuple(values)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1627
                output += "\n"
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1628
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1629
        return output
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1630
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1631
def truncate_file(f, size=0):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1632
        """Truncate the specified file."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1633
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1634
                f.truncate(size)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1635
        except IOError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1636
                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1637
        except OSError, e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1638
                # Access to protected member; pylint: disable-msg=W0212
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1639
                raise api_errors._convert_error(e)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1640
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1641
def flush_output():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1642
        """flush stdout and stderr"""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1643
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1644
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1645
                sys.stdout.flush()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1646
        except IOError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1647
                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1648
        except OSError, e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1649
                # Access to protected member; pylint: disable-msg=W0212
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1650
                raise api_errors._convert_error(e)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1651
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1652
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1653
                sys.stderr.flush()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1654
        except IOError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1655
                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1656
        except OSError, e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1657
                # Access to protected member; pylint: disable-msg=W0212
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1658
                raise api_errors._convert_error(e)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1659
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1660
# valid json types
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1661
json_types_immediates = (bool, float, int, long, str, type(None), unicode)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1662
json_types_collections = (dict, list)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1663
json_types = tuple(json_types_immediates + json_types_collections)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1664
json_debug = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1665
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1666
def json_encode(name, data, desc, commonize=None, je_state=None):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1667
        """A generic json encoder.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1668
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1669
        'name' a descriptive name of the data we're encoding.  If encoding a
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1670
        class, this would normally be the class name.  'name' is used when
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1671
        displaying errors to identify the data that caused the errors.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1672
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1673
        'data' data to encode.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1674
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1675
        'desc' a description of the data to encode.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1676
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1677
        'commonize' a list of objects that should be cached by reference.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1678
        this is used when encoding objects which may contain multiple
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1679
        references to a single object.  In this case, each reference will be
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1680
        replaced with a unique id, and the object that was pointed to will
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1681
        only be encoded once.  This ensures that upon decoding we can restore
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1682
        the original object and all references to it."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1683
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1684
        # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1685
        if je_state is None and json_debug:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1686
                print >> sys.stderr, "json_encode name: ", name
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1687
                print >> sys.stderr, "json_encode data: ", data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1688
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1689
        # we don't encode None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1690
        if data is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1691
                return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1692
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1693
        # initialize parameters to default
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1694
        if commonize is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1695
                commonize = frozenset()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1696
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1697
        if je_state is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1698
                # this is the first invocation of this function, so "data"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1699
                # points to the top-level object that we want to encode.  this
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1700
                # means that if we're commonizing any objects we should
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1701
                # finalize the object cache when we're done encoding this
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1702
                # object.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1703
                finish = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1704
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1705
                # initialize recursion state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1706
                obj_id = [0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1707
                obj_cache = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1708
                je_state = [obj_id, obj_cache, commonize]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1709
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1710
                # we're being invoked recursively, do not finalize the object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1711
                # cache (since that will be done by a previous invocation of
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1712
                # this function).
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1713
                finish = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1714
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1715
                # get recursion state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1716
                obj_id, obj_cache, commonize_old = je_state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1717
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1718
                # check if we're changing the set of objects to commonize
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1719
                if not commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1720
                        commonize = commonize_old
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1721
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1722
                        # update the set of objects to commonize
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1723
                        # make a copy so we don't update our callers state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1724
                        commonize = frozenset(commonize_old | commonize)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1725
                        je_state = [obj_id, obj_cache, commonize]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1726
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1727
        # verify state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1728
        assert type(name) == str
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1729
        assert type(obj_cache) == dict
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1730
        assert type(obj_id) == list and len(obj_id) == 1 and obj_id[0] >= 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1731
        assert type(commonize) == frozenset
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1732
        assert type(je_state) == list and len(je_state) == 3
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1733
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1734
        def je_return(name, data, finish, je_state):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1735
                """if necessary, finalize the object cache and merge it into
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1736
                the state data.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1737
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1738
                while encoding, the object cache is a dictionary which
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1739
                contains tuples consisting of an assigned unique object id
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1740
                (obj_id) and an encoded object.  these tuples are hashed by
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1741
                the python object id of the original un-encoded python object.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1742
                so the hash contains:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1743
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1744
                       { id(<obj>): ( <obj_id>, <obj_state> ) }
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1745
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1746
                when we finish the object cache we update it so that it
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1747
                contains just encoded objects hashed by their assigned object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1748
                id (obj_id).  so the hash contains:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1749
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1750
                       { str(<obj_id>): <obj_state> }
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1751
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1752
                then we merge the state data and object cache into a single
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1753
                dictionary and return that.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1754
                """
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1755
                # Unused argument; pylint: disable-msg=W0613
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1756
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1757
                if not finish:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1758
                        return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1759
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1760
                # json.dump converts integer dictionary keys into strings, so
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1761
                # we'll convert the object id keys (which are integers) into
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1762
                # strings (that way we're encoder/decoder independent).
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1763
                obj_cache = je_state[1]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1764
                obj_cache2 = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1765
                for obj_id, obj_state in obj_cache.itervalues():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1766
                        obj_cache2[str(obj_id)] = obj_state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1767
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1768
                data = { "json_state": data, "json_objects": obj_cache2 }
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1769
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1770
                if DebugValues["plandesc_validate"]:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1771
                        json_validate(name, data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1772
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1773
                # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1774
                if json_debug:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1775
                        print >> sys.stderr, "json_encode finished name: ", name
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1776
                        print >> sys.stderr, "json_encode finished data: ", data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1777
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1778
                return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1779
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1780
        # check if the description is a type object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1781
        if isinstance(desc, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1782
                desc_type = desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1783
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1784
                # get the expected data type from the description
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1785
                desc_type = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1786
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1787
        # get the data type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1788
        data_type = getattr(data, "__metaclass__", type(data))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1789
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1790
        # sanity check that the data type matches the description
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1791
        assert desc_type == data_type, \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1792
            "unexpected %s for %s, expected: %s, value: %s" % \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1793
                (data_type, name, desc_type, data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1794
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1795
        # we don't need to do anything for basic types
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1796
        if desc_type in json_types_immediates:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1797
                return je_return(name, data, finish, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1798
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1799
        # encode elements nested in a dictionary like object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1800
        # return elements in a dictionary
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1801
        if desc_type in (dict, collections.defaultdict):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1802
                # we always return a new dictionary
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1803
                rv = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1804
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1805
                # check if we're not encoding nested elements
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1806
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1807
                        rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1808
                        return je_return(name, rv, finish, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1809
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1810
                # lookup the first descriptor to see if we have
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1811
                # generic type description.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1812
                desc_k, desc_v = desc.items()[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1813
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1814
                # if the key in the first type pair is a type then we
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1815
                # have a generic type description that applies to all
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1816
                # keys and values in the dictionary.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1817
                # check if the description is a type object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1818
                if isinstance(desc_k, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1819
                        # there can only be one generic type desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1820
                        assert len(desc) == 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1821
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1822
                        # encode all key / value pairs
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1823
                        for k, v in data.iteritems():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1824
                                # encode the key
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1825
                                name2 = "%s[%s].key()" % (name, desc_k)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1826
                                k2 = json_encode(name2, k, desc_k,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1827
                                    je_state=je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1828
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1829
                                # encode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1830
                                name2 = "%s[%s].value()" % (name, desc_k)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1831
                                v2 = json_encode(name2, v, desc_v,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1832
                                    je_state=je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1833
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1834
                                # save the result
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1835
                                rv[k2] = v2
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1836
                        return je_return(name, rv, finish, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1837
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1838
                # we have element specific value type descriptions.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1839
                # encode the specific values.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1840
                rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1841
                for desc_k, desc_v in desc.iteritems():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1842
                        # check for the specific key
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1843
                        if desc_k not in rv:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1844
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1845
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1846
                        # encode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1847
                        name2 = "%s[%s].value()" % (name, desc_k)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1848
                        rv[desc_k] = json_encode(name2, rv[desc_k], desc_v,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1849
                            je_state=je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1850
                return je_return(name, rv, finish, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1851
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1852
        # encode elements nested in a list like object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1853
        # return elements in a list
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1854
        if desc_type in (tuple, list, set, frozenset):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1855
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1856
                # we always return a new list
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1857
                rv = []
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1858
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1859
                # check for an empty list since we use izip_longest
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1860
                if len(data) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1861
                        return je_return(name, rv, finish, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1862
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1863
                # check if we're not encoding nested elements
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1864
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1865
                        rv.extend(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1866
                        return je_return(name, rv, finish, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1867
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1868
                # don't accidentally generate data via izip_longest
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1869
                assert len(data) >= len(desc), \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1870
                    "%d >= %d" % (len(data), len(desc))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1871
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1872
                i = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1873
                for data2, desc2 in itertools.izip_longest(data, desc,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1874
                    fillvalue=list(desc)[0]):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1875
                        name2 = "%s[%i]" % (name, i)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1876
                        i += 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1877
                        rv.append(json_encode(name2, data2, desc2,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1878
                            je_state=je_state))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1879
                return je_return(name, rv, finish, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1880
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1881
        # if we're commonizing this object and it's already been encoded then
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1882
        # just return its encoded object id.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1883
        if desc_type in commonize and id(data) in obj_cache:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1884
                rv = obj_cache[id(data)][0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1885
                return je_return(name, rv, finish, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1886
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1887
        # find an encoder for this class, which should be:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1888
        #     <class>.getstate(obj, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1889
        encoder = getattr(desc_type, "getstate", None)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1890
        assert encoder is not None, "no json encoder for: %s" % desc_type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1891
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1892
        # encode the data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1893
        rv = encoder(data, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1894
        assert rv is not None, "json encoder returned none for: %s" % desc_type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1895
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1896
        # if we're commonizing this object, then assign it an object id and
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1897
        # save that object id and the encoded object into the object cache
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1898
        # (which is indexed by the python id for the object).
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1899
        if desc_type in commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1900
                obj_cache[id(data)] = (obj_id[0], rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1901
                rv = obj_id[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1902
                obj_id[0] += 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1903
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1904
        # return the encoded element
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1905
        return je_return(name, rv, finish, je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1906
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1907
def json_decode(name, data, desc, commonize=None, jd_state=None):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1908
        """A generic json decoder.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1909
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1910
        'name' a descriptive name of the data.  (used to identify unexpected
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1911
        data errors.)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1912
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1913
        'desc' a programmatic description of data types.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1914
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1915
        'data' data to decode."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1916
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1917
        # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1918
        if jd_state is None and json_debug:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1919
                print >> sys.stderr, "json_decode name: ", name
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1920
                print >> sys.stderr, "json_decode data: ", data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1921
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1922
        # we don't decode None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1923
        if data is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1924
                return (data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1925
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1926
        # initialize parameters to default
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1927
        if commonize is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1928
                commonize = frozenset()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1929
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1930
        if jd_state is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1931
                # this is the first invocation of this function, so when we
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1932
                # return we're done decoding data.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1933
                finish = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1934
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1935
                # first time here, initialize recursion state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1936
                if not commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1937
                        # no common state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1938
                        obj_cache = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1939
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1940
                        # load commonized state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1941
                        obj_cache = data["json_objects"]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1942
                        data = data["json_state"]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1943
                jd_state = [obj_cache, commonize]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1944
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1945
                # we're being invoked recursively.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1946
                finish = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1947
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1948
                obj_cache, commonize_old = jd_state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1949
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1950
                # check if the first object using commonization
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1951
                if not commonize_old and commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1952
                        obj_cache = data["json_objects"]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1953
                        data = data["json_state"]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1954
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1955
                # merge in any new commonize requests
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1956
                je_state_changed = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1957
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1958
                # check if we're updating the set of objects to commonize
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1959
                if not commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1960
                        commonize = commonize_old
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1961
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1962
                        # update the set of objects to commonize
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1963
                        # make a copy so we don't update our callers state.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1964
                        commonize = frozenset(commonize_old | commonize)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1965
                        je_state_changed = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1966
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1967
                if je_state_changed:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1968
                        jd_state = [obj_cache, commonize]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1969
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1970
        # verify state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1971
        assert type(name) == str, "type(name) == %s" % type(name)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1972
        assert type(obj_cache) == dict
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1973
        assert type(commonize) == frozenset
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1974
        assert type(jd_state) == list and len(jd_state) == 2
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1975
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1976
        def jd_return(name, data, desc, finish, jd_state):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1977
                """Check if we're done decoding data."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1978
                # Unused argument; pylint: disable-msg=W0613
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1979
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1980
                # check if the description is a type object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1981
                if isinstance(desc, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1982
                        desc_type = desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1983
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1984
                        # get the expected data type from the description
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1985
                        desc_type = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1986
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1987
                # get the data type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1988
                data_type = getattr(data, "__metaclass__", type(data))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1989
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1990
                # sanity check that the data type matches the description
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1991
                assert desc_type == data_type, \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1992
                    "unexpected %s for %s, expected: %s, value: %s" % \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1993
                        (data_type, name, desc_type, rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1994
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1995
                if not finish:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1996
                        return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1997
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1998
                # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1999
                if json_debug:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2000
                        print >> sys.stderr, "json_decode finished name: ", name
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2001
                        print >> sys.stderr, "json_decode finished data: ", data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2002
                return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2003
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2004
        # check if the description is a type object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2005
        if isinstance(desc, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2006
                desc_type = desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2007
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2008
                # get the expected data type from the description
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2009
                desc_type = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2010
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2011
        # we don't need to do anything for basic types
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2012
        if desc_type in json_types_immediates:
2708
4dac3e277ccf 7137406 pkg image-update could display release notes (or pointer to)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2701
diff changeset
  2013
                rv = None
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2014
                return jd_return(name, data, desc, finish, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2015
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2016
        # decode elements nested in a dictionary
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2017
        # return elements in the specified dictionary like object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2018
        if isinstance(desc, dict):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2019
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2020
                # allocate the return object.  we don't just use
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2021
                # type(desc) because that won't work for things like
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2022
                # collections.defaultdict types.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2023
                rv = desc.copy()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2024
                rv.clear()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2025
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2026
                # check if we're not decoding nested elements
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2027
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2028
                        rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2029
                        return jd_return(name, rv, desc, finish, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2030
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2031
                # lookup the first descriptor to see if we have
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2032
                # generic type description.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2033
                desc_k, desc_v = desc.items()[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2034
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2035
                # if the key in the descriptor is a type then we have
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2036
                # a generic type description that applies to all keys
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2037
                # and values in the dictionary.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2038
                # check if the description is a type object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2039
                if isinstance(desc_k, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2040
                        # there can only be one generic type desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2041
                        assert len(desc) == 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2042
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2043
                        # decode all key / value pairs
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2044
                        for k, v in data.iteritems():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2045
                                # decode the key
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2046
                                name2 = "%s[%s].key()" % (name, desc_k)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2047
                                k2 = json_decode(name2, k, desc_k,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2048
                                    jd_state=jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2049
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2050
                                # decode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2051
                                name2 = "%s[%s].value()" % (name, desc_k)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2052
                                v2 = json_decode(name2, v, desc_v,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2053
                                    jd_state=jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2054
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2055
                                # save the result
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2056
                                rv[k2] = v2
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2057
                        return jd_return(name, rv, desc, finish, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2058
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2059
                # we have element specific value type descriptions.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2060
                # copy all data and then decode the specific values
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2061
                rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2062
                for desc_k, desc_v in desc.iteritems():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2063
                        # check for the specific key
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2064
                        if desc_k not in rv:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2065
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2066
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2067
                        # decode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2068
                        name2 = "%s[%s].value()" % (name, desc_k)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2069
                        rv[desc_k] = json_decode(name2, rv[desc_k],
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2070
                            desc_v, jd_state=jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2071
                return jd_return(name, rv, desc, finish, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2072
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2073
        # decode elements nested in a list
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2074
        # return elements in the specified list like object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2075
        if isinstance(desc, (tuple, list, set, frozenset)):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2076
                # get the return type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2077
                rvtype = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2078
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2079
                # check for an empty list since we use izip_longest
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2080
                if len(data) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2081
                        rv = rvtype([])
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2082
                        return jd_return(name, rv, desc, finish, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2083
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2084
                # check if we're not encoding nested elements
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2085
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2086
                        rv = rvtype(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2087
                        return jd_return(name, rv, desc, finish, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2088
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2089
                # don't accidentally generate data via izip_longest
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2090
                assert len(data) >= len(desc), \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2091
                    "%d >= %d" % (len(data), len(desc))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2092
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2093
                rv = []
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2094
                i = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2095
                for data2, desc2 in itertools.izip_longest(data, desc,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2096
                    fillvalue=list(desc)[0]):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2097
                        name2 = "%s[%i]" % (name, i)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2098
                        i += 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2099
                        rv.append(json_decode(name2, data2, desc2,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2100
                            jd_state=jd_state))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2101
                rv = rvtype(rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2102
                return jd_return(name, rv, desc, finish, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2103
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2104
        # find a decoder for this data, which should be:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2105
        #     <class>.fromstate(state, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2106
        decoder = getattr(desc_type, "fromstate", None)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2107
        assert decoder is not None, "no json decoder for: %s" % desc_type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2108
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2109
        # if this object was commonized then get a reference to it from the
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2110
        # object cache.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2111
        if desc_type in commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2112
                assert type(data) == int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2113
                # json.dump converts integer dictionary keys into strings, so
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2114
                # obj_cache was indexed by integer strings.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2115
                data = str(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2116
                rv = obj_cache[data]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2117
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2118
                # get the data type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2119
                data_type = getattr(rv, "__metaclass__", type(rv))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2120
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2121
                if data_type != desc_type:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2122
                        # this commonized object hasn't been decoded yet
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2123
                        # decode it and update the cache with the decoded obj
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2124
                        rv = decoder(rv, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2125
                        obj_cache[data] = rv
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2126
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2127
                # decode the data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2128
                rv = decoder(data, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2129
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2130
        return jd_return(name, rv, desc, finish, jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2131
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2132
def json_validate(name, data):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2133
        """Validate that a named piece of data can be represented in json and
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2134
        that the data can be passed directly to json.dump().  If the data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2135
        can't be represented as json we'll trigger an assert.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2136
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2137
        'name' is the name of the data to validate
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2138
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2139
        'data' is the data to validate
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2140
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2141
        'recurse' is an optional integer that controls recursion.  if it's a
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2142
        negative number (the default) we recursively check any nested lists or
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2143
        dictionaries.  if it's a positive integer than we only recurse to
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2144
        the specified depth."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2145
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2146
        assert isinstance(data, json_types), \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2147
            "invalid json type \"%s\" for \"%s\", value: %s" % \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2148
            (type(data), name, str(data))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2149
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2150
        if type(data) == dict:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2151
                for k in data:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2152
                        # json.dump converts integer dictionary keys into
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2153
                        # strings, which is a bit unexpected.  so make sure we
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2154
                        # don't have any of those.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2155
                        assert type(k) != int, \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2156
                            "integer dictionary keys detected for: %s" % name
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2157
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2158
                        # validate the key and the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2159
                        new_name = "%s[%s].key()" % (name, k)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2160
                        json_validate(new_name, k)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2161
                        new_name = "%s[%s].value()" % (name, k)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2162
                        json_validate(new_name, data[k])
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2163
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2164
        if type(data) == list:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2165
                for i in range(len(data)):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2166
                        new_name = "%s[%i]" % (name, i)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2167
                        json_validate(new_name, data[i])
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2168
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2169
def json_diff(name, d0, d1, alld0, alld1):
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2170
        """Compare two json encoded objects to make sure they are
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2171
        identical, assert() if they are not."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2172
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2173
        def dbg():
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2174
                """dump debug info for json_diff"""
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2175
                def d(s):
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2176
                        """dbg helper"""
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2177
                        return json.dumps(s, sort_keys=True, indent=4)
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2178
                return "\n--- d0\n" + d(d0) + "\n+++ d1\n" + d(d1) + \
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2179
                    "\n--- alld0\n" + d(alld0) + "\n+++ alld1\n" + d(alld1)
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2180
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2181
        assert type(d0) == type(d1), ("Json data types differ for \"%s\":\n"
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2182
                "type 1: %s\ntype 2: %s\n") % (name, type(d0), type(d1)) + dbg()
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2183
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2184
        if type(d0) == dict:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2185
                assert set(d0) == set(d1), (
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2186
                   "Json dictionary keys differ for \"%s\":\n"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2187
                   "dict 1 missing: %s\n"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2188
                   "dict 2 missing: %s\n") % (name,
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2189
                   set(d1) - set(d0), set(d0) - set(d1)) + dbg()
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2190
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2191
                for k in d0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2192
                        new_name = "%s[%s]" % (name, k)
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2193
                        json_diff(new_name, d0[k], d1[k], alld0, alld1)
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2194
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2195
        if type(d0) == list:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2196
                assert len(d0) == len(d1), (
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2197
                   "Json list lengths differ for \"%s\":\n"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2198
                   "list 1 length: %s\n"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2199
                   "list 2 length: %s\n") % (name,
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2200
                   len(d0), len(d1)) + dbg()
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2201
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2202
                for i in range(len(d0)):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2203
                        new_name = "%s[%i]" % (name, i)
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2204
                        json_diff(new_name, d0[i], d1[i], alld0, alld1)
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2205
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2206
class Timer(object):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2207
        """A class which can be used for measuring process times (user,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2208
        system, and wait)."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2209
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2210
        __precision = 3
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2211
        __log_fmt = "utime: %7.3f; stime: %7.3f; wtime: %7.3f"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2212
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2213
        def __init__(self, module):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2214
                self.__module = module
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2215
                self.__timings = []
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2216
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2217
                # we initialize our time values to account for all time used
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2218
                # since the start of the process.  (user and system time are
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2219
                # obtained relative to process start time, but wall time is an
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2220
                # absolute time value so here we initialize out initial wall
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2221
                # time value to the time our process was started.)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2222
                self.__utime = self.__stime = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2223
                self.__wtime = _prstart()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2224
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2225
        def __zero1(self, delta):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2226
                """Return True if a number is zero (up to a certain level of
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2227
                precision.)"""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2228
                return int(delta * (10 ** self.__precision)) == 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2229
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2230
        def __zero(self, udelta, sdelta, wdelta):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2231
                """Return True if all the passed in values are zero."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2232
                return self.__zero1(udelta) and \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2233
                    self.__zero1(sdelta) and \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2234
                    self.__zero1(wdelta)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2235
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2236
        def __str__(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2237
                s = "\nTimings for %s: [\n" % self.__module
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2238
                utotal = stotal = wtotal = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2239
                phases = [i[0] for i in self.__timings] + ["total"]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2240
                phase_width = max([len(i) for i in phases]) + 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2241
                fmt = "  %%-%ss %s;\n" % (phase_width, Timer.__log_fmt)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2242
                for phase, udelta, sdelta, wdelta in self.__timings:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2243
                        if self.__zero(udelta, sdelta, wdelta):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2244
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2245
                        utotal += udelta
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2246
                        stotal += sdelta
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2247
                        wtotal += wdelta
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2248
                        s += fmt % (phase + ":", udelta, sdelta, wdelta)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2249
                s += fmt % ("total:", utotal, stotal, wtotal)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2250
                s += "]\n"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2251
                return s
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2252
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2253
        def reset(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2254
                """Update saved times to current process values."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2255
                self.__utime, self.__stime, self.__wtime = self.__get_time()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2256
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2257
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2258
        def __get_time():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2259
                """Get current user, system, and wait times for this
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2260
                process."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2261
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2262
                rusage = resource.getrusage(resource.RUSAGE_SELF)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2263
                utime = rusage[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2264
                stime = rusage[1]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2265
                wtime = time.time()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2266
                return (utime, stime, wtime)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2267
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2268
        def record(self, phase, logger=None):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2269
                """Record the difference between the previously saved process
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2270
                time values and the current values.  Then update the saved
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2271
                values to match the current values"""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2272
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2273
                utime, stime, wtime = self.__get_time()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2274
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2275
                udelta = utime - self.__utime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2276
                sdelta = stime - self.__stime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2277
                wdelta = wtime - self.__wtime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2278
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2279
                self.__timings.append((phase, udelta, sdelta, wdelta))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2280
                self.__utime, self.__stime, self.__wtime = utime, stime, wtime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2281
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2282
                rv = "%s: %s: " % (self.__module, phase)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2283
                rv += Timer.__log_fmt % (udelta, sdelta, wdelta)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2284
                if logger:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2285
                        logger.debug(rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2286
                return rv
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2287
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2288
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2289
class AsyncCallException(Exception):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2290
        """Exception class for AsyncCall() errors.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2291
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2292
        Any exceptions caught by the async call thread get bundled into this
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2293
        Exception because otherwise we'll lose the stack trace associated with
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2294
        the original exception."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2295
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2296
        def __init__(self, e=None):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2297
                Exception.__init__(self)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2298
                self.e = e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2299
                self.tb = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2300
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2301
        def __str__(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2302
                if self.tb:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2303
                        return str(self.tb) + str(self.e)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2304
                return str(self.e)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2305
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2306
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2307
class AsyncCall(object):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2308
        """Class which can be used to call a function asynchronously.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2309
        The call is performed via a dedicated thread."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2310
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2311
        def __init__(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2312
                self.rv = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2313
                self.e = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2314
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2315
                # keep track of what's been done
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2316
                self.started = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2317
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2318
                # internal state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2319
                self.__thread = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2320
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2321
                # pre-allocate an exception that we'll used in case everything
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2322
                # goes horribly wrong.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2323
                self.__e = AsyncCallException(
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2324
                    Exception("AsyncCall Internal Error"))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2325
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2326
        def __thread_cb(self, dummy, cb, *args, **kwargs):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2327
                """Dedicated call thread.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2328
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2329
                'dummy' is a dummy parameter that is not used.  this is done
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2330
                because the threading module (which invokes this function)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2331
                inspects the first argument of "args" to check if it's
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2332
                iterable, and that may cause bizarre failures if cb is a
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2333
                dynamically bound class (like xmlrpclib._Method).
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2334
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2335
                We need to be careful here and catch all exceptions.  Since
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2336
                we're executing in our own thread, any exceptions we don't
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2337
                catch get dumped to the console."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2338
                # Catch "Exception"; pylint: disable-msg=W0703
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2339
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2340
                try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2341
                        if DebugValues["async_thread_error"]:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2342
                                raise Exception("async_thread_error")
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2343
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2344
                        rv = e = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2345
                        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2346
                                rv = cb(*args, **kwargs)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2347
                        except Exception, e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2348
                                self.e = self.__e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2349
                                self.e.e = e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2350
                                self.e.tb = traceback.format_exc()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2351
                                return
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2352
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2353
                        self.rv = rv
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2354
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2355
                except Exception, e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2356
                        # if we raise an exception here, we're hosed
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2357
                        self.rv = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2358
                        self.e = self.__e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2359
                        self.e.e = e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2360
                        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2361
                                if DebugValues["async_thread_error"]:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2362
                                        raise Exception("async_thread_error")
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2363
                                self.e.tb = traceback.format_exc()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2364
                        except Exception:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2365
                                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2366
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2367
        def start(self, cb, *args, **kwargs):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2368
                """Start a call to an rpc server."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2369
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2370
                assert not self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2371
                self.started = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2372
                # prepare the arguments for the thread
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2373
                if args:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2374
                        args = (0, cb) + args
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2375
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2376
                        args = (0, cb)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2377
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2378
                # initialize and return the thread
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2379
                self.__thread = threading.Thread(target=self.__thread_cb,
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2380
                    args=args, kwargs=kwargs)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2381
                self.__thread.daemon = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2382
                self.__thread.start()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2383
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2384
        def join(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2385
                """Wait for an rpc call to finish."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2386
                assert self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2387
                self.__thread.join()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2388
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2389
        def is_done(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2390
                """Check if an rpc call is done."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2391
                assert self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2392
                return not self.__thread.is_alive()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2393
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2394
        def result(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2395
                """Finish a call to an rpc server."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2396
                assert self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2397
                # wait for the async call thread to exit
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2398
                self.join()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2399
                assert self.is_done()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2400
                if self.e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2401
                        # if the calling thread hit an exception, re-raise it
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2402
                        # Raising NoneType; pylint: disable-msg=E0702
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2403
                        raise self.e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2404
                return self.rv
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2405
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2406
2711
4def00cb6bbc 7179670 Use of environment variables in proxy URIs considered harmful
Tim Foster <tim.s.foster@oracle.com>
parents: 2708
diff changeset
  2407
def get_runtime_proxy(proxy, uri):
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2408
        """Given a proxy string and a URI we want to access using it, determine
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2409
        whether any OS environment variables should override that value.
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2410
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2411
        The special value "-" is returned when a no_proxy environment variable
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2412
        was found which should apply to this URI, indicating that no proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2413
        should be used at runtime."""
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2414
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2415
        runtime_proxy = proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2416
        # There is no upper case version of http_proxy, according to curl(1)
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2417
        environ_http_proxy = os.environ.get("http_proxy")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2418
        environ_https_proxy = os.environ.get("https_proxy")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2419
        environ_https_proxy_upper = os.environ.get("HTTPS_PROXY")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2420
        environ_all_proxy = os.environ.get("all_proxy")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2421
        environ_all_proxy_upper = os.environ.get("ALL_PROXY")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2422
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2423
        no_proxy = os.environ.get("no_proxy", [])
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2424
        no_proxy_upper = os.environ.get("NO_PROXY", [])
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2425
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2426
        if no_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2427
                no_proxy = no_proxy.split(",")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2428
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2429
        if no_proxy_upper:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2430
                no_proxy_upper = no_proxy_upper.split(",")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2431
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2432
        # Give precedence to protocol-specific proxies, and lowercase versions.
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2433
        if uri and uri.startswith("http") and environ_http_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2434
                runtime_proxy = environ_http_proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2435
        elif uri and uri.startswith("https") and environ_https_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2436
                runtime_proxy = environ_https_proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2437
        elif uri and uri.startswith("https") and environ_https_proxy_upper:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2438
                runtime_proxy = environ_https_proxy_upper
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2439
        elif environ_all_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2440
                runtime_proxy = environ_all_proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2441
        elif environ_all_proxy_upper:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2442
                runtime_proxy = environ_all_proxy_upper
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2443
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2444
        if no_proxy or no_proxy_upper:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2445
                # SplitResult has a netloc member; pylint: disable-msg=E1103
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2446
                netloc = urlparse.urlsplit(uri, allow_fragments=0).netloc
2764
7220dcd7755f 7189150 zone install can fail w/ proxy env vars set; tries to reach sysrepo through proxy
Tim Foster <tim.s.foster@oracle.com>
parents: 2756
diff changeset
  2447
                host = netloc.split(":")[0]
7220dcd7755f 7189150 zone install can fail w/ proxy env vars set; tries to reach sysrepo through proxy
Tim Foster <tim.s.foster@oracle.com>
parents: 2756
diff changeset
  2448
                if host in no_proxy or no_proxy == ["*"]:
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2449
                        return "-"
2764
7220dcd7755f 7189150 zone install can fail w/ proxy env vars set; tries to reach sysrepo through proxy
Tim Foster <tim.s.foster@oracle.com>
parents: 2756
diff changeset
  2450
                if host in no_proxy_upper or no_proxy_upper == ["*"]:
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2451
                        return "-"
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2452
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2453
        if not runtime_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2454
                return
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2455
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2456
        return runtime_proxy
2729
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2457
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2458
def decode(s):
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2459
        """convert non-ascii strings to unicode;
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2460
        replace non-convertable chars"""
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2461
        try:
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2462
                # this will fail if any 8 bit chars in string
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2463
                # this is a nop if string is ascii.
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2464
                s = s.encode("ascii")
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2465
        except ValueError:
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2466
                # this will encode 8 bit strings into unicode
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2467
                s = s.decode("utf-8", "replace")
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2468
        return s