epm-download 8.02 KB
Newer Older
1 2
#!/bin/sh
#
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3 4
# Copyright (C) 2016-2018  Etersoft
# Copyright (C) 2016-2018  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# 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/>.
#

20 21
alt_base_dist_url="http://ftp.basealt.ru/pub/distributions"

22 23
__use_url_install()
{
24 25
    # force download if wildcard is used
    echo "$pkg_urls" | grep -q "[?*]" && return 1
26

27 28 29
    # force download if repack is asked
    [ -n "$repack" ] && return 1

30 31 32 33 34 35 36 37 38 39
    # install of remote files has a side affect
    # (more fresh package from a repo can be installed instead of the file)
    #case $DISTRNAME in
    #    "ALTLinux")
    #        # do not support https yet
    #        echo "$pkg_urls" | grep -q "https://" && return 1
    #        pkg_names="$pkg_names $pkg_urls"
    #        return 0
    #        ;;
    #esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
40

41 42 43 44 45 46 47 48 49 50 51
    case $PMTYPE in
        #apt-rpm)
        #    pkg_names="$pkg_names $pkg_urls"
        #    ;;
        #deepsolver-rpm)
        #    pkg_names="$pkg_names $pkg_urls"
        #    ;;
        #urpm-rpm)
        #    pkg_names="$pkg_names $pkg_urls"
        #    ;;
        pacman)
52
            true
53 54
            ;;
        yum-rpm|dnf-rpm)
55
            true
56 57 58 59 60 61 62 63
            ;;
        #zypper-rpm)
        #    pkg_names="$pkg_names $pkg_urls"
        #    ;;
        *)
            return 1
            ;;
    esac
64
    [ -n "$pkg_names" ] && pkg_names="$pkg_names $pkg_urls" || pkg_names="$pkg_urls"
65
    return 0
66 67 68 69 70
}

# for download before install / checking
__download_pkg_urls()
{
71 72 73
    local url
    [ -z "$pkg_urls" ] && return
    for url in $pkg_urls ; do
74
        local tmppkg
75
        tmppkg="$(mktemp -d --tmpdir=$BIGTMPDIR)" || fatal
76
        remove_on_exit $tmppkg
77
        chmod $verbose a+rX $tmppkg
78 79 80 81 82 83 84
        cd $tmppkg || fatal
        local latest='--latest'
        # hack: download all if mask is *.something
        basename "$url" | grep -q "^\*\." && latest=''
        # download packages
        if docmd eget $latest "$url" ; then
            local i
85
            for i in * ; do
86
                [ "$i" = "*" ] && warning "Incorrect true status from eget. No saved files from download $url, ignoring" && continue
87 88
                [ -s "$tmppkg/$i" ] || continue
                chmod $verbose a+r "$tmppkg/$i"
89
                [ -n "$pkg_files" ] && pkg_files="$pkg_files $tmppkg/$i" || pkg_files="$tmppkg/$i"
90 91 92 93 94 95 96 97
            done
        else
            warning "Failed to download $url, ignoring"
        fi
        cd - >/dev/null
    done
    # reconstruct
    pkg_filenames=$(strip_spaces "$pkg_files $pkg_names")
98 99 100 101 102
}

# NOTE: call __clean_downloaded_pkg_files after
__handle_pkg_urls_to_install()
{
103
    #[ -n "$pkg_urls" ] || return
104

105 106 107 108 109
    # FIXME: check type of pkg_urls separately?
    if [ "$(get_package_type "$pkg_urls")" != $PKGFORMAT ] || ! __use_url_install ; then
        # use workaround with eget: download and put in pkg_files
        __download_pkg_urls
    fi
110

111
    pkg_urls=
112 113
}

114 115
__handle_pkg_urls_to_checking()
{
116
    #[ -n "$pkg_urls" ] || return
117

118 119
    # use workaround with eget: download and put in pkg_files
    __download_pkg_urls
120

121
    pkg_urls=
122 123
}

124

125
# obsoleted
126 127
__epm_get_altpkg_url()
{
128 129 130 131 132 133 134 135 136 137 138 139
    info "TODO: https://packages.altlinux.org/api/branches"
    load_helper epm-site
    local arch=$(paoapi packages/$1 | get_pao_var arch)
    # FIXME: arch can be list
    [ "$arch" = "noarch" ] || arch=$(arch)
    # HACK: filename can be list
    local filename=$(paoapi packages/$1 | get_pao_var filename | grep $arch)
    [ -n "$filename" ] || fatal "Can't get filename"
    # fixme: get from /branches
    local dv=$DISTRNAME/$DISTRVERSION/branch
    [ "$DISTRVERSION" = "Sisyphus" ] && dv=$DISTRNAME/$DISTRVERSION
    echo "$alt_base_dist_url/$dv/$arch/RPMS.classic/$filename"
140 141
}

