epm-pack 5.34 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

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

26 27 28
# <packname> <abstarname> [packversion]
# fills returntarname with returned tar name or empty
__epm_pack_run_handler()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
29
{
30 31
    local packname="$1"
    local tarname="$2"
32
    local packversion="$3"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
33
    local url="$3"
34 35 36
    returntarname=''

    local repackcode="$EPM_PACK_SCRIPTS_DIR/$packname.sh"
37
    [ -x "$repackcode" ] || return
38 39
    [ -f "$repackcode.rpmnew" ] && warning "There is .rpmnew file(s) in $EPM_PACK_SCRIPTS_DIR dir. The pack script can be outdated."

40 41
    # a file to keep filename of generated tarball
    filefortarname="$(pwd)/filefortarname"
42

43
    [ "$PROGDIR" = "/usr/bin" ] && SCPATH="$PATH" || SCPATH="$PROGDIR:$PATH"
44
    local bashopt=''
45
    [ -n "$debug" ] && bashopt='-x'
46
    #info "Running $($script --description 2>/dev/null) ..."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
47 48
    # TODO: add url info here
    ( unset EPMCURDIR ; export PATH=$SCPATH ; docmd $CMDSHELL $bashopt $repackcode "$tarname" "$filefortarname" "$packversion" "$url") || fatal
49
    returntarname="$(cat "$filefortarname")" || fatal "pack script $repackcode didn't set tarname"
50 51 52 53 54 55

    local i
    for i in $returntarname ; do
        [ -s "$i" ] || fatal "pack script for $packname returned unexist $i file"
    done

56 57
    return 0
}
58

59 60 61 62
# we run this function in a tmpdir
__epm_pack()
{
    local packname="$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
63
    local URL="$4"
64 65 66

    # fills returntarname with packed tar
    __epm_pack_run_handler "$@" || fatal "Can't find pack script for packname $packname"
67 68

    if [ -n "$download_only" ] ; then
69
        mv $returntarname $EPMCURDIR
70 71 72
        return
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
73 74 75
    # TODO: merge eepm.yaml here (common with $returntarname.eepm.yaml)
    # add upstream_url: $URL too

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    # note: this repack related code here for follow reasons:
    #  * repack by default if we have repack rule
    #  * get repacked files
    #  * install (repacked) files
    # the most replacement is epm repack [--install] or epm install [--repack]

    # FIXME: check for every package would be more reliable
    # by default
    dorepack='--repack'
    # don't repack by default there is our pkg format
    __epm_split_by_pkg_type $PKGFORMAT $returntarname && dorepack=''
    # repack if we have a repack rule for it
    [ -z "$norepack" ] && __epm_have_repack_rule $returntarname && dorepack='--repack'
    # repack if forced
    [ -n "$repack" ] && dorepack='--repack'
91

92
    local pkgnames
93 94 95
    if [ -n "$dorepack" ]  ; then
        __epm_repack $returntarname
        [ -n "$repacked_pkgs" ] || fatal "Can't repack $returntarname"
96
        # remove packed file if we have repacked one
Vitaly Lipatov's avatar
Vitaly Lipatov committed
97
        rm -f $returntarname
98
        pkgnames="$repacked_pkgs"
99
    else
100
        pkgnames="$returntarname"
101 102 103
    fi

    if [ -n "$install" ] ; then
104
        docmd epm install $pkgnames
105 106 107
        return
    fi

108
    # we need put result in the cur dir
109 110 111 112 113 114
    mv -v $pkgnames $EPMCURDIR || fatal

    local i
    for i in $pkgnames ; do
        [ -r "$i.eepm.yaml" ] && mv -v "$i.eepm.yaml" $EPMCURDIR
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
115

Vitaly Lipatov's avatar
Vitaly Lipatov committed
116
    return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
117 118 119 120 121 122
}

epm_pack_help()
{
    cat <<EOF
epm pack - create rpm package from files
123
Usage: epm pack [options] <packname> <tar|url|dir> [version]
Vitaly Lipatov's avatar
Vitaly Lipatov committed
124
Options:
Vitaly Lipatov's avatar
Vitaly Lipatov committed
125
    <packname>            - receipt
Vitaly Lipatov's avatar
Vitaly Lipatov committed
126 127
    <dir>                 - create tarball from the dir before
    <url>                 - download tar from url
128
    [version]             - force version for unversioned sources
Vitaly Lipatov's avatar
Vitaly Lipatov committed
129 130 131 132
    --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
133 134 135 136 137 138 139 140 141 142 143 144
EOF
}


epm_pack()
{

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

145 146 147
    local tmpdir
    tmpdir="$(mktemp -d --tmpdir=$BIGTMPDIR)" || fatal
    remove_on_exit "$tmpdir"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
148 149 150

    local packname="$1"
    local tarname="$2"
151
    local packversion="$3"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
152
    local url=''
Vitaly Lipatov's avatar
Vitaly Lipatov committed
153 154 155 156

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

    if is_url "$tarname"; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
157
        url="$tarname"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
158 159
        pkg_urls="$tarname"
        load_helper epm-download
160
        cd $tmpdir || fatal
Vitaly Lipatov's avatar
Vitaly Lipatov committed
161
        __handle_pkg_urls_to_install
162
        [ -n "$pkg_files" ] || fatal "Can't download $tarname"
163
        tarname="$(realpath "$pkg_files")"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
164
    elif [ -d "$tarname" ] ; then
165
        tarname="$(realpath "$tarname")"
166 167 168 169 170 171
    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
172 173
    fi

174
    cd $tmpdir || fatal
Vitaly Lipatov's avatar
Vitaly Lipatov committed
175
    __epm_pack "$packname" "$tarname" "$packversion" "$url"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
176

Vitaly Lipatov's avatar
Vitaly Lipatov committed
177
}