File: apt

package info (click to toggle)
debootstick 2.8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 296 kB
  • sloc: sh: 1,364; makefile: 34
file content (83 lines) | stat: -rw-r--r-- 2,534 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# vim: filetype=sh

# Package management
# ------------------
package_is_installed()
{
    dpkg-query -W --showformat='${Status}\n' \
                    $1 2>/dev/null | happy_grep -c "^i"
}

list_available_packages()
{
    apt-cache search "$1" | awk '{print $1}'
}

package_exists()
{
    [ "$(list_available_packages "^$1$")" = "$1" ]
}

apt_sources_add_section()
{
    section="$1"
    if grep "^#.*\<deb[[:space:]].*${section}" /etc/apt/sources.list
    then
        # lines are there, but they are commented, just uncomment them
        sed -i -e "s/^#[[:space:]#]*\(\<deb[[:space:]].*\<${section}\>\)/\1/g" /etc/apt/sources.list
    else
        # no lines for this section, edit lines with section main
        sed -i -e "s/\(^[[:space:]]*deb[[:space:]].*\<main\>\)/\1 ${section}/g" /etc/apt/sources.list
    fi
}

# with some OS versions, package installation
# causes many things to be printed to stderr. Some of those things
# are just informational, others are very minor warnings.
# we will silence them with grep.
INSTALL_GREP_PATTERN="$(cat << EOF | tr -d '\n'
(delaying package configuration)|(Done.)|(^Moving old)|(^Running)|
(^update-initramfs: deferring)|(^Examining)|(^run-parts: executing)|
(^update-initramfs: Generating)|(^initrd.img)|(points to)|
(doing nothing)|(^vmlinu)|(connect to Upstart)|(policy-rc.d denied)|
(Creating config)|(^$)|(start and stop actions)|(^Created symlink)|
(Initializing machine ID)|(:$)|(etc.modprobe.d)|
(initramfs support missing)|(policy-rc.d returned 101)|
(update-alternatives: warning: skip creation)|
(Secure Boot validation state)|(if you wish to change)|
(default time zone)|(time is now)|(Time is now)|
(^Creating group)|(^Creating user)|(using gzip)
EOF
)"

install_packages()
{
    packages=$*

    # disable service startup at package installation
    if [ -e /usr/sbin/policy-rc.d ]
    then
	mv /usr/sbin/policy-rc.d /usr/sbin/policy-rc.d.saved
    fi
    echo exit 101 > /usr/sbin/policy-rc.d
    chmod +x /usr/sbin/policy-rc.d

    err_output="$(
        apt-get -qq --no-install-recommends -o=Dpkg::Use-Pty=0 \
                install $packages 2>&1 >/dev/null
    )" || return_code=$?

    echo "$err_output" | happy_grep -vE "$INSTALL_GREP_PATTERN" 1>&2

    # restore policy-rc.d conf
    if [ -e /usr/sbin/policy-rc.d.saved ]
    then
	mv /usr/sbin/policy-rc.d.saved /usr/sbin/policy-rc.d
    else
        rm /usr/sbin/policy-rc.d
    fi

    # the return value we want is the one we caught
    # earlier (or none if all went well):
    return $return_code
}