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