epm-install 11.4 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012-2013  Etersoft
# Copyright (C) 2012-2013  Vitaly Lipatov <lav@etersoft.ru>
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5
#
6 7 8
# 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
Vitaly Lipatov's avatar
Vitaly Lipatov committed
9 10 11 12 13
# (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
14
# GNU Affero General Public License for more details.
Vitaly Lipatov's avatar
Vitaly Lipatov committed
15
#
16 17
# 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/>.
Vitaly Lipatov's avatar
Vitaly Lipatov committed
18 19
#

20
load_helper epm-query
21
load_helper epm-check_updated_repo
22

23 24 25 26 27 28 29 30 31 32
# TODO: use when run install with epm --skip-installed install
filter_out_installed_packages()
{
	[ -z "$skip_installed" ] && cat && return

	case $PKGFORMAT in
		"rpm")
			LANG=C LC_ALL=C xargs -n1 rpm -q 2>&1 | grep 'is not installed' |
				sed -e 's|^.*package \(.*\) is not installed.*|\1|g'
			;;
33 34 35 36 37
		# dpkg -l lists some non ii status (un, etc)
		#"deb")
		#	LANG=C LC_ALL=C xargs -n1 dpkg -l 2>&1 | grep -i 'no packages found matching' |
		#		sed -e 's|\.\+$||g' -e 's|^.*[Nn]o packages found matching \(.*\)|\1|g'
		#	;;
38
		*)
39 40 41
			for i in $(cat) ; do
				is_installed $i || echo $i
			done
42
			;;
43
	esac | sed -e "s|rpm-build-altlinux-compat[^ ]*||g" | filter_strip_spaces
44 45
}

46 47 48 49 50
# for zypper before SUSE/11.0
__use_zypper_no_gpg_checks()
{
    a= zypper install --help 2>&1 | grep -q -- "--no-gpg-checks" && echo "--no-gpg-checks"
}
51

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
# args: cmd_reinstall, cmd_install, packages
__separate_sudocmd_foreach()
{
    local cmd_re=$1
    local cmd_in=$2
    shift 2
    separate_installed $@
    if [ -n "$pkg_noninstalled" ] ; then
        sudocmd_foreach "$cmd_re" $pkg_noninstalled || return
    fi
    if [ -n "$pkg_installed" ] ; then
        sudocmd_foreach "$cmd_in" $pkg_installed || return
    fi
    return 0
}

# args: cmd_reinstall, cmd_install, packages
__separate_sudocmd()
{
    local cmd_re=$1
    local cmd_in=$2
    shift 2
    separate_installed $@
    if [ -n "$pkg_noninstalled" ] ; then
        sudocmd "$cmd_re" $pkg_noninstalled || return
    fi
    if [ -n "$pkg_installed" ] ; then
        sudocmd "$cmd_in" $pkg_installed || return
    fi
    return 0
}

84 85 86 87 88 89 90 91 92
download_pkg_urls()
{
	local url
	[ -z "$1" ] && return
	for url in $* ; do
	    eget $url || warning "Skipped"
	done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
93
# copied from etersoft-build-utils/share/eterbuild/functions/rpmpkg
94
epm_install_names()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
95
{
96 97 98 99 100
	if [ -n "$non_interactive" ] ; then
		epm_ni_install_names "$@"
		return
	fi

101
	[ -z "$1" ] && return
102
	case $PMTYPE in
103
		apt-rpm|apt-dpkg)
104
			sudocmd apt-get $APTOPTIONS install $@
105
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
106 107 108
		aptitude-dpkg)
			sudocmd aptitude install $@
			return ;;
109 110 111
		deepsolver-rpm)
			sudocmd ds-install $@
			return ;;
112
		urpm-rpm)
113
			sudocmd urpmi $URPMOPTIONS $@
114
			return ;;
115
		pkgsrc)
116
			sudocmd pkg_add -r $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
117
			return ;;
118 119 120
		pkgng)
			sudocmd pkg install $@
			return ;;
121
		emerge)
122
			sudocmd emerge -uD $@
123
			return ;;
124
		pacman)
125
			sudocmd pacman -S $force $nodeps $@
126
			return ;;
127 128 129
		aura)
			sudocmd aura -A $force $nodeps $@
			return ;;
130
		yum-rpm)
131
			sudocmd yum $YUMOPTIONS install $@
132
			return ;;
133 134 135
		dnf-rpm)
			sudocmd dnf install $@
			return ;;
136 137 138
		snappy)
			sudocmd snappy install $@
			return ;;
139
		zypper-rpm)
140
			sudocmd zypper install $ZYPPEROPTIONS $@
141 142
			return ;;
		mpkg)
143
			sudocmd mpkg install $@
144
			return ;;
145 146 147
		conary)
			sudocmd conary update $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
148
		npackd)
149 150
			# FIXME: correct arg
			__separate_sudocmd_foreach "npackdcl add --package=" "npackdcl update --package=" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
151
			return ;;
152
		slackpkg)
