|

楼主 |
发表于 2007-6-17 11:27:36
|
显示全部楼层
谢谢这么多热心朋友的捧场。有些朋友以邮件或者悄悄话的形式询问了一些问题,在此统一回答一下:
1: 关于Fedora 7的问题。 由于Fedora 7 暂时还不能支持我的ATI的显卡,短期内没有升级到Fedora 7的打算,所以相关问题暂时无法回答。
2: 关于遇到Not Found的问题。 可能是在某些情况下拼凑remote url的方法不对,实际地址中少了 i386这个目录,增加上就可以了。
3:对于为什么要在开始下载前删除原先的文件或者在下载彻底失败后删除下载了一半的文件,主要是因为,下载失败以后将会使用yum自己的方式下载,如果目标文件已经存在,会导致yum下载到不可用的文件。
我对这个插件进行了一些改进,主要增加的功能如下:
第一: 增加了对目标文件的判断,如果目标文件已经存在并且大小和要求的大小相同,则认为文件已经存在,不再重复下载。
第二: 根据目标文件的大小自适应axel连接的数目,目前采用文件大小除以最小文件大小的方式来计算连接数。因为这个连接数并不是越多越好,对于小文件,很多的连接数不但导致下载速度降低,而且也容易出现下载错误。
第三: 下载下一个文件的时候将优先使用前一个下载的mirror。
第四:修正了原有的一些缺陷。比如如果第一个文件是小文件则后面的都不用axel下载,默认仅尝试前三个mirror等。
下面给出完整代码,有任何意见建议欢迎跟帖。
[PHP]
from yum.plugins import PluginYumExit, TYPE_CORE, TYPE_INTERACTIVE
from urlparse import urljoin
import os
requires_api_version = '2.3'
plugin_type = (TYPE_CORE, TYPE_INTERACTIVE)
enablesize=300000
trymirrornum=-1
maxconn=10
def init_hook(conduit):
global enablesize,trymirrornum,maxconn
enablesize = conduit.confInt('main','enablesize',default=30000)
trymirrornum = conduit.confInt('main','trymirrornum',default=-1)
maxconn = conduit.confInt('main','maxconn',default=10)
def predownload_hook(conduit):
global enablesize
preffermirror=""
for po in (conduit.getDownloadPackages()):
if hasattr(po, 'pkgtype') and po.pkgtype == 'local':
continue
totsize = long(po.size)
ret = False
if totsize <= enablesize:
conduit.info(2, " ackage %s download size %d less than %d,Skip plugin!" % (po.repo.id,totsize,enablesize))
continue
else:
conduit.info(2, "Ok,we will try to use axel to download this big file:%d" % totsize)
local = po.localPkg()
if os.path.exists(local):
if not os.path.exists(local+".st"):
fstate=os.stat(local)
if totsize == fstate.st_size:
conduit.info(2,"Target already exists,skip to next file!")
continue
localall = "%s %s" % (local,local+".st")
rmcmd = "rm -f %s" % (localall)
curmirroridx = 0
conduit.info(2,"Before we start,clean all the key files")
os.system(rmcmd)
connnum = totsize / enablesize
if connnum > maxconn:
connnum = maxconn
mirrors=[]
mirrors[:0]=po.repo.urls
if preffermirror != "":
mirrors[:0] = [preffermirror]
for url in mirrors:
curmirroridx += 1
if (curmirroridx > trymirrornum) and (trymirrornum != -1):
conduit.info(2, " ackage %s has tried %d mirrors,Skip plugin!" % (po.repo.id,trymirrornum))
break
remoteurl = urljoin(url, "i386/%s" % po.remote_path)
syscmd = "axel -a -n %s %s -o %s" % (connnum,remoteurl,local)
conduit.info(2, "Execute axel cmd:\n%s" % syscmd)
os.system(syscmd)
if os.path.exists(local+".st"):
conduit.info(2,"axel exit by exception,let's try another mirror")
continue
else:
ret = True
preffermirror=url
break
if not ret:
conduit.info (2,"try to run rm cmd:%s" % rmcmd)
os.system(rmcmd)
[/PHP] |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|