[yum-cvs] yum yummain.py,1.96,1.97
Menno Smits
mjs at login.linux.duke.edu
Tue Jun 7 13:46:23 UTC 2005
Update of /home/groups/yum/cvs/yum
In directory login:/tmp/cvs-serv22802
Modified Files:
yummain.py
Log Message:
- refactored to avoid code duplication and clarify
- specialised handling for PluginYumExit exceptions
- fixed exception catching typo
Index: yummain.py
===================================================================
RCS file: /home/groups/yum/cvs/yum/yummain.py,v
retrieving revision 1.96
retrieving revision 1.97
diff -u -r1.96 -r1.97
--- yummain.py 5 May 2005 10:35:19 -0000 1.96
+++ yummain.py 7 Jun 2005 13:46:21 -0000 1.97
@@ -15,47 +15,70 @@
# Copyright 2005 Duke University
-import os
import sys
import locale
import time # test purposes only
-import yum
-import yum.Errors as Errors
+from yum import Errors
+from yum import plugins
import cli
-
from i18n import _
+YUM_PID_FILE = '/var/run/yum.pid'
def main(args):
"""This does all the real work"""
- locale.setlocale(locale.LC_ALL, '')
- # our core object for the cli
- base = cli.YumBaseCli()
+ def exUserCancel():
+ base.errorlog(0, '\n\nExiting on user cancel')
+ unlock()
+ sys.exit(1)
+
+ def exIOError(e):
+ if e.errno == 32:
+ base.errorlog(0, '\n\nExiting on Broken Pipe')
+ unlock()
+ sys.exit(1)
+
+ def exPluginExit(e):
+ '''Called when a plugin raises PluginYumExit.
+
+ Log the plugin's exit message if one was supplied.
+ '''
+ exitmsg = str(e)
+ if exitmsg:
+ base.errorlog(2, '\n\n%s' % exitmsg)
+ unlock()
+ sys.exit(1)
+
+ def exFatal(e):
+ base.errorlog(0, '\n\n%s' % str(e))
+ unlock()
+ sys.exit(1)
def unlock():
try:
base.closeRpmDB()
- base.doUnlock('/var/run/yum.pid')
+ base.doUnlock(YUM_PID_FILE)
except Errors.LockError, e:
sys.exit(200)
+ locale.setlocale(locale.LC_ALL, '')
+
+ # our core object for the cli
+ base = cli.YumBaseCli()
+
# do our cli parsing and config file setup
# also sanity check the things being passed on the cli
try:
base.getOptionsConfig(args)
+ except plugins.PluginYumExit, e:
+ exPluginExit(e)
except Errors.YumBaseError, e:
- result = 1
- resultmsgs = [str(e)]
- for msg in resultmsgs:
- print >> sys.stderr, msg
-
- sys.exit(1)
-
+ exFatal(e)
try:
- base.doLock('/var/run/yum.pid')
+ base.doLock(YUM_PID_FILE)
except Errors.LockError, e:
base.errorlog(0,'%s' % e.msg)
sys.exit(200)
@@ -67,101 +90,97 @@
do = base.doCommands
try:
result, resultmsgs = do()
- except Errors, e:
+ except plugins.PluginYumExit, e:
+ exPluginExit(e)
+ except Errors.YumBaseError, e:
result = 1
resultmsgs = [str(e)]
- except KeyboardInterrupt, e:
- base.errorlog(0, '\n\nExiting on user cancel')
- unlock()
- sys.exit(1)
+ except KeyboardInterrupt:
+ exUserCancel()
except IOError, e:
- if e.errno == 32:
- base.errorlog(0, '\n\nExiting on Broken Pipe')
- unlock()
- sys.exit(1)
-
- if result not in [0, 1, 2, 100]:
- base.errorlog(0, 'Unknown Error(s): Exit Code: %d:' % result)
- for msg in resultmsgs:
- base.errorlog(0, msg)
- unlock()
- sys.exit(3)
-
- if result == 100:
- unlock()
- sys.exit(100)
+ exIOError(e)
- elif result == 0:
+ # Act on the command/shell result
+ if result == 0:
+ # Normal exit
for msg in resultmsgs:
base.log(2, '%s' % msg)
unlock()
sys.exit(0)
-
elif result == 1:
+ # Fatal error
for msg in resultmsgs:
base.errorlog(0, 'Error: %s' % msg)
unlock()
sys.exit(1)
+ elif result == 2:
+ # Continue on
+ pass
+ elif result == 100:
+ unlock()
+ sys.exit(100)
+ else:
+ base.errorlog(0, 'Unknown Error(s): Exit Code: %d:' % result)
+ for msg in resultmsgs:
+ base.errorlog(0, msg)
+ unlock()
+ sys.exit(3)
# Depsolve stage
base.log(2, 'Resolving Dependencies')
base.log(3, time.time())
try:
(result, resultmsgs) = base.buildTransaction()
+ except plugins.PluginYumExit, e:
+ exPluginExit(e)
except Errors.YumBaseError, e:
result = 1
resultmsgs = [str(e)]
- except KeyboardInterrupt, e:
- base.errorlog(0, '\n\nExiting on user cancel')
- unlock()
- sys.exit(1)
+ except KeyboardInterrupt:
+ exUserCancel()
except IOError, e:
- if e.errno == 32:
- base.errorlog(0, '\n\nExiting on Broken Pipe')
- unlock()
- sys.exit(1)
-
- if result not in [0, 1, 2]:
- base.errorlog(0, 'Unknown Error(s): Exit Code: %d:' % result)
- for msg in resultmsgs:
- base.errorlog(0, msg)
- unlock()
- sys.exit(3)
-
+ exIOError(e)
+
+ # Act on the depsolve result
if result == 0:
+ # Normal exit
unlock()
sys.exit(0)
-
elif result == 1:
+ # Fatal error
for msg in resultmsgs:
base.errorlog(0, 'Error: %s' % msg)
unlock()
sys.exit(1)
+ elif result == 2:
+ # Continue on
+ pass
+ else:
+ base.errorlog(0, 'Unknown Error(s): Exit Code: %d:' % result)
+ for msg in resultmsgs:
+ base.errorlog(0, msg)
+ unlock()
+ sys.exit(3)
base.log(2, '\nDependencies Resolved')
base.log(3, time.time())
- # run the transaction
+ # Run the transaction
try:
base.doTransaction()
+ except plugins.PluginYumExit, e:
+ exPluginExit(e)
except Errors.YumBaseError, e:
- base.errorlog(0, '%s' % e)
- unlock()
- sys.exit(1)
- except KeyboardInterrupt, e:
- base.errorlog(0, '\n\nExiting on user cancel')
- unlock()
- sys.exit(1)
+ exFatal(e)
+ except KeyboardInterrupt:
+ exUserCancel()
except IOError, e:
- if e.errno == 32:
- base.errorlog(0, '\n\nExiting on Broken Pipe')
- unlock()
- sys.exit(1)
+ exIOError(e)
base.log(2, 'Complete!')
unlock()
sys.exit(0)
-
+
if __name__ == "__main__":
#import hotshot
More information about the Yum-cvs-commits
mailing list