红联Linux门户
Linux帮助

yum源码分析

发布时间:2014-10-31 15:23:41来源:linux网站作者:cucme

(1):yum clean的源码,在/usr/share/yum-cli/cli.py 中 882行
def cleanCli(self, userlist):
函数中.
支持的参数有:
yum clean:
header
packages
metadata
dbcache
expire-cache
plugins


针对不同的输入,会调用
self.cleanPackages()
self.cleanHeaders()
self.cleanMetadata()
self.cleanSqlite()
这几个函数,删除不同的项


然后我们在文件
/usr/lib/python2.4/site-packages/yum/__init__.py
中能够看到上面几个函数的实现:
ef cleanHeaders(self):
exts = ['hdr']
return self._cleanFiles(exts, 'hdrdir', 'header')

def cleanPackages(self):
exts = ['rpm']
return self._cleanFiles(exts, 'pkgdir', 'package')
等等。


这几个clean函数都是通过封装 __cleanFiles来实现的,看看这个函数:
def _cleanFiles(self, exts, pathattr, filetype):
filelist = []
removed = 0
for ext in exts:
for repo in self.repos.listEnabled():
repo.dirSetup()
path = getattr(repo, pathattr)
if os.path.exists(path) and os.path.isdir(path):
filelist = misc.getFileList(path, ext, filelist)
for item in filelist:
try:
os.unlink(item)
except OSError, e:
self.logger.critical(_('Cannot remove %s file %s'), filetype, item)
continue
else:
self.verbose_logger.log(logginglevels.DEBUG_4,
_('%s file %s removed'), filetype, item)
removed+=1
msg = _('%d %s files removed') % (removed, filetype)
return 0, [msg]


其实就是去对应的目录下删除文件而已。

可以如下测试:
修改代码段:
for item in filelist:
try:
print "====:", item
os.unlink(item)
添加打印,保存


然后先 yum list 生成cache
可以 yum list 看下生成的文件
tree
.
|-- ops.5.i386
|   |-- cachecookie
|   |-- packages
|   |-- primary.sqlite
|   `-- repomd.xml
|-- ops.5.noarch
|   |-- cachecookie
|   |-- packages
|   |-- primary.sqlite
|   `-- repomd.xml
`-- rhel.5.i386
|-- cachecookie
|-- packages
|-- primary.sqlite
`-- repomd.xml


然后 sudo yum clean all
输出如下:
====: //var/cache/yum/ops.5.noarch/repomd.xml
====: //var/cache/yum/ops.5.i386/repomd.xml
====: //var/cache/yum/rhel.5.i386/repomd.xml
====: //var/cache/yum/ops.5.noarch/cachecookie
====: //var/cache/yum/ops.5.i386/cachecookie
====: //var/cache/yum/rhel.5.i386/cachecookie
====: //var/cache/yum/ops.5.noarch/primary.sqlite
====: //var/cache/yum/ops.5.i386/primary.sqlite
====: //var/cache/yum/rhel.5.i386/primary.sqlite


再去/va/cache/yum下tree下,看不到yum list 后的文件了。