epm-sh-functions 12.6 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2014  Etersoft
# Copyright (C) 2012, 2014  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
# copied from /etc/init.d/outformat (ALT Linux)
21

22 23 24 25 26 27 28 29
check_core_commands()
{
	which --help >/dev/null || fatal "Can't find which command (which package is missed?)"
	grep --help >/dev/null || fatal "Can't find grep command (coreutils package is missed?)"
	sed --help >/dev/null || fatal "Can't find sed command (sed package is missed?)"
}


30
# FIXME on Android: FIX ME! implement ttyname_r() bionic/libc/bionic/stubs.c:366
31 32 33
inputisatty()
{
	# check stdin
34 35
	#tty -s 2>/dev/null
	test -t 0
36 37
}

38 39
isatty()
{
40 41 42 43 44 45
	# check stdout
	test -t 1
}

isatty2()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
46 47
	# check stderr
	test -t 2
48 49
}

50 51
check_tty()
{
52 53 54 55 56 57
	isatty2 || return

	# Set a sane TERM required for tput
	[ -n "$TERM" ] || TERM=dumb
	export TERM

58 59
	check_core_commands

60
	# egrep from busybox may not --color
Vitaly Lipatov's avatar
Vitaly Lipatov committed
61
	# egrep from MacOS print help to stderr
Vitaly Lipatov's avatar
Vitaly Lipatov committed
62
	if grep -E --help 2>&1 | grep -q -- "--color" ; then
63
		export EGREPCOLOR="--color"
64 65
	fi

66 67 68 69
	which tput >/dev/null 2>/dev/null || return
	# FreeBSD does not support tput -S
	echo | tput -S >/dev/null 2>/dev/null || return
	[ -z "$USETTY" ] || return
70 71 72
	export USETTY=1
}

73 74 75 76
: ${BLACK:=0} ${RED:=1} ${GREEN:=2} ${YELLOW:=3} ${BLUE:=4} ${MAGENTA:=5} ${CYAN:=6} ${WHITE:=7}

set_boldcolor()
{
77
	[ "$USETTY" = "1" ] || return
78 79 80 81 82 83 84 85
	{
		echo bold
		echo setaf $1
	} |tput -S
}

restore_color()
{
86
	[ "$USETTY" = "1" ] || return
87 88 89 90 91 92
	{
		echo op; # set Original color Pair.
		echo sgr0; # turn off all special graphics mode (bold in our case).
	} |tput -S
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
93 94
echover()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
95
    [ -z "$verbose" ] && return
96
    echo "$*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
97 98
}

99 100 101
# echo string without EOL
echon()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
102
	# default /bin/sh on MacOS does not recognize -n
Vitaly Lipatov's avatar
Vitaly Lipatov committed
103
	/bin/echo -n "$*"
104 105 106
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
107 108 109
# Used DISTRNAME
set_target_pkg_env()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
110
	[ -n "$DISTRNAME" ] || fatal "Missing DISTRNAME in set_target_pkg_env."
111 112 113 114 115
	local ver="$DISTRVERSION"
	[ -n "$ver" ] && ver="/$ver"
	PKGFORMAT=$($DISTRVENDOR -p "$DISTRNAME$ver")
	PKGVENDOR=$($DISTRVENDOR -s "$DISTRNAME$ver")
	RPMVENDOR=$($DISTRVENDOR -n "$DISTRNAME$ver")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
116 117 118
}

# Print command line and run command line
119
showcmd()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
120
{
121 122 123
	if [ -z "$quiet" ] ; then
		set_boldcolor $GREEN
		local PROMTSIG="\$"
124
		is_root && PROMTSIG="#"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
125
		echo " $PROMTSIG $*"
126
		restore_color
127
	fi >&2
128 129 130 131 132
}

# Print command line and run command line
docmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
133
	showcmd "$*$EXTRA_SHOWDOCMD"
134 135
#FIXME
	$@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
136 137
}

138
# Run every arg with docmd
139 140
docmd_foreach()
{
141
	local cmd pkg
142 143 144 145
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
146
		docmd "$cmd" $pkg
147 148 149
	done
}

150 151 152 153
# run command line with SUDO
sudorun()
{
	set_sudo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
154 155 156 157 158
	if [ -z "$SUDO" ] ; then
		"$@"
		return
	fi
	$SUDO "$@"
159 160
}

161
# Print command line and run command line with SUDO
162
sudocmd()
163
{
164
	set_sudo
165
	[ -n "$SUDO" ] && showcmd "$SUDO $*" || showcmd "$*"
166
	sudorun "$@"
167 168
}

