Changeset 516 for yum/trunk/utils.py


Ignore:
Timestamp:
Feb 3, 2015, 9:39:54 AM (11 years ago)
Author:
Yuri Dario
Message:

yum: update trunk to 3.4.3.

Location:
yum/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • yum/trunk

  • yum/trunk/utils.py

    r2 r516  
    2222from yum import Errors
    2323from yum import _
     24from yum.i18n import utf8_width
    2425from yum import logginglevels
    2526from optparse import OptionGroup
     
    5859        return
    5960
     61    try:
     62        pid = int(pid)
     63    except ValueError, e:
     64        return
     65       
    6066    # Maybe true if /proc isn't mounted, or not Linux ... or something.
    6167    if (not os.path.exists("/proc/%d/status" % pid) or
     
    101107
    102108def show_lock_owner(pid, logger):
    103     if not pid:
    104         return
    105 
    106109    ps = get_process_info(pid)
     110    if not ps:
     111        return None
     112
    107113    # This yumBackend isn't very friendly, so...
    108114    if ps['name'] == 'yumBackend.py':
     
    120126                    (time.ctime(ps['start_time']), ago))
    121127    logger.critical(_("    State  : %s, pid: %d") % (ps['state'], pid))
     128
     129    return ps
     130
     131
     132def exception2msg(e):
     133    """ DIE python DIE! Which one works:
     134        to_unicode(e.value); unicode(e); str(e);
     135        Call this so you don't have to care. """
     136    try:
     137        return to_unicode(e.value)
     138    except:
     139        pass
     140
     141    try:
     142        return unicode(e)
     143    except:
     144        pass
     145
     146    try:
     147        return str(e)
     148    except:
     149        pass
     150    return "<exception failed to convert to text>"
     151
    122152
    123153class YumUtilBase(YumBaseCli):
     
    133163        logger = logging.getLogger("yum.util")
    134164        verbose_logger = logging.getLogger("yum.verbose.util")
     165        # Add yum-utils version to history records.
     166        if hasattr(self, 'run_with_package_names'):
     167            self.run_with_package_names.add("yum-utils")
     168
     169    def exUserCancel(self):
     170        self.logger.critical(_('\n\nExiting on user cancel'))
     171        if self.unlock(): return 200
     172        return 1
     173
     174    def exIOError(self, e):
     175        if e.errno == 32:
     176            self.logger.critical(_('\n\nExiting on Broken Pipe'))
     177        else:
     178            self.logger.critical(_('\n\n%s') % exception2msg(e))
     179        if self.unlock(): return 200
     180        return 1
     181
     182    def exPluginExit(self, e):
     183        '''Called when a plugin raises PluginYumExit.
     184
     185        Log the plugin's exit message if one was supplied.
     186        ''' # ' xemacs hack
     187        exitmsg = exception2msg(e)
     188        if exitmsg:
     189            self.logger.warn('\n\n%s', exitmsg)
     190        if self.unlock(): return 200
     191        return 1
     192
     193    def exFatal(self, e):
     194        self.logger.critical('\n\n%s', exception2msg(e))
     195        if self.unlock(): return 200
     196        return 1
     197       
     198    def unlock(self):
     199        try:
     200            self.closeRpmDB()
     201            self.doUnlock()
     202        except Errors.LockError, e:
     203            return 200
     204        return 0
    135205       
    136206       
     
    148218                self.doLock()
    149219            except Errors.LockError, e:
    150                 if "%s" %(e.msg,) != lockerr:
    151                     lockerr = "%s" %(e.msg,)
     220                if exception2msg(e) != lockerr:
     221                    lockerr = exception2msg(e)
    152222                    self.logger.critical(lockerr)
    153                 self.logger.critical("Another app is currently holding the yum lock; waiting for it to exit...") 
    154                 show_lock_owner(e.pid, self.logger)
    155                 time.sleep(2)
     223                if not self.conf.exit_on_lock:
     224                    self.logger.critical("Another app is currently holding the yum lock; waiting for it to exit...") 
     225                    show_lock_owner(e.pid, self.logger)
     226                    time.sleep(2)
     227                else:
     228                    raise Errors.YumBaseError, _("Another app is currently holding the yum lock; exiting as configured by exit_on_lock")
    156229            else:
    157230                break
     
    163236        # Parse only command line options that affect basic yum setup
    164237        opts = self._parser.firstParse(args)
     238
     239        # go through all the setopts and set the global ones
     240        self._parseSetOpts(opts.setopts)
     241
     242        if self.main_setopts:
     243            for opt in self.main_setopts.items:
     244                setattr(opts, opt, getattr(self.main_setopts, opt))
     245
    165246        # Just print out the version if that's what the user wanted
    166247        if opts.version:
     
    188269            if hasattr(opts, "enableplugins"):
    189270                pc.enabled_plugins = self._parser._splitArg(opts.enableplugins)
     271            if hasattr(opts, "releasever"):
     272                pc.releasever = opts.releasever
    190273            self.conf
    191274
     275            # now set  all the non-first-start opts from main from our setopts
     276            if self.main_setopts:
     277                for opt in self.main_setopts.items:
     278                    setattr(self.conf, opt, getattr(self.main_setopts, opt))
     279
    192280        except Errors.ConfigError, e:
    193             self.logger.critical(_('Config Error: %s'), e)
     281            self.logger.critical(_('Config Error: %s'), exception2msg(e))
    194282            sys.exit(1)
    195283        except ValueError, e:
    196             self.logger.critical(_('Options Error: %s'), e)
     284            self.logger.critical(_('Options Error: %s'), exception2msg(e))
    197285            sys.exit(1)
    198286        except plugins.PluginYumExit, e:
    199             self.logger.critical(_('PluginExit Error: %s'), e)
     287            self.logger.critical(_('PluginExit Error: %s'), exception2msg(e))
    200288            sys.exit(1)
    201289        except Errors.YumBaseError, e:
    202             self.logger.critical(_('Yum Error: %s'), e)
     290            self.logger.critical(_('Yum Error: %s'), exception2msg(e))
    203291            sys.exit(1)
    204292           
     
    228316            self._getSacks()
    229317        except Errors.YumBaseError, msg:
    230             self.logger.critical(str(msg))
     318            self.logger.critical(exception2msg(msg))
    231319            sys.exit(1)
    232    
     320
     321    def doUtilBuildTransaction(self, unfinished_transactions_check=True):
     322        try:
     323            (result, resultmsgs) = self.buildTransaction(unfinished_transactions_check = unfinished_transactions_check)
     324        except plugins.PluginYumExit, e:
     325            return self.exPluginExit(e)
     326        except Errors.YumBaseError, e:
     327            result = 1
     328            resultmsgs = [exception2msg(e)]
     329        except KeyboardInterrupt:
     330            return self.exUserCancel()
     331        except IOError, e:
     332            return self.exIOError(e)
     333       
     334        # Act on the depsolve result
     335        if result == 0:
     336            # Normal exit
     337            if self.unlock(): return 200
     338            return 0
     339        elif result == 1:
     340            # Fatal error
     341            for msg in resultmsgs:
     342                prefix = _('Error: %s')
     343                prefix2nd = (' ' * (utf8_width(prefix) - 2))
     344                self.logger.critical(prefix, msg.replace('\n', '\n' + prefix2nd))
     345            if not self.conf.skip_broken:
     346                self.verbose_logger.info(_(" You could try using --skip-broken to work around the problem"))
     347            if not self._rpmdb_warn_checks(out=self.verbose_logger.info, warn=False):
     348                self.verbose_logger.info(_(" You could try running: rpm -Va --nofiles --nodigest"))
     349            if self.unlock(): return 200
     350            return 1
     351        elif result == 2:
     352            # Continue on
     353            pass
     354        else:
     355            self.logger.critical(_('Unknown Error(s): Exit Code: %d:'), result)
     356            for msg in resultmsgs:
     357                self.logger.critical(msg)
     358            if self.unlock(): return 200
     359            return 3
     360
     361        self.verbose_logger.log(logginglevels.INFO_2, _('\nDependencies Resolved'))
     362       
    233363    def doUtilTransaction(self):
    234         def exUserCancel():
    235             self.logger.critical(_('\n\nExiting on user cancel'))
    236             if unlock(): return 200
    237             return 1
    238 
    239         def exIOError(e):
    240             if e.errno == 32:
    241                 self.logger.critical(_('\n\nExiting on Broken Pipe'))
    242             else:
    243                 self.logger.critical(_('\n\n%s') % str(e))
    244             if unlock(): return 200
    245             return 1
    246 
    247         def exPluginExit(e):
    248             '''Called when a plugin raises PluginYumExit.
    249 
    250             Log the plugin's exit message if one was supplied.
    251             ''' # ' xemacs hack
    252             exitmsg = str(e)
    253             if exitmsg:
    254                 self.logger.warn('\n\n%s', exitmsg)
    255             if unlock(): return 200
    256             return 1
    257 
    258         def exFatal(e):
    259             self.logger.critical('\n\n%s', to_unicode(e.value))
    260             if unlock(): return 200
    261             return 1
    262 
    263         def unlock():
    264             try:
    265                 self.closeRpmDB()
    266                 self.doUnlock()
    267             except Errors.LockError, e:
    268                 return 200
    269             return 0
    270364
    271365        try:
    272366            return_code = self.doTransaction()
    273367        except plugins.PluginYumExit, e:
    274             return exPluginExit(e)
     368            return self.exPluginExit(e)
    275369        except Errors.YumBaseError, e:
    276             return exFatal(e)
     370            return self.exFatal(e)
    277371        except KeyboardInterrupt:
    278             return exUserCancel()
     372            return self.exUserCancel()
    279373        except IOError, e:
    280             return exIOError(e)
     374            return self.exIOError(e,)
    281375
    282376        self.verbose_logger.log(logginglevels.INFO_2, _('Complete!'))
    283         if unlock(): return 200
     377        if self.unlock(): return 200
    284378        return return_code
    285379       
Note: See TracChangeset for help on using the changeset viewer.