epm-repack 7.45 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2017-2018, 2020  Etersoft
# Copyright (C) 2017-2018, 2020  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# 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

23 24
[ -n "$EPM_REPACK_SCRIPTS_DIR" ] || EPM_REPACK_SCRIPTS_DIR="$CONFIGDIR/repack.d"

25
__epm_have_repack_rule()
26
{
27 28 29
    # skip repacking on non ALT systems
    [ "$BASEDISTRNAME" = "alt" ] || return 1

30 31 32 33
    local packager="$(epm print field Packager for "$1" 2>/dev/null)"
    [ "$packager" = "EPM <support@etersoft.ru>" ] && return 1
    [ "$packager" = "EPM <support@eepm.ru>" ] && return 1

34
    # FIXME: use real way (for any archive)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
35
    local pkgname="$(epm print name for package "$1")"
36
    local repackcode="$EPM_REPACK_SCRIPTS_DIR/$pkgname.sh"
37 38 39 40 41 42
    [ -s "$repackcode" ]
}

__epm_check_if_needed_repack()
{
    __epm_have_repack_rule "$@" || return
43
    local pkgname="$(epm print name for package "$1")"
44
    warning "There is repack rule for '$pkgname' package. It is better install this package via 'epm install --repack' or 'epm play'."
45 46
}

47 48 49
# arg: rpm or deb
# fills split_replaced_pkgs with packages of that type
__epm_split_by_pkg_type()
50
{
51 52
    local type="$1"
    shift
53

54
    split_replaced_pkgs=''
55

56 57 58 59 60
    for pkg in "$@" ; do
        [ "$(get_package_type "$pkg")" = "$type" ] || return 1
        [ -e "$pkg" ] || fatal "Can't read $pkg"
        split_replaced_pkgs="$split_replaced_pkgs $pkg"
    done
61

62
    [ -n "$split_replaced_pkgs" ]
63 64 65
}


66 67 68 69 70 71 72 73 74
# $spec $PKGNAME $VERSION
__set_name_version()
{
    SPEC="$1"
    PKGNAME="$2"
    VERSION="$3"
    [ -n "$PKGNAME" ] && subst "s|^Name:.*|Name: $PKGNAME|" $SPEC
    [ -n "$VERSION" ] && subst "s|^Version:.*|Version: $VERSION|" $SPEC
}
75 76


77 78
__check_stoplist()
{
79 80 81 82 83
    local pkg="$1"
    local alf="$CONFIGDIR/repackstoplist.list"
    [ -s "$alf" ] || return 1
    [ -n "$pkg" ] || return 1
    grep -E -q "^$1$" $alf
84 85 86
}


87 88 89
__set_version_pkgname()
{
    local alpkg="$1"
90
    VERSION="$(echo "$alpkg" | grep -o -P '[-_.][0-9][0-9]*([.]*[0-9])*' | head -n1 | sed -e 's|^[-_.]||')" #"
91
    [ -n "$VERSION" ] && PKGNAME="$(echo "$alpkg" | sed -e "s|[-_.]$VERSION.*||")"
92 93 94 95 96
    # set version as all between name and extension
    #local woext="$(echo "alpkg" | sed -e 's|\.tar.*||')"
    #if [ "$woext" != "$alpkg" ] ; then
    #    VERSION="$(echo "$woext" " | sed -e "s|^$PKGNAME[-_.]||")"
    #fi
97 98
}

99 100 101 102 103 104

# AppImage version
# hack for ktalk2.4.2 -> ktalk 2.4.2
__set_version_apppkgname()
{
    local alpkg="$1"
105 106
    VERSION="$(echo "$alpkg" | grep -o -P "[-_.a-zA-Z]([0-9])([0-9])*([.]*[0-9])*" | head -n1 | sed -e 's|^[-_.a-zA-Z]||' -e 's|--|-|g' )"  #"
    [ -n "$VERSION" ] && PKGNAME="$(echo "$alpkg" | sed -e "s|[-_.]$VERSION.*||")"
107 108 109
}


