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
fixes    
pcy committed
29
        exec_subcommand src-db reg "$PKG" "$AFTER" || return 2
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
    rm /tmp/upgrade_list
pcy's avatar
pcy committed
55
56
57
58

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

update_selection() {
pcy's avatar
fixes    
pcy committed
62
63
    local ERRORS=""

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

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

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

PoroCYon's avatar
PoroCYon committed
89
90
91
92
    upgrade_pkgs
}

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

    upgrade_pkgs
}

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

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

114
update_selection "$@"
PoroCYon's avatar
PoroCYon committed
115