|
|
发表于 2007-6-23 13:35:04
|
显示全部楼层
这是我的完整代码
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
if 'updates' in url:
remoteurl = urljoin(url, "i386/%s" % po.remote_path)
else:
remoteurl = urljoin(url, "os/%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) |
|