110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
# arg: <package file>
# sets:
#   alpkg      - package file name without path
#   PKGNAME    - package name
#   VERSION    - version of the package
#   SUBGENERIC - name of generic file's extension
__prepare_source_package()
{
    local pkg="$1"

    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 package instead of $alpkg repacking (It is not recommended to use --force to skip this checking."

    PKGNAME=''
    VERSION=''
    SUBGENERIC=''

    # convert tarballs to tar (for alien)
    if rhas "$alpkg" "\.(rpm|deb)$" ; then
        return
    fi

    if rhas "$alpkg" "\.AppImage$" ; then
137
        __set_version_apppkgname $alpkg
138 139 140
        [ -n "$VERSION" ] || fatal "Can't get version from $alpkg."
        SUBGENERIC='appimage'
        # TODO: move repack archive to erc?
141
        [ -x "$alpkg" ] || docmd chmod u+x $verbose "$alpkg"
142 143 144
        ./$alpkg --appimage-extract || fatal
        alpkg=$PKGNAME-$VERSION.tar
        # make a tar for alien
145
        erc a $alpkg squashfs-root
146 147 148 149 150
        return
    fi

    __set_version_pkgname $alpkg
    if [ -n "$VERSION" ] ; then
151
        # TODO: don't use erc for detect type? then we potentially can skip install it
152
        pkgtype="$(erc type $alpkg)"
153 154 155
        local newalpkg
        newalpkg=$PKGNAME-$VERSION.$pkgtype
        #[ -n "$PKGNAME" ] || PKGNAME=$(basename $alpkg .$pkgtype)
156
        if [ "$pkgtype" = "tar" ] || [ "$pkgtype" = "tar.gz" ] || [ "$pkgtype" = "tgz" ] ; then
157 158 159 160
            # just rename supported formats
            if [ "$alpkg" != "$newalpkg" ] ; then
                mv $alpkg $newalpkg
            fi
161
        else
162
            # converts directly unsupported formats
163 164
            newalpkg=$PKGNAME-$VERSION.tar
            #newalpkg=$(basename $alpkg .$pkgtype).tar
165
            erc repack $alpkg $newalpkg || fatal
166 167
        fi
        if [ "$alpkg" != "$newalpkg" ] ; then
168 169 170
            rm -f $verbose $alpkg
            alpkg=$newalpkg
        fi
171 172
    else
        warning "Can't detect version in $alpkg. We have almost no chance it will supported in alien."
173 174 175 176
    fi
}


177

178 179 180 181 182 183

# FIXME: Нужно как-то обеспечить непродолжение выполнения.
# used in epm install
# fill repacked_pkgs
__epm_repack()
{
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    repacked_pkgs=''
    case $PKGFORMAT in
        rpm)
            load_helper epm-repack-rpm
            __epm_repack_to_rpm "$@" || return
            ;;
        deb)
            # FIXME: only one package in $@ is supported
            #local pkgname="$(epm print name from "$@")"
            __set_version_pkgname "$1"
            local repackcode="$EPM_REPACK_SCRIPTS_DIR/$PKGNAME.sh"
            if [ -x "$repackcode" ] ; then
                load_helper epm-repack-rpm
                load_helper epm-repack-deb
                __epm_repack_to_rpm "$@" || return
                [ -n "$repacked_pkgs" ] || return
                __epm_repack_to_deb $repacked_pkgs
            else
                load_helper epm-repack-deb
                __epm_repack_to_deb "$@" || return
            fi
            ;;
        *)
            fatal "$PKGFORMAT is not supported for repack yet"
            ;;
    esac

    return 0
212 213
}

214 215
__epm_repack_if_needed()
{
216 217
    # return 1 if there is a package in host package format
    __epm_split_by_pkg_type $PKGFORMAT "$@" && return 1
218

219 220
    __epm_repack "$@"
    return 0
221
}
222

223 224 225 226 227 228 229 230
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

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

234
    if __epm_repack $pkg_files && [ -n "$repacked_pkgs" ] ; then
235 236 237 238 239
        if [ -n "$install" ] ; then
            epm install $repacked_pkgs
            return
        fi

240
        cp $repacked_pkgs "$EPMCURDIR"
241
        if [ -z "$quiet" ] ; then
242 243
            echo
            echo "Adapted packages:"
244
            for i in $repacked_pkgs ; do
245
                echo "    $EPMCURDIR/$(basename "$i")"
246
            done
247
        fi
248 249
    fi

250
    __epm_remove_tmp_files
251
}