Python API for Yum Apply

I have had some knowledge about Python API for yum through several days' learning of  yum python source.


Now I will show one example about how to use Python API for yum.

   repo = gerRepo() # get the repo path

    # create a temporary repo to point to repoid packages
    newrepo = yum.yumRepo.YumRepository('repoid') #'repoid' is set to be the temporary repo ID

    # check gpg locally
    newrepo.gpgcheck = True
    newrepo.gpgkey = 'file://%s' % GPGKEY_PATH

    newrepo.name = 'reponame'
    newrepo.baseurl = [repo] #set repo url
    newrepo.basecachedir = basecache # basecache is the cache dir
    newrepo.enablegroups = 1 # Enable group installation. This step is very important for using yum installs packages from group xml definition.


    yb.repos.add(newrepo)
    yb.repos.enableRepo(newrepo.id)
    yb.doRepoSetup(thisrepo=newrepo.id)

    # mark all packages to install
    yb.repos.populateSack()
    yb.pkgSack.buildIndexes()
    pkgs = yb.repos.getPackageSack().returnPackages()
   
    for i in pkgs:

        logger.debug('* Package: %s' % i.name)
        try:
            yb.install(name=i.name) # Mark package i.name to install and add its transaction to the transaction set.

        except:
            logger.debug("Package %s was not marked to install\n" % i)
            pass

    # Mark the all packages in group grpid to install. Note: first of all, newrepo.enablegroups = 1 should be set, otherwise, the group can not be found.

    yb.selectGroup(grpid = 'grpid')


    # configure callback to watch installation progress
    downloadCallback = DownloadCallback(callback, len(pkgs))
    yb.repos.setProgressBar(downloadCallback)

    # Yum exception happened when the window was changed.
    # Here yum doesn't need to process window change signal that is generated
    # by mouse dragging, so the signal is ignored.
    sig = signal.SIGWINCH
    signal.signal(sig, signal.SIG_IGN)

    # execute installation
    try:
        yb.resolveDeps()
        yb.buildTransaction()
        yb.processTransaction(
            TransactionCallback(downloadCallback),
            None,
            InstallationCallback(callback, len(pkgs)))

    except Exception, e:
        logger.critical('Error installing packages: %s' % str(e))
        raise

    # close yum instance
    yb.closeRpmDB()
    yb.close()
                                                

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值