src/checkforupdates.py
author Tim Foster <tim.s.foster@oracle.com>
Fri, 16 Nov 2012 10:43:52 +1300
changeset 2828 d1d4d120c4c5
parent 2793 521a80afd53a
child 2938 1d287dc7a674
permissions -rw-r--r--
15875724 pylint update broke the build
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
     1
#!/usr/bin/python2.6
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
     2
#
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
     3
# CDDL HEADER START
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
     4
#
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
     5
# The contents of this file are subject to the terms of the
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
     6
# Common Development and Distribution License (the "License").
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
     7
# You may not use this file except in compliance with the License.
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
     8
#
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
     9
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    10
# or http://www.opensolaris.org/os/licensing.
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    11
# See the License for the specific language governing permissions
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    12
# and limitations under the License.
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    13
#
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    14
# When distributing Covered Code, include this CDDL HEADER in each
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    15
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    16
# If applicable, add the following below this CDDL HEADER, with the
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    17
# fields enclosed by brackets "[]" replaced with your own identifying
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    18
# information: Portions Copyright [yyyy] [name of copyright owner]
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    19
#
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    20
# CDDL HEADER END
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    21
#
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    22
# Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    23
#
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    24
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    25
"""This utility checks to see if there are any available updates for
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    26
the relevant image.  If so, it stashes information about the updates in
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    27
the gui cache file, for retrieval by other desktop utilities.  See also
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    28
the update-refresh cron job."""
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    29
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    30
import errno
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    31
import getopt
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    32
import gettext
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    33
import locale
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    34
import logging
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    35
import os
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    36
import sys
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    37
import traceback
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    38
import warnings
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    39
2793
521a80afd53a 7140506 Package Manager and Update Manager should use get_default_image_dir
thejaswini.k@oracle.com
parents: 2792
diff changeset
    40
import pkg.client.api as api
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    41
import pkg.client.api_errors as apx
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    42
import pkg.client.progress as progress
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    43
import pkg.client.printengine as printengine
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    44
import pkg.gui.enumerations as enumerations
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    45
import pkg.gui.misc_non_gui as nongui_misc
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    46
import pkg.misc as misc
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    47
import pkg.nrlock as nrlock
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    48
from cPickle import UnpicklingError
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    49
from pkg.client import global_settings
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    50
from pkg.client.pkgdefs import EXIT_OOPS
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    51
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    52
logger = global_settings.logger
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    53
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    54
PKG_CLIENT_NAME = "updatemanager"
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    55
CACHE_VERSION =  3
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    56
CACHE_NAME = ".last_refresh_cache"
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    57
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    58
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    59
class CheckForUpdates:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    60
        """Implements the main logic for this utility"""
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    61
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    62
        def __init__(self, image_directory, application_path, check_all,
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    63
            check_cache):
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    64
                global_settings.client_name = nongui_misc.get_um_name()
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    65
                self.api_lock = nrlock.NRLock()
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    66
                self.image_dir_arg = image_directory
2793
521a80afd53a 7140506 Package Manager and Update Manager should use get_default_image_dir
thejaswini.k@oracle.com
parents: 2792
diff changeset
    67
                self.exact_match = True
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    68
                if self.image_dir_arg == None:
2793
521a80afd53a 7140506 Package Manager and Update Manager should use get_default_image_dir
thejaswini.k@oracle.com
parents: 2792
diff changeset
    69
                        self.image_dir_arg, self.exact_match =  \
521a80afd53a 7140506 Package Manager and Update Manager should use get_default_image_dir
thejaswini.k@oracle.com
parents: 2792
diff changeset
    70
                            api.get_default_image_root()
521a80afd53a 7140506 Package Manager and Update Manager should use get_default_image_dir
thejaswini.k@oracle.com
parents: 2792
diff changeset
    71
                if not self.exact_match:
521a80afd53a 7140506 Package Manager and Update Manager should use get_default_image_dir
thejaswini.k@oracle.com
parents: 2792
diff changeset
    72
                        logger.debug("Unable to get image directory")