169
# Run every arg with sudocmd
170
# Returns on any error
171 172
sudocmd_foreach()
{
173
	local cmd pkg
174 175 176 177
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
178 179
		# don't quote $cmd here: it can be a command with an args
		sudocmd $cmd $pkg || return
180 181 182
	done
}

183 184 185 186 187 188 189 190
# add realpath if missed
if ! which realpath 2>/dev/null >/dev/null ; then
realpath()
{
	readlink -f "$@"
}
fi

191 192
get_firstarg()
{
193
	echon "$1"
194 195 196 197 198
}

get_lastarg()
{
	local lastarg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
199
	eval "lastarg=\${$#}"
200
	echon "$lastarg"
201 202
}

203 204 205 206 207 208
# TODO: see etersoft-build-utils/tests/test_isnumber.sh
isnumber()
{
	echo "$*" | filter_strip_spaces | grep -q "^[0-9]\+$"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
# copied from strings
# CHECKME: the same like estrlist has ?
# Note: used egrep! write '[0-9]+(first|two)', not '[0-9]\+...'
rhas()
{
	echo "$1" | grep -E -q -- "$2"
}

# copied from strings
is_dirpath()
{
    [ "$1" = "." ] && return $?
    rhas "$1" "/"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
224 225 226 227 228 229 230 231 232 233 234 235 236
filter_strip_spaces()
{
        # possible use just
        #xargs echo
        sed -e "s| \+| |g" | \
                sed -e "s|^ ||" | sed -e "s| \$||"
}

strip_spaces()
{
        echo "$*" | filter_strip_spaces
}

237 238 239 240 241 242
# param true false
subst_option()
{
	eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
}

243 244 245 246
store_output()
{
    # use make_temp_file from etersoft-build-utils
    RC_STDOUT=$(mktemp)
247 248
    local CMDSTATUS=$RC_STDOUT.pipestatus
    echo 1 >$CMDSTATUS
249
    #RC_STDERR=$(mktemp)
250
    ( LANG=C $@ 2>&1 ; echo $? >$CMDSTATUS ) | tee $RC_STDOUT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
251
    return "$(cat $CMDSTATUS)"
252
    # bashism
253
    # http://tldp.org/LDP/abs/html/bashver3.html#PIPEFAILREF
254
    #return $PIPESTATUS
255 256
}

257 258 259 260 261 262
showcmd_store_output()
{
    showcmd "$@"
    store_output "$@"
}

263 264
clean_store_output()
{
265
    rm -f $RC_STDOUT $RC_STDOUT.pipestatus
266 267
}

268
# run epm, possible from side repo
269 270
epm()
{
271
	[ -n "$PROGNAME" ] || fatal "Can't use epm call from the piped script"
272 273 274 275 276 277 278 279
	$PROGDIR/$PROGNAME --inscript "$@"
}

# run $SUDO epm, possible from side repo
sudoepm()
{
	[ -n "$PROGNAME" ] || fatal "Can't use epm call from the piped script"
	sudorun $PROGDIR/$PROGNAME --inscript "$@"
280
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
281 282 283 284 285

# Print error message and stop the program
fatal()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
286
		echo "Error: $*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
287 288
#	else
#		echog "Error in $0: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
289
	fi
290
#	[ "$TERM" = "screen" ] && echo "(screen detected: waiting ten seconds to exit ...)" >&2 && sleep 10
Vitaly Lipatov's avatar
Vitaly Lipatov committed
291 292
	exit 1
}
293

Vitaly Lipatov's avatar
Vitaly Lipatov committed
294 295 296 297
# Print warning message
warning()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
298
		echo "Warning: $*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
299 300 301 302 303
#	else
#		echog "Error in $0: $@" >&2
	fi
}

304 305 306 307
info()
{
	[ -n "$quiet" ] && return

308 309 310
	# print message to stderr if stderr forwarded to (a file)
	if isatty2 ; then
		isatty || return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
311
		echo "$*"
312
	else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
313
		echo "$*" >&2
314
	fi
315 316
}

317 318 319
# if we have not sudo, returns 1 and set SUDO variable to fatal
SUDO_TESTED=''
SUDO_CMD='sudo'
320 321
set_sudo()
{
322 323 324 325 326 327
	local nofail="$1"

	# cache the result
	[ -n "$SUDO_TESTED" ] && return "$SUDO_TESTED"
	SUDO_TESTED="0"

328 329 330
	SUDO=""
	# skip SUDO if disabled
	[ -n "$EPMNOSUDO" ] && return
331 332 333 334
	if [ "$DISTRNAME" = "Cygwin" ] || [ "$DISTRNAME" = "Windows" ] ; then
		# skip sudo using on Windows
		return
	fi
335

336
	# if we are root, do not need sudo
337
	is_root && return
338

339 340 341
	# start error section
	SUDO_TESTED="1"

342
	if ! which $SUDO_CMD >/dev/null 2>/dev/null ; then
343 344
		[ "$nofail" = "nofail" ] || SUDO="fatal 'Can't find sudo. Please install and tune sudo ('# epm install sudo') or run epm under root.'"
		return "$SUDO_TESTED"
345
	fi
346

347 348
	# if input is a console
	if inputisatty && isatty && isatty2 ; then
349
		if ! $SUDO_CMD -l >/dev/null ; then
350 351
			[ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only without password sudo is supported in non interactive using). Please run epm under root.'"
			return "$SUDO_TESTED"
352 353 354
		fi
	else
		# use sudo if one is tuned and tuned without password
355
		if ! $SUDO_CMD -l -n >/dev/null 2>/dev/null ; then
356
			[ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only without password sudo is supported). Please run epm under root or check http://altlinux.org/sudo.'"
357
			return "$SUDO_TESTED"
358
		fi
359 360
	fi

361
	SUDO_TESTED="0"
362
	SUDO="$SUDO_CMD --"
363
	# check for < 1.7 version which do not support -- (and --help possible too)
364
	$SUDO_CMD -h 2>/dev/null | grep -q "  --" || SUDO="$SUDO_CMD"
365

366 367
}

368 369
# wait for n seconds (if possible) during executing command
# args: seconds command
370 371 372
withtimeout()
{
	local TO=$(which timeout 2>/dev/null || which gtimeout 2>/dev/null)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
373
	if [ -x "$TO" ] ; then
374
		$TO "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
375 376
		return
	fi
377
	fatal "Possible indefinite wait due timeout command is missed"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
378
	# fallback: drop time arg and run without timeout
379 380
	#shift
	#"$@"
381 382
}

383 384 385 386 387 388
set_eatmydata()
{
	# skip if disabled
	[ -n "$EPMNOEATMYDATA" ] && return
	# use if possible
	which eatmydata >/dev/null 2>/dev/null || return
389
	set_sudo
390
	[ -n "$SUDO" ] && SUDO="$SUDO eatmydata" || SUDO="eatmydata"
391
	[ -n "$verbose" ] && info "Uwaga! eatmydata is installed, we will use it for disable all sync operations."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
392
	return 0
393 394
}

395 396
# 
__get_package_for_command()
397
{
398
	case "$1" in
399
		equery|revdep-rebuild)
400
			echo 'gentoolkit'
401
			;;
402
		update-kernel|remove-old-kernels)
