epm-repack 9.95 KB
#!/bin/sh
#
# Copyright (C) 2017-2018, 2020  Etersoft
# Copyright (C) 2017-2018, 2020  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

load_helper epm-sh-altlinux
load_helper epm-assure

# arg: rpm or deb
# fills split_replaced_pkgs with packages of that type
__epm_split_by_pkg_type()
{
	local type="$1"
	shift

	split_replaced_pkgs=''

	for pkg in "$@" ; do
		[ "$(get_package_type "$pkg")" = "$type" ] || return 1
		[ -e "$pkg" ] || fatal "Can't read $pkg"
		split_replaced_pkgs="$split_target_pkgs $(realpath "$pkg")"
	done

	[ -n "$split_replaced_pkgs" ]
}

# fills repacked_debs
__epm_repack_rpm_to_deb()
{
	local pkg

	assure_exists alien
	assure_exists fakeroot
	assure_exists rpm

	repacked_debs=''

	local TDIR=$(mktemp -d)
	cd $TDIR || fatal

	for pkg in $rpmpkgs ; do
		showcmd_store_output fakeroot alien -d -k $scripts "$pkg"
		local DEBCONVERTED=$(grep "deb generated" $RC_STDOUT | sed -e "s| generated||g")
		repacked_debs="$repacked_rpms $(realpath $DEBCONVERTED)"
		to_remove_pkg_files="$to_remove_pkg_files $(realpath $DEBCONVERTED)"
		clean_store_output
	done

	# TODO: move it to exit handler
	if [ -z "$DEBUG" ] ; then
		# TODO: reinvent
		[ -n "$to_remove_pkg_files" ] && rm -f $to_remove_pkg_files
		[ -n "$to_remove_pkg_files" ] && rmdir $(dirname $to_remove_pkg_files | head -n1) 2>/dev/null
		[ -n "$to_remove_pkg_dirs" ] && rmdir $to_remove_pkg_dirs
	fi

	cd - >/dev/null
	return 0
}


# TODO: rewrite to convert (get some code from Korinf?)
__epm_check_if_try_install_rpm()
{
	__epm_split_by_pkg_type rpm "$@" || return 1
	__epm_repack_rpm_to_deb $split_replaced_pkgs

	# TODO: move to install
	docmd epm install $repacked_debs

	return 0
}


# args: buildroot spec
__fix_spec()
{
    local buildroot="$1"
    local spec="$2"
    local pkgname="$3"
    local i

    # drop forbidded paths
    # https://bugzilla.altlinux.org/show_bug.cgi?id=38842
    for i in / /etc /etc/init.d /etc/systemd /bin /opt /usr /usr/bin /usr/share /usr/share/doc /var /var/log /var/run \
            /etc/cron.daily /usr/share/icons /usr/share/pixmaps /usr/share/man /usr/share/man/man1 /usr/share/appdata /usr/share/applications /usr/share/menu ; do
        sed -i -e "s|^%dir \"$i/*\"$||" \
            -e "s|^\"$i/*\"$||" \
            -e "s|^$i/*$||" \
            $spec
    done

    # drop %dir for existed system dirs
    for i in $(grep '^%dir "' $spec | sed -e 's|^%dir  *"\(.*\)".*|\1|' ) ; do #"
        echo "$i" | grep -q '^/opt/' && continue
        [ -d "$i" ] && [ -n "$verbose" ] && echo "drop dir $i from packing, it exists in the system"
    done

    # replace dir "/path/dir" -> %dir /path/dir
    grep '^"/' $spec | sed -e 's|^"\(/.*\)"$|\1|' | while read i ; do
        # add dir as %dir in the filelist
        if [ -d "$buildroot$i" ] ; then
            subst 's|^\("'$i'"\)$|%dir \1|' $spec
        #else
        #    subst 's|^\("'$i'"\)$|\1|' $spec
        fi
    done

    # FIXME: where is a source of the bug with empty Summary?
    subst "s|Summary: *$|Summary: $pkgname (was empty Summary after alien)|" $spec
    subst "s|^Release: |Release: alt1.repacked.with.epm.|" $spec
    subst "s|^\((Converted from a\) \(.*\) \(package.*\)|(Repacked from binary \2 package with epm $EPMVERSION)\n\1 \2 \3|" $spec
    #" hack for highlight
}