521a80afd53a 7140506 Package Manager and Update Manager should use get_default_image_dir
thejaswini.k@oracle.com
parents: 2792
diff changeset
    73
                        sys.exit(enumerations.UPDATES_UNDETERMINED)
521a80afd53a 7140506 Package Manager and Update Manager should use get_default_image_dir
thejaswini.k@oracle.com
parents: 2792
diff changeset
    74
                        
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    75
                self.application_path = application_path
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    76
                self.check_all = check_all
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    77
                self.check_cache_only = check_cache
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    78
                self.application_dir = \
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    79
                    os.environ.get("PACKAGE_MANAGER_ROOT", "/")
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    80
                misc.setlocale(locale.LC_ALL, "")
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    81
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    82
                if global_settings.verbose:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    83
                        pe = printengine.LoggingPrintEngine(
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    84
                            logger, logging.DEBUG)
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    85
                        self.progress_tracker = \
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    86
                            progress.CommandLineProgressTracker(print_engine=pe)
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    87
                else:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    88
                        self.progress_tracker = progress.NullProgressTracker()
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    89
                self.api_obj = None
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    90
                self.return_status = enumerations.UPDATES_UNDETERMINED
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    91
                self.pylintstub = None
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    92
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    93
                # Check Updates - by default check all
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    94
                self.api_obj = self.__get_api_obj()
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    95
                if self.api_obj == None:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    96
                        self.return_status = enumerations.UPDATES_UNDETERMINED
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    97
                        return
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
    98
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
    99
                if self.check_all:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   100
                        self.__check_for_updates()
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   101
                elif self.check_cache_only:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   102
                        self.__check_for_updates_cache_only()
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   103
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   104
        def __get_api_obj(self):
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   105
                """Returns a singleton api instance."""
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   106
                if self.api_obj == None:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   107
                        api_obj = nongui_misc.get_api_object(self.image_dir_arg,
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   108
                            self.progress_tracker)
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   109
                return api_obj
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   110
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   111
        def __check_for_updates_cache_only(self):
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   112
                """Reports on the cached status of available updates"""
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   113
                assert self.api_obj
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   114
                self.return_status = ret = self.__check_last_refresh()
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   115
                if ret == enumerations.UPDATES_AVAILABLE:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   116
                        logger.debug("From cache: Updates Available")
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   117
                elif ret == enumerations.NO_UPDATES_AVAILABLE:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   118
                        logger.debug("From cache: No Updates Available")
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   119
                else:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   120
                        logger.debug("From cache: Updates Undetermined")
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   121
                return ret
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   122
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   123
        def __check_for_updates(self):
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   124
                """Plans an update for the image."""
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   125
                assert self.api_obj
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   126
                ret = self.__check_for_updates_cache_only()
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   127
                if ret != enumerations.UPDATES_UNDETERMINED:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   128
                        # Definitive answer from cache.
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   129
                        return
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   130
                logger.debug("Checking image for updates...")
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   131
                self.return_status = enumerations.UPDATES_UNDETERMINED
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   132
                try:
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   133
                        #
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   134
                        # Since this program is intended to primarily be a
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   135
                        # helper for the gui components, and since the gui
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   136
                        # components are currently unaware of child images,
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   137
                        # we'll limit the available update check we're about
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   138
                        # to do to just the parent image.  If we didn't do
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   139
                        # this we could end up in a situation where the parent
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   140
                        # has no available updates, but a child image does,
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   141
                        # and then the gui (which is unaware of children)
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   142
                        # would show that no updates are available to the
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   143
                        # parent.
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   144
                        #
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   145
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2793
diff changeset
   146
                        # Unused variable; pylint: disable=W0612
2339
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   147
                        for pd in self.api_obj.gen_plan_update(
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   148
                            refresh_catalogs=True, noexecute=True,
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   149
                            force=True, li_ignore=[]):
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   150
                                continue
