sspt-update 1.99 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
pcy committed
25
    exec_subcommand pkgcmd pull "$PKG" || exit 1
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" || exit 1
PoroCYon's avatar
PoroCYon committed
30
31
32
33
34
35
36

        return 1
    fi

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

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

update_selection() {
PoroCYon's avatar
PoroCYon committed
58
    local ALLARGS=("$@")
PoroCYon's avatar
PoroCYon committed
59
    for PKG in "${ALLARGS[@]}"; do
PoroCYon's avatar
PoroCYon committed
60
        echo -n "$PKG:	"
PoroCYon's avatar
PoroCYon committed
61
        if ! update_package "$PKG"; then
PoroCYon's avatar
PoroCYon committed
62
            echo "$PKG" >> /tmp/upgrade_list
PoroCYon's avatar
PoroCYon committed
63
64
65
66
67
68
69
        fi
    done

    upgrade_pkgs
}

update_all() {
pcy's avatar
pcy committed
70
    # TODO: speed this up, maybe by parallellising this?
PoroCYon's avatar
PoroCYon committed
71
    while read -r PKG; do
PoroCYon's avatar
PoroCYon committed
72
        echo -n "$PKG:	"
PoroCYon's avatar
PoroCYon committed
73
        if ! update_package "$PKG"; then
PoroCYon's avatar
PoroCYon committed
74
            echo "$PKG" >> /tmp/upgrade_list
PoroCYon's avatar
PoroCYon committed
75
        fi
PoroCYon's avatar
PoroCYon committed
76
    done < "$SSPT_BIN_DB_FILE"
PoroCYon's avatar
PoroCYon committed
77
78
79
80

    upgrade_pkgs
}

PoroCYon's avatar
PoroCYon committed
81
82
83
84
85
if [ -f "/tmp/upgrade_list" ]; then
    rm /tmp/upgrade_list
fi
touch /tmp/upgrade_list

PoroCYon's avatar
PoroCYon committed
86
87
88
89
90
if [ $# -eq 0 ]; then
    update_all
    exit
fi

91
update_selection "$@"
PoroCYon's avatar
PoroCYon committed
92