summaryrefslogtreecommitdiff
path: root/src/shared/deptree.sh
blob: e1ecee726fad4ad7ec5704d81a0a6f0fed4ec1a5 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
 ##############################################################################
 #                      parabola-riscv64-bootstrap                            #
 #                                                                            #
 #    Copyright (C) 2018  Andreas Grapentin                                   #
 #                                                                            #
 #    This program is free software: you can redistribute it and/or modify    #
 #    it under the terms of the GNU General Public License as published by    #
 #    the Free Software Foundation, either version 3 of the License, or       #
 #    (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           #
 #    GNU General Public License for more details.                            #
 #                                                                            #
 #    You should have received a copy of the GNU General Public License       #
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.   #
 ##############################################################################

check_deptree() {
  echo -n "checking for complete deptree ... "

  local have_deptree=yes
  [ -f "$DEPTREE".FULL ] || have_deptree=no
  echo $have_deptree

  [ "x$have_deptree" == "xyes" ] || return "$ERROR_MISSING"
}

build_deptree() {
  check_exe ed pacman sed || return

  # create empty deptree
  truncate -s0 "$DEPTREE".FULL || return

  # add the packages listed in the given groups
  local g p r
  for g in "$@"; do
    if pacman -Si "$g" &>/dev/null; then
      deptree_add_entry "$g" "<immediate>"
    else
      for p in $(pacman -Sg "$g" | awk '{print $2}'); do
        r=$(make_realpkg "$p") || return "$ERROR_MISSING"
        deptree_add_entry "$r" "$g"
      done
    fi
  done

  return 0
}

prepare_deptree() {
  check_deptree || build_deptree "$@" || return

  [ -f "$DEPTREE" ] || cp "$DEPTREE"{.FULL,}
  chown "$SUDO_USER" "$DEPTREE"{,.FULL}
}

deptree_next() {
  local pkg
  pkg=$(grep '\[ *\]' "$DEPTREE" | head -n1 | awk '{print $1}')
  [ -n "$pkg" ] || return "$ERROR_MISSING"
  echo "$pkg"
}

deptree_remove() {
  sed -i "/^$1 :/d; s/ /  /g; s/ $1 / /g; s/  */ /g" "$DEPTREE"
}

deptree_is_satisfyable() {
  grep -q "^$1 : \\[ *\\]" "$DEPTREE"
}

deptree_check_depend() {
  local OPTIND o needed=yes
  while getopts "n" o; do
    case "$o" in
      n) needed=no ;;
      *) die -e "$ERROR_INVOCATION" "Usage: ${FUNCNAME[0]} [-p] deptree pkgname depend" ;;
    esac
  done
  shift $((OPTIND-1))

  local pkg="$1"
  shift

  local r
  r=$(make_realpkg "$1") || return

  local blacklist
  blacklist=$(grep "^$r:" "$TOPSRCDIR"/blacklist.txt)

  if [ -n "$blacklist" ]; then
    error -n "$pkg: bad dependency $r: $(cut -d':' -f2 <<< "$blacklist")"
    return "$ERROR_MISSING"
  fi

  deptree_add_entry "$r" "$pkg"

  local have_pkg=no
  local path
  for path in "${DEPPATH[@]}"; do
    check_pkgfile "$path" "$r" && { have_pkg=yes; break; }
    check_pkgfile -p "breakdeps" "$path" "$r" && { have_pkg=yes; break; }
  done

  if [ "x$needed" == "xyes" ] && [ "x$have_pkg" == "xno" ]; then
    sed -i "s/^$pkg : \\[/& $r/" "$DEPTREE"{,.FULL}
  fi
}

deptree_add_entry() {
  local r="${2:-<immediate>}"

  if grep -q "^$1 :" "$DEPTREE".FULL; then
    # if pkg is in deptree, append requestee to list
    sed -i "/#.* $r\\(\$\\|[ ,]\\)/! s/^$1 : \\[.*/&, $r/" "$DEPTREE"*
  elif grep -q "^$r :" "$DEPTREE".FULL; then
    # elif requestee is in deptree, insert after requestee
    sed -i "/^$r :/a $1 : [ ] # $r" "$DEPTREE"*
  elif [ "x$r" == "x<immediate>" ]; then
    # elif requested directly, add to top of file
    sed -i "1i $1 : [ ] # $r" "$DEPTREE"*
  else
    # else append to deptree
    echo "$1 : [ ] # $r" >> "$DEPTREE".FULL
    [ -f "$DEPTREE" ] && echo "$1 : [ ] # $r" >> "$DEPTREE"
  fi
}