sspt-update 2.56 KB
Newer Older
PoroCYon's avatar
PoroCYon committed
1
2
3
4
#!/usr/bin/env bash

print_help() {
    (>&2 cat <<EOF
PoroCYon's avatar
PoroCYon committed
5
6
7
Update (pull, build and install) packages. Only if a newer version
exists, the package is recompiled and reinstalled.
Usage: sspt update [-h|-?|--help]
PoroCYon's avatar
PoroCYon committed
8
           Print this help text and exit.
PoroCYon's avatar
PoroCYon committed
9
10
11
     - sspt update <package-name...>
           Update the selected packages. If none are given, all packages
           are updated.
PoroCYon's avatar
PoroCYon committed
12
13
14
15
EOF
)
}

PoroCYon's avatar
PoroCYon committed
16
17
18
# TODO: src-db getver $pkg?
read_pkg_version() {
    local EPKG=$(escape_string "$1")
PoroCYon's avatar
PoroCYon committed
19
    < "$SSPT_SRC_DB_FILE" grep -E "^$EPKG " | grep -E -o "[A-Za-z0-9_\.\-]*$"
PoroCYon's avatar
PoroCYon committed
20
21
}

PoroCYon's avatar
PoroCYon committed
22
update_package() {
PoroCYon's avatar
PoroCYon committed
23
    local PKG="$1"
PoroCYon's avatar
PoroCYon committed
24
    local BEFORE="$(read_pkg_version "$1")"
pcy's avatar
fixes    
pcy committed
25
    exec_subcommand pkgcmd pull "$PKG" || return 2
PoroCYon's avatar
PoroCYon committed
26
    local AFTER="$(exec_subcommand pkgcmd version "$PKG")"
PoroCYon's avatar
PoroCYon committed
27

PoroCYon's avatar
PoroCYon committed
28
    if [ "$BEFORE" != "$AFTER" ]; then
pcy's avatar
pcy committed
29
        exec_subcommand src-db reg "$PKG" "$AFTER" && return 1 || return 2
PoroCYon's avatar
PoroCYon committed
30
31
32
33
34
    fi

    return 0
}
upgrade_package() {
pcy's avatar
pcy committed
35
36
    exec_subcommand pkgcmd config "$1" && \
        exec_subcommand pkgcmd build "$1" && \
pcy's avatar
pcy committed
37
38
        exec_subcommand strace "$1"
    return $?
PoroCYon's avatar
PoroCYon committed
39
40
}
upgrade_pkgs() {
pcy's avatar
pcy committed
41
42
    local ERRORS=""

PoroCYon's avatar
PoroCYon committed
43
    while read -r PKG; do
pcy's avatar
pcy committed
44
45
46
47
48
49
50
        if ! upgrade_package "$PKG"; then
            if [ -z "$ERRORS" ]; then
                ERRORS="$PKG"
            else
                ERRORS="$ERRORS, $PKG"
            fi
        fi
51
    done < "/tmp/upgrade_list"
PoroCYon's avatar
PoroCYon committed
52
    rm /tmp/upgrade_list
pcy's avatar
pcy committed
53
54
55
56

    if ! [ -z "$ERRORS" ]; then
        (>&2 echo "Failed to compile the following packages: $ERRORS")
    fi
PoroCYon's avatar
PoroCYon committed
57
58
59
}

update_selection() {
pcy's avatar
fixes    
pcy committed
60
61
    local ERRORS=""

PoroCYon's avatar
PoroCYon committed
62
    local ALLARGS=("$@")
PoroCYon's avatar
PoroCYon committed
63
    for PKG in "${ALLARGS[@]}"; do
PoroCYon's avatar
PoroCYon committed
64
        echo -n "$PKG:	"
pcy's avatar
fixes    
pcy committed
65
66
67
68
69
70
71
72
        update_package "$PKG"
        local RETV="$?"
        if [ "$RETV" = "2" ]; then
            if [ -z "$ERRORS" ]; then
                ERRORS="$PKG"
            else
                ERRORS="$ERRORS, $PKG"
            fi
pcy's avatar
pcy committed
73
        elif [ "$RETV" = "1" ]; then
PoroCYon's avatar
PoroCYon committed
74
            echo "$PKG" >> /tmp/upgrade_list
PoroCYon's avatar
PoroCYon committed
75
76
77
        fi
    done

pcy's avatar
fixes    
pcy committed
78
    if ! [ -z "$ERRORS" ]; then
pcy's avatar
pcy committed
79
80
        (>&2 echo -e "Failed to update the following packages: $ERRORS\n" \
                     "Press any key to continue.")
pcy's avatar
fixes    
pcy committed
81
82
83
84
85
86

        local FOOBAR=""
        read -r FOOBAR
        unset FOOBAR
    fi

PoroCYon's avatar
PoroCYon committed
87
88
89
90
    upgrade_pkgs
}

update_all() {
pcy's avatar
pcy committed
91
    # TODO: speed this up, maybe by parallellising this?
PoroCYon's avatar
PoroCYon committed
92
    while read -r PKG; do
PoroCYon's avatar
PoroCYon committed
93
        echo -n "$PKG:	"
PoroCYon's avatar
PoroCYon committed
94
        if ! update_package "$PKG"; then
PoroCYon's avatar
PoroCYon committed
95
            echo "$PKG" >> /tmp/upgrade_list
PoroCYon's avatar
PoroCYon committed
96
        fi
PoroCYon's avatar
PoroCYon committed
97
    done < "$SSPT_BIN_DB_FILE"
PoroCYon's avatar
PoroCYon committed
98
99
100
101

    upgrade_pkgs
}

PoroCYon's avatar
PoroCYon committed
102
103
104
105
106
if [ -f "/tmp/upgrade_list" ]; then
    rm /tmp/upgrade_list
fi
touch /tmp/upgrade_list

PoroCYon's avatar
PoroCYon committed
107
108
109
110
111
if [ $# -eq 0 ]; then
    update_all
    exit
fi

112
update_selection "$@"
PoroCYon's avatar
PoroCYon committed
113