Python脚本自动化编译RPM包

我的Python脚本

实现的功能

  • 扫描一个文件夹下的所有文件
  • 自动进行执行rpm软件包的编译
  • 自动进行相关依赖的安装
  • 将安装编译成功的软件包的spec文件拷贝到相关文件夹下
  • 将编译失败的软件包spec文件拷贝到错误文件夹下

#!/bin/env python
import os, commands, re

#define the function to search all the file in the dir
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

#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(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' # = raw_input("Please enter the spec file dir:\n")

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:
        #print "build err " + e + " rpmbuild return number is:"
        #print status
        #print "\n"
        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的一些依赖)自动化安装
  • 还没有实现多线程版本,编译速度较慢
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值