403
			echo 'update-kernel'
404 405 406 407
			;;
	esac
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
# TODO:
confirm() {
    local response
    # call with a prompt string or use a default
    read -r -p "${1:-Are you sure? [y/N]} " response
    case $response in
        [yY][eE][sS]|[yY])
            true
            ;;
        *)
            false
            ;;
    esac
}

423 424 425 426 427 428 429 430 431 432 433

confirm_info()
{
	info "$*"
	if [ -z "$non_interactive" ] ; then
		confirm "Are you sure? [y/N]" || fatal "Exiting"
	fi

}


434 435 436 437 438 439
is_root()
{
	local EFFUID="$(id -u)"
	[ "$EFFUID" = "0" ]
}

440 441
assure_root()
{
442
	is_root || fatal "run me only under root"
443 444 445 446 447 448 449 450 451
}

regexp_subst()
{
	local expression="$1"
	shift
	sed -i -r -e "$expression" "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
452
# TODO: we we can't use epm directly?
453 454 455
assure_exists()
{
	load_helper epm-assure
Vitaly Lipatov's avatar
Vitaly Lipatov committed
456
	local package="$2"
457
	local textpackage=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
458
	[ -n "$package" ] || package="$(__get_package_for_command "$1")"
459
	[ -n "$3" ] && textpackage=" >= $3"
460
	( direct='' epm_assure "$1" $package $3 ) || fatal "Can't assure in '$1' command from $package$textpackage package"
461 462
}

463 464 465 466 467 468 469
__set_EGET()
{
	# use internal eget only if exists
	if [ -s $SHAREDIR/tools_eget ] ; then
		export EGET="$SHAREDIR/tools_eget"
		return
	fi
470
	fatal "Internal error: missed tools_eget"
471 472

	# FIXME: we need disable output here, eget can be used for get output
473
	assure_exists eget eget 3.3 >/dev/null
474 475 476 477
	# use external command, not the function
	export EGET="$(which eget)" || fatal "Missed command eget from installed package eget"
}

478
# will replaced within disabled_eget in packaged version
Vitaly Lipatov's avatar
Vitaly Lipatov committed
479
eget()
480
{
481
	local EGET
482 483 484 485 486
	# use internal eget only if exists
	if [ -s $SHAREDIR/tools_eget ] ; then
		$SHAREDIR/tools_eget "$@"
		return
	fi
487
	fatal "Internal error: missed tools_eget"
488

489
	# FIXME: we need disable output here, eget can be used for get output
490
	assure_exists eget eget 3.3 >/dev/null
491 492 493 494 495
	# run external command, not the function
	EGET=$(which eget) || fatal "Missed command eget from installed package eget"
	$EGET "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509
estrlist()
{
	if [ -s $SHAREDIR/tools_estrlist ] ; then
		$SHAREDIR/tools_estrlist "$@"
		return
	fi
	fatal "missed tools_estrlist"
}

onefile_estrlist()
{
	internal_tools_estrlist "$@"
}

510 511
# will replaced within eget() in packed version
onefile_eget()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
512
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
513
	assure_exists wget
514
	internal_tools_eget "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
515 516
}

517
# TODO: improve and drop!
518 519 520
get_package_type()
{
	local i
521 522 523 524 525 526 527 528 529
	case $1 in
		*.deb)
			echo "deb"
			return
			;;
		*.rpm)
			echo "rpm"
			return
			;;
