deb包转rpm包

deb包转rpm包

#!/bin/bash

ValueArch=""
ValueDebArch=""
ValuePackage=""
ValueDescription=""

function createSPEC()
{
cat << END > ${1}
Name:    ${ValuePackage}
Version: $(date "+%Y%m%d")
Release: 1%{?dist}
License: GPLv2
Summary: this is a rpm created by deb2rpm at $(date "+%Y%m%d")

%description
${ValueDescription}
END
}

function findInstallFilesInDebDir()
{
array=$(find ${1})
for file in ${array[@]}
do
	if [ -f ${file} ]; then
		pathfile=${file#${1}}
		if [[ ! ${pathfile} =~ /DEBIAN/ ]]; then
			echo ${pathfile}
		fi
	fi
done
}

function debdir2rpm()
{
    DebDir=${1}
    TmpRpmDir=${2}
    if [ -d ${DebDir}/DEBIAN ]; then
        if [ -f ${DebDir}/DEBIAN/control ]; then
            array=(BUILD BUILDROOT RPMS SOURCES SPECS SRPMS)
            for dir in ${array[@]}
            do
                mkdir -p ${TmpRpmDir}/${dir}
            done
            ValueDebArch=$(cat ${DebDir}/DEBIAN/control | grep -n "^Architecture:" | cut -d ' ' -f 2)
            ValuePackage=$(cat ${DebDir}/DEBIAN/control | grep -n "^Package:" | cut -d ' ' -f 2)
            ValueDescription=$(cat ${DebDir}/DEBIAN/control | grep Description: | cut -d ' ' -f 2-)
            PathRpmSpec=${TmpRpmDir}/SPECS/${ValuePackage}.spec
            PathRpmData=${TmpRpmDir}/BUILDROOT/${ValuePackage}
            case ${ValueDebArch} in
            amd64)
                ValueArch=x86_64
                ;;
            arm64)
                ValueArch=aarch64
                ;;
            *)
                echo "出现未知架构软件包"
                exit -1
                ;;
            esac
            createSPEC ${PathRpmSpec}
            if [ -f ${DebDir}/DEBIAN/preinst ]; then
                echo "%prep"                 >> ${PathRpmSpec}
                cat ${DebDir}/DEBIAN/preinst >> ${PathRpmSpec}
            fi
            if [ -f ${DebDir}/DEBIAN/postinst ]; then
                echo "%post"                  >> ${PathRpmSpec}
                cat ${DebDir}/DEBIAN/postinst >> ${PathRpmSpec}
            fi
            if [ -f ${DebDir}/DEBIAN/prerm ]; then
                echo "%preun"              >> ${PathRpmSpec}
                cat ${DebDir}/DEBIAN/prerm >> ${PathRpmSpec}
            fi
            if [ -f ${DebDir}/DEBIAN/postrm ]; then
                echo "%postun"              >> ${PathRpmSpec}
                cat ${DebDir}/DEBIAN/postrm >> ${PathRpmSpec}
            fi
            echo "%files"                   >> ${PathRpmSpec}
            array=$(find ${DebDir})
            for file in ${array[@]}
            do
	            if [ -f ${file} ]; then
		            pathfile=${file#${DebDir}}
		            if [[ ! ${pathfile} =~ /DEBIAN/ ]]; then
			            echo ${pathfile} >> ${PathRpmSpec}
                        path=${PathRpmData}/$(dirname ${pathfile})
                        if [ ! -e ${path} ]; then
                            mkdir -p ${path}
                        fi
                        cp ${file} ${path}
		            fi
	            fi
            done
            echo "%changelog"                      >> ${PathRpmSpec}
            echo "# This is helloworld changelog." >> ${PathRpmSpec}
            mv    ${PathRpmData}                     ${PathRpmData}-$(date "+%Y%m%d")-1
            cp -r ${PathRpmData}-$(date "+%Y%m%d")-1 ${PathRpmData}-$(date "+%Y%m%d")-1.${ValueArch}
            rpmbuild -bb --target=${ValueArch} --define "_topdir ${TmpRpmDir}" ${PathRpmSpec}
        else
            echo "错误: ${DebDir}/DEBIAN文件夹中不存在control文件"
        fi
    else
        echo "异常的deb文件夹: ${DebDir}"
    fi
}

if [[ ${#} != 2 ]]; then
    echo "${0}: 参数个数异常, 个数为${@}"
    echo "请输入${0} 参数1(Deb包路径/Deb文件夹路径)  参数2(目标rpm包生成路径)"
else
    DebDirPath=${1}
    DebDirBaseName=$(basename -s .deb ${1})
    if [ -f ${1} ]; then
        DebDirPath=$(dirname ${1})/${DebDirBaseName}
        dpkg -x ${1} ${DebDirPath}
        dpkg -e ${1} ${DebDirPath}/DEBIAN
    fi
    debdir2rpm ${DebDirPath} ${2}/TmpRPM_${DebDirBaseName}
    mv ${2}/TmpRPM_${DebDirBaseName}/RPMS/${${ValueArch}}/*.rpm ${2}
    rm -rf ${2}/TmpRPM_${DebDirBaseName}
    if [ -f ${1} ]; then
        rm -r ${DebDirPath}
    fi
fi


posted @ 2024-04-08 21:45  Linux袁浩  阅读(353)  评论(0)    收藏  举报