src/modules/misc.py
author Xiaobo Shen <xiaobo.shen@oracle.com>
Wed, 05 Mar 2014 10:36:13 -0800
changeset 3032 d4403c135a96
parent 3031 14865bd92db6
child 3041 9846c3f45f79
permissions -rw-r--r--
17642065 stacktrace when passing invalid argument to -F (fix pylint)
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
3031
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
    23
# Copyright (c) 2007, 2014, 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
2910
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
    35
import fnmatch
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    36
import getopt
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
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
    44
import signal
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
    45
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
    46
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
    47
import struct
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
    48
import sys
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    49
import threading
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
    50
import time
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    51
import traceback
145
08bee7fd13f6 Add versioning to the protocols
Danek Duvall <danek.duvall@sun.com>
parents: 119
diff changeset
    52
import urllib
08bee7fd13f6 Add versioning to the protocols
Danek Duvall <danek.duvall@sun.com>
parents: 119
diff changeset
    53
import urlparse
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
    54
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
    55
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
    56
from collections import defaultdict
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
    57
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
    58
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
    59
    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
    60
    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
    61
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
    62
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
    63
import pkg.portable as portable
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
    64
import pkg.digest as digest
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
    65
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    66
from pkg import VERSION
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
    67
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
    68
from pkg.client.debugvalues import DebugValues
319
39b8b5c538bf 1104 want custom user-agent string
Danek Duvall <danek.duvall@sun.com>
parents: 310
diff changeset
    69
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
    70
from pkg.pkggzip import PkgGzipFile
310
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
    71
2820
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
    72
# Default path where the temporary directories will be created.
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
    73
DEFAULT_TEMP_PATH = "/var/tmp"
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
    74
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
    75
# 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
    76
MIN_WARN_DAYS = datetime.timedelta(days=30)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
    77
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
    78
# 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
    79
SIGNATURE_POLICY = "signature-policy"
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
    80
2569
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    81
# Bug URI Constants (deprecated)
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
    82
# Line too long; pylint: disable=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
    83
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
    84
BUG_URI_GUI = "https://defect.opensolaris.org/bz/enter_bug.cgi?product=pkg&component=gui"
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
    85
# pylint: enable=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
    86
2569
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    87
# 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
    88
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
    89
        """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
    90
        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
    91
        setup."""
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    92
1f9f50d0cde3 18977 pkg stack traces should tell customers to report issues to service
Shawn Walker <shawn.walker@oracle.com>
parents: 2566
diff changeset
    93
        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
    94
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
    95
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
    96
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
    97
1023
73f73773b4c9 6358 Release Notes message after Update All needs to be updated
John Rice <john.rice@sun.com>
parents: 956
diff changeset
    98
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
    99
        """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
   100
           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
   101
73f73773b4c9 6358 Release Notes message after Update All needs to be updated
John Rice <john.rice@sun.com>
parents: 956
diff changeset
   102
        # 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
   103
        # 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
   104
        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
   105
443
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   106
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
   107
        """convert seconds since epoch to %Y%m%dT%H%M%SZ format"""
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   108
        # XXX optimize?; pylint: disable=W0511
443
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   109
        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
   110
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   111
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
   112
        """convert %Y%m%dT%H%M%SZ format to seconds since epoch"""
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   113
        # XXX optimize?; pylint: disable=W0511
443
5ffa5b7dac9c 2589 pyc files generate lots of verify chaff
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 429
diff changeset
   114
        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
   115
2135
6eeb55920e13 3419 history command -- ability to specify dates or date ranges desired
Tim Foster <tim.s.foster@oracle.com>
parents: 2126
diff changeset
   116
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
   117
        """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
   118
        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
   119
462
910600c14093 45 "move" action for files that are renamed, but must be preserved
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 461
diff changeset
   120
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
   121
        """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
   122
        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
   123
        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
   124
        try:
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   125
                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
   126
        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
   127
                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
   128
                        raise
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   129
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   130
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
   131
        """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
   132
        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
   133
        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
   134
        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
   135
        """
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   136
2756
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   137
        problem = None
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   138
        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
   139
        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
   140
        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
   141
                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
   142
                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
   143
                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
   144
                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
   145
                        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
   146
                        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
   147
                        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
   148
                        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
   149
                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
   150
                        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
   151
                        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
   152
                        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
   153
                        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
   154
                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
   155
                        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
   156
                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
   157
                        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
   158
                        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
   159
                        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
   160
                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
   161
                        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
   162
                        # 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
   163
                        # 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
   164
                        # hit when archiving the test suite.
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   165
                        # E1101 Module '%s' has no '%s' member
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   166
                        # pylint: disable=E1101
2756
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   167
                        if hasattr(os, "mknod"):
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   168
                                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
   169
                        else:
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   170
                                try:
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   171
                                        sock.bind(d_path)
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   172
                                        sock.close()
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   173
                                except sock.error, _e:
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   174
                                        # Store original exception so that the
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   175
                                        # 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
   176
                                        # this fails.
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   177
                                        problem = sys.exc_info()
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   178
                                        continue
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   179
                        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
   180
                        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
   181
                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
   182
                        # 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
   183
                        # E1101 Module '%s' has no '%s' member
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   184
                        # pylint: disable=E1101
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   185
                        if hasattr(os, "mknod"):
2620
12fff26f76ad 7133778 zones p2v failing due to pkg error
Danek Duvall <danek.duvall@oracle.com>
parents: 2617
diff changeset
   186
                                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
   187
                                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
   188
                                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
   189
                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
   190
                        pass
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   191
                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
   192
                        pass
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   193
                else:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   194
                        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
   195
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   196
        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
   197
        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
   198
        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
   199
        if problem:
04d926cf9f51 7140807 sysrepo should not cache catalog responses
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2729
diff changeset
   200
                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
   201
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   202
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
   203
        """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
   204
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   205
        # 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
   206
        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
   207
                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
   208
                    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
   209
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   210
        try:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   211
                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
   212
        except EnvironmentError, e:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   213
                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
   214
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   215
                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
   216
                        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
   217
                                copytree(src, dst)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   218
                                shutil.rmtree(src)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   219
                        else:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   220
                                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
   221
                                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
   222
                                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
   223
                                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
   224
                                os.unlink(src)
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   225
                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
   226
                        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
   227
                            "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
   228
                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
   229
                        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
   230
                            "exists" % dst
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   231
                else:
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   232
                        raise
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
   233
956
adf6bdfdb3b5 6904 image.installed_file_authority always tries to open installed file r+
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 941
diff changeset
   234
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
   235
        """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
   236
        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
   237
        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
   238
        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
   239
                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
   240
                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
   241
                        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
   242
                        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
   243
        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
   244
310
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   245
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
   246
        """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
   247
310
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   248
        if u[-1] != '/':
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   249
                u = u + '/'
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   250
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   251
        return u
8fa744df663a 975 Catalogs need to know their origins
johansen <johansen@sun.com>
parents: 285
diff changeset
   252
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
   253
_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
   254
    (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
   255
    portable.util.get_os_release(), platform.version())
39b8b5c538bf 1104 want custom user-agent string
Danek Duvall <danek.duvall@sun.com>
parents: 310
diff changeset
   256
1191
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   257
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
   258
        """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
   259
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   260
        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
   261
                imgtype = IMG_NONE
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   262
        else:
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   263
                imgtype = img.type
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   264
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   265
        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
   266
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   267
        return useragent
a48bee2a4b2e 305 http_proxy value needs more checking for valid url syntax
johansen <johansen@sun.com>
parents: 1142
diff changeset
   268
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
   269
# 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
   270
_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
   271
                   |(?:\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
   272
                   |\[([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
   273
2230
76f30e842e82 15750 depot bind address should be configurable
Shawn Walker <shawn.walker@oracle.com>
parents: 2219
diff changeset
   274
_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
   275
_valid_proto = ["file", "http", "https"]
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   276
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   277
def valid_pub_prefix(prefix):
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
   278
        """Verify that the publisher prefix only contains valid characters."""
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   279
1142
64d164b5bf04 9164 misc.valid_pub_prefix and misc.valid_pub_url should handle None
Shawn Walker <srw@sun.com>
parents: 1072
diff changeset
   280
        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
   281
                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
   282
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   283
        # 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
   284
        # it comes to finding invalid characters in the prefix string.
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   285
        if _invalid_host_chars.match(prefix):
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   286
                # prefix bad chars
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   287
                return False
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   288
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   289
        if _hostname_re.match(prefix):
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   290
                return True
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   291
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   292
        return False
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   293
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   294
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
   295
        """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
   296
        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
   297
