epm-download 5.24 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()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
24 25
	case $DISTRNAME in
		"ALTLinux")
26
			# do not support https yet
27
			echo "$pkg_urls" | grep -q "https://" && return 1
28 29
			# force download if wildcard is used
			echo "$pkg_urls" | grep -q "[?*]" && return 1
30
			pkg_names="$pkg_names $pkg_urls"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
31
			return 0
32
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
33 34 35 36 37 38
	esac

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

# for download before install / checking
__download_pkg_urls()
{
	local url
	[ -z "$pkg_urls" ] && return
	for url in $pkg_urls ; do
67 68 69 70 71 72 73 74 75
		local tmppkg=$(mktemp -d) || fatal "failed mktemp -d"
		cd $tmppkg || fatal
		if docmd eget "$url" ; then
			local i
			for i in $(basename $url) ; do
				[ -s "$tmppkg/$i" ] || continue
				pkg_files="$pkg_files $tmppkg/$i"
				to_remove_pkg_files="$to_remove_pkg_files $tmppkg/$i"
			done
76 77 78
		else
			warning "Failed to download $url, ignoring"
		fi
79
		cd - >/dev/null
80
	done
81
	# reconstruct
82
	pkg_filenames=$(strip_spaces "$pkg_files $pkg_names")
83 84 85 86 87 88 89 90 91
}

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

	# TODO: do it correctly
	to_remove_pkg_files=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
92 93
	# FIXME: check type of pkg_urls separately?
	if [ "$(get_package_type "$pkg_urls")" != $PKGFORMAT ] || ! __use_url_install ; then
94 95 96 97 98 99 100
		# use workaround with eget: download and put in pkg_files
		__download_pkg_urls
	fi

	pkg_urls=
}

101 102 103 104 105 106 107 108 109 110 111 112 113
__handle_pkg_urls_to_checking()
{
	#[ -n "$pkg_urls" ] || return

	# TODO: do it correctly
	to_remove_pkg_files=
	
	# use workaround with eget: download and put in pkg_files
	__download_pkg_urls

	pkg_urls=
}

114 115 116 117 118 119
#__clean_downloaded_pkg_files()
#{
#	[ -z "$to_remove_pkg_files" ] && return
#	rm -fv $to_remove_pkg_files
#}

120 121 122 123 124 125 126 127 128
__epm_get_altpkg_url()
{
	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)
129
	[ -n "$filename" ] || fatal "Can't get filename"
130 131 132
	# fixme: get from /branches
	local dv=$DISTRNAME/$DISTRVERSION/branch
	[ "$DISTRVERSION" = "Sisyphus" ] && dv=$DISTRNAME/$DISTRVERSION
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	echo "$alt_base_dist_url/$dv/$arch/RPMS.classic/$filename"
}

__epm_print_url_alt()
{
	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"
}

__epm_print_url_alt_check()
{
	local pkg=$1
	shift
	local tm=$(mktemp)
	epm assure 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
Vitaly Lipatov's avatar
Vitaly Lipatov committed
156
		a='' curl -s --head $url >$tm || { echo "$url: missed" ; continue ; }
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		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
}

__epm_download_alt()
{
	local pkg
	if [ "$1" = "--check" ] ; then
		local checkflag="$1"
		shift
	fi
	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
181
}
182

183 184 185 186
epm_download()
{
	local CMD

187 188
	case $DISTRNAME in
		ALTLinux)
189
			__epm_download_alt $pkg_filenames
190 191 192 193
			return
			;;
	esac

194 195
	case $PMTYPE in
	dnf-rpm)
196 197 198 199
		sudocmd dnf download $pkg_filenames
		;;
	aptcyg)
		sudocmd apt-cyg download $pkg_filenames
200
		;;
201 202 203
	yum-rpm)
		# TODO: check yum install --downloadonly --downloaddir=/tmp <package-name>
		assure_exists yumdownloader yum-utils
204 205 206 207 208
		sudocmd yumdownloader $pkg_filenames
		;;
	dnf-rpm)
		sudocmd dnf download $pkg_filenames
		;;
209 210 211
	urpm-rpm)
		sudocmd urpmi --no-install $URPMOPTIONS $@
		;;
212 213
	tce)
		sudocmd tce-load -w $pkg_filenames
214
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
215 216 217
	opkg)
		docmd opkg $pkg_filenames
		;;
218 219 220 221 222
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
	esac
}