# TODO: move this list from external file
__check_stoplist()
{
    cat <<EOF | grep -q "^$1$"
kesl
kesl-astra
klnagent
klnagent64
klnagent64-astra
EOF
}


__apply_fix_code()
{
    local repackcode="$CONFIGDIR/repack.d/$1.sh"
    [ -x "$repackcode" ] || return
    shift
    export PATH=$PROGDIR:$PATH
    local bashopt=''
    [ -n "$verbose" ] && bashopt='-x'
    docmd bash $bashopt $repackcode "$1" "$2" || fatal "There is an error from $repackcode script"
}

__create_rpmmacros()
{
    cat <<EOF >$HOME/.rpmmacros
%_topdir	$HOME/RPM
%_tmppath	$TMPDIR

%packager	EPM <support@etersoft.ru>
%_gpg_name	support@etersoft.ru
EOF
    to_remove_pkg_files="$to_remove_pkg_files $HOME/.rpmmacros"
}

# will fill repacked_rpms var
__epm_repack_to_rpm()
{
    local pkgs="$*"
    assure_distr ALTLinux "install --repack"

    # install epm-repack for static (package based) dependencies
    assure_exists fakeroot || fatal
    assure_exists alien || fatal
    assure_exists rpmbuild rpm-build || fatal

    # TODO: improve
    if echo "$pkgs" | grep -q "\.deb" ; then
        assure_exists dpkg || fatal
        # TODO: Для установки требует: /usr/share/debconf/confmodule но пакет не может быть установлен
        # assure_exists debconf
    fi

    local pkg
    export HOME=$(mktemp -d)
    __create_rpmmacros

    local alpkg
    local abspkg
    local tmpbuilddir
    repacked_rpms=''
    for pkg in $pkgs ; do
        tmpbuilddir=$HOME/$(basename $pkg).tmpdir
        mkdir $tmpbuilddir
        abspkg="$(realpath $pkg)"
        info ""
        info "Repacking $abspkg to local rpm format ..."
        # alien failed with spaced names
        # alpkg=$abspkg
        alpkg=$(basename $pkg)
        # TODO: use func for get name from deb pkg
        # TODO: epm print name from deb package
        # TODO: use stoplist only for deb?
        [ -z "$force" ] && __check_stoplist $(echo $alpkg | sed -e "s|_.*||") && fatal "Please use official rpm package instead of $alpkg (It is not recommended to use --force to skip this checking."
        # don't use abs package path: copy package to temp dir and use there
        cp $verbose $pkg $tmpbuilddir/../$alpkg
        cd $tmpbuilddir || fatal
        if [ -n "$verbose" ] ; then
            docmd fakeroot alien --generate --to-rpm $verbose $scripts "../$alpkg" || fatal
        else
            showcmd fakeroot alien --generate --to-rpm $scripts "../$alpkg"
            a='' fakeroot alien --generate --to-rpm $scripts "../$alpkg" >/dev/null || fatal
        fi

        local subdir="$(echo *)"
        [ -d "$subdir" ] || fatal "can't find subdir"

        # detect spec and move to prev dir
        local spec="$(echo $tmpbuilddir/$subdir/*.spec)"
        [ -s "$spec" ] || fatal "can't find spec"
        mv $spec $tmpbuilddir || fatal
        spec="$tmpbuilddir/$(basename "$spec")"
        local pkgname="$(grep "^Name: " $spec | sed -e "s|Name: ||g" | head -n1)"
        __fix_spec $tmpbuilddir/$subdir $spec $pkgname
        __apply_fix_code $pkgname $tmpbuilddir/$subdir $spec
        # TODO: we need these dirs to be created
        to_remove_pkg_dirs="$to_remove_pkg_dirs $HOME/RPM/BUILD $HOME/RPM"
        showcmd fakeroot rpmbuild --buildroot $tmpbuilddir/$subdir --define='_allow_root_build 1' -bb $spec
        if [ -n "$verbose" ] ; then
            a='' fakeroot rpmbuild --buildroot $tmpbuilddir/$subdir  --define='_allow_root_build 1' -bb $spec || fatal
        else
            a='' fakeroot rpmbuild --buildroot $tmpbuilddir/$subdir  --define='_allow_root_build 1' -bb $spec >/dev/null || fatal
        fi
        # remove copy of source binary package (don't mix with generated)
        rm -f $tmpbuilddir/../$alpkg
        local repacked_rpm="$(realpath $tmpbuilddir/../*.rpm)"
        if [ -s "$repacked_rpm" ] ; then
            repacked_rpms="$repacked_rpms $repacked_rpm"
            to_remove_pkg_files="$to_remove_pkg_files $repacked_rpm"
        else
            warning "Can't find converted rpm for source binary package '$pkg'"
        fi
        cd - >/dev/null
        rm -rf $tmpbuilddir/$subdir/
        rm -rf $spec
    done

    to_remove_pkg_dirs="$to_remove_pkg_dirs $HOME"
    rmdir $tmpbuilddir
    #rmdir $tmpbuilddir/..
    true
}