530 531 532 533 534 535 536 537
		*.txz)
			echo "txz"
			return
			;;
		*.tbz)
			echo "tbz"
			return
			;;
538 539 540 541
		*.exe)
			echo "exe"
			return
			;;
542 543 544 545
		*.msi)
			echo "msi"
			return
			;;
546 547 548 549
		*.AppImage)
			echo "AppImage"
			return
			;;
550 551 552 553 554 555 556
		*)
			#fatal "Don't know type of $1"
			# return package name for info
			echo "$1"
			return 1
			;;
	esac
557 558 559
}


560
# print options description from HELPCMD/HELPOPT lines in the code
561
# args: section_name, [file with code]
562 563
get_help()
{
564 565 566
    if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
        return
    fi
567
    local F="$0"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
568 569 570
    if [ -n "$2" ] ; then
        is_dirpath "$2" && F="$2" || F="$(dirname $0)/$2"
    fi
571

572
    cat "$F" | grep -- "# $1" | while read -r n ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
573 574 575 576 577 578
        if echo "$n" | grep -q "# $1: PART: " ; then
            echo
            echo "$n" | sed -e "s|# $1: PART: ||"
            continue
        fi
        echo "$n" | grep -q "^ *#" && continue
579 580 581
        opt=`echo $n | sed -e "s|) # $1:.*||g" -e 's|"||g' -e 's@^|@@'`
        desc=`echo $n | sed -e "s|.*) # $1:||g"`
        printf "    %-20s %s\n" "$opt" "$desc"
582 583 584
    done
}

585 586
# TODO: get all info by one request (too slow)
set_distro_info()
587
{
588
	# use external distro_info if internal one is missed
589
	DISTRVENDOR=$PROGDIR/distr_info
590
	[ -x $DISTRVENDOR ] || DISTRVENDOR=distro_info
591
	export DISTRVENDOR
592

593
	[ -n "$DISTRNAME" ] || DISTRNAME=$($DISTRVENDOR -d) || fatal "Can't get distro name."
594
	[ -n "$DISTRVERSION" ] || DISTRVERSION=$($DISTRVENDOR -v)
595
	if [ -z "$DISTRARCH" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
596
		DISTRARCH=$($DISTRVENDOR --distro-arch)
597
	fi
598
	DISTRCONTROL="$($DISTRVENDOR -y)"
599 600 601 602 603 604 605
}

# FIXME: detect if not recognized
set_pm_type()
{
	local CMD
	set_distro_info
606 607
	set_target_pkg_env

608 609 610 611 612 613
# override package manager detection result
if [ -n "$FORCEPM" ] ; then
	PMTYPE=$FORCEPM
	return
fi

614
	PMTYPE="$($DISTRVENDOR -g $DISTRNAME/$DISTRVERSION)"
615
}
616

617 618
is_active_systemd()
{
619
	[ "$DISTRCONTROL" = "systemd" ]
620
}
621 622 623 624 625 626 627

assure_distr()
{
	local TEXT="this option"
	[ -n "$2" ] && TEXT="$2"
	[ "$DISTRNAME" = "$1" ] || fatal "$TEXT supported only for $1 distro"
}
628 629 630 631 632 633 634 635 636 637

# return delimiter sign in depend of package type
get_pkg_name_delimiter()
{
   local pkgtype="$1"
   [ -n "$pkgtype" ] || pkgtype="$($DISTRVENDOR -p)"

   [ "$pkgtype" = "deb" ] && echo "_" && return
   echo "-"
}
638 639 640

has_space()
{
641
    estrlist -- has_space "$@"
642
}