1142
64d164b5bf04 9164 misc.valid_pub_prefix and misc.valid_pub_url should handle None
Shawn Walker <srw@sun.com>
parents: 1072
diff changeset
   298
        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
   299
                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
   300
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   301
        # 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
   302
        o = urlparse.urlsplit(url)
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   303
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   304
        if not o[0] in _valid_proto:
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   305
                return False
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   306
1895
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   307
        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
   308
                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
   309
                path = urllib.url2pathname(path)
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   310
                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
   311
                        return False
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   312
                # 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
   313
                return True
0a260cc2a689 15762 client support for filesystem-based repository access
Shawn Walker <shawn.walker@oracle.com>
parents: 1836
diff changeset
   314
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   315
        # 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
   316
        host = urllib.splitport(o[1])[0]
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   317
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   318
        if proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   319
                # 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
   320
                # 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
   321
                host_parts = host.split("@")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   322
                if len(host_parts) == 2:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   323
                        host = host[1]
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
   324
327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   325
        if not host or _invalid_host_chars.match(host):
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   326
                return False
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   327
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   328
        if _hostname_re.match(host):
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   329
                return True
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   330
6c6bd07efe8d 1031 authority prefix needs validation
johansen <johansen@sun.com>
parents: 319
diff changeset
   331
        return False
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   332
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   333
def gunzip_from_stream(gz, outfile, hash_func=None, hash_funcs=None,
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   334
    ignore_hash=False):
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   335
        """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
   336
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   337
        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
   338
        is is an output stream.  gunzip_from_stream() decompresses data from
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   339
        'gz' and writes it to 'outfile', and returns the hexadecimal SHA sum
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   340
        of that data using the hash_func supplied.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   341
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   342
        'hash_funcs', if supplied, is a list of hash functions which we should
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   343
        use to compute the hash. If 'hash_funcs' is supplied, a list of
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   344
        hexadecimal digests computed using those functions is returned. The
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   345
        returned list is in the same order as 'hash_funcs'.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   346
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   347
        If 'ignore_hash' is False, we do not compute a hash when decompressing
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   348
        the content and do not return any value.
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   349
        """
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   350
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   351
        FHCRC = 2
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   352
        FEXTRA = 4
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   353
        FNAME = 8
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   354
        FCOMMENT = 16
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   355
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   356
        if not (hash_func or hash_funcs) and not ignore_hash:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   357
                raise ValueError("no hash functions for gunzip_from_stream")
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   358
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   359
        # Read the header
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   360
        magic = gz.read(2)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   361
        if magic != "\037\213":
621
6c144915eed1 551 IPS should handle socket errors
johansen <johansen@sun.com>
parents: 576
diff changeset
   362
                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
   363
        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
   364
        if method != 8:
621
6c144915eed1 551 IPS should handle socket errors
johansen <johansen@sun.com>
parents: 576
diff changeset
   365
                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
   366
        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
   367
        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
   368
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   369
        # Discard an extra field
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   370
        if flag & FEXTRA:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   371
                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
   372
                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
   373
                gz.read(xlen)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   374
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   375
        # 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
   376
        if flag & FNAME:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   377
                while True:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   378
                        s = gz.read(1)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   379
                        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
   380
                                break
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   381
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   382
        # 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
   383
        if flag & FCOMMENT:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   384
                while True:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   385
                        s = gz.read(1)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   386
                        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
   387
                                break
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   388
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   389
        # 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
   390
        if flag & FHCRC:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   391
                gz.read(2)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   392
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   393
        if ignore_hash:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   394
                pass
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   395
        elif hash_funcs:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   396
                shasums = []
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   397
                for f in hash_funcs:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   398
                        shasums.append(digest.HASH_ALGS[f]())
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   399
        else:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   400
                shasum = hash_func()
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   401
        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
   402
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   403
        while True:
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   404
                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
   405
                if buf == "":
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   406
                        ubuf = dcobj.flush()
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   407
                        if ignore_hash:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   408
                                pass
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   409
                        elif hash_funcs:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   410
                                for sha in shasums:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   411
                                        sha.update(ubuf)
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   412
                        else:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   413
                                shasum.update(ubuf) # pylint: disable=E1101
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   414
                        outfile.write(ubuf)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   415
                        break
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   416
                ubuf = dcobj.decompress(buf)
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   417
                if ignore_hash:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   418
                        pass
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   419
                elif hash_funcs:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   420
                        for sha in shasums:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   421
                                sha.update(ubuf)
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   422
                else:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   423
                        shasum.update(ubuf) # pylint: disable=E1101
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   424
                outfile.write(ubuf)
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   425
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   426
        if ignore_hash:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   427
                return
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   428
        elif hash_funcs:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   429
                hexdigests = []
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   430
                for sha in shasums:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   431
                        hexdigests.append(sha.hexdigest())
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   432
                return hexdigests
342
5e1f4d8429bf 669 Need method to print package licenses before installation
Danek Duvall <danek.duvall@sun.com>
parents: 327
diff changeset
   433
        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
   434
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   435
class PipeError(Exception):
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   436
        """ Pipe exception. """
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   437
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   438
        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
   439
                Exception.__init__(self)
1516
8c950a3b4171 10485 move pkg(5) to Python 2.6
Rich Burridge <rich.burridge@sun.com>
parents: 1507
diff changeset
   440
                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
   441
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   442
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
   443
        """ 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
   444
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   445
        try:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   446
                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
   447
        except IOError, e:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   448
                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
   449
                        raise PipeError, e
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   450
                raise
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   451
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   452
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
   453
        """ 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
   454
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   455
        try:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   456
                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
   457
        except IOError, e:
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   458
                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
   459
                        raise PipeError, e
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   460
                raise
9d4746e5dd3e 115 pkg needs to not have a cow on a SIGPIPE
Shawn Walker <swalker@opensolaris.org>
parents: 351
diff changeset
   461
791
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   462
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
   463
        """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
   464
        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
   465
        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
   466
        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
   467
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   468
        if printer is None:
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   469
                printer = emsg
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   470
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   471
        try:
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   472
                locale.setlocale(category, loc)
1318
a12f41da2189 8263 misc.setlocale should handle locale.getdefaultlocale failure
Tom Mueller <Tom.Mueller@sun.com>
parents: 1191
diff changeset
   473
                # 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
   474
                # 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
   475
                # 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
   476
                # 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
   477
                locale.getdefaultlocale()
a12f41da2189 8263 misc.setlocale should handle locale.getdefaultlocale failure
Tom Mueller <Tom.Mueller@sun.com>
parents: 1191
diff changeset
   478
        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
   479
                try:
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   480
                        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
   481
                except ValueError:
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   482
                        dl = ""
123e9401e939 5778 Inability to set locale leads to stack trace
Danek Duvall <danek.duvall@sun.com>
parents: 779
diff changeset
   483
                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
   484
                    "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
   485
                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
   486
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
   487
        """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
   488
        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
   489
        return message
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
   490
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   491
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
   492
        """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
   493
        in the largest unit possible.
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
   494
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   495
        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
   496
        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
   497
        '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
   498
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   499
        units = [
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   500
            (_("B"), _("B"), 2**10),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   501
            (_("kB"), _("k"), 2**20),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   502
            (_("MB"), _("M"), 2**30),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   503
            (_("GB"), _("G"), 2**40),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   504
            (_("TB"), _("T"), 2**50),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   505
            (_("PB"), _("P"), 2**60),
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   506
            (_("EB"), _("E"), 2**70)
451
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   507
        ]
386
5cd680466abe 2147 depot should check port instead of showing traceback if port binding fails
Shawn Walker <swalker@opensolaris.org>
parents: 384
diff changeset
   508
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   509
        for uom, shortuom, limit in units:
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   510
                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
   511
                        # 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
   512
                        # 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
   513
                        # unit of measure's range.
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   514
                        continue
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   515
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   516
                return fmt % {
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   517
                    "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
   518
                    "unit": uom,
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   519
                    "shortunit": shortuom
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
   520
                }
451
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   521
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
   522
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
   523
        """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
   524
        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
   525
        calculating depth."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   526
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
   527
        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
   528
        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
   529
                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
   530
        depth = rpath.count("/") - 1
451
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   531
        return ("../" * depth) + uri
74f1fe85fe2d 1324 RSS / Atom feeds of repository updates
Shawn Walker <shawn.walker@sun.com>
parents: 443
diff changeset
   532