__epm_check_if_try_install_deb()
{
	__epm_split_by_pkg_type deb "$@" || return 1
	__epm_repack_to_rpm $split_replaced_pkgs || fatal

	# TODO: move to install
	docmd epm install $repacked_rpms

	# TODO: move it to exit handler
	if [ -z "$DEBUG" ] ; then
		# TODO: reinvent
		[ -n "$to_remove_pkg_files" ] && rm -f $to_remove_pkg_files
		[ -n "$to_remove_pkg_files" ] && rmdir $(dirname $to_remove_pkg_files | head -n1) 2>/dev/null
		[ -n "$to_remove_pkg_dirs" ] && rmdir $to_remove_pkg_dirs 2>/dev/null
	fi

	return 0
}


epm_repack()
{
    # if possible, it will put pkg_urls into pkg_files and reconstruct pkg_filenames
    if [ -n "$pkg_urls" ] ; then
        load_helper epm-download
        __handle_pkg_urls_to_install
    fi

    [ -n "$pkg_names" ] && warning "Can't find $pkg_names"
    [ -z "$pkg_files" ] && info "Skip empty repack list" && return 22

    case $PKGFORMAT in
        rpm)
            __epm_repack_to_rpm $pkg_files || fatal
            echo
            echo "Adapted packages:"
            cp $repacked_rpms .
            for i in $repacked_rpms ; do
                echo "	$(pwd)/$(basename "$i")"
            done
            ;;
        deb)
            if __epm_split_by_pkg_type rpm $pkg_files ; then
                __epm_repack_rpm_to_deb $split_replaced_pkgs
                cp -v $repacked_debs .
                pkg_files="$(estrlist exclude $split_replaced_pkgs $pkg_files)"
                [ -n "$pkg_files" ] && warning "There are left unconverted packages $pkg_files."
            fi
            ;;
        *)
            fatal "$PKGFORMAT is not supported for repack yet"
            ;;
    esac

    # TODO: move it to exit handler
    if [ -z "$DEBUG" ] ; then
        # TODO: reinvent
        [ -n "$to_remove_pkg_files" ] && rm -f $to_remove_pkg_files
        # hack??
        [ -n "$to_remove_pkg_files" ] && rmdir $(dirname $to_remove_pkg_files | head -n1) 2>/dev/null
        [ -n "$to_remove_pkg_dirs" ] && rmdir $to_remove_pkg_dirs 2>/dev/null
    fi

}