- 之前在进行软件包编译的时候使用的是单线方式,rpm包构建的速度较慢,现软脚本修改为多线程版本,具实代码如下。
import commands, os, re
import Queue
import threading
__author__ = 'dollboy'
def get_file_list(dirs, file_list):
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
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
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
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/')
thread1 = RpmThread('A')
thread2 = RpmThread('B')
thread3 = RpmThread('C')
thread4 = RpmThread('D')
thread1.start()
thread2.start()
thread3.start()
thread4.start()
目前还存在的问题:
- 目前在进行有些软件包的依赖无法安装,还存在着一些问题,正在修改。
- 无法自定义软件包安装成功及失败之后的spec文件的重新定向输出。