487
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   533
def get_pkg_otw_size(action):
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   534
        """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
   535
        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
   536
        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
   537
        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
   538
2910
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
   539
        size = action.attrs.get("pkg.csize")
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
   540
        if size is None:
487
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   541
                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
   542
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   543
        return int(size)
9cb85c3d8491 2556 client should measure download progress by compressed size
johansen <johansen@sun.com>
parents: 462
diff changeset
   544
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   545
def get_data_digest(data, length=None, return_content=False,
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   546
    hash_attrs=None, hash_algs=None, hash_func=None):
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   547
        """Returns a tuple of ({hash attribute name: hash value}, content)
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   548
        or a tuple of (hash value, content) if hash_attrs has only one element.
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   549
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   550
        '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
   551
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   552
        '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
   553
        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
   554
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   555
        '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
   556
        second tuple value should contain the content of 'data' or
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   557
        if the content should be discarded during processing.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   558
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   559
        'hash_attrs' is a list of keys describing the hashes we want to compute
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   560
        for this data. The keys must be present in 'hash_algs', a dictionary
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   561
        mapping keys to the factory methods that are used to create objects
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   562
        to compute them. The factory method must take no parameters, and must
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   563
        return an object that has 'update()' and 'hexdigest()' methods. In the
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   564
        current implementation, these are all hashlib factory methods.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   565
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   566
        'hash_func' is provided as a convenience to simply hash the data with
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   567
        a single hash algorithm. The value of 'hash_func' should be the factory
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   568
        method used to compute that hash value, as described in the previous
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   569
        paragraph.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   570
        """
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   571
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   572
        bufsz = 128 * 1024
1974
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   573
        closefobj = False
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   574
        if isinstance(data, basestring):
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   575
                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
   576
                closefobj = True
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   577
        else:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   578
                f = data
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   579
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   580
        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
   581
                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
   582
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   583
        # Setup our results dictionary so that each attribute maps to a
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   584
        # new hashlib object.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   585
        if hash_func:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   586
                hsh = hash_func()
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   587
        else:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   588
                if hash_algs is None or hash_attrs is None:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   589
                        assert False, "get_data_digest without hash_attrs/algs"
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   590
                hash_results = {}
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   591
                for attr in hash_attrs:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   592
                        hash_results[attr] = hash_algs[attr]()
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   593
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   594
        # Read the data in chunks and compute the SHA hashes as the data comes
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   595
        # in.  A large read on some platforms (e.g. Windows XP) may fail.
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   596
        content = cStringIO.StringIO()
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   597
        while length > 0:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   598
                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
   599
                if return_content:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   600
                        content.write(data)
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   601
                if hash_func:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   602
                        hsh.update(data)
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   603
                else:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   604
                        # update each hash with this data
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   605
                        for attr in hash_attrs:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   606
                                hash_results[attr].update(
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   607
                                    data) # pylint: disable=E1101
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   608
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   609
                l = len(data)
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   610
                if l == 0:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   611
                        break
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   612
                length -= l
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   613
        content.reset()
1974
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   614
        if closefobj:
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   615
                f.close()
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   616
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   617
        if hash_func:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   618
                return hsh.hexdigest(), content.read()
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   619
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   620
        # The returned dictionary can now be populated with the hexdigests
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   621
        # instead of the hashlib objects themselves.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   622
        for attr in hash_results:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   623
                hash_results[attr] = hash_results[attr].hexdigest()
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   624
        return hash_results, content.read()
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   625
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   626
def compute_compressed_attrs(fname, file_path, data, size, compress_dir,
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   627
    bufsz=64*1024, chash_attrs=None, chash_algs=None):
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   628
        """Returns the size and one or more hashes of the compressed data.  If
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   629
        the file located at file_path doesn't exist or isn't gzipped, it creates
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   630
        a file in compress_dir named fname.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   631
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   632
        'chash_attrs' is a list of the chash attributes we should compute, with
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   633
        'chash_algs' being a dictionary that maps the attribute names to the
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   634
        algorithms used to compute them.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   635
        """
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   636
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   637
        if chash_attrs is None:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   638
                chash_attrs = digest.DEFAULT_CHASH_ATTRS
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   639
        if chash_algs is None:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   640
                chash_algs = digest.CHASH_ALGS
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   641
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   642
        #
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   643
        # 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
   644
        # 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
   645
        # 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
   646
        # 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
   647
        # 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
   648
        # 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
   649
        # properties to work right.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   650
        #
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   651
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   652
        fileneeded = True
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   653
        if file_path:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   654
                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
   655
                        fileneeded = False
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   656
                        opath = file_path
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   657
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   658
        if fileneeded:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   659
                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
   660
                ofile = PkgGzipFile(opath, "wb")
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   661
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   662
                nbuf = size / bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   663
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   664
                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
   665
                        l = n * bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   666
                        h = (n + 1) * bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   667
                        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
   668
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   669
                m = nbuf * bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   670
                ofile.write(data[m:])
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   671
                ofile.close()
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   672
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   673
        data = None
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   674
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   675
        # 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
   676
        # size.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   677
        fs = os.stat(opath)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   678
        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
   679
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   680
        # 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
   681
        # 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
   682
        # 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
   683
        # 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
   684
        # content.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   685
        cfile = open(opath, "rb")
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   686
        chashes = {}
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   687
        for chash_attr in chash_attrs:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   688
                chashes[chash_attr] = chash_algs[chash_attr]()
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   689
        while True:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   690
                cdata = cfile.read(bufsz)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   691
                if cdata == "":
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   692
                        break
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   693
                for chash_attr in chashes:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   694
                        chashes[chash_attr].update(
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   695
                            cdata) # pylint: disable=E1101
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   696
        cfile.close()
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   697
        return csize, chashes
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   698
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   699
class ProcFS(object):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   700
        """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
   701
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   702
        _ctype_formats = {
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   703
            # 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
   704
            # 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
   705
            # dictionary is:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   706
            #    <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
   707
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   708
            # 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
   709
            # 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
   710
            "char":        (1,  "c"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   711
            "char[]":      (1,  "s"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   712
            "int":         (1,  "i"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   713
            "long":        (1,  "l"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   714
            "uintptr_t":   (1,  "I"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   715
            "ushort_t":    (1,  "H"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   716
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   717
            # 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
   718
            "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
   719
            "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
   720
            "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
   721
            "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
   722
            "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
   723
            "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
   724
            "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
   725
            "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
   726
            "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
   727
            "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
   728
            "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
   729
            "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
   730
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   731
            # 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
   732
            "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
   733
        }
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
        _timestruct_desc = [
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   736
            # 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
   737
            # 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
   738
            ("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
   739
            ("long",   1, "tv_nsec"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   740
        ]
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
        _psinfo_desc = [
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   743
            # 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
   744
            # 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
   745
            ("int",         1,  "pr_flag"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   746
            ("int",         1,  "pr_nlwp"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   747
            ("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
   748
            ("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
   749
            ("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
   750
            ("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
   751
            ("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
   752
            ("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
   753
            ("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
   754
            ("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
   755
            ("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
   756
            ("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
   757
            ("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
   758
            ("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
   759
            ("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
   760
            ("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
   761
            ("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
   762
            ("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
   763
            ("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
   764
            ("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
   765
            ("char[]",      16, "pr_fname"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   766
            ("char[]",      80, "pr_psargs"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   767
            ("int",         1,  "pr_wstat"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   768
            ("int",         1,  "pr_argc"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   769
            ("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
   770
            ("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
   771
            ("char",        1,  "pr_dmodel"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   772
            ("char[]",      3,  "pr_pad2"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   773
            ("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
   774
            ("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
   775
            ("int",         1,  "pr_nzomb"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   776
            ("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
   777
            ("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
   778
            ("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
   779
            ("int",         1,  "pr_filler"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   780
        ]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   781
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   782
        _struct_descriptions = {
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   783
            # 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
   784
            # 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
   785
            #    [ <description>, <format string>, <namedtuple> ]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   786
            #
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   787
            # 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
   788
            # 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
   789
            # 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
   790
            #
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   791
            "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
   792
            "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
   793
        }
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   794
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   795
        # 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
   796
        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
   797
                desc = v[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   798
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   799
                # 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
   800
                v[1] = ""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   801
                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
   802
                        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
   803
                        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
   804
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   805
                # 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
   806
                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
   807
                    [ 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
   808
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   809
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   810
        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
   811
                """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
   812
                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
   813
                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
   814
                decode the data)."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   815
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   816
                # 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
   817
                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
   818
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   819
                # 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
   820
                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
   821
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   822
                # 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
   823
                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
   824
                        ctype = v[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   825
                        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
   826
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   827
                        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
   828
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   829
                # 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
   830
                return nt(*rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   831
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   832
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   833
        def psinfo():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   834
                """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
   835
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   836
                # 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
   837
                # 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
   838
                # 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
   839
                # 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
   840
                # 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
   841
                # fail safe.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   842
                psinfo_size = 232
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   843
                try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   844
                        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
   845
                            psinfo_size)
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   846
                # Catch "Exception"; pylint: disable=W0703
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   847
                except Exception:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   848
                        return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   849
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   850
                # 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
   851
                # unpacking it will fail.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   852
                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
   853
                        return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   854
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   855
                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
   856
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   857
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   858
def __getvmusage():
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   859
        """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
   860
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   861
        psinfo = ProcFS.psinfo()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   862
        if psinfo is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   863
                return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   864
        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
   865
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   866
def _prstart():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   867
        """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
   868
        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
   869
        psinfo = ProcFS.psinfo()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   870
        if psinfo is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   871
                return 0.0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   872
        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
   873
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   874
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
   875
        """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
   876
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   877
        # 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
   878
        # 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
   879
        vsz = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   880
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   881
                vmusage = __getvmusage()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   882
                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
   883
                        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
   884
        except (MemoryError, EnvironmentError), __e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   885
                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
   886
                    __e.errno != errno.ENOMEM:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   887
                        raise
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   888
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   889
        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
   890
                error = """\
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   891
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
   892
%(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
   893
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
   894
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
   895
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
   896
        else:
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   897
                error = """\
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   898
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
   899
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
   900
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
   901
operation again."""
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   902
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   903
        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
   904
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   905
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   906
# 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
   907
EmptyI = tuple()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   908
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   909
# 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
   910
class ImmutableDict(dict):
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   911
        # Missing docstring; pylint: disable=C0111
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   912
        # Unused argument; pylint: disable=W0613
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   913
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   914
        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
   915
                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
   916
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   917
        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
   918
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   919
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   920
        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
   921
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   922
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   923
        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
   924
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   925
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   926
        def popitem(self):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   927
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   928
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   929
        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
   930
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   931
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   932
        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
   933
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   934
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   935
        def copy(self):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   936
                return ImmutableDict()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   937
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   938
        def clear(self):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   939
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   940
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   941
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   942
        def __oops():
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   943
                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
   944
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   945
# 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
   946
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   947
class DictProperty(object):
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   948
        # Missing docstring; pylint: disable=C0111
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   949
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   950
        class __InternalProxy(object):
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   951
                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
   952
                    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
   953
                        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
   954
                        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
   955
                        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
   956
                        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
   957
                        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
   958
                        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
   959
                        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
   960
                        self.__iter = iterator
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   961
                        self.__fgetdefault = fgetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   962
                        self.__fsetdefault = fsetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   963
                        self.__update = update
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   964
                        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
   965
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   966
                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
   967
                        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
   968
                                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
   969
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   970
                        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
   971
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   972
                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
   973
                        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
   974
                                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
   975
                        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
   976
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   977
                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
   978
                        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
   979
                                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
   980
                        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
   981
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   982
                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
   983
                        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
   984
                                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
   985
                        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
   986
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   987
                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
   988
                        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
   989
                                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
   990
                        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
   991
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   992
                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
   993
                        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
   994
                                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
   995
                                    "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
   996
                        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
   997
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   998
                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
   999
                        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
  1000
                                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
  1001
                        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
  1002
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1003
                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
  1004
                        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
  1005
                                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
  1006
                        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
  1007
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1008
                def update(self, d):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1009
                        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
  1010
                                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
  1011
                        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
  1012
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1013
                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
  1014
                        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
  1015
                                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
  1016
                        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
  1017
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1018
                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
  1019
                        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
  1020
                                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
  1021
                        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
  1022
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1023
        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
  1024
            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
  1025
            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
  1026
                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
  1027
                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
  1028
                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
  1029
                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
  1030
                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
  1031
                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
  1032
                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
  1033
                self.__iter = iterator
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1034
                self.__fgetdefault = fgetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1035
                self.__fsetdefault = fsetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1036
                self.__update = update
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1037
                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
  1038
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1039
        def __get__(self, obj, objtype=None):
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1040
                # Unused argument; pylint: disable=W0613
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1041
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1042
                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
  1043
                        return self
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1044
                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
  1045
                    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
  1046
                    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
  1047
                    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
  1048
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1049
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1050
def build_cert(path, uri=None, pub=None):
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1051
        """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
  1052
        an X509 certificate object.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1053
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1054
        '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
  1055
        requires the certificate for access.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1056
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1057
        '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
  1058
        related publisher."""
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1059
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1060
        try:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1061
                cf = file(path, "rb")
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1062
                certdata = cf.read()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1063
                cf.close()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1064
        except EnvironmentError, e:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1065
                if e.errno == errno.ENOENT:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1066
                        raise api_errors.NoSuchCertificate(path, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1067
                            publisher=pub)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1068
                if e.errno == errno.EACCES:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1069
                        raise api_errors.PermissionsException(e.filename)
1431
62b6033670e4 10416 server catalog v1 support desired
Shawn Walker <srw@sun.com>
parents: 1352
diff changeset
  1070
                if e.errno == errno.EROFS:
62b6033670e4 10416 server catalog v1 support desired
Shawn Walker <srw@sun.com>
parents: 1352
diff changeset
  1071
                        raise api_errors.ReadOnlyFileSystemException(e.filename)
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1072
                raise
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1073
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1074
        try:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1075
                return osc.load_certificate(osc.FILETYPE_PEM, certdata)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1076
        except osc.Error, e:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1077
                # OpenSSL.crypto.Error
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1078
                raise api_errors.InvalidCertificate(path, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1079
                    publisher=pub)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1080
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1081
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
  1082
        """Validates the indicated certificate and returns a pyOpenSSL object
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1083
        representing it if it is valid."""
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1084
        cert = build_cert(ssl_cert, uri=uri, pub=prefix)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1085
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1086
        if cert.has_expired():
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1087
                raise api_errors.ExpiredCertificate(ssl_cert, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1088
                    publisher=prefix)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1089
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1090
        now = datetime.datetime.utcnow()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1091
        nb = cert.get_notBefore()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1092
        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
  1093
        nbdt = datetime.datetime.utcfromtimestamp(
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1094
            calendar.timegm(t))
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1095
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1096
        # PyOpenSSL's has_expired() doesn't validate the notBefore
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1097
        # time on the certificate.  Don't ask me why.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1098
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1099
        if nbdt > now:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1100
                raise api_errors.NotYetValidCertificate(ssl_cert, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1101
                    publisher=prefix)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1102
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1103
        na = cert.get_notAfter()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1104
        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
  1105
        nadt = datetime.datetime.utcfromtimestamp(
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1106
            calendar.timegm(t))
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1107
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1108
        diff = nadt - now
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1109
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1110
        if diff <= MIN_WARN_DAYS:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1111
                raise api_errors.ExpiringCertificate(ssl_cert, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1112
                    publisher=prefix, days=diff.days)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1113
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1114
        return cert
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1115
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1116
# 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
  1117
char_list = "0123456789abcdef"
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1118
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1119
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
  1120
        """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
  1121
        """
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1122
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1123
        res = ""
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1124
        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
  1125
                p = ord(p)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1126
                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
  1127
                p = p/16
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1128
                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
  1129
                res += b + a
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1130
        return res
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1131
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1132
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
  1133
        """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
  1134
        """
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1135
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1136
        res = ""
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1137
        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
  1138
                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
  1139
                    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
  1140
        return res
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1141
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1142
def config_temp_root():
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1143
        """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
  1144
        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
  1145
        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
  1146
        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
  1147
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1148
        # 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
  1149
        # 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
  1150
        # 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
  1151
        # 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
  1152
        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
  1153
        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
  1154
                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
  1155
                if env_val:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1156
                        return None
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1157
2820
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1158
        return DEFAULT_TEMP_PATH
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1159
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1160
def get_temp_root_path():
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1161
        """Return the directory path where the temporary directories or
2820
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1162
        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
  1163
        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
  1164
        default value."""
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1165
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1166
        temp_env = [ "TMPDIR", "TEMP", "TMP" ]
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1167
        for env in temp_env:
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1168
                env_val = os.getenv(env)
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1169
                if env_val:
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1170
                        return env_val
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1171
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1172
        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
  1173
2219
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1174
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
  1175
        """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
  1176
        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
  1177
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1178
        '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
  1179
        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
  1180
        """
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1181
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1182
        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
  1183
                # 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
  1184
                if not cwd:
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1185
                        uri = os.path.abspath(uri)
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1186
                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
  1187
                        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
  1188
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1189
                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
  1190
                    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
  1191
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1192
        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
  1193
            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
  1194
        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
  1195
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1196
        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
  1197
                # 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
  1198
                # 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
  1199
                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
  1200
                        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
  1201
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1202
        # 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
  1203
        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
  1204
            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
  1205
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1206
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1207
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
  1208
        """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
  1209
        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
  1210
        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
  1211
        """
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1212
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1213
        try:
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1214
                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
  1215
        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
  1216
                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
  1217
                    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
  1218
                        return
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1219
                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
  1220
                        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
  1221
                            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
  1222
                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
  1223
                        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
  1224
                            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
  1225
                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
  1226
                        raise
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1227
2043
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1228
class DummyLock(object):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1229
        """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
  1230
        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
  1231
        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
  1232
        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
  1233
        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
  1234
        provide mutual exclusion in any way whatsoever."""
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1235
        # Missing docstring; pylint: disable=C0111
2043
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1236
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1237
        def __init__(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1238
                self.held = False
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1239
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1240
        def acquire(self, blocking=1):
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1241
                # Unused argument; pylint: disable=W0613
2043
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1242
                self.held = True
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1243
                return True
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1244
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1245
        def release(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1246
                self.held = False
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1247
                return
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1248
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1249
        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
  1250
                return self.held
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1251
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1252
        @property
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1253
        def locked(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1254
                return self.held
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1255
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1256
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1257
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
  1258
        """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
  1259
        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
  1260
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1261
        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
  1262
                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
  1263
                mcs.instance = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1264
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1265
        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
  1266
                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
  1267
                        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
  1268
                            **kw)
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1269
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1270
                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
  1271
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1272
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
  1273
EmptyDict = ImmutableDict()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
  1274
1026
d4aa3ac69dc0 7878 token_byte_offset file not correctly sorted
Brock Pytlik <bpytlik@sun.com>
parents: 1023
diff changeset
  1275
# 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
  1276
# gains on certain files.
d4aa3ac69dc0 7878 token_byte_offset file not correctly sorted
Brock Pytlik <bpytlik@sun.com>
parents: 1023
diff changeset
  1277
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
  1278
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
  1279
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
  1280
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
  1281
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
  1282
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1283
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
  1284
        """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
  1285
            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
  1286
        """
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1287
        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
  1288
                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
  1289
        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
  1290
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1291
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
  1292
        """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
  1293
        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
  1294
                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
  1295
                        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
  1296
                        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
  1297
                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
  1298
                        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
  1299
                        portable.chown(path, uid, gid)
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1300
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1301
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1302
def opts_parse(op, args, opts_table, opts_mapping, usage_cb=None):
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1303
        """Generic table-based options parsing function.  Returns a tuple
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1304
        consisting of a list of parsed options in the form (option, argument)
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1305
        and the remaining unparsed options. The parsed-option list may contain
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1306
        duplicates if an option is passed multiple times.
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1307
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1308
        '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
  1309
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1310
        '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
  1311
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1312
        'opts_table' is a list of options the operation supports.
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1313
        The format of the list entries should be a tuple containing the
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1314
        option and its default value:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1315
            (option, default_value)
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1316
        It is valid to have other entries in the list when they are required
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1317
        for additional option processing elsewhere. These are ignore here. If
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1318
        the list entry is a tuple it must conform to the format oulined above.
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1319
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1320
        The default value not only represents the default value assigned to the
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1321
        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
  1322
        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
  1323
        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
  1324
        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
  1325
        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
  1326
        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
  1327
        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
  1328
        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
  1329
        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
  1330
        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
  1331
        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
  1332
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1333
        'opts_mapping' is a dict containing a mapping between the option name
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1334
        and the short and long CLI specifier for that option in the form
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1335
        { option : (short, long), ... }
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1336
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1337
        An example of a short opt is "f", which maps to a "-f" option.  An
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1338
        example of a long opt is "foo", which maps to a "--foo" option.  Option
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1339
        is the value of this option in the parsed option dictionary.
2339
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
        '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
  1342
        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
  1343
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1344
        # list for getopt long options
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1345
        opts_l_list = []
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1346
        # getopt str for short options
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1347
        opts_s_str = ""
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1348
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1349
        # 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
  1350
        opts_keys = dict()
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1351
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1352
        for entry in opts_table:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1353
                # option table contains functions for verification, ignore here
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1354
                if type(entry) != tuple:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1355
                        continue
3031
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1356
                if len(entry) == 2:
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1357
                        opt, default = entry
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1358
                elif len(entry) == 3:
3032
d4403c135a96 17642065 stacktrace when passing invalid argument to -F (fix pylint)
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 3031
diff changeset
  1359
                        opt, default, dummy_valid_args = entry
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1360
                assert opt in opts_mapping
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1361
                sopt, lopt = opts_mapping[opt]
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1362
                # make sure an option was specified
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1363
                assert sopt or lopt
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1364
                if lopt != "":
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1365
                        if default is None or type(default) == list:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1366
                                opts_l_list.append("%s=" % lopt)
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1367
                        else:
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1368
                                opts_l_list.append("%s" % lopt)
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1369
                        opts_keys["--%s" % lopt] = opt
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1370
                if sopt != "":
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1371
                        if default is None or type(default) == list:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1372
                                opts_s_str += "%s:" % sopt
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1373
                        else:
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1374
                                opts_s_str += "%s" % sopt
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1375
                        opts_keys["-%s" % sopt] = opt
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1376
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1377
        # Parse options.
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1378
        try:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1379
                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
  1380
        except getopt.GetoptError, e:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1381
                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
  1382
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1383
        def get_default(option):
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1384
                """Find the default value for a given option from opts_table."""
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1385
                for x in opts_table:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1386
                        if type(x) != tuple:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1387
                                continue
3031
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1388
                        if len(x) == 2:
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1389
                                opt, default = x
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1390
                        elif len(x) == 3:
3032
d4403c135a96 17642065 stacktrace when passing invalid argument to -F (fix pylint)
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 3031
diff changeset
  1391
                                opt, default, dummy_valid_args = x
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1392
                        if option == opt:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1393
                                return default
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1394
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1395
        # Assemble the options dictionary by passing in the right data types and
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1396
        # take care of duplicates.
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1397
        opt_dict = {}
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1398
        for x in opts:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1399
                cli_opt, arg = x
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1400
                opt = opts_keys[cli_opt]
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1401
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1402
                # Determine required option type based on the default value.
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1403
                default = get_default(opt)
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1404
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1405
                # Handle duplicates for integer and list types.
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1406
                if type(default) == int:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1407
                        if opt in opt_dict:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1408
                                opt_dict[opt] += 1
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1409
                        else:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1410
                                opt_dict[opt] = 1
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1411
                        continue
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1412
                if type(default) == list:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1413
                        if opt in opt_dict:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1414
                                opt_dict[opt].append(arg)
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1415
                        else:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1416
                                opt_dict[opt] = [arg]
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1417
                        continue
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1418
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1419
                # Boolean and string types can't be repeated.
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1420
                if opt in opt_dict:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1421
                        raise api_errors.InvalidOptionError(
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1422
                            api_errors.InvalidOptionError.OPT_REPEAT, [opt])
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1423
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1424
                # For boolean options we have to toggle the default value.
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1425
                if type(default) == bool:
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1426
                        opt_dict[opt] = not default
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1427
                else:
2875
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1428
                        opt_dict[opt] = arg
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1429
09e276ba70c6 15804991 option mangling and verification should be part of the API, not the CLI client
Erik Trauschke <Erik.Trauschke@oracle.com>
parents: 2828
diff changeset
  1430
        return opt_dict, pargs
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1431
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1432
def api_cmdpath():
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1433
        """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
  1434
        interfaces."""
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1435
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1436
        cmdpath = None
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1437
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1438
        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
  1439
                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
  1440
                    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
  1441
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1442
        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
  1443
                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
  1444
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1445
        # DebugValues is a singleton, hence no 'self' arg; pylint: disable=E1120
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1446
        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
  1447
                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
  1448
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1449
        return cmdpath
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1450
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1451
def api_pkgcmd():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1452
        """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
  1453
        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
  1454
        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
  1455
        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
  1456
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1457
        pkg_bin = "pkg"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1458
        cmdpath = api_cmdpath()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1459
        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
  1460
                try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1461
                        # 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
  1462
                        # exists and is accessible.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1463
                        os.stat(cmdpath)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1464
                        pkg_bin = cmdpath
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1465
                except OSError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1466
                        pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1467
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1468
        pkg_cmd = [pkg_bin]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1469
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1470
        # propagate debug options
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1471
        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
  1472
                pkg_cmd.append("-D")
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1473
                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
  1474
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1475
        return pkg_cmd
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1476
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1477
def liveroot():
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1478
        """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
  1479
        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
  1480
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1481
        # DebugValues is a singleton, hence no 'self' arg; pylint: disable=E1120
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1482
        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
  1483
        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
  1484
                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
  1485
        if not live_root:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1486
                live_root = "/"
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1487
        return live_root
2407
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1488
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1489
def spaceavail(path):
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1490
        """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
  1491
        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
  1492
        try:
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1493
                res = os.statvfs(path)
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1494
                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
  1495
        except OSError:
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1496
                return -1
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1497
2446
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1498
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
  1499
        """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
  1500
        try:
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1501
                return sum(
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1502
                    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
  1503
                    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
  1504
                    for fname in fnames
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1505
                )
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1506
        except EnvironmentError, e:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1507
                # Access to protected member; pylint: disable=W0212
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1508
                raise api_errors._convert_error(e)
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1509
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1510
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
  1511
    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
  1512
        """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
  1513
        in the specified output format.
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1514
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1515
        '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
  1516
        they should be output left to right.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1517
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1518
        '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
  1519
          {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1520
            field_name1: {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1521
              [(output formats), field header, initial field value]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1522
            },
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1523
            field_nameN: {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1524
              [(output formats), field header, initial field value]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1525
            }
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1526
          }
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1527
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1528
        '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
  1529
          {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1530
            field_name1: field_value,
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1531
            field_nameN: field_value
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1532
          }
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1533
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1534
        '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
  1535
        '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
  1536
        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
  1537
        parsable output.
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1538
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1539
        '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
  1540
        '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
  1541
        in 'field_data'.
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1542
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1543
        '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
  1544
        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
  1545
        format.)
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1546
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1547
        '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
  1548
        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
  1549
        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
  1550
        """
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1551
        # Missing docstring; pylint: disable=C0111
2453
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
        # Custom sort function for preserving field ordering
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1554
        def sort_fields(one, two):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1555
                return desired_field_order.index(get_header(one)) - \
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1556
                    desired_field_order.index(get_header(two))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1557
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1558
        # Functions for manipulating field_data records
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1559
        def filter_default(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1560
                return "default" in record[0]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1561
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1562
        def filter_tsv(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1563
                return "tsv" in record[0]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1564
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1565
        def get_header(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1566
                return record[1]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1567
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1568
        def get_value(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1569
                return record[2]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1570
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1571
        def quote_value(val):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1572
                if out_format == "tsv":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1573
                        # Expand tabs if tsv output requested.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1574
                        val = val.replace("\t", " " * 8)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1575
                nval = val
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1576
                # Escape bourne shell metacharacters.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1577
                for c in ("\\", " ", "\t", "\n", "'", "`", ";", "&", "(", ")",
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1578
                    "|", "^", "<", ">"):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1579
                        nval = nval.replace(c, "\\" + c)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1580
                return nval
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1581
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1582
        def set_value(entry):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1583
                val = entry[1]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1584
                multi_value = False
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1585
                if isinstance(val, (list, tuple, set, frozenset)):
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1586
                        multi_value = True
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1587
                elif val == "":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1588
                        entry[0][2] = '""'
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1589
                        return
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1590
                elif val is None:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1591
                        entry[0][2] = ''
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1592
                        return
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1593
                else:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1594
                        val = [val]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1595
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1596
                nval = []
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1597
                for v in val:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1598
                        if v == "":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1599
                                # Indicate empty string value using "".
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1600
                                nval.append('""')
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1601
                        elif v is None:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1602
                                # Indicate no value using empty string.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1603
                                nval.append('')
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1604
                        elif escape_output:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1605
                                # Otherwise, escape the value to be displayed.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1606
                                nval.append(quote_value(str(v)))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1607
                        else:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1608
                                # Caller requested value not be escaped.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1609
                                nval.append(str(v))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1610
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1611
                val = " ".join(nval)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1612
                nval = None
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1613
                if multi_value:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1614
                        val = "(%s)" % val
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1615
                entry[0][2] = val
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1616
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1617
        if out_format == "default":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1618
                # Create a formatting string for the default output
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1619
                # format.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1620
                fmt = def_fmt
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1621
                filter_func = filter_default
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1622
        elif out_format == "tsv":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1623
                # Create a formatting string for the tsv output
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1624
                # format.
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1625
                num_fields = sum(
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1626
                    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
  1627
                    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
  1628
                )
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1629
                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
  1630
                filter_func = filter_tsv
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1631
        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
  1632
                args = { "sort_keys": True }
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1633
                if out_format == "json-formatted":
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1634
                        args["indent"] = 2
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1635
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1636
                # '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
  1637
                # 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
  1638
                # is explicitly listed.
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1639
                def fmt_val(v):
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1640
                        if isinstance(v, basestring):
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1641
                                return v
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1642
                        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
  1643
                                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
  1644
                        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
  1645
                                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
  1646
                                        v[k] = fmt_val(e)
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1647
                                return v
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1648
                        return str(v)
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1649
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1650
                output = json.dumps([
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1651
                    dict(
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1652
                        (k, fmt_val(entry[k]))
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1653
                        for k in entry
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1654
                        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
  1655
                    )
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1656
                    for entry in field_values
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1657
                ], **args)
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1658
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1659
                if out_format == "json-formatted":
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1660
                        # 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
  1661
                        return output + "\n"
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1662
                return output
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1663
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1664
        # 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
  1665
        # 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
  1666
        # function.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1667
        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
  1668
            sort_fields))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1669
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1670
        # Output a header if desired.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1671
        output = ""
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1672
        if not omit_headers:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1673
                output += fmt % tuple(hdrs)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1674
                output += "\n"
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1675
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1676
        for entry in field_values:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1677
                map(set_value, (
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1678
                    (field_data[f], v)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1679
                    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
  1680
                    if f in field_data
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1681
                ))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1682
                values = map(get_value, sorted(filter(filter_func,
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1683
                    field_data.values()), sort_fields))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1684
                output += fmt % tuple(values)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1685
                output += "\n"
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1686
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1687
        return output
2690
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
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
  1690
        """Truncate the specified file."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1691
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1692
                f.truncate(size)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1693
        except IOError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1694
                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1695
        except OSError, e:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1696
                # Access to protected member; pylint: disable=W0212
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1697
                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
  1698
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1699
def flush_output():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1700
        """flush stdout and stderr"""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1701
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1702
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1703
                sys.stdout.flush()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1704
        except IOError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1705
                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1706
        except OSError, e:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1707
                # Access to protected member; pylint: disable=W0212
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1708
                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
  1709
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1710
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1711
                sys.stderr.flush()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1712
        except IOError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1713
                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1714
        except OSError, e:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1715
                # Access to protected member; pylint: disable=W0212
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1716
                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
  1717
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1718
# valid json types
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1719
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
  1720
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
  1721
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
  1722
json_debug = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1723
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1724
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
  1725
        """A generic json encoder.
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
        '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
  1728
        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
  1729
        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
  1730
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1731
        'data' data to encode.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1732
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1733
        '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
  1734
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1735
        '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
  1736
        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
  1737
        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
  1738
        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
  1739
        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
  1740
        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
  1741
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1742
        # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1743
        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
  1744
                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
  1745
                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
  1746
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1747
        # 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
  1748
        if data is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1749
                return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1750
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1751
        # initialize parameters to default
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1752
        if commonize is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1753
                commonize = frozenset()
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
        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
  1756
                # 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
  1757
                # 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
  1758
                # 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
  1759
                # 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
  1760
                # object.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1761
                finish = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1762
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1763
                # initialize recursion state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1764
                obj_id = [0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1765
                obj_cache = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1766
                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
  1767
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1768
                # 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
  1769
                # 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
  1770
                # this function).
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1771
                finish = False
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
                # get recursion state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1774
                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
  1775
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1776
                # 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
  1777
                if not commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1778
                        commonize = commonize_old
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1779
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1780
                        # 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
  1781
                        # 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
  1782
                        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
  1783
                        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
  1784
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1785
        # verify state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1786
        assert type(name) == str
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1787
        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
  1788
        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
  1789
        assert type(commonize) == frozenset
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1790
        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
  1791
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1792
        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
  1793
                """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
  1794
                the state data.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1795
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1796
                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
  1797
                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
  1798
                (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
  1799
                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
  1800
                so the hash contains:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1801
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1802
                       { 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
  1803
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1804
                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
  1805
                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
  1806
                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
  1807
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1808
                       { 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
  1809
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1810
                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
  1811
                dictionary and return that.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1812
                """
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1813
                # Unused argument; pylint: disable=W0613
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1814
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1815
                if not finish:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1816
                        return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1817
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1818
                # 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
  1819
                # 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
  1820
                # 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
  1821
                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
  1822
                obj_cache2 = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1823
                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
  1824
                        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
  1825
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1826
                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
  1827
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1828
                if DebugValues["plandesc_validate"]:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1829
                        json_validate(name, data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1830
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1831
                # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1832
                if json_debug:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1833
                        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
  1834
                        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
  1835
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1836
                return data
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
        # 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
  1839
        if isinstance(desc, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1840
                desc_type = desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1841
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1842
                # 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
  1843
                desc_type = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1844
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1845
        # get the data type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1846
        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
  1847
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1848
        # 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
  1849
        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
  1850
            "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
  1851
                (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
  1852
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1853
        # 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
  1854
        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
  1855
                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
  1856
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1857
        # 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
  1858
        # 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
  1859
        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
  1860
                # 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
  1861
                rv = {}
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.update(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
                # 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
  1869
                # generic type description.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1870
                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
  1871
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1872
                # 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
  1873
                # 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
  1874
                # 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
  1875
                # 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
  1876
                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
  1877
                        # 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
  1878
                        assert len(desc) == 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1879
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1880
                        # 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
  1881
                        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
  1882
                                # encode the key
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1883
                                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
  1884
                                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
  1885
                                    je_state=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
                                # encode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1888
                                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
  1889
                                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
  1890
                                    je_state=je_state)
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
                                # save the result
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1893
                                rv[k2] = v2
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1894
                        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
  1895
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1896
                # 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
  1897
                # encode the specific values.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1898
                rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1899
                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
  1900
                        # 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
  1901
                        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
  1902
                                continue
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
                        # encode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1905
                        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
  1906
                        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
  1907
                            je_state=je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1908
                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
  1909
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1910
        # 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
  1911
        # 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
  1912
        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
  1913
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1914
                # 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
  1915
                rv = []
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
                # 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
  1918
                if len(data) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1919
                        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
  1920
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1921
                # 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
  1922
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1923
                        rv.extend(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1924
                        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
  1925
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1926
                # 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
  1927
                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
  1928
                    "%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
  1929
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1930
                i = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1931
                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
  1932
                    fillvalue=list(desc)[0]):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1933
                        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
  1934
                        i += 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1935
                        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
  1936
                            je_state=je_state))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1937
                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
  1938
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1939
        # 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
  1940
        # 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
  1941
        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
  1942
                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
  1943
                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
  1944
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1945
        # 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
  1946
        #     <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
  1947
        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
  1948
        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
  1949
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1950
        # encode the data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1951
        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
  1952
        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
  1953
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1954
        # 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
  1955
        # 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
  1956
        # (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
  1957
        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
  1958
                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
  1959
                rv = obj_id[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1960
                obj_id[0] += 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1961
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1962
        # return the encoded element
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1963
        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
  1964
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1965
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
  1966
        """A generic json decoder.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1967
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1968
        '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
  1969
        data errors.)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1970
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1971
        '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
  1972
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1973
        'data' data to decode."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1974
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1975
        # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1976
        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
  1977
                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
  1978
                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
  1979
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1980
        # 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
  1981
        if data is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1982
                return (data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1983
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1984
        # initialize parameters to default
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1985
        if commonize is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1986
                commonize = frozenset()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1987
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1988
        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
  1989
                # 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
  1990
                # 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
  1991
                finish = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1992
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1993
                # 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
  1994
                if not commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1995
                        # no common state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1996
                        obj_cache = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1997
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1998
                        # load commonized state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1999
                        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
  2000
                        data = data["json_state"]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2001
                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
  2002
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2003
                # 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
  2004
                finish = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2005
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2006
                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
  2007
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2008
                # 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
  2009
                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
  2010
                        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
  2011
                        data = data["json_state"]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2012
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2013
                # 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
  2014
                je_state_changed = False
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
                # 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
  2017
                if not commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2018
                        commonize = commonize_old
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2019
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2020
                        # 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
  2021
                        # 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
  2022
                        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
  2023
                        je_state_changed = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2024
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2025
                if je_state_changed:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2026
                        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
  2027
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2028
        # verify state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2029
        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
  2030
        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
  2031
        assert type(commonize) == frozenset
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2032
        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
  2033
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2034
        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
  2035
                """Check if we're done decoding data."""
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  2036
                # Unused argument; pylint: disable=W0613
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2037
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, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2040
                        desc_type = desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2041
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2042
                        # 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
  2043
                        desc_type = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2044
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2045
                # get the data type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2046
                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
  2047
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2048
                # 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
  2049
                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
  2050
                    "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
  2051
                        (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
  2052
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2053
                if not finish:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2054
                        return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2055
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2056
                # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2057
                if json_debug:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2058
                        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
  2059
                        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
  2060
                return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2061
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2062
        # 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
  2063
        if isinstance(desc, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2064
                desc_type = desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2065
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2066
                # 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
  2067
                desc_type = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2068
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2069
        # 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
  2070
        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
  2071
                rv = None
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2072
                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
  2073
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2074
        # 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
  2075
        # 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
  2076
        if isinstance(desc, dict):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2077
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2078
                # 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
  2079
                # 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
  2080
                # collections.defaultdict types.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2081
                rv = desc.copy()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2082
                rv.clear()
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 decoding 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.update(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
                # 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
  2090
                # generic type description.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2091
                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
  2092
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2093
                # 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
  2094
                # 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
  2095
                # 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
  2096
                # 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
  2097
                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
  2098
                        # 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
  2099
                        assert len(desc) == 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2100
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2101
                        # 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
  2102
                        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
  2103
                                # decode the key
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2104
                                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
  2105
                                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
  2106
                                    jd_state=jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2107
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2108
                                # decode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2109
                                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
  2110
                                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
  2111
                                    jd_state=jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2112
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2113
                                # save the result
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2114
                                rv[k2] = v2
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2115
                        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
  2116
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2117
                # 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
  2118
                # 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
  2119
                rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2120
                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
  2121
                        # 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
  2122
                        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
  2123
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2124
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2125
                        # decode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2126
                        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
  2127
                        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
  2128
                            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
  2129
                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
  2130
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2131
        # 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
  2132
        # 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
  2133
        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
  2134
                # get the return type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2135
                rvtype = type(desc)
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
                # 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
  2138
                if len(data) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2139
                        rv = rvtype([])
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2140
                        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
  2141
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2142
                # 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
  2143
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2144
                        rv = rvtype(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2145
                        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
  2146
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2147
                # 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
  2148
                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
  2149
                    "%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
  2150
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2151
                rv = []
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2152
                i = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2153
                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
  2154
                    fillvalue=list(desc)[0]):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2155
                        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
  2156
                        i += 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2157
                        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
  2158
                            jd_state=jd_state))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2159
                rv = rvtype(rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2160
                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
  2161
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2162
        # 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
  2163
        #     <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
  2164
        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
  2165
        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
  2166
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2167
        # 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
  2168
        # object cache.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2169
        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
  2170
                assert type(data) == int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2171
                # 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
  2172
                # 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
  2173
                data = str(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2174
                rv = obj_cache[data]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2175
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2176
                # get the data type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2177
                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
  2178
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2179
                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
  2180
                        # 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
  2181
                        # 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
  2182
                        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
  2183
                        obj_cache[data] = rv
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2184
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2185
                # decode the data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2186
                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
  2187
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2188
        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
  2189
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2190
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
  2191
        """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
  2192
        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
  2193
        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
  2194
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2195
        '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
  2196
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2197
        '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
  2198
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2199
        '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
  2200
        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
  2201
        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
  2202
        the specified depth."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2203
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2204
        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
  2205
            "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
  2206
            (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
  2207
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2208
        if type(data) == dict:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2209
                for k in data:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2210
                        # 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
  2211
                        # 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
  2212
                        # 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
  2213
                        assert type(k) != int, \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2214
                            "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
  2215
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2216
                        # 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
  2217
                        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
  2218
                        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
  2219
                        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
  2220
                        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
  2221
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2222
        if type(data) == list:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2223
                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
  2224
                        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
  2225
                        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
  2226
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2227
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
  2228
        """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
  2229
        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
  2230
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2231
        def dbg():
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2232
                """dump debug info for json_diff"""
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2233
                def d(s):
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2234
                        """dbg helper"""
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2235
                        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
  2236
                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
  2237
                    "\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
  2238
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2239
        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
  2240
                "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
  2241
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2242
        if type(d0) == dict:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2243
                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
  2244
                   "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
  2245
                   "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
  2246
                   "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
  2247
                   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
  2248
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2249
                for k in d0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2250
                        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
  2251
                        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
  2252
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2253
        if type(d0) == list:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2254
                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
  2255
                   "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
  2256
                   "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
  2257
                   "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
  2258
                   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
  2259
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2260
                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
  2261
                        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
  2262
                        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
  2263
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2264
class Timer(object):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2265
        """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
  2266
        system, and wait)."""
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
        __precision = 3
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2269
        __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
  2270
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2271
        def __init__(self, module):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2272
                self.__module = module
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2273
                self.__timings = []
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
                # 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
  2276
                # 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
  2277
                # 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
  2278
                # 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
  2279
                # 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
  2280
                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
  2281
                self.__wtime = _prstart()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2282
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2283
        def __zero1(self, delta):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2284
                """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
  2285
                precision.)"""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2286
                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
  2287
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2288
        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
  2289
                """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
  2290
                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
  2291
                    self.__zero1(sdelta) and \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2292
                    self.__zero1(wdelta)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2293
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2294
        def __str__(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2295
                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
  2296
                utotal = stotal = wtotal = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2297
                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
  2298
                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
  2299
                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
  2300
                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
  2301
                        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
  2302
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2303
                        utotal += udelta
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2304
                        stotal += sdelta
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2305
                        wtotal += wdelta
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2306
                        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
  2307
                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
  2308
                s += "]\n"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2309
                return s
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 reset(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2312
                """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
  2313
                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
  2314
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2315
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2316
        def __get_time():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2317
                """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
  2318
                process."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2319
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2320
                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
  2321
                utime = rusage[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2322
                stime = rusage[1]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2323
                wtime = time.time()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2324
                return (utime, stime, wtime)
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 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
  2327
                """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
  2328
                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
  2329
                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
  2330
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2331
                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
  2332
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2333
                udelta = utime - self.__utime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2334
                sdelta = stime - self.__stime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2335
                wdelta = wtime - self.__wtime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2336
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2337
                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
  2338
                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
  2339
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2340
                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
  2341
                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
  2342
                if logger:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2343
                        logger.debug(rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2344
                return rv
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2345
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2346
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2347
class AsyncCallException(Exception):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2348
        """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
  2349
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2350
        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
  2351
        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
  2352
        the original exception."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2353
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2354
        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
  2355
                Exception.__init__(self)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2356
                self.e = e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2357
                self.tb = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2358
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2359
        def __str__(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2360
                if self.tb:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2361
                        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
  2362
                return str(self.e)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2363
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2364
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2365
class AsyncCall(object):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2366
        """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
  2367
        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
  2368
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2369
        def __init__(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2370
                self.rv = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2371
                self.e = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2372
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2373
                # 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
  2374
                self.started = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2375
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2376
                # internal state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2377
                self.__thread = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2378
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2379
                # 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
  2380
                # goes horribly wrong.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2381
                self.__e = AsyncCallException(
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2382
                    Exception("AsyncCall Internal Error"))
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 __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
  2385
                """Dedicated call thread.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2386
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2387
                '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
  2388
                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
  2389
                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
  2390
                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
  2391
                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
  2392
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2393
                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
  2394
                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
  2395
                catch get dumped to the console."""
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  2396
                # Catch "Exception"; pylint: disable=W0703
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2397
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2398
                try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2399
                        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
  2400
                                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
  2401
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2402
                        rv = e = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2403
                        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2404
                                rv = cb(*args, **kwargs)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2405
                        except Exception, e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2406
                                self.e = self.__e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2407
                                self.e.e = e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2408
                                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
  2409
                                return
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2410
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2411
                        self.rv = rv
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2412
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2413
                except Exception, e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2414
                        # 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
  2415
                        self.rv = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2416
                        self.e = self.__e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2417
                        self.e.e = e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2418
                        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2419
                                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
  2420
                                        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
  2421
                                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
  2422
                        except Exception:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2423
                                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2424
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2425
        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
  2426
                """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
  2427
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2428
                assert not self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2429
                self.started = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2430
                # 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
  2431
                if args:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2432
                        args = (0, cb) + args
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2433
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2434
                        args = (0, cb)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2435
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2436
                # 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
  2437
                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
  2438
                    args=args, kwargs=kwargs)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2439
                self.__thread.daemon = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2440
                self.__thread.start()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2441
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2442
        def join(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2443
                """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
  2444
                assert self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2445
                self.__thread.join()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2446
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2447
        def is_done(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2448
                """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
  2449
                assert self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2450
                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
  2451
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2452
        def result(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2453
                """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
  2454
                assert self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2455
                # 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
  2456
                self.join()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2457
                assert self.is_done()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2458
                if self.e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2459
                        # if the calling thread hit an exception, re-raise it
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  2460
                        # Raising NoneType; pylint: disable=E0702
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2461
                        raise self.e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2462
                return self.rv
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2463
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2464
2711
4def00cb6bbc 7179670 Use of environment variables in proxy URIs considered harmful
Tim Foster <tim.s.foster@oracle.com>
parents: 2708
diff changeset
  2465
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
  2466
        """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
  2467
        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
  2468
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2469
        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
  2470
        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
  2471
        should be used at runtime."""
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2472
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2473
        runtime_proxy = proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2474
        # 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
  2475
        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
  2476
        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
  2477
        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
  2478
        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
  2479
        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
  2480
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2481
        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
  2482
        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
  2483
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2484
        if no_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2485
                no_proxy = no_proxy.split(",")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2486
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2487
        if no_proxy_upper:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2488
                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
  2489
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2490
        # 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
  2491
        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
  2492
                runtime_proxy = environ_http_proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2493
        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
  2494
                runtime_proxy = environ_https_proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2495
        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
  2496
                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
  2497
        elif environ_all_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2498
                runtime_proxy = environ_all_proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2499
        elif environ_all_proxy_upper:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2500
                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
  2501
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2502
        if no_proxy or no_proxy_upper:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  2503
                # SplitResult has a netloc member; pylint: disable=E1103
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2504
                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
  2505
                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
  2506
                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
  2507
                        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
  2508
                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
  2509
                        return "-"
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2510
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2511
        if not runtime_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2512
                return
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2513
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2514
        return runtime_proxy
2729
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2515
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2516
def decode(s):
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2517
        """convert non-ascii strings to unicode;
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2518
        replace non-convertable chars"""
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2519
        try:
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2520
                # 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
  2521
                # 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
  2522
                s = s.encode("ascii")
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2523
        except ValueError:
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2524
                # 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
  2525
                s = s.decode("utf-8", "replace")
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2526
        return s
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2527
2910
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2528
def yield_matching(pat_prefix, items, patterns):
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2529
        """Helper function for yielding items that match one of the provided
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2530
        patterns."""
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2531
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2532
        if patterns:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2533
                # Normalize patterns and determine whether to glob.
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2534
                npatterns = []
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2535
                for p in patterns:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2536
                        if pat_prefix:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2537
                                pat = p.startswith(pat_prefix) and \
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2538
                                    p or (pat_prefix + p)
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2539
                        else:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2540
                                pat = p
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2541
                        if "*" in p or "?" in p:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2542
                                pat = re.compile(fnmatch.translate(pat)).match
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2543
                                glob_match = True
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2544
                        else:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2545
                                glob_match = False
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2546
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2547
                        npatterns.append((pat, glob_match))
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2548
                patterns = npatterns
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2549
                npatterns = None
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2550
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2551
        for item in items:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2552
                for (pat, glob_match) in patterns:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2553
                        if glob_match:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2554
                                if pat(item):
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2555
                                        break
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2556
                        elif item == pat:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2557
                                break
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2558
                else:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2559
                        if patterns:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2560
                                continue
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2561
                # No patterns or matched at least one.
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2562
                yield item
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2563
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2564
2906
ede940fbc11e 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies (fix pylint)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2905
diff changeset
  2565
sigdict = defaultdict(list)
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2566
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2567
def signame(signal_number):
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2568
        """convert signal number to name(s)"""
2906
ede940fbc11e 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies (fix pylint)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2905
diff changeset
  2569
        if not sigdict:
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2570
                for name in dir(signal):
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2571
                        if name.startswith("SIG") and "_" not in name:
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2572
                                sigdict[getattr(signal, name)].append(name)
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2573
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2574
        return "/".join(sigdict.get(signal_number, ["Unnamed signal: %d" %
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2575
            signal_number]))