142
# obsoleted
143 144
__epm_print_url_alt()
{
145 146 147 148
    local url="$1"
    echo "$url"
    echo "$url" | sed -e "s|$alt_base_dist_url/$DISTRNAME|http://mirror.yandex.ru/altlinux|g"
    echo "$url" | sed -e "s|$alt_base_dist_url/$DISTRNAME|http://download.etersoft.ru/pub/ALTLinux|g"
149 150 151 152
}

__epm_print_url_alt_check()
{
153 154
    local pkg=$1
    shift
155 156
    local tm
    tm="$(mktemp)" || fatal
157
    remove_on_exit $tm
158 159 160 161 162 163 164
    assure_exists curl
    load_helper epm-site
    quiet=1
    local buildtime=$(paoapi packages/$pkg | get_pao_var buildtime)
    echo
    echo "Latest release: $(paoapi packages/$pkg | get_pao_var sourcepackage) $buildtime"
    __epm_print_url_alt "$1" | while read url ; do
165
        eget --get-response $url >$tm || { echo "$url: missed" ; continue ; }
166 167 168 169 170 171
        local http=$(cat $tm | grep "^HTTP" | sed -e "s|\r||g")
        local lastdate=$(cat $tm | grep "^Last-Modified:" | sed -e "s|\r||g")
        local size=$(cat $tm | grep "^Content-Length:" | sed -e "s|^Content-Length: ||g"  | sed -e "s|\r||g")
        echo "$url ($http $lastdate) Size: $size"
    done
    rm -f $tm
172 173 174 175
}

__epm_download_alt()
{
176 177 178 179 180
    local pkg
    if [ "$1" = "--check" ] ; then
        local checkflag="$1"
        shift
    fi
181

182
    load_helper epm-sh-altlinux
183

184 185 186 187 188 189
    # TODO: enable if install --download-only will works
    if tasknumber "$@" >/dev/null ; then
        load_helper epm-addrepo
        load_helper epm-reposave
        load_helper epm-removerepo
        load_helper epm-Install
190

191 192 193 194
        local installlist="$(get_task_packages $*)"
        # hack: drop -devel packages to avoid package provided by multiple packages
        installlist="$(estrlist reg_exclude ".*-devel .*-devel-static .*-checkinstall .*-debuginfo" "$installlist")"
        [ -n "$verbose" ] && info "Packages from task(s): $installlist"
195

196 197 198 199 200 201 202
        try_change_alt_repo
        epm_addrepo "$@"
        epm update
        [ -n "$verbose" ] && epm repo list
        docmd epm download $print_url $installlist
        epm_removerepo "$@"
        end_change_alt_repo
203

204 205
        return
    fi
206 207

    # old systems ignore reinstall ?
208 209 210 211 212 213 214 215
    for pkg in "$@" ; do
        for i in $(sudocmd apt-get install -y --print-uris --reinstall "$pkg" | cut -f1 -d " " | grep ".rpm'$" | sed -e "s|^'||" -e "s|'$||") ; do
            echo "$(basename "$i")" | grep -q "^$pkg" || continue
            [ -n "$print_url" ] && echo "$i" && continue
            eget "$i"
        done
    done
    return
216

217 218 219 220 221 222 223 224 225 226
    # old code:
    for pkg in "$@" ; do
        local url=$(__epm_get_altpkg_url $pkg)
        [ -n "$url" ] || warning "Can't get URL for $pkg"
        if [ -n "$checkflag" ] ; then
            __epm_print_url_alt_check "$pkg" "$url"
        else
            docmd eget $url || return
        fi
    done
227
}
228

229 230
epm_download()
{
231
    local CMD
232

233 234 235 236 237 238
    case "$BASEDISTRNAME" in
        "alt")
            __epm_download_alt $*
            return
            ;;
    esac
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    case $PMTYPE in
    apt-dpkg)
        if [ -n "$print_url" ] ; then
            docmd apt-get download --print-uris $* | cut -f1 -d " " | grep ".deb'$" | sed -e "s|^'||" -e "s|'$||"
            return
        fi
        docmd apt-get download $*
        ;;
    dnf-rpm)
        sudocmd dnf download $print_url $*
        ;;
    aptcyg)
        sudocmd apt-cyg download $*
        ;;
    packagekit)
        docmd pkcon download $*
        ;;
    yum-rpm)
        # TODO: check yum install --downloadonly --downloaddir=/tmp <package-name>
        assure_exists yumdownloader yum-utils
        sudocmd yumdownloader $*
        ;;
    dnf-rpm)
        sudocmd dnf download $*
        ;;
    urpm-rpm)
        sudocmd urpmi --no-install $URPMOPTIONS $@
        ;;
    tce)
        sudocmd tce-load -w $*
        ;;
    opkg)
        docmd opkg $*
        ;;
    eopkg)
        docmd eopkg fetch $*
        ;;
    homebrew)
        docmd brew fetch $*
        ;;
    *)
        fatal "Have no suitable command for $PMTYPE"
        ;;
    esac
284
}