aa5954c06b9d 16148 need linked image support for zones, phase 1
Edward Pilatowicz <edward.pilatowicz@oracle.com>
parents: 2092
diff changeset
   151
                        stuff_to_do = not self.api_obj.planned_nothingtodo()
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   152
                except apx.CatalogRefreshException, cre:
2415
2df3e2e5922f 18428 Fail to add https repos by PM
Padraig O'Briain <padraig.obriain@oracle.com>
parents: 2339
diff changeset
   153
                        res = nongui_misc.get_catalogrefresh_exception_msg(cre)
2df3e2e5922f 18428 Fail to add https repos by PM
Padraig O'Briain <padraig.obriain@oracle.com>
parents: 2339
diff changeset
   154
                        logger.error(res[0])
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   155
                        return
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   156
                except apx.ApiException, e:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   157
                        logger.error(str(e))
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   158
                        return
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   159
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   160
                self.__dump_updates_available(stuff_to_do)
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   161
                if stuff_to_do:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   162
                        logger.debug("From image: Updates Available")
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   163
                        self.return_status = enumerations.UPDATES_AVAILABLE
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   164
                else:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   165
                        logger.debug("From image: No Updates Available")
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   166
                        self.return_status = enumerations.NO_UPDATES_AVAILABLE
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   167
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   168
        def __check_last_refresh(self):
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   169
                """Reads the cache if possible; if it isn't stale or corrupt
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   170
                or out of date, return whether updates are available.
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   171
                Otherwise return 'undetermined'."""
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   172
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   173
                cache_dir = nongui_misc.get_cache_dir(self.api_obj)
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   174
                if not cache_dir:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   175
                        return enumerations.UPDATES_UNDETERMINED
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   176
                try:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   177
                        info = nongui_misc.read_cache_file(os.path.join(
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   178
                            cache_dir, CACHE_NAME + '.cpl'))
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   179
                        if len(info) == 0:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   180
                                logger.debug("No cache")
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   181
                                return enumerations.UPDATES_UNDETERMINED
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2793
diff changeset
   182
                        # Non-portable API used; pylint: disable=E0901
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   183
                        utsname = os.uname()
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2793
diff changeset
   184
                        # pylint: disable=E1103
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   185
                        if info.get("version") != CACHE_VERSION:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   186
                                logger.debug("Cache version mismatch: %s" %
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   187
                                    (info.get("version") + " " + CACHE_VERSION))
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   188
                                return enumerations.UPDATES_UNDETERMINED
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   189
                        if info.get("os_release") != utsname[2]:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   190
                                logger.debug("OS release mismatch: %s" %
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   191
                                    (info.get("os_release") + " " + utsname[2]))
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   192
                                return enumerations.UPDATES_UNDETERMINED
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   193
                        if info.get("os_version") != utsname[3]:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   194
                                logger.debug("OS version mismatch: %s" %
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   195
                                    (info.get("os_version") + " " + utsname[3]))
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   196
                                return enumerations.UPDATES_UNDETERMINED
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   197
                        old_publishers = info.get("publishers")
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   198
                        count = 0
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   199
                        for p in self.api_obj.get_publishers():
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   200
                                if p.disabled:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   201
                                        continue
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   202
                                if old_publishers.get(p.prefix, -1) != \
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   203
                                    p.last_refreshed:
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   204
                                        return enumerations.UPDATES_UNDETERMINED
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   205
                                count += 1
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   206
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   207
                        if count != len(old_publishers):
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   208
                                return enumerations.UPDATES_UNDETERMINED
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   209
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   210
                        n_updates = n_installs = n_removes = 0
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   211
                        if info.get("updates_available"):
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   212
                                n_updates = info.get("updates")
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   213
                                n_installs = info.get("installs")
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   214
                                n_removes = info.get("removes")
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2793
diff changeset
   215
                        # pylint: enable=E1103
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   216
                        if self.check_cache_only:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   217
                                print "n_updates: %d" % n_updates
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   218
                                print "n_installs: %d" % n_installs
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   219
                                print "n_removes: %d" % n_removes
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   220
                        if (n_updates + n_installs + n_removes) > 0:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   221
                                return enumerations.UPDATES_AVAILABLE
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   222
                        else:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   223
                                return enumerations.NO_UPDATES_AVAILABLE
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   224
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   225
                except (UnpicklingError, IOError):
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   226
                        return enumerations.UPDATES_UNDETERMINED
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   227
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   228
        def __dump_updates_available(self, stuff_to_do):
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   229
                """Record update information to the cache file."""
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   230
                cache_dir = nongui_misc.get_cache_dir(self.api_obj)
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   231
                if not cache_dir:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   232
                        return
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   233
                publisher_list = {}
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   234
                for p in self.api_obj.get_publishers():
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   235
                        if p.disabled:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   236
                                continue
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   237
                        publisher_list[p.prefix] = p.last_refreshed
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   238
                n_installs = 0
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   239
                n_removes = 0
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   240
                n_updates = 0
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   241
                plan_desc = self.api_obj.describe()
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   242
                if plan_desc:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   243
                        plan = plan_desc.get_changes()
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   244
                        for (orig, dest) in plan:
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   245
                                if orig and dest:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   246
                                        n_updates += 1
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   247
                                elif not orig and dest:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   248
                                        n_installs += 1
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   249
                                elif orig and not dest:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   250
                                        n_removes += 1
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   251
                dump_info = {}
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   252
                dump_info["version"] = CACHE_VERSION
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2793
diff changeset
   253
                # Non-portable API used; pylint: disable=E0901
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   254
                dump_info["os_release"] = os.uname()[2]
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   255
                dump_info["os_version"] = os.uname()[3]
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   256
                dump_info["updates_available"] = stuff_to_do
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   257
                dump_info["publishers"] = publisher_list
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   258
                dump_info["updates"] = n_updates
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   259
                dump_info["installs"] = n_installs
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   260
                dump_info["removes"] = n_removes
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   261
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   262
                try:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   263
                        nongui_misc.dump_cache_file(os.path.join(
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   264
                            cache_dir, CACHE_NAME + '.cpl'), dump_info)
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   265
                except IOError, e:
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   266
                        logger.error("Failed to dump cache: %s" % e)
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   267
                return
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   268
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   269
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   270
def main_func():
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   271
        """Main routine for this utility"""
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   272
        set_check_all = True
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   273
        set_check_cache = False
