Python多线程进行rpm包编译

  • 之前在进行软件包编译的时候使用的是单线方式,rpm包构建的速度较慢,现软脚本修改为多线程版本,具实代码如下。
#!/bin/env python
import commands, os, re
import Queue
import threading
__author__ = 'dollboy'


def get_file_list(dirs, file_list):
    #define the function to search all the file in the dir
    new_dir = dirs
    if os.path.isfile(dirs):
        file_list.append(dirs)
    elif os.path.isdir(dirs):
        for s in os.listdir(dirs):
            new_dir = os.path.join(dirs, s)
            get_file_list(new_dir, file_list)
    return file_list


#get require list from the errmsg
def get_req_list(errmsg):
    fir_pos = errmsg.index('\n')
    fir_pos = fir_pos + 1
    all_req_str = errmsg[fir_pos:]
    req_list_bf = all_req_str.split('\n')
    req_list = []
    for tmp in req_list_bf:
        tmp_list = re.findall(r'[a-zA-z]', tmp)
        pos = tmp.index(tmp_list[0])
        tmp = tmp[pos:]
        pos = tmp.index(' ')
        tmp = tmp[:pos]
        try:
            pos = tmp.index('(')
            tmp = tmp[:pos]
            req_list.append(tmp)
        except:
            req_list.append(tmp)
    return req_list


#install require function
def install_req(req_list):
    print "yum install the require start:\n"
    for tmp in req_list:
        print tmp + "\n"
        yum_str = "yum install " + tmp + " -y"
        (status, output) = commands.getstatusoutput(yum_str)
        if status == 0:
            print "install " + tmp + " success!\n"
        else:
            print " install " + tmp + " failed!\n"
            return -1
    print "install all \n"
    return 0


#move file to a place
def move_file_to(files, dirs):
    mv_str = 'mv ' + files + ' ' + dirs
    (status, output) = commands.getstatusoutput(mv_str)
    if status == 0:
        print "move file success:" + files + "\n"

i = 0
count = 0
lists = []
flag = 0
#
mylock = threading.RLock()
lock_yum = threading.RLock()
spec_dir = '/root/rpmbuild/SPECS'
q = Queue.Queue(0)
lists = get_file_list(spec_dir, [])
count = len(lists)


class RpmThread(threading.Thread):

    def __init__(self, name):
        threading.Thread.__init__(self)
        self.t_name = name

    def run(self):
        global i
        global count
        c = 0
        while True:
            mylock.acquire()
            if i < count and i != -1:
                c = i
                i = i + 1
            else:
                c = -1
                i = -1
                mylock.release()
            mylock.release()
            if c == -1:
                break
            e = lists[c]
            print 'this is the ' + str(self.c) + "'s spec file\n"
            build_str = "rpmbuild -bb " + e
            (status, output) = commands.getstatusoutput(build_str)
            if status == 0:
                print "Build " + e + " success!\n"
                move_file_to(e, '/opt/success_spec/')
            else:
                try:
                    flags = output.index('Failed build dependencies')
                    if flags < 0:
                        print "other err\n"
                        move_file_to(e, '/opt/err_spec/')

                    else:
                        output = output[flag:]
                        req_list = get_req_list(output)
                        lock_yum.acquire()
                        ret = install_req(req_list)
                        lock_yum.release()
                        if ret == 0:
                            (status, output) = commands.getstatusoutput(build_str)
                            if status == 0:
                                print "Build " + e + " success!\n"
                                move_file_to(e, '/opt/success_spec/')
                            else:
                                move_file_to(e, '/opt/err_spec/')
                        else:
                            move_file_to(e, '/opt/err_spec/')
                except:
                    move_file_to(e, '/opt/err_spec/')

#create for threads to run the rpm build task
thread1 = RpmThread('A')
thread2 = RpmThread('B')
thread3 = RpmThread('C')
thread4 = RpmThread('D')
thread1.start()
thread2.start()
thread3.start()
thread4.start()
目前还存在的问题:
  • 目前在进行有些软件包的依赖无法安装,还存在着一些问题,正在修改。
  • 无法自定义软件包安装成功及失败之后的spec文件的重新定向输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值