我的Python脚本
实现的功能
- 扫描一个文件夹下的所有文件
- 自动进行执行rpm软件包的编译
- 自动进行相关依赖的安装
- 将安装编译成功的软件包的spec文件拷贝到相关文件夹下
- 将编译失败的软件包spec文件拷贝到错误文件夹下
import os, commands, re
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(file, dir):
mv_str = 'mv ' + file + ' ' + dir
(status, output) = commands.getstatusoutput(mv_str)
if status == 0:
print "move file success:" + file + "\n"
spec_dir ='/root/rpmbuild/SPECS'
list = get_file_list(spec_dir,[])
i = 1
count = len(list)
for e in list:
print 'this is the ' + str(i) + "'s spec file," + \
" Now " + str(count) + "'s spec file left!\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:
flag = output.index('Failed build dependencies')
if flag < 0:
print "other err\n"
move_file_to(e, '/opt/err_spec/')
else:
output = output[flag:]
req_list = get_req_list(output)
ret = install_req(req_list)
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/')
i = i + 1
count = count -1
目前还存在的BUG:
- 无法将软件依赖(类似perl的一些依赖)自动化安装
- 还没有实现多线程版本,编译速度较慢