2793
521a80afd53a 7140506 Package Manager and Update Manager should use get_default_image_dir
thejaswini.k@oracle.com
parents: 2792
diff changeset
   274
        image_dir = None 
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   275
        try:
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2793
diff changeset
   276
                # Unused variable pargs; pylint: disable=W0612
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   277
                opts, pargs = getopt.getopt(sys.argv[1:], "hdnacR:",
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   278
                    ["help", "debug", "nice", "checkupdates-cache",
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   279
                    "image-dir="])
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   280
        except getopt.GetoptError, oex:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   281
                print >> sys.stderr, \
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   282
                    ("Usage: illegal option -- %s, for help use -h or --help" %
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   283
                    oex.opt )
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   284
                sys.exit(enumerations.UPDATES_UNDETERMINED)
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   285
        for opt, arg in opts:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   286
                if opt in ("-h", "--help"):
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   287
                        print >> sys.stderr, """\n\
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   288
Use -h (--help) to print out help.
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   289
Use -d (--debug) to run in debug mode.
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   290
Use -n (--nice) to run at nice level 20.
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   291
Use -c (--checkupdates-cache) to check for updates from cache only (output results to stdout).
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   292
Use -R (--image-dir) to specify image directory (defaults to '/')"""
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   293
                        sys.exit(0)
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   294
                elif opt in ( "-n", "--nice"):
