forked from rpm-software-management/yum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yummain.py
executable file
·393 lines (342 loc) · 12.5 KB
/
yummain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/python -t
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright 2005 Duke University
"""
Entrance point for the yum command line interface.
"""
import os
import os.path
import sys
import logging
import time
import errno
from yum import Errors
from yum import plugins
from yum import logginglevels
from yum import _
from yum.i18n import utf8_width, exception2msg
import yum.misc
import cli
from utils import suppress_keyboard_interrupt_message
def main(args):
"""Run the yum program from a command line interface."""
yum.misc.setup_locale(override_time=True)
def exUserCancel():
logger.critical(_('\n\nExiting on user cancel'))
if unlock(): return 200
return 1
def exIOError(e):
if e.errno == 32:
logger.critical(_('\n\nExiting on Broken Pipe'))
else:
logger.critical(_('\n\n%s') % exception2msg(e))
if unlock(): return 200
return 1
def exPluginExit(e):
'''Called when a plugin raises PluginYumExit.
Log the plugin's exit message if one was supplied.
''' # ' xemacs hack
exitmsg = exception2msg(e)
if exitmsg:
logger.warn('\n\n%s', exitmsg)
if unlock(): return 200
return 1
def exFatal(e):
logger.critical('\n\n%s', exception2msg(e.value))
if unlock(): return 200
return 1
def exRepoError(e):
# For RepoErrors ... help out by forcing new repodata next time.
# XXX: clean only the repo that has failed?
try:
base.cleanExpireCache()
except Errors.YumBaseError:
# Let's not confuse the user further (they don't even know we tried
# the clean).
pass
msg = _("""\
One of the configured repositories failed (%(repo)s),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to "fix" this:
1. Contact the upstream for the repository and get them to fix the problem.
2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).
3. Run the command with the repository temporarily disabled
yum --disablerepo=%(repoid)s ...
4. Disable the repository permanently, so yum won't use it by default. Yum
will then just ignore the repository until you permanently enable it
again or use --enablerepo for temporary usage:
yum-config-manager --disable %(repoid)s
or
subscription-manager repos --disable=%(repoid)s
5. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:
yum-config-manager --save --setopt=%(repoid)s.skip_if_unavailable=true
""")
repoui = _('Unknown')
repoid = _('<repoid>')
try:
repoid = e.repo.id
repoui = e.repo.name
except AttributeError:
pass
msg = msg % {'repoid' : repoid, 'repo' : repoui}
logger.critical('\n\n%s\n%s', msg, exception2msg(e))
if unlock(): return 200
return 1
def unlock():
try:
base.closeRpmDB()
base.doUnlock()
except Errors.LockError, e:
return 200
return 0
def rpmdb_warn_checks():
try:
probs = base._rpmdb_warn_checks(out=verbose_logger.info, warn=False)
except Errors.YumBaseError, e:
# This is mainly for PackageSackError from rpmdb.
verbose_logger.info(_(" Yum checks failed: %s"), exception2msg(e))
probs = []
if not probs:
verbose_logger.info(_(" You could try running: rpm -Va --nofiles --nodigest"))
logger = logging.getLogger("yum.main")
verbose_logger = logging.getLogger("yum.verbose.main")
# Try to open the current directory to see if we have
# read and execute access. If not, chdir to /
try:
f = open(".")
except IOError, e:
if e.errno == errno.EACCES:
logger.critical(_('No read/execute access in current directory, moving to /'))
os.chdir("/")
else:
f.close()
try:
os.getcwd()
except OSError, e:
if e.errno == errno.ENOENT:
logger.critical(_('No getcwd() access in current directory, moving to /'))
os.chdir("/")
# 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:
return exPluginExit(e)
except Errors.YumBaseError, e:
return exFatal(e)
except (OSError, IOError), e:
return exIOError(e)
try:
base.waitForLock()
except Errors.YumBaseError, e:
return exFatal(e)
try:
result, resultmsgs = base.doCommands()
except plugins.PluginYumExit, e:
return exPluginExit(e)
except Errors.RepoError, e:
return exRepoError(e)
except Errors.YumBaseError, e:
result = 1
resultmsgs = [exception2msg(e)]
except KeyboardInterrupt:
return exUserCancel()
except IOError, e:
return exIOError(e)
# Act on the command/shell result
if result == 0:
# Normal exit
for msg in resultmsgs:
verbose_logger.log(logginglevels.INFO_2, '%s', msg)
if unlock(): return 200
return base.exit_code
elif result == 1:
# Fatal error
for msg in resultmsgs:
logger.critical(_('Error: %s'), msg)
if unlock(): return 200
return 1
elif result == 2:
# Continue on
pass
elif result == 100:
if unlock(): return 200
return 100
else:
logger.critical(_('Unknown Error(s): Exit Code: %d:'), result)
for msg in resultmsgs:
logger.critical(msg)
if unlock(): return 200
return 3
# Mainly for ostree, but might be useful for others.
if base.conf.usr_w_check:
usrinstpath = base.conf.installroot + "/usr"
usrinstpath = usrinstpath.replace('//', '/')
if (os.path.exists(usrinstpath) and
not os.access(usrinstpath, os.W_OK)):
logger.critical(_('No write access to %s directory') % usrinstpath)
logger.critical(_(' Maybe this is an ostree image?'))
logger.critical(_(' To disable you can use --setopt=usr_w_check=false'))
if unlock(): return 200
return 1
# Depsolve stage
verbose_logger.log(logginglevels.INFO_2, _('Resolving Dependencies'))
try:
(result, resultmsgs) = base.buildTransaction()
except plugins.PluginYumExit, e:
return exPluginExit(e)
except Errors.RepoError, e:
return exRepoError(e)
except Errors.YumBaseError, e:
result = 1
resultmsgs = [exception2msg(e)]
except KeyboardInterrupt:
return exUserCancel()
except IOError, e:
return exIOError(e)
# Act on the depsolve result
if result == 0:
# Normal exit
if unlock(): return 200
return base.exit_code
elif result == 1:
# Fatal error
for prefix, msg in base.pretty_output_restring(resultmsgs):
logger.critical(prefix, msg)
if base._depsolving_failed:
if not base.conf.skip_broken:
verbose_logger.info(_(" You could try using --skip-broken to work around the problem"))
rpmdb_warn_checks()
if unlock(): return 200
return 1
elif result == 2:
# Continue on
pass
else:
logger.critical(_('Unknown Error(s): Exit Code: %d:'), result)
for msg in resultmsgs:
logger.critical(msg)
if unlock(): return 200
return 3
verbose_logger.log(logginglevels.INFO_2, _('\nDependencies Resolved'))
# Run the transaction
try:
inhibit = {'what' : 'shutdown:idle',
'who' : 'yum cli',
'why' : 'Running transaction', # i18n?
'mode' : 'block'}
return_code = base.doTransaction(inhibit=inhibit)
except plugins.PluginYumExit, e:
return exPluginExit(e)
except Errors.RepoError, e:
return exRepoError(e)
except Errors.YumBaseError, e:
return exFatal(e)
except KeyboardInterrupt:
return exUserCancel()
except IOError, e:
return exIOError(e)
# rpm ts.check() failed.
if type(return_code) == type((0,)) and len(return_code) == 2:
(result, resultmsgs) = return_code
for msg in resultmsgs:
logger.critical("%s", msg)
rpmdb_warn_checks()
return_code = result
if base._ts_save_file:
verbose_logger.info(_("Your transaction was saved, rerun it with:\n yum load-transaction %s") % base._ts_save_file)
elif return_code < 0:
return_code = 1 # Means the pre-transaction checks failed...
# This includes:
# . No packages.
# . Hitting N at the prompt.
# . GPG check failures.
if base._ts_save_file:
verbose_logger.info(_("Your transaction was saved, rerun it with:\n yum load-transaction %s") % base._ts_save_file)
else:
verbose_logger.log(logginglevels.INFO_2, _('Complete!'))
if unlock(): return 200
return return_code or base.exit_code
def hotshot(func, *args, **kwargs):
"""Profile the given function using the hotshot profiler.
:param func: the function to profile
:return: the return code given by the hotshot profiler
"""
import hotshot.stats
fn = os.path.expanduser("~/yum.prof")
prof = hotshot.Profile(fn)
rc = prof.runcall(func, *args, **kwargs)
prof.close()
print_stats(hotshot.stats.load(fn))
return rc
def cprof(func, *args, **kwargs):
"""Profile the given function using the cprof profiler.
:param func: the function to profile
:return: the return code given by the cprof profiler
"""
import cProfile, pstats
fn = os.path.expanduser("~/yum.prof")
prof = cProfile.Profile()
rc = prof.runcall(func, *args, **kwargs)
prof.dump_stats(fn)
print_stats(pstats.Stats(fn))
return rc
def print_stats(stats):
"""Print out information from a :class:`Stats` object.
:param stats: the :class:`Stats` object to print information from
"""
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(20)
stats.sort_stats('cumulative')
stats.print_stats(40)
def user_main(args, exit_code=False):
"""Call one of the multiple main() functions based on environment variables.
:param args: command line arguments passed into yum
:param exit_code: if *exit_code* is True, this function will exit
python with its exit code when it has finished executing.
Otherwise, it will return its exit code.
:return: the exit code from yum execution
"""
errcode = None
if 'YUM_PROF' in os.environ:
if os.environ['YUM_PROF'] == 'cprof':
errcode = cprof(main, args)
if os.environ['YUM_PROF'] == 'hotshot':
errcode = hotshot(main, args)
if 'YUM_PDB' in os.environ:
import pdb
pdb.run(main(args))
if errcode is None:
errcode = main(args)
if exit_code:
sys.exit(errcode)
return errcode
suppress_keyboard_interrupt_message()
if __name__ == "__main__":
try:
user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
print >> sys.stderr, _("\n\nExiting on user cancel.")
sys.exit(1)