forked from rpm-software-management/yum-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo-check.py
executable file
·242 lines (194 loc) · 7.1 KB
/
repo-check.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
#!/usr/bin/python
# 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
sys.path.insert(0,'/usr/share/yum-cli')
import logging
from utils import YumUtilBase
from yum.misc import getCacheDir, setup_locale
import yum.Errors
class UtilCheckBase:
def __init__(self):
pass
def getNames(self):
'''
@return the names of the check used at the command line
'''
return []
def getUsage(self):
"""
@return: A usage string for the command, including arguments.
"""
raise NotImplementedError
def getSummary(self):
"""
@return: A one line summary of what the command does.
"""
raise NotImplementedError
def doSetupParser(self, parser):
'''
Setup the check's parser options
@param parser: a OptionParser instance
'''
pass
def doPreSetup(self, base, args, opts):
'''
Setup the check before yum is setup
@param base: yum base class
@param args: command line args
@param opts: command line options
'''
pass
def runCheck(self, base,args, opts):
'''
Run the check
@param base: yum base class
@param args: command line args
@param opts: command line options
'''
raise NotImplementedError
class TestCheck(UtilCheckBase):
def __init__(self):
pass
def getNames(self):
'''
@return the names of the check used at the command line
'''
return ['test']
def getUsage(self):
'''
@return: A usage string for the command, including arguments.
'''
return 'test <package> [-all]'
def getSummary(self):
'''
@return: A one line summary of what the command does.
'''
return 'Just a test check'
def doSetupParser(self, parser):
'''
Setup the tool and add tool cmd options
@param parser: OptionParser instance so the tool can add options
'''
parser.add_option("--test", default="", dest="test",
help='test option')
def doPreSetup(self, base, args, opts):
'''
Setup the check before yum is setup
@param base: yum base class
@param args: command line args
@param opts: command line options
'''
for repo in base.repos.findRepos('*-source'):
base.logger.info("Sourcerepo : %s" % repo.id)
def runCheck(self, base, args, opts):
'''
Run the check
@param args: command line args
@param opts: command line options
'''
if opts.test:
base.logger.info('OPTION: --test=%s is used' % opts.test)
for pkg in base.pkgSack:
if pkg.name.startswith('yum'):
print pkg
class RepoCheck(YumUtilBase):
NAME = 'repo-check'
VERSION = '1.0'
def __init__(self):
self._checks = {}
# Register checks
self.registerCheck(TestCheck())
# setup the base
YumUtilBase.__init__(self,
RepoCheck.NAME,
RepoCheck.VERSION,
self._makeUsage())
self.logger = logging.getLogger("yum.verbose.cli.repo-check")
# get the parser
self.optparser = self.getOptionParser()
self.main()
def do_parser_setup(self):
if hasattr(self,'getOptionGroup'): # check if the group option API is available
parser = self.getOptionGroup()
else:
parser = self.optparser
# Call the checks parser setup methods
for check in self._checks.values():
check.doSetupParser(parser)
def main(self):
self.do_parser_setup()
try:
opts = self.doUtilConfigSetup()
except yum.Errors.RepoError, e:
self.logger.error(str(e))
sys.exit(50)
# Check if there is anything to do.
if len(self.cmds) < 1:
print self.optparser.format_help()
sys.exit(0)
if not self.cmds[0] in self._checks:
print "\nUnknown Command : %s \n" % self.cmds[0]
print self.optparser.format_help()
sys.exit(0)
# Make it work as non root
if self.conf.uid != 0:
cachedir = getCacheDir()
self.logger.debug('Running as non-root, using %s as cachedir' % cachedir)
if cachedir is None:
self.logger.error("Error: Could not make cachedir, exiting")
sys.exit(50)
self.repos.setCacheDir(cachedir)
# Turn off cache
self.conf.cache = 0
# make sure the repos know about it, too
self.repos.setCache(0)
# Run the checks preSetup methods
if self.cmds[0] in self._checks:
check = self._checks[self.cmds[0]]
args = self.cmds[1:]
check.doPreSetup(self,args,opts)
# Setup yum (Ts, RPM db, Repo & Sack)
self.doUtilYumSetup()
# Run the check
if self.cmds[0] in self._checks:
check = self._checks[self.cmds[0]]
args = self.cmds[1:]
self.logger.info("Running the %s check" % self.cmds[0])
check.runCheck(self,args,opts)
def registerCheck(self, command):
names = command.getNames()
for name in names:
if name in self._checks:
self.logger.error('Command "%s" already defined' % name)
self._checks[name] = command
def _makeUsage(self):
"""
Format an attractive usage string for yum, listing subcommand
names and summary usages.
"""
usage = 'repo-check [options] COMMAND\n\nList of Commands:\n\n'
commands = yum.misc.unique(self._checks.values())
commands.sort(cmp=lambda x,y : cmp(x.getNames()[0], y.getNames()[0]))
for command in commands:
# XXX Remove this when getSummary is common in plugins
try:
summary = command.getSummary()
usage += "%-14s %s\n" % (command.getNames()[0], summary)
except (AttributeError, NotImplementedError):
usage += "%s\n" % command.getNames()[0]
return usage
if __name__ == '__main__':
setup_locale()
util = RepoCheck()