153
			__separate_sudocmd_foreach "/usr/sbin/slackpkg install" "/usr/sbin/slackpkg upgrade" $@
154 155
			return ;;
		homebrew)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
156
			# FIXME: sudo and quote
157
			__separate_sudocmd "brew install" "brew upgrade" $@
158 159 160 161
			return ;;
		ipkg)
			[ -n "$force" ] && force=-force-depends
			sudocmd ipkg $force install $@
162
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
163
		nix)
164
			__separate_sudocmd "nix-env --install" "nix-env --upgrade" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
165
			return ;;
166 167 168
		guix)
			__separate_sudocmd "guix package -i" "guix package -i" $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
169
		android)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
170
			fatal "We still have no idea how to use package repository, ever if it is F-Droid."
171
			return ;;
172
		*)
173
			fatal "Have no suitable install command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
174
			;;
175 176 177
	esac
}

178
# Non interactive install
179 180
epm_ni_install_names()
{
Danil Mikhailov's avatar
Danil Mikhailov committed
181
	[ -z "$1" ] && return
182
	case $PMTYPE in
183
		apt-rpm|apt-dpkg)
184
			export DEBIAN_FRONTEND=noninteractive
185
			sudocmd apt-get -y --force-yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" $APTOPTIONS install $@
186
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
187 188 189
		aptitude-dpkg)
			sudocmd aptitde -y install $@
			return ;;
190
		yum-rpm)
191
			sudocmd yum -y $YUMOPTIONS install $@
192 193
			return ;;
		urpm-rpm)
194
			sudocmd urpmi --auto $URPMOPTIONS $@
195 196
			return ;;
		zypper-rpm)
197
			# FIXME: returns true ever no package found, need check for "no found", "Nothing to do."
198
			yes | sudocmd zypper --non-interactive $ZYPPEROPTIONS install $@
199
			return ;;
200
		pkgsrc)
201
			sudocmd pkg_add -r $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
202
			return ;;
203 204 205
		pkgng)
			sudocmd pkg install -y $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
206 207 208
		emerge)
			sudocmd emerge -uD $@
			return ;;
209
		pacman)
210
			sudocmd pacman -S --noconfirm $force $nodeps $@
211
			return ;;
212 213 214
		aura)
			sudocmd aura -A $force $nodeps $@
			return ;;
215 216
		npackd)
			#  npackdcl update --package=<package> (remove old and install new)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
217
			sudocmd npackdcl add --package=$@
218
			return ;;
219 220 221
		chocolatey)
			docmd chocolatey install $@
			return ;;
222 223 224
		ipkg)
			sudocmd ipkg -force-defaults install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
225 226 227
		nix)
			sudocmd nix-env --install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
228 229 230
		#android)
		#	sudocmd pm install $@
		#	return ;;
231
		slackpkg)
232
			# FIXME: broken status when use batch and default answer
233
			__separate_sudocmd_foreach "/usr/sbin/slackpkg -batch=on -default_answer=yes install" "/usr/sbin/slackpkg -batch=on -default_answer=yes upgrade" $@
234
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
235
		*)
236
			fatal "Have no suitable appropriate install command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
237 238 239 240
			;;
	esac
}

241 242 243 244 245 246 247
__epm_check_if_rpm_already_installed()
{
	# Not: we can make optimize if just check version?
	LANG=C $SUDO rpm -Uvh $force $nodeps $@ 2>&1 | grep -q "is already installed"
}


248
epm_install_files()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
249
{
Danil Mikhailov's avatar
Danil Mikhailov committed
250
    [ -z "$1" ] && return
251

Vitaly Lipatov's avatar
Vitaly Lipatov committed
252 253 254 255
    # TODO: check read permissions
    # sudo test -r FILE
    # do not fallback to install_names if we have no permissions

256
    case $PMTYPE in
257
        apt-rpm)
258
            sudocmd rpm -Uvh $force $nodeps $@ && return
259
            local RES=$?
260 261 262

            __epm_check_if_rpm_already_installed $@ && return

263
            # if run with --nodeps, do not fallback on hi level
264
            [ -n "$nodeps" ] && return $RES
265

266 267
            # use install_names
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
268
        apt-dpkg|aptitude-dpkg)
269 270 271 272
            # the new version of the conf. file is installed with a .dpkg-dist suffix
            if [ -n "$non_interactive" ] ; then
                DPKGOPTIONS="--force-confdef --force-confold"
            fi
273
            # FIXME: return false in case no install and in case install with broken deps
274
            sudocmd dpkg $DPKGOPTIONS -i $@
275
            local RES=$?
276 277
            # if run with --nodeps, do not fallback on hi level

278
            [ -n "$nodeps" ] && return $RES
279
            # fall to apt-get -f install for fix deps
Vitaly Lipatov's avatar
Vitaly Lipatov committed
280 281
            # can't use APTOPTIONS with empty install args
            epm_install_names -f
282 283 284

            # repeat install for get correct status
            sudocmd dpkg $DPKGOPTIONS -i $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
285
            return
286
            ;;