2828
d1d4d120c4c5 15875724 pylint update broke the build
Tim Foster <tim.s.foster@oracle.com>
parents: 2793
diff changeset
   295
                        # Non-portable API used; pylint: disable=E0901
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   296
                        os.nice(20)
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   297
                elif opt in ("-d", "--debug"):
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   298
                        global_settings.verbose = True
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   299
                elif opt in ( "-c", "--checkupdates-cache"):
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   300
                        set_check_cache = True
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   301
                        set_check_all = False
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   302
                elif opt in ("-R", "--image-dir"):
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   303
                        image_dir = arg
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   304
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   305
        if os.path.isabs(sys.argv[0]):
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   306
                app_path = sys.argv[0]
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   307
        else:
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   308
                cmd = os.path.join(os.getcwd(), sys.argv[0])
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   309
                app_path = os.path.realpath(cmd)
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   310
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   311
        checkforupdates = CheckForUpdates(image_dir, app_path,
2078
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   312
            set_check_all, set_check_cache)
a13afe24c79a 17024 pm-updatemanager can't find display when running from cron
John Rice <john.rice@oracle.com>
parents:
diff changeset
   313
2744
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   314
        return checkforupdates.return_status
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   315
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   316
#
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   317
# Establish a specific exit status which means: "python barfed an exception"
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   318
# so that we can more easily detect these in testing of the CLI commands.
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   319
#
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   320
def handle_errors(func, *args, **kwargs):
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   321
        """Catch exceptions raised by the main program function and then print
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   322
        a message and/or exit with an appropriate return code.
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   323
        """
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   324
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   325
        traceback_str = misc.get_traceback_message()
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   326
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   327
        try:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   328
                # Out of memory errors can be raised as EnvironmentErrors with
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   329
                # an errno of ENOMEM, so in order to handle those exceptions
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   330
                # with other errnos, we nest this try block and have the outer
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   331
                # one handle the other instances.
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   332
                try:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   333
                        __ret = func(*args, **kwargs)
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   334
                except (MemoryError, EnvironmentError), __e:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   335
                        if isinstance(__e, EnvironmentError) and \
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   336
                            __e.errno != errno.ENOMEM:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   337
                                raise
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   338
                        logger.error("\n" + misc.out_of_memory())
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   339
                        __ret = EXIT_OOPS
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   340
        except SystemExit, __e:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   341
                raise __e
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   342
        except (IOError, misc.PipeError, KeyboardInterrupt), __e:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   343
                # Don't display any messages here to prevent possible further
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   344
                # broken pipe (EPIPE) errors.
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   345
                if isinstance(__e, IOError) and __e.errno != errno.EPIPE:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   346
                        logger.error(str(__e))
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   347
                __ret = EXIT_OOPS
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   348
        except apx.VersionException, __e:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   349
                logger.error("The pmcheckforupdates command appears out of "
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   350
                    "sync with the libraries provided\nby pkg:/package/pkg. "
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   351
                    "The client version is %(client)s while the library\n"
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   352
                    "API version is %(api)s." % \
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   353
                    {'client': __e.received_version,
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   354
                     'api': __e.expected_version})
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   355
                __ret = EXIT_OOPS
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   356
        except:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   357
                traceback.print_exc()
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   358
                logger.error(traceback_str)
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   359
                __ret = 99
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   360
        return __ret
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   361
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   362
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   363
if __name__ == "__main__":
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   364
        misc.setlocale(locale.LC_ALL, "")
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   365
        gettext.install("pkg", "/usr/share/locale",
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   366
            codeset=locale.getpreferredencoding())
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   367
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   368
        # Make all warnings be errors.
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   369
        warnings.simplefilter('error')
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   370
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   371
        __retval = handle_errors(main_func)
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   372
        try:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   373
                logging.shutdown()
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   374
        except IOError:
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   375
                # Ignore python's spurious pipe problems.
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   376
                pass
a7f73341a2b3 7185799 pmcheckforupdate could use some code cleanup
Dan Price <daniel.price@oracle.com>
parents: 2415
diff changeset
   377
        sys.exit(__retval)