src/modules/misc.py
author Yiteng Zhang <yiteng.zhang@oracle.com>
Fri, 04 Apr 2014 18:29:53 -0700
changeset 3053 7c1dfe878489
parent 3041 9846c3f45f79
child 3105 e25e9198193c
permissions -rw-r--r--
17478601 provide a pkg(5) private module to compute SHA512/256
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
3053
7c1dfe878489 17478601 provide a pkg(5) private module to compute SHA512/256
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3041
diff changeset
   563
        return an object that has 'update()' and 'hexdigest()' methods.
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   564
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   565
        '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
   566
        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
   567
        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
   568
        paragraph.
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   569
        """
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   570
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   571
        bufsz = 128 * 1024
1974
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   572
        closefobj = False
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   573
        if isinstance(data, basestring):
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   574
                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
   575
                closefobj = True
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   576
        else:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   577
                f = data
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   578
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   579
        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
   580
                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
   581
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   582
        # Setup our results dictionary so that each attribute maps to a
3053
7c1dfe878489 17478601 provide a pkg(5) private module to compute SHA512/256
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3041
diff changeset
   583
        # new hash object.
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   584
        if hash_func:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   585
                hsh = hash_func()
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   586
        else:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   587
                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
   588
                        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
   589
                hash_results = {}
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   590
                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
   591
                        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
   592
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   593
        # 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
   594
        # 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
   595
        content = cStringIO.StringIO()
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   596
        while length > 0:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   597
                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
   598
                if return_content:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   599
                        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
   600
                if hash_func:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   601
                        hsh.update(data)
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   602
                else:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   603
                        # 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
   604
                        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
   605
                                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
   606
                                    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
   607
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   608
                l = len(data)
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   609
                if l == 0:
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   610
                        break
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   611
                length -= l
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   612
        content.reset()
1974
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   613
        if closefobj:
6af683c5bde5 9203 pkgrecv should check for valid destination before beginning download
johansen <johansen@opensolaris.org>
parents: 1970
diff changeset
   614
                f.close()
873
b95d76c53b5e 2691 ability to publish packages to local disk repository
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 838
diff changeset
   615
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   616
        if hash_func:
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   617
                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
   618
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   619
        # The returned dictionary can now be populated with the hexdigests
3053
7c1dfe878489 17478601 provide a pkg(5) private module to compute SHA512/256
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3041
diff changeset
   620
        # instead of the hash objects themselves.
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   621
        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
   622
                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
   623
        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
   624
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   625
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
   626
    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
   627
        """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
   628
        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
   629
        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
   630
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   631
        '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
   632
        '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
   633
        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
   634
        """
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
        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
   637
                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
   638
        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
   639
                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
   640
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
        # 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
   643
        # 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
   644
        # 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
   645
        # 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
   646
        # 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
   647
        # 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
   648
        # properties to work right.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   649
        #
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
        fileneeded = True
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   652
        if file_path:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   653
                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
   654
                        fileneeded = False
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   655
                        opath = file_path
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   656
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   657
        if fileneeded:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   658
                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
   659
                ofile = PkgGzipFile(opath, "wb")
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   660
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   661
                nbuf = size / bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   662
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   663
                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
   664
                        l = n * bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   665
                        h = (n + 1) * bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   666
                        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
   667
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   668
                m = nbuf * bufsz
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   669
                ofile.write(data[m:])
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   670
                ofile.close()
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   671
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   672
        data = None
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   673
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   674
        # 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
   675
        # size.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   676
        fs = os.stat(opath)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   677
        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
   678
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   679
        # 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
   680
        # 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
   681
        # 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
   682
        # 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
   683
        # content.
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   684
        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
   685
        chashes = {}
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   686
        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
   687
                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
   688
        while True:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   689
                cdata = cfile.read(bufsz)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   690
                if cdata == "":
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   691
                        break
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   692
                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
   693
                        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
   694
                            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
   695
        cfile.close()
2962
ce8cd4c07986 15433013 content hash handling should handle different hash functions
Tim Foster <tim.s.foster@oracle.com>
parents: 2910
diff changeset
   696
        return csize, chashes
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   697
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   698
class ProcFS(object):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   699
        """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
   700
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   701
        _ctype_formats = {
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   702
            # 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
   703
            # 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
   704
            # dictionary is:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   705
            #    <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
   706
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   707
            # 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
   708
            # 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
   709
            "char":        (1,  "c"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   710
            "char[]":      (1,  "s"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   711
            "int":         (1,  "i"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   712
            "long":        (1,  "l"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   713
            "uintptr_t":   (1,  "I"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   714
            "ushort_t":    (1,  "H"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   715
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   716
            # 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
   717
            "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
   718
            "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
   719
            "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
   720
            "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
   721
            "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
   722
            "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
   723
            "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
   724
            "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
   725
            "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
   726
            "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
   727
            "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
   728
            "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
   729
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   730
            # 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
   731
            "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
   732
        }
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
        _timestruct_desc = [
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   735
            # 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
   736
            # 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
   737
            ("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
   738
            ("long",   1, "tv_nsec"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   739
        ]
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
        _psinfo_desc = [
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   742
            # 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
   743
            # 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
   744
            ("int",         1,  "pr_flag"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   745
            ("int",         1,  "pr_nlwp"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   746
            ("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
   747
            ("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
   748
            ("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
   749
            ("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
   750
            ("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
   751
            ("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
   752
            ("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
   753
            ("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
   754
            ("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
   755
            ("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
   756
            ("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
   757
            ("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
   758
            ("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
   759
            ("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
   760
            ("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
   761
            ("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
   762
            ("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
   763
            ("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
   764
            ("char[]",      16, "pr_fname"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   765
            ("char[]",      80, "pr_psargs"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   766
            ("int",         1,  "pr_wstat"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   767
            ("int",         1,  "pr_argc"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   768
            ("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
   769
            ("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
   770
            ("char",        1,  "pr_dmodel"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   771
            ("char[]",      3,  "pr_pad2"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   772
            ("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
   773
            ("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
   774
            ("int",         1,  "pr_nzomb"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   775
            ("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
   776
            ("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
   777
            ("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
   778
            ("int",         1,  "pr_filler"),
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   779
        ]
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
        _struct_descriptions = {
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   782
            # 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
   783
            # 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
   784
            #    [ <description>, <format string>, <namedtuple> ]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   785
            #
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   786
            # 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
   787
            # 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
   788
            # 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
   789
            #
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   790
            "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
   791
            "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
   792
        }
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
        # 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
   795
        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
   796
                desc = v[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   797
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   798
                # 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
   799
                v[1] = ""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   800
                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
   801
                        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
   802
                        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
   803
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   804
                # 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
   805
                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
   806
                    [ 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
   807
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   808
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   809
        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
   810
                """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
   811
                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
   812
                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
   813
                decode the data)."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   814
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   815
                # 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
   816
                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
   817
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   818
                # 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
   819
                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
   820
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   821
                # 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
   822
                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
   823
                        ctype = v[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   824
                        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
   825
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   826
                        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
   827
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   828
                # 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
   829
                return nt(*rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   830
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   831
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   832
        def psinfo():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   833
                """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
   834
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   835
                # 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
   836
                # 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
   837
                # 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
   838
                # 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
   839
                # 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
   840
                # fail safe.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   841
                psinfo_size = 232
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   842
                try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   843
                        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
   844
                            psinfo_size)
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   845
                # 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
   846
                except Exception:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   847
                        return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   848
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   849
                # 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
   850
                # unpacking it will fail.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   851
                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
   852
                        return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   853
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   854
                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
   855
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   856
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   857
def __getvmusage():
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   858
        """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
   859
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   860
        psinfo = ProcFS.psinfo()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   861
        if psinfo is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   862
                return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   863
        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
   864
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   865
def _prstart():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   866
        """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
   867
        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
   868
        psinfo = ProcFS.psinfo()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   869
        if psinfo is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   870
                return 0.0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   871
        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
   872
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   873
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
   874
        """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
   875
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   876
        # 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
   877
        # 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
   878
        vsz = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   879
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   880
                vmusage = __getvmusage()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   881
                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
   882
                        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
   883
        except (MemoryError, EnvironmentError), __e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   884
                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
   885
                    __e.errno != errno.ENOMEM:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   886
                        raise
1035
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   887
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   888
        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
   889
                error = """\
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   890
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
   891
%(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
   892
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
   893
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
   894
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
   895
        else:
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   896
                error = """\
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   897
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
   898
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
   899
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
   900
operation again."""
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   901
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   902
        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
   903
0e243b7eb121 2297 top level error handler for out of memory conditions needed
Danek Duvall <danek.duvall@sun.com>
parents: 1026
diff changeset
   904
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   905
# 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
   906
EmptyI = tuple()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   907
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   908
# 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
   909
class ImmutableDict(dict):
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   910
        # Missing docstring; pylint: disable=C0111
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   911
        # 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
   912
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   913
        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
   914
                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
   915
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   916
        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
   917
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   918
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   919
        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
   920
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   921
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   922
        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
   923
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   924
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   925
        def popitem(self):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   926
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   927
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   928
        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
   929
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   930
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   931
        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
   932
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   933
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   934
        def copy(self):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   935
                return ImmutableDict()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   936
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   937
        def clear(self):
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   938
                self.__oops()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   939
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   940
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
   941
        def __oops():
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
   942
                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
   943
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   944
# 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
   945
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   946
class DictProperty(object):
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
   947
        # 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
   948
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   949
        class __InternalProxy(object):
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   950
                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
   951
                    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
   952
                        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
   953
                        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
   954
                        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
   955
                        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
   956
                        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
   957
                        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
   958
                        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
   959
                        self.__iter = iterator
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   960
                        self.__fgetdefault = fgetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   961
                        self.__fsetdefault = fsetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   962
                        self.__update = update
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   963
                        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
   964
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   965
                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
   966
                        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
   967
                                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
   968
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   969
                        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
   970
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   971
                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
   972
                        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
   973
                                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
   974
                        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
   975
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   976
                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
   977
                        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
   978
                                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
   979
                        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
   980
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   981
                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
   982
                        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
   983
                                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
   984
                        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
   985
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   986
                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
   987
                        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
   988
                                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
   989
                        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
   990
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
   991
                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
   992
                        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
   993
                                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
   994
                                    "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
   995
                        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
   996
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
   997
                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
   998
                        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
   999
                                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
  1000
                        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
  1001
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1002
                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
  1003
                        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
  1004
                                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
  1005
                        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
  1006
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1007
                def update(self, d):
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1008
                        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
  1009
                                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
  1010
                        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
  1011
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1012
                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
  1013
                        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
  1014
                                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
  1015
                        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
  1016
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1017
                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
  1018
                        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
  1019
                                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
  1020
                        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
  1021
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1022
        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
  1023
            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
  1024
            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
  1025
                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
  1026
                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
  1027
                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
  1028
                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
  1029
                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
  1030
                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
  1031
                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
  1032
                self.__iter = iterator
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1033
                self.__fgetdefault = fgetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1034
                self.__fsetdefault = fsetdefault
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1035
                self.__update = update
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1036
                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
  1037
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1038
        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
  1039
                # 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
  1040
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1041
                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
  1042
                        return self
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1043
                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
  1044
                    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
  1045
                    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
  1046
                    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
  1047
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1048
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1049
def build_cert(path, uri=None, pub=None):
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1050
        """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
  1051
        an X509 certificate object.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1052
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1053
        '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
  1054
        requires the certificate for access.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1055
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1056
        '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
  1057
        related publisher."""
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1058
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1059
        try:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1060
                cf = file(path, "rb")
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1061
                certdata = cf.read()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1062
                cf.close()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1063
        except EnvironmentError, e:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1064
                if e.errno == errno.ENOENT:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1065
                        raise api_errors.NoSuchCertificate(path, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1066
                            publisher=pub)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1067
                if e.errno == errno.EACCES:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1068
                        raise api_errors.PermissionsException(e.filename)
1431
62b6033670e4 10416 server catalog v1 support desired
Shawn Walker <srw@sun.com>
parents: 1352
diff changeset
  1069
                if e.errno == errno.EROFS:
62b6033670e4 10416 server catalog v1 support desired
Shawn Walker <srw@sun.com>
parents: 1352
diff changeset
  1070
                        raise api_errors.ReadOnlyFileSystemException(e.filename)
926
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1071
                raise
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1072
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1073
        try:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1074
                return osc.load_certificate(osc.FILETYPE_PEM, certdata)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1075
        except osc.Error, e:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1076
                # OpenSSL.crypto.Error
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1077
                raise api_errors.InvalidCertificate(path, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1078
                    publisher=pub)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1079
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1080
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
  1081
        """Validates the indicated certificate and returns a pyOpenSSL object
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1082
        representing it if it is valid."""
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1083
        cert = build_cert(ssl_cert, uri=uri, pub=prefix)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1084
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1085
        if cert.has_expired():
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1086
                raise api_errors.ExpiredCertificate(ssl_cert, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1087
                    publisher=prefix)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1088
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1089
        now = datetime.datetime.utcnow()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1090
        nb = cert.get_notBefore()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1091
        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
  1092
        nbdt = datetime.datetime.utcfromtimestamp(
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1093
            calendar.timegm(t))
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1094
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1095
        # PyOpenSSL's has_expired() doesn't validate the notBefore
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1096
        # time on the certificate.  Don't ask me why.
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1097
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1098
        if nbdt > now:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1099
                raise api_errors.NotYetValidCertificate(ssl_cert, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1100
                    publisher=prefix)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1101
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1102
        na = cert.get_notAfter()
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1103
        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
  1104
        nadt = datetime.datetime.utcfromtimestamp(
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1105
            calendar.timegm(t))
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1106
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1107
        diff = nadt - now
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1108
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1109
        if diff <= MIN_WARN_DAYS:
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1110
                raise api_errors.ExpiringCertificate(ssl_cert, uri=uri,
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1111
                    publisher=prefix, days=diff.days)
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1112
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1113
        return cert
6ee411c9026a 5871 publisher apis desired
Shawn Walker <Shawn.Walker@Sun.COM>
parents: 873
diff changeset
  1114
2026
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1115
# 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
  1116
char_list = "0123456789abcdef"
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1117
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1118
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
  1119
        """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
  1120
        """
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
        res = ""
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1123
        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
  1124
                p = ord(p)
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1125
                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
  1126
                p = p/16
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1127
                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
  1128
                res += b + a
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1129
        return res
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1130
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1131
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
  1132
        """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
  1133
        """
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
        res = ""
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1136
        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
  1137
                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
  1138
                    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
  1139
        return res
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1140
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1141
def config_temp_root():
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1142
        """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
  1143
        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
  1144
        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
  1145
        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
  1146
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1147
        # 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
  1148
        # 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
  1149
        # 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
  1150
        # 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
  1151
        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
  1152
        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
  1153
                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
  1154
                if env_val:
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1155
                        return None
d1b30615bc99 9196 pkg(5) should have support for cryptographic manifest signatures
Brock Pytlik <bpytlik@sun.com>
parents: 1974
diff changeset
  1156
2820
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1157
        return DEFAULT_TEMP_PATH
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1158
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1159
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
  1160
        """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
  1161
        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
  1162
        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
  1163
        default value."""
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1164
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1165
        temp_env = [ "TMPDIR", "TEMP", "TMP" ]
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1166
        for env in temp_env:
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1167
                env_val = os.getenv(env)
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1168
                if env_val:
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1169
                        return env_val
8c33db61e6a6 7197510 pkgrecv error handling traces back
thejaswini.k@oracle.com
parents: 2764
diff changeset
  1170
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
  1171
        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
  1172
2219
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1173
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
  1174
        """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
  1175
        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
  1176
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1177
        '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
  1178
        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
  1179
        """
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
        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
  1182
                # 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
  1183
                if not cwd:
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1184
                        uri = os.path.abspath(uri)
60ad60f7592c 2152 standalone package support needed (on-disk format)
Shawn Walker <shawn.walker@oracle.com>
parents: 2197
diff changeset
  1185
                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
  1186
                        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
  1187
2028
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1188
                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
  1189
                    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
  1190
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1191
        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
  1192
            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
  1193
        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
  1194
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1195
        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
  1196
                # 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
  1197
                # 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
  1198
                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
  1199
                        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
  1200
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1201
        # 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
  1202
        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
  1203
            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
  1204
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
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
  1207
        """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
  1208
        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
  1209
        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
  1210
        """
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
        try:
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1213
                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
  1214
        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
  1215
                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
  1216
                    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
  1217
                        return
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1218
                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
  1219
                        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
  1220
                            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
  1221
                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
  1222
                        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
  1223
                            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
  1224
                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
  1225
                        raise
b2c674e6ee28 16744 repository multi-publisher on-disk format should be formalized and implemented
Shawn Walker <shawn.walker@oracle.com>
parents: 2026
diff changeset
  1226
2043
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1227
class DummyLock(object):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1228
        """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
  1229
        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
  1230
        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
  1231
        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
  1232
        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
  1233
        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
  1234
        # 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
  1235
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1236
        def __init__(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1237
                self.held = False
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1238
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1239
        def acquire(self, blocking=1):
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1240
                # 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
  1241
                self.held = True
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1242
                return True
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1243
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1244
        def release(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1245
                self.held = False
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1246
                return
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1247
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1248
        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
  1249
                return self.held
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1250
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1251
        @property
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1252
        def locked(self):
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1253
                return self.held
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1254
852501e586d0 14615 image-update claims failure for un-aquired lock and still succeeds
johansen <johansen@opensolaris.org>
parents: 2028
diff changeset
  1255
1505
cc598d70bbbe 4425 pkg install should deal w/ complex dependency changes in one install
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 1452
diff changeset
  1256
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
  1257
        """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
  1258
        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
  1259
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1260
        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
  1261
                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
  1262
                mcs.instance = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1263
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1264
        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
  1265
                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
  1266
                        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
  1267
                            **kw)
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1268
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1269
                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
  1270
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1271
838
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
  1272
EmptyDict = ImmutableDict()
aafbe4737188 3245 need to support creation of multi-architecture (fat) packages
Bart Smaalders <Bart.Smaalders@Sun.COM>
parents: 835
diff changeset
  1273
1026
d4aa3ac69dc0 7878 token_byte_offset file not correctly sorted
Brock Pytlik <bpytlik@sun.com>
parents: 1023
diff changeset
  1274
# 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
  1275
# gains on certain files.
d4aa3ac69dc0 7878 token_byte_offset file not correctly sorted
Brock Pytlik <bpytlik@sun.com>
parents: 1023
diff changeset
  1276
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
  1277
2580
92480b147363 2707 directory salvage doesn't deal with special files well
Danek Duvall <danek.duvall@oracle.com>
parents: 2569
diff changeset
  1278
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
  1279
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
  1280
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
  1281
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1282
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
  1283
        """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
  1284
            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
  1285
        """
5e0694e53b38 17225 license files mia on image-update from 149 to 150
Shawn Walker <shawn.walker@oracle.com>
parents: 2097
diff changeset
  1286
        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
  1287
                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
  1288
        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
  1289
ce10607d5332 11684 desire option to not propagate certs to non-global zones
Brock Pytlik <brock.pytlik@oracle.com>
parents: 2230
diff changeset
  1290
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
  1291
        """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
  1292
        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
  1293
                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
  1294
                        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
  1295
                        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
  1296
                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
  1297
                        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
  1298
                        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
  1299
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
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
  1302
        """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
  1303
        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
  1304
        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
  1305
        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
  1306
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1307
        '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
  1308
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1309
        '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
  1310
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
  1311
        '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
  1312
        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
  1313
        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
  1314
            (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
  1315
        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
  1316
        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
  1317
        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
  1318
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
        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
  1320
        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
  1321
        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
  1322
        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
  1323
        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
  1324
        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
  1325
        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
  1326
        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
  1327
        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
  1328
        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
  1329
        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
  1330
        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
  1331
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
  1332
        '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
  1333
        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
  1334
        { 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
  1335
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
        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
  1337
        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
  1338
        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
  1339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1340
        '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
  1341
        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
  1342
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
  1343
        # 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
  1344
        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
  1345
        # 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
  1346
        opts_s_str = ""
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1347
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1348
        # 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
  1349
        opts_keys = dict()
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1350
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
  1351
        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
  1352
                # 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
  1353
                if type(entry) != tuple:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1354
                        continue
3031
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1355
                if len(entry) == 2:
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1356
                        opt, default = entry
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1357
                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
  1358
                        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
  1359
                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
  1360
                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
  1361
                # 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
  1362
                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
  1363
                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
  1364
                        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
  1365
                                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
  1366
                        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
  1367
                                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
  1368
                        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
  1369
                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
  1370
                        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
  1371
                                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
  1372
                        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
  1373
                                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
  1374
                        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
  1375
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
        # Parse options.
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1377
        try:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1378
                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
  1379
        except getopt.GetoptError, e:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1380
                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
  1381
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
  1382
        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
  1383
                """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
  1384
                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
  1385
                        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
  1386
                                continue
3031
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1387
                        if len(x) == 2:
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1388
                                opt, default = x
14865bd92db6 17642065 stacktrace when passing invalid argument to -F
Xiaobo Shen <xiaobo.shen@oracle.com>
parents: 2962
diff changeset
  1389
                        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
  1390
                                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
  1391
                        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
  1392
                                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
  1393
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
        # 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
  1395
        # 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
  1396
        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
  1397
        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
  1398
                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
  1399
                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
  1400
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
                # 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
  1402
                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
  1403
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
                # 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
  1405
                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
  1406
                        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
  1407
                                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
  1408
                        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
  1409
                                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
  1410
                        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
  1411
                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
  1412
                        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
  1413
                                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
  1414
                        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
  1415
                                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
  1416
                        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
  1417
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
                # 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
  1419
                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
  1420
                        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
  1421
                            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
  1422
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
                # 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
  1424
                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
  1425
                        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
  1426
                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
  1427
                        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
  1428
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
        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
  1430
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1431
def api_cmdpath():
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1432
        """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
  1433
        interfaces."""
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1434
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1435
        cmdpath = None
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1436
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1437
        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
  1438
                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
  1439
                    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
  1440
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1441
        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
  1442
                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
  1443
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1444
        # 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
  1445
        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
  1446
                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
  1447
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1448
        return cmdpath
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1449
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1450
def api_pkgcmd():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1451
        """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
  1452
        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
  1453
        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
  1454
        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
  1455
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1456
        pkg_bin = "pkg"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1457
        cmdpath = api_cmdpath()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1458
        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
  1459
                try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1460
                        # 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
  1461
                        # exists and is accessible.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1462
                        os.stat(cmdpath)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1463
                        pkg_bin = cmdpath
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1464
                except OSError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1465
                        pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1466
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1467
        pkg_cmd = [pkg_bin]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1468
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1469
        # propagate debug options
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1470
        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
  1471
                pkg_cmd.append("-D")
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1472
                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
  1473
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1474
        return pkg_cmd
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1475
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1476
def liveroot():
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1477
        """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
  1478
        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
  1479
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1480
        # 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
  1481
        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
  1482
        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
  1483
                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
  1484
        if not live_root:
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1485
                live_root = "/"
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2329
diff changeset
  1486
        return live_root
2407
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1487
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1488
def spaceavail(path):
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1489
        """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
  1490
        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
  1491
        try:
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1492
                res = os.statvfs(path)
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1493
                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
  1494
        except OSError:
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1495
                return -1
a831f1491c86 1769 pkg(5) doesn't check for disk space
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2339
diff changeset
  1496
2446
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1497
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
  1498
        """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
  1499
        try:
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1500
                return sum(
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1501
                    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
  1502
                    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
  1503
                    for fname in fnames
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1504
                )
ba222bc0b1ce 18625 new disk space information needs improved formatting, wording
Shawn Walker <shawn.walker@oracle.com>
parents: 2430
diff changeset
  1505
        except EnvironmentError, e:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1506
                # 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
  1507
                raise api_errors._convert_error(e)
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1508
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1509
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
  1510
    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
  1511
        """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
  1512
        in the specified output format.
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1513
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1514
        '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
  1515
        they should be output left to right.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1516
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1517
        '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
  1518
          {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1519
            field_name1: {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1520
              [(output formats), field header, initial field value]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1521
            },
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1522
            field_nameN: {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1523
              [(output formats), field header, initial field value]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1524
            }
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
        '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
  1528
          {
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1529
            field_name1: field_value,
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1530
            field_nameN: field_value
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1531
          }
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1532
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1533
        '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
  1534
        '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
  1535
        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
  1536
        parsable output.
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1537
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1538
        '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
  1539
        '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
  1540
        in 'field_data'.
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1541
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1542
        '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
  1543
        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
  1544
        format.)
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1545
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1546
        '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
  1547
        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
  1548
        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
  1549
        """
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1550
        # Missing docstring; pylint: disable=C0111
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1551
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1552
        # Custom sort function for preserving field ordering
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1553
        def sort_fields(one, two):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1554
                return desired_field_order.index(get_header(one)) - \
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1555
                    desired_field_order.index(get_header(two))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1556
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1557
        # Functions for manipulating field_data records
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1558
        def filter_default(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1559
                return "default" in record[0]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1560
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1561
        def filter_tsv(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1562
                return "tsv" in record[0]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1563
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1564
        def get_header(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1565
                return record[1]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1566
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1567
        def get_value(record):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1568
                return record[2]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1569
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1570
        def quote_value(val):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1571
                if out_format == "tsv":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1572
                        # Expand tabs if tsv output requested.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1573
                        val = val.replace("\t", " " * 8)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1574
                nval = val
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1575
                # Escape bourne shell metacharacters.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1576
                for c in ("\\", " ", "\t", "\n", "'", "`", ";", "&", "(", ")",
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1577
                    "|", "^", "<", ">"):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1578
                        nval = nval.replace(c, "\\" + c)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1579
                return nval
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1580
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1581
        def set_value(entry):
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1582
                val = entry[1]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1583
                multi_value = False
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1584
                if isinstance(val, (list, tuple, set, frozenset)):
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1585
                        multi_value = True
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1586
                elif val == "":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1587
                        entry[0][2] = '""'
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1588
                        return
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1589
                elif val is None:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1590
                        entry[0][2] = ''
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1591
                        return
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1592
                else:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1593
                        val = [val]
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1594
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1595
                nval = []
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1596
                for v in val:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1597
                        if v == "":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1598
                                # Indicate empty string value using "".
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1599
                                nval.append('""')
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1600
                        elif v is None:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1601
                                # Indicate no value using empty string.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1602
                                nval.append('')
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1603
                        elif escape_output:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1604
                                # Otherwise, escape the value to be displayed.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1605
                                nval.append(quote_value(str(v)))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1606
                        else:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1607
                                # Caller requested value not be escaped.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1608
                                nval.append(str(v))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1609
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1610
                val = " ".join(nval)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1611
                nval = None
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1612
                if multi_value:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1613
                        val = "(%s)" % val
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1614
                entry[0][2] = val
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1615
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1616
        if out_format == "default":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1617
                # Create a formatting string for the default output
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1618
                # format.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1619
                fmt = def_fmt
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1620
                filter_func = filter_default
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1621
        elif out_format == "tsv":
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1622
                # Create a formatting string for the tsv output
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1623
                # format.
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1624
                num_fields = sum(
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1625
                    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
  1626
                    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
  1627
                )
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1628
                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
  1629
                filter_func = filter_tsv
2510
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1630
        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
  1631
                args = { "sort_keys": True }
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1632
                if out_format == "json-formatted":
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1633
                        args["indent"] = 2
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1634
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1635
                # '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
  1636
                # 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
  1637
                # is explicitly listed.
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1638
                def fmt_val(v):
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1639
                        if isinstance(v, basestring):
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1640
                                return v
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1641
                        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
  1642
                                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
  1643
                        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
  1644
                                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
  1645
                                        v[k] = fmt_val(e)
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1646
                                return v
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1647
                        return str(v)
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1648
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1649
                output = json.dumps([
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1650
                    dict(
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1651
                        (k, fmt_val(entry[k]))
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1652
                        for k in entry
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1653
                        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
  1654
                    )
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1655
                    for entry in field_values
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1656
                ], **args)
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1657
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1658
                if out_format == "json-formatted":
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1659
                        # 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
  1660
                        return output + "\n"
f48530fd135d 18440 pkgrepo should be able to list packages
Shawn Walker <shawn.walker@oracle.com>
parents: 2453
diff changeset
  1661
                return output
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1662
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1663
        # 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
  1664
        # 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
  1665
        # function.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1666
        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
  1667
            sort_fields))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1668
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1669
        # Output a header if desired.
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1670
        output = ""
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1671
        if not omit_headers:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1672
                output += fmt % tuple(hdrs)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1673
                output += "\n"
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1674
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1675
        for entry in field_values:
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1676
                map(set_value, (
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1677
                    (field_data[f], v)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1678
                    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
  1679
                    if f in field_data
2453
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1680
                ))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1681
                values = map(get_value, sorted(filter(filter_func,
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1682
                    field_data.values()), sort_fields))
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1683
                output += fmt % tuple(values)
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1684
                output += "\n"
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1685
330443795456 18619 link mediation support needed
Shawn Walker <shawn.walker@oracle.com>
parents: 2446
diff changeset
  1686
        return output
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1687
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1688
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
  1689
        """Truncate the specified file."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1690
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1691
                f.truncate(size)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1692
        except IOError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1693
                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1694
        except OSError, e:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1695
                # 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
  1696
                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
  1697
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1698
def flush_output():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1699
        """flush stdout and stderr"""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1700
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1701
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1702
                sys.stdout.flush()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1703
        except IOError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1704
                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1705
        except OSError, e:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1706
                # 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
  1707
                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
  1708
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1709
        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1710
                sys.stderr.flush()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1711
        except IOError:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1712
                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1713
        except OSError, e:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1714
                # 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
  1715
                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
  1716
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1717
# valid json types
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1718
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
  1719
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
  1720
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
  1721
json_debug = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1722
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1723
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
  1724
        """A generic json encoder.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1725
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1726
        '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
  1727
        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
  1728
        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
  1729
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1730
        'data' data to encode.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1731
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1732
        '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
  1733
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1734
        '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
  1735
        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
  1736
        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
  1737
        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
  1738
        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
  1739
        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
  1740
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1741
        # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1742
        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
  1743
                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
  1744
                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
  1745
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1746
        # 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
  1747
        if data is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1748
                return None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1749
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1750
        # initialize parameters to default
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1751
        if commonize is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1752
                commonize = frozenset()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1753
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1754
        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
  1755
                # 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
  1756
                # 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
  1757
                # 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
  1758
                # 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
  1759
                # object.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1760
                finish = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1761
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1762
                # initialize recursion state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1763
                obj_id = [0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1764
                obj_cache = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1765
                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
  1766
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1767
                # 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
  1768
                # 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
  1769
                # this function).
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1770
                finish = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1771
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1772
                # get recursion state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1773
                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
  1774
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1775
                # 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
  1776
                if not commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1777
                        commonize = commonize_old
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1778
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1779
                        # 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
  1780
                        # 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
  1781
                        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
  1782
                        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
  1783
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1784
        # verify state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1785
        assert type(name) == str
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1786
        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
  1787
        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
  1788
        assert type(commonize) == frozenset
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1789
        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
  1790
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1791
        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
  1792
                """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
  1793
                the state data.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1794
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1795
                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
  1796
                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
  1797
                (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
  1798
                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
  1799
                so the hash contains:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1800
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1801
                       { 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
  1802
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1803
                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
  1804
                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
  1805
                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
  1806
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1807
                       { 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
  1808
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1809
                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
  1810
                dictionary and return that.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1811
                """
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2826
diff changeset
  1812
                # 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
  1813
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1814
                if not finish:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1815
                        return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1816
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1817
                # 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
  1818
                # 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
  1819
                # 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
  1820
                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
  1821
                obj_cache2 = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1822
                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
  1823
                        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
  1824
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1825
                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
  1826
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1827
                if DebugValues["plandesc_validate"]:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1828
                        json_validate(name, data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1829
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1830
                # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1831
                if json_debug:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1832
                        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
  1833
                        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
  1834
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1835
                return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1836
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1837
        # 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
  1838
        if isinstance(desc, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1839
                desc_type = desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1840
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1841
                # 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
  1842
                desc_type = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1843
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1844
        # get the data type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1845
        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
  1846
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1847
        # 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
  1848
        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
  1849
            "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
  1850
                (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
  1851
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1852
        # 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
  1853
        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
  1854
                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
  1855
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1856
        # 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
  1857
        # 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
  1858
        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
  1859
                # 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
  1860
                rv = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1861
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1862
                # 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
  1863
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1864
                        rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1865
                        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
  1866
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1867
                # 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
  1868
                # generic type description.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1869
                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
  1870
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1871
                # 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
  1872
                # 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
  1873
                # 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
  1874
                # 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
  1875
                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
  1876
                        # 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
  1877
                        assert len(desc) == 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1878
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1879
                        # 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
  1880
                        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
  1881
                                # encode the key
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1882
                                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
  1883
                                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
  1884
                                    je_state=je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1885
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1886
                                # encode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1887
                                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
  1888
                                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
  1889
                                    je_state=je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1890
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1891
                                # save the result
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1892
                                rv[k2] = v2
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1893
                        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
  1894
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1895
                # 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
  1896
                # encode the specific values.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1897
                rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1898
                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
  1899
                        # 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
  1900
                        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
  1901
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1902
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1903
                        # encode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1904
                        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
  1905
                        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
  1906
                            je_state=je_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1907
                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
  1908
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1909
        # 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
  1910
        # 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
  1911
        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
  1912
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1913
                # 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
  1914
                rv = []
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1915
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1916
                # 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
  1917
                if len(data) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1918
                        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
  1919
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1920
                # 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
  1921
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1922
                        rv.extend(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1923
                        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
  1924
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1925
                # 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
  1926
                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
  1927
                    "%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
  1928
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1929
                i = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1930
                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
  1931
                    fillvalue=list(desc)[0]):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1932
                        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
  1933
                        i += 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1934
                        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
  1935
                            je_state=je_state))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1936
                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
  1937
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1938
        # 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
  1939
        # 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
  1940
        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
  1941
                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
  1942
                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
  1943
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1944
        # 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
  1945
        #     <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
  1946
        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
  1947
        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
  1948
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1949
        # encode the data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1950
        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
  1951
        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
  1952
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1953
        # 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
  1954
        # 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
  1955
        # (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
  1956
        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
  1957
                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
  1958
                rv = obj_id[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1959
                obj_id[0] += 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1960
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1961
        # return the encoded element
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1962
        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
  1963
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1964
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
  1965
        """A generic json decoder.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1966
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1967
        '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
  1968
        data errors.)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1969
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1970
        '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
  1971
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1972
        'data' data to decode."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1973
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1974
        # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1975
        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
  1976
                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
  1977
                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
  1978
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1979
        # 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
  1980
        if data is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1981
                return (data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1982
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1983
        # initialize parameters to default
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1984
        if commonize is None:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1985
                commonize = frozenset()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1986
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1987
        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
  1988
                # 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
  1989
                # 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
  1990
                finish = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1991
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1992
                # 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
  1993
                if not commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1994
                        # no common state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1995
                        obj_cache = {}
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1996
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1997
                        # load commonized state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  1998
                        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
  1999
                        data = data["json_state"]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2000
                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
  2001
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2002
                # 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
  2003
                finish = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2004
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2005
                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
  2006
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2007
                # 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
  2008
                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
  2009
                        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
  2010
                        data = data["json_state"]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2011
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2012
                # 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
  2013
                je_state_changed = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2014
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2015
                # 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
  2016
                if not commonize:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2017
                        commonize = commonize_old
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2018
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2019
                        # 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
  2020
                        # 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
  2021
                        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
  2022
                        je_state_changed = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2023
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2024
                if je_state_changed:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2025
                        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
  2026
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2027
        # verify state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2028
        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
  2029
        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
  2030
        assert type(commonize) == frozenset
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2031
        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
  2032
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2033
        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
  2034
                """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
  2035
                # 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
  2036
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2037
                # 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
  2038
                if isinstance(desc, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2039
                        desc_type = desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2040
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2041
                        # 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
  2042
                        desc_type = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2043
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2044
                # get the data type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2045
                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
  2046
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2047
                # 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
  2048
                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
  2049
                    "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
  2050
                        (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
  2051
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2052
                if not finish:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2053
                        return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2054
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2055
                # debugging
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2056
                if json_debug:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2057
                        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
  2058
                        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
  2059
                return data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2060
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2061
        # 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
  2062
        if isinstance(desc, type):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2063
                desc_type = desc
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2064
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2065
                # 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
  2066
                desc_type = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2067
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2068
        # 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
  2069
        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
  2070
                rv = None
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2071
                return jd_return(name, 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
  2072
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2073
        # decode elements nested in a dictionary
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2074
        # return elements in the specified dictionary like object
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2075
        if isinstance(desc, dict):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2076
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2077
                # 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
  2078
                # 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
  2079
                # collections.defaultdict types.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2080
                rv = desc.copy()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2081
                rv.clear()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2082
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2083
                # 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
  2084
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2085
                        rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2086
                        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
  2087
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2088
                # 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
  2089
                # generic type description.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2090
                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
  2091
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2092
                # 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
  2093
                # 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
  2094
                # 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
  2095
                # 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
  2096
                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
  2097
                        # 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
  2098
                        assert len(desc) == 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2099
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2100
                        # 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
  2101
                        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
  2102
                                # decode the key
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2103
                                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
  2104
                                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
  2105
                                    jd_state=jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2106
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2107
                                # decode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2108
                                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
  2109
                                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
  2110
                                    jd_state=jd_state)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2111
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2112
                                # save the result
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2113
                                rv[k2] = v2
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2114
                        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
  2115
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2116
                # 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
  2117
                # 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
  2118
                rv.update(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2119
                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
  2120
                        # 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
  2121
                        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
  2122
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2123
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2124
                        # decode the value
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2125
                        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
  2126
                        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
  2127
                            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
  2128
                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
  2129
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2130
        # 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
  2131
        # 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
  2132
        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
  2133
                # get the return type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2134
                rvtype = type(desc)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2135
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2136
                # 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
  2137
                if len(data) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2138
                        rv = rvtype([])
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2139
                        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
  2140
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2141
                # 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
  2142
                if len(desc) == 0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2143
                        rv = rvtype(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2144
                        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
  2145
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2146
                # 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
  2147
                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
  2148
                    "%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
  2149
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2150
                rv = []
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2151
                i = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2152
                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
  2153
                    fillvalue=list(desc)[0]):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2154
                        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
  2155
                        i += 1
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2156
                        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
  2157
                            jd_state=jd_state))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2158
                rv = rvtype(rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2159
                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
  2160
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2161
        # 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
  2162
        #     <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
  2163
        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
  2164
        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
  2165
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2166
        # 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
  2167
        # object cache.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2168
        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
  2169
                assert type(data) == int
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2170
                # 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
  2171
                # 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
  2172
                data = str(data)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2173
                rv = obj_cache[data]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2174
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2175
                # get the data type
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2176
                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
  2177
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2178
                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
  2179
                        # 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
  2180
                        # 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
  2181
                        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
  2182
                        obj_cache[data] = rv
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2183
        else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2184
                # decode the data
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2185
                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
  2186
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2187
        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
  2188
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2189
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
  2190
        """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
  2191
        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
  2192
        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
  2193
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2194
        '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
  2195
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2196
        '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
  2197
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2198
        '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
  2199
        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
  2200
        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
  2201
        the specified depth."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2202
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2203
        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
  2204
            "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
  2205
            (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
  2206
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2207
        if type(data) == dict:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2208
                for k in data:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2209
                        # 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
  2210
                        # 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
  2211
                        # 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
  2212
                        assert type(k) != int, \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2213
                            "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
  2214
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2215
                        # 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
  2216
                        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
  2217
                        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
  2218
                        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
  2219
                        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
  2220
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2221
        if type(data) == list:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2222
                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
  2223
                        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
  2224
                        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
  2225
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2226
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
  2227
        """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
  2228
        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
  2229
2693
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2230
        def dbg():
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2231
                """dump debug info for json_diff"""
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2232
                def d(s):
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2233
                        """dbg helper"""
cfee571ea6d5 7154388 Progress tracking API and functionality improvements
Dan Price <daniel.price@oracle.com>
parents: 2690
diff changeset
  2234
                        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
  2235
                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
  2236
                    "\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
  2237
2690
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2238
        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
  2239
                "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
  2240
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2241
        if type(d0) == dict:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2242
                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
  2243
                   "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
  2244
                   "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
  2245
                   "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
  2246
                   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
  2247
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2248
                for k in d0:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2249
                        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
  2250
                        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
  2251
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2252
        if type(d0) == list:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2253
                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
  2254
                   "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
  2255
                   "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
  2256
                   "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
  2257
                   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
  2258
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2259
                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
  2260
                        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
  2261
                        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
  2262
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2263
class Timer(object):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2264
        """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
  2265
        system, and wait)."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2266
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2267
        __precision = 3
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2268
        __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
  2269
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2270
        def __init__(self, module):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2271
                self.__module = module
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2272
                self.__timings = []
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2273
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2274
                # 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
  2275
                # 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
  2276
                # 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
  2277
                # 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
  2278
                # 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
  2279
                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
  2280
                self.__wtime = _prstart()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2281
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2282
        def __zero1(self, delta):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2283
                """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
  2284
                precision.)"""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2285
                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
  2286
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2287
        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
  2288
                """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
  2289
                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
  2290
                    self.__zero1(sdelta) and \
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2291
                    self.__zero1(wdelta)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2292
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2293
        def __str__(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2294
                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
  2295
                utotal = stotal = wtotal = 0
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2296
                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
  2297
                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
  2298
                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
  2299
                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
  2300
                        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
  2301
                                continue
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2302
                        utotal += udelta
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2303
                        stotal += sdelta
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2304
                        wtotal += wdelta
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2305
                        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
  2306
                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
  2307
                s += "]\n"
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2308
                return s
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2309
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2310
        def reset(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2311
                """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
  2312
                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
  2313
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2314
        @staticmethod
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2315
        def __get_time():
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2316
                """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
  2317
                process."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2318
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2319
                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
  2320
                utime = rusage[0]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2321
                stime = rusage[1]
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2322
                wtime = time.time()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2323
                return (utime, stime, wtime)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2324
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2325
        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
  2326
                """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
  2327
                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
  2328
                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
  2329
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2330
                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
  2331
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2332
                udelta = utime - self.__utime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2333
                sdelta = stime - self.__stime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2334
                wdelta = wtime - self.__wtime
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2335
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2336
                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
  2337
                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
  2338
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2339
                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
  2340
                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
  2341
                if logger:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2342
                        logger.debug(rv)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2343
                return rv
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2344
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
class AsyncCallException(Exception):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2347
        """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
  2348
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2349
        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
  2350
        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
  2351
        the original exception."""
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2352
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2353
        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
  2354
                Exception.__init__(self)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2355
                self.e = e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2356
                self.tb = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2357
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2358
        def __str__(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2359
                if self.tb:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2360
                        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
  2361
                return str(self.e)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2362
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
class AsyncCall(object):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2365
        """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
  2366
        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
  2367
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2368
        def __init__(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2369
                self.rv = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2370
                self.e = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2371
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2372
                # 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
  2373
                self.started = False
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2374
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2375
                # internal state
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2376
                self.__thread = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2377
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2378
                # 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
  2379
                # goes horribly wrong.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2380
                self.__e = AsyncCallException(
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2381
                    Exception("AsyncCall Internal Error"))
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2382
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2383
        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
  2384
                """Dedicated call thread.
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2385
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2386
                '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
  2387
                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
  2388
                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
  2389
                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
  2390
                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
  2391
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2392
                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
  2393
                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
  2394
                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
  2395
                # 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
  2396
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2397
                try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2398
                        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
  2399
                                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
  2400
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2401
                        rv = e = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2402
                        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2403
                                rv = cb(*args, **kwargs)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2404
                        except Exception, e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2405
                                self.e = self.__e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2406
                                self.e.e = e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2407
                                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
  2408
                                return
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2409
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2410
                        self.rv = rv
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2411
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2412
                except Exception, e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2413
                        # 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
  2414
                        self.rv = None
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2415
                        self.e = self.__e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2416
                        self.e.e = e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2417
                        try:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2418
                                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
  2419
                                        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
  2420
                                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
  2421
                        except Exception:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2422
                                pass
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2423
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2424
        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
  2425
                """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
  2426
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2427
                assert not self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2428
                self.started = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2429
                # 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
  2430
                if args:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2431
                        args = (0, cb) + args
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2432
                else:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2433
                        args = (0, cb)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2434
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2435
                # 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
  2436
                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
  2437
                    args=args, kwargs=kwargs)
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2438
                self.__thread.daemon = True
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2439
                self.__thread.start()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2440
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2441
        def join(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2442
                """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
  2443
                assert self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2444
                self.__thread.join()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2445
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2446
        def is_done(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2447
                """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
  2448
                assert self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2449
                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
  2450
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2451
        def result(self):
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2452
                """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
  2453
                assert self.started
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2454
                # 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
  2455
                self.join()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2456
                assert self.is_done()
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2457
                if self.e:
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2458
                        # 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
  2459
                        # 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
  2460
                        raise self.e
11a8cae074e0 7140224 package downloaded messages displayed twice for each zone
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2620
diff changeset
  2461
                return self.rv
2701
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2462
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2463
2711
4def00cb6bbc 7179670 Use of environment variables in proxy URIs considered harmful
Tim Foster <tim.s.foster@oracle.com>
parents: 2708
diff changeset
  2464
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
  2465
        """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
  2466
        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
  2467
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2468
        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
  2469
        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
  2470
        should be used at runtime."""
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2471
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2472
        runtime_proxy = proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2473
        # 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
  2474
        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
  2475
        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
  2476
        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
  2477
        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
  2478
        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
  2479
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2480
        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
  2481
        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
  2482
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2483
        if no_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2484
                no_proxy = no_proxy.split(",")
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2485
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2486
        if no_proxy_upper:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2487
                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
  2488
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2489
        # 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
  2490
        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
  2491
                runtime_proxy = environ_http_proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2492
        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
  2493
                runtime_proxy = environ_https_proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2494
        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
  2495
                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
  2496
        elif environ_all_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2497
                runtime_proxy = environ_all_proxy
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2498
        elif environ_all_proxy_upper:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2499
                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
  2500
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2501
        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
  2502
                # 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
  2503
                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
  2504
                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
  2505
                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
  2506
                        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
  2507
                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
  2508
                        return "-"
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2509
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2510
        if not runtime_proxy:
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2511
                return
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2512
55bf0cb749ae 7136244 granular configuration of http_proxy option
Tim Foster <tim.s.foster@oracle.com>
parents: 2696
diff changeset
  2513
        return runtime_proxy
2729
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2514
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2515
def decode(s):
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2516
        """convert non-ascii strings to unicode;
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2517
        replace non-convertable chars"""
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2518
        try:
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2519
                # 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
  2520
                # 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
  2521
                s = s.encode("ascii")
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2522
        except ValueError:
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2523
                # 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
  2524
                s = s.decode("utf-8", "replace")
fbe29295150f 7127117 pkg update --parsable option is not working
saurabh.vyas@oracle.com
parents: 2711
diff changeset
  2525
        return s
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2526
2910
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2527
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
  2528
        """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
  2529
        patterns."""
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2530
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2531
        if patterns:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2532
                # 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
  2533
                npatterns = []
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2534
                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
  2535
                        if pat_prefix:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2536
                                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
  2537
                                    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
  2538
                        else:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2539
                                pat = p
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2540
                        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
  2541
                                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
  2542
                                glob_match = True
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2543
                        else:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2544
                                glob_match = False
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2545
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2546
                        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
  2547
                patterns = npatterns
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2548
                npatterns = None
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2549
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2550
        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
  2551
                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
  2552
                        if glob_match:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2553
                                if pat(item):
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2554
                                        break
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2555
                        elif item == pat:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2556
                                break
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2557
                else:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2558
                        if patterns:
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2559
                                continue
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2560
                # 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
  2561
                yield item
2bb9f6ffdff9 15654935 pkg facet/variant should also display those implicitly set
Shawn Walker <shawn.walker@oracle.com>
parents: 2906
diff changeset
  2562
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2563
2906
ede940fbc11e 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies (fix pylint)
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2905
diff changeset
  2564
sigdict = defaultdict(list)
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2565
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2566
def signame(signal_number):
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2567
        """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
  2568
        if not sigdict:
2905
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2569
                for name in dir(signal):
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2570
                        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
  2571
                                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
  2572
16b34634c06c 15817160 SUNBT7199714 Enhance IPS to monitor firmware dependencies
Bart Smaalders <Bart.Smaalders@Oracle.COM>
parents: 2875
diff changeset
  2573
        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
  2574
            signal_number]))
3041
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2575
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2576
def list_actions_by_attrs(actionlist, attrs, show_all=False,
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2577
    remove_consec_dup_lines=False, last_res=None):
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2578
        """Produces a list of n tuples (where n is the length of attrs)
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2579
        containing the relevant information about the actions.
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2580
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2581
        The "actionlist" parameter is a list of tuples which contain the fmri
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2582
        of the package that's the source of the action, the action, and the
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2583
        publisher the action's package came from. If the actionlist was
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2584
        generated by searching, the last two pieces, "match" and "match_type"
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2585
        contain information about why this action was selected.
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2586
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2587
        The "attrs" parameter is a list of the attributes of the action that
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2588
        should be displayed.
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2589
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2590
        The "show_all" parameter determines whether an action that lacks one
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2591
        or more of the desired attributes will be displayed or not.
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2592
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2593
        The "remove_consec_dup_lines" parameter determines whether consecutive
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2594
        duplicate lines should be removed from the results.
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2595
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2596
        The "last_res" parameter is a seed to compare the first result against
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2597
        for duplicate removal.
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2598
        """
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2599
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2600
        # Assert that if last_res is set, we should be removing duplicate
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2601
        # lines.
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2602
        assert remove_consec_dup_lines or not last_res
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2603
        last_line = last_res
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2604
        for pfmri, action, pub, match, match_type in actionlist:
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2605
                line = []
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2606
                for attr in attrs:
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2607
                        if action and attr in action.attrs:
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2608
                                a = action.attrs[attr]
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2609
                        elif attr == "action.name":
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2610
                                a = action.name
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2611
                        elif attr == "action.key":
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2612
                                a = action.attrs[action.key_attr]
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2613
                        elif attr == "action.raw":
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2614
                                a = action
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2615
                        elif attr in ("hash", "action.hash"):
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2616
                                a = getattr(action, "hash", "")
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2617
                        elif attr == "pkg.name":
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2618
                                a = pfmri.get_name()
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2619
                        elif attr == "pkg.fmri":
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2620
                                a = pfmri.get_fmri(include_build=False)
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2621
                        elif attr == "pkg.shortfmri":
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2622
                                a = pfmri.get_short_fmri()
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2623
                        elif attr == "pkg.publisher":
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2624
                                a = pfmri.get_publisher()
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2625
                                if a is None:
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2626
                                        a = pub
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2627
                                        if a is None:
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2628
                                                a = ""
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2629
                        elif attr == "search.match":
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2630
                                a = match
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2631
                        elif attr == "search.match_type":
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2632
                                a = match_type
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2633
                        else:
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2634
                                a = ""
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2635
                        line.append(a)
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2636
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2637
                if (line and [l for l in line if str(l) != ""] or show_all) \
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2638
                    and (not remove_consec_dup_lines or last_line is None or
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2639
                    last_line != line):
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2640
                        last_line = line
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2641
                        yield line
9846c3f45f79 17789466 pkgrepo contents subcommand desired
Yiteng Zhang <yiteng.zhang@oracle.com>
parents: 3032
diff changeset
  2642