epm-pack 4.53 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/bin/sh
#
# Copyright (C) 2023  Etersoft
# Copyright (C) 2023  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
Vitaly Lipatov's avatar
Vitaly Lipatov committed
22
load_helper epm-repack
Vitaly Lipatov's avatar
Vitaly Lipatov committed
23 24 25 26 27 28

[ -n "$EPM_PACK_SCRIPTS_DIR" ] || EPM_PACK_SCRIPTS_DIR="$CONFIGDIR/pack.d"


__epm_pack()
{
29 30
    local packname="$1"
    local tarname="$2"
31
    local packversion="$3"
32 33 34 35 36 37
    returntarname=''

    local repackcode="$EPM_PACK_SCRIPTS_DIR/$packname.sh"
    [ -x "$repackcode" ] || fatal "Can't find script $repackcode for packname $packname"
    [ -f "$repackcode.rpmnew" ] && warning "There is .rpmnew file(s) in $EPM_PACK_SCRIPTS_DIR dir. The pack script can be outdated."

38
    tmpdir="$(mktemp -d)"
39 40 41 42 43 44 45 46 47 48 49 50
    filefortarname="$tmpdir/filefortarname"
    trap "rm -rf $tmpdir" EXIT

    set_sudo
    export SUDO

    # TODO: inside () ?
    export PATH=$PROGDIR:$PATH

    local bashopt=''
    [ -n "$verbose" ] && bashopt='-x'
    #info "Running $($script --description 2>/dev/null) ..."
51
    ( unset EPMCURDIR ; docmd $CMDSHELL $bashopt $repackcode "$tarname" "$filefortarname" "$packversion" ) || fatal
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    returntarname="$(cat "$filefortarname")" || fatal "pack script $repackcode didn't set tarname"
    rm -rf $tmpdir

    [ -s "$returntarname" ] || fatal "pack script $repackcode didn't return tarname"

    #local newname="$(basename "$returntarname")"

    # now it is by default in the current dir
    #if [ "$returntarname" != "$(realpath $newname)" ; then
    #    # repack put its result to the current dir
    #    mv -v $returntarname . || fatal
    #fi

    if [ -n "$download_only" ] ; then
        return
    fi

    trap "rm -v $returntarname" EXIT

    # FIXME: __epm_repack will drop trap
    repacked_pkgs=''
    # repack to our target
    if __epm_repack_if_needed $returntarname ; then
        [ -n "$repacked_pkgs" ] || fatal "Can't repack $returntarname"
    # if repack is forced or repack rule (not disabled) is exists
    elif [ -n "$repack" ] || [ -z "$norepack" ] && __epm_have_repack_rule $returntarname ; then
        __epm_repack "$returntarname"
        [ -n "$repacked_pkgs" ] || fatal "Can't repack $returntarname"
    fi

    if [ -n "$repacked_pkgs" ] ; then
        rm -v $returntarname
        mv -v $repacked_pkgs . || fatal
        pkgname=$(pwd)/"$(basename $repacked_pkgs)"
    else
        pkgname="$returntarname"
    fi

    if [ -n "$install" ] ; then
        trap "rm -v $pkgname" EXIT
        docmd epm install $pkgname
        return
    fi

    trap "" EXIT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
97 98 99 100 101 102 103

}

epm_pack_help()
{
    cat <<EOF
epm pack - create rpm package from files
104
Usage: epm pack [options] <packname> <tar|url|dir> [version]
Vitaly Lipatov's avatar
Vitaly Lipatov committed
105
Options:
Vitaly Lipatov's avatar
Vitaly Lipatov committed
106
    <packname>            - receipt
Vitaly Lipatov's avatar
Vitaly Lipatov committed
107 108
    <dir>                 - create tarball from the dir before
    <url>                 - download tar from url
109
    [version]             - force version for unversioned sources
Vitaly Lipatov's avatar
Vitaly Lipatov committed
110 111 112 113
    --install             - install after pack result
    --repack              - force repack ever if returned package can be installed without repack
    --download-only       - save pack result and exit
    --save-only           - save repacked packages and exit (this is default behaviour)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
EOF
}


epm_pack()
{

    if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
        epm_pack_help
        exit
    fi

#TODO
    trap "__epm_remove_tmp_files" EXIT

    local packname="$1"
    local tarname="$2"
131
    local packversion="$3"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
132 133 134 135 136 137 138

    [ -n "$packname" ] || fatal "run with packname, see --help"

    if is_url "$tarname"; then
        pkg_urls="$tarname"
        load_helper epm-download
        __handle_pkg_urls_to_install
139 140
        # drop spaces and get full path
        tarname="$(realpath $pkg_files)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
141
    elif [ -d "$tarname" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
142
        fatal "FIXME: implement packing of the dir $tarname?"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
143
        tarname=''
144 145 146 147 148 149
    elif [ -s "$tarname" ] ; then
        # get full path for real name
        tarname="$(realpath "$tarname")"
    else
        # just pass name
        true
Vitaly Lipatov's avatar
Vitaly Lipatov committed
150 151
    fi

152
    __epm_pack "$packname" "$tarname" "$packversion"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
153

Vitaly Lipatov's avatar
Vitaly Lipatov committed
154
}