287 288 289 290
        yum-rpm|dnf-rpm)
            sudocmd rpm -Uvh $force $nodeps $@ && return
            # if run with --nodeps, do not fallback on hi level

291 292
            __epm_check_if_rpm_already_installed $@ && return

293 294 295 296
            [ -n "$nodeps" ] && return
            YUMOPTIONS=--nogpgcheck
            # use install_names
            ;;
297 298
        zypper-rpm)
            sudocmd rpm -Uvh $force $nodeps $@ && return
299 300
            local RES=$?

301 302
            __epm_check_if_rpm_already_installed $@ && return

303 304
            # if run with --nodeps, do not fallback on hi level

305
            [ -n "$nodeps" ] && return $RES
306
            ZYPPEROPTIONS=$(__use_zypper_no_gpg_checks)
307 308
            # use install_names
            ;;
309 310
        urpm-rpm)
            sudocmd rpm -Uvh $force $nodeps $@ && return
311 312
            local RES=$?

313 314
            __epm_check_if_rpm_already_installed $@ && return

315
            # if run with --nodeps, do not fallback on hi level
316
            [ -n "$nodeps" ] && return $RES
317 318

            URPMOPTIONS=--no-verify-rpm
319 320
            # use install_names
            ;;
321
        pkgsrc)
322
            sudocmd pkg_add $@
323
            return ;;
324 325 326 327 328 329 330 331 332 333 334
        pkgng)
            local PKGTYPE="$(get_package_type $@)"
            case "$PKGTYPE" in
                tbz)
                    sudocmd pkg_add $@
                    ;;
                *)
                    sudocmd pkg add $@
                    ;;
            esac
            return ;;
335 336 337
        android)
            sudocmd pm install $@
            return ;;
338
        emerge)
339 340
            load_helper epm-install-emerge
            sudocmd epm_install_emerge $@
341
            return ;;
342
        pacman)
343
            sudocmd pacman -U --noconfirm $force $nodeps $@ && return
344 345 346
            local RES=$?

            [ -n "$nodeps" ] && return $RES
347
            sudocmd pacman -U $force $@
348
            return ;;
349
        slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
350
            # FIXME: check for full package name
Vitaly Lipatov's avatar
Vitaly Lipatov committed
351
            # FIXME: broken status when use batch and default answer
352
            __separate_sudocmd_foreach "/sbin/installpkg" "/sbin/upgradepkg" $@
353
            return ;;
354 355
    esac

356
    # other systems can install file package via ordinary command
357
    epm_install_names "$@"
358 359
}

360 361 362
epm_print_install_command()
{
    case $PMTYPE in
363
        apt-rpm|yum-rpm|urpm-rpm|zypper-rpm|dnf-rpm)
364 365
            echo "rpm -Uvh --force $nodeps $@"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
366
        apt-dpkg|aptitude-dpkg)
367 368
            echo "dpkg -i $@"
            ;;
369
        pkgsrc)
370 371
            echo "pkg_add $@"
            ;;
372 373 374
        pkgng)
            echo "pkg add $@"
            ;;
375 376 377 378
        emerge)
            # need be placed in /usr/portage/packages/somewhere
            echo "emerge --usepkg $@"
            ;;
379
        pacman)
380
            echo "pacman -U --noconfirm --force $nodeps $@"
381 382
            ;;
        slackpkg)
383
            echo "/sbin/installpkg $@"
384
            ;;
385 386 387
        npackd)
            echo "npackdcl add --package=$@"
            ;;
388 389 390
        ipkg)
            echo "ipkg install $@"
            ;;
391 392 393
        android)
            echo "pm install $@"
            ;;
394
        *)
395
            fatal "Have no suitable appropriate install command for $PMTYPE"
396 397 398 399
            ;;
    esac
}

400 401 402

epm_install()
{
403
    if [ -n "$show_command_only" ] ; then
404
        epm_print_install_command $pkg_filenames
405 406 407
        return
    fi

408 409
    # Download urls via eget pkg_urls and use eget
    # TODO: use optimization (rpm can download packages by url, yum too?)
410 411 412 413 414
    #[ -n "$pkg_urls" ] && warning "URL using does not realize yet"
    #download_pkg_urls "$pkg_urls"
    # temp. hack
    pkg_files="$pkg_files $pkg_urls"
    # TODO: add downloaded files to $pkg_files
415

416
    [ -z "$pkg_files$pkg_names$pkg_urls" ] && info "Skip empty install list" && return 22
417

418 419
    local names="$(echo $pkg_names | filter_out_installed_packages)"
    local files="$(echo $pkg_files | filter_out_installed_packages)"
420
    local urls="$(echo $pkg_urls | filter_out_installed_packages)"
421

422
    [ -z "$files$names" ] && info "Skip empty install list" && return 22
423

424 425 426
    # it is useful for first time running
    update_repo_if_needed

427
    epm_install_names $names || return
428
    epm_install_files $files
Vitaly Lipatov's avatar
Vitaly Lipatov committed
429
}