summaryrefslogtreecommitdiff
path: root/bin/why-dont-you
blob: 2e3ea860f594b78a5224d50e60142c32f07d776d (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/bin/sh

# usage: why_dont_you $action $package1 $package2 ...

# investigate, why a certain operation is not done with certain packages

# shellcheck source=conf/default.conf
. "${0%/*}/../conf/default.conf"

action="$1"
shift

tmp_dir=$(mktemp -d 'tmp.why-dont-you.XXXXXXXXXX' --tmpdir)
trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT

case "${action}" in

  'build')

    for pkg in "$@"; do
      {
        grep "^$(str_to_regex "${pkg}") " "${work_dir}/build-list" || \
          >&2 printf '"%s" is not on the build list.\n' "${pkg}"
      } | \
        while read -r package git_revision mod_git_revision repository; do

          if [ -f "${work_dir}/package-states/$1.$2.$3.$4.locked" ]; then
            printf '"%s" is locked by ' "$1"
            sort -u < \
              "${work_dir}/package-states/$1.$2.$3.$4.locked" \
              sed '
                :a
                  $!{
                    N
                    s/\n/, /
                    ba
                  }
                s/$/./
              '
            continue
          fi
          if [ -f "${work_dir}/package-states/$1.$2.$3.$4.blocked" ]; then
            printf '"%s" is blocked: "' "${pkg}"
            tr '[:space:]' ' ' < \
              "${work_dir}/package-states/$1.$2.$3.$4.blocked" | \
              sed '
                s|  \+| |
                s|^ ||
                s| $||
              '
            printf '"\n'
            continue
          fi

          unmet_dependencies=$(find_dependencies_on_build_list "${package}" "${git_revision}" "${mod_git_revision}" "${repository}")
          if [ -n "${unmet_dependencies}" ]; then
            printf '"%s" has unmet dependencies:\n' "${package}"
            echo "${unmet_dependencies}" | \
              while read -r dep; do
                grep -lxF "${dep}" "${work_dir}/package-infos/"*".builds" | \
                  sed '
                    s|^.*/||
                    s|\(\.[^.]\+\)\{4\}||
                  '
              done | \
              sort -u
            printf '\n'

            continue
          fi

          if [ -f "${work_dir}/package-states/${package}.${git_revision}.${mod_git_revision}.${repository}.broken" ]; then
            printf '"%s" is broken (%sx built), but would be built\n' \
              "${pkg}" \
              "$(wc -l < "${work_dir}/package-states/${package}.${git_revision}.${mod_git_revision}.${repository}.broken")"
          else
            printf '"%s" would be built\n' "${pkg}"
          fi
        done

    done

  ;;

  'stabilize'|'unstage')

    if [ "${action}" = 'stabilize' ]; then
      suffix='tested'
    else
      suffix='done'
    fi

    {
      awk '{print $1 "." $2 "." $3 "." $4}' < \
        "${work_dir}/build-list"
      if [ "${action}" = 'stabilize' ]; then
        find "${work_dir}/package-states" -maxdepth 1 \( \
          -name '*.done' -o \
          -name '*.testing' \
        \) -printf '%f\n' | \
          sed 's|\.[^.]\+$||'
      fi
    } | \
      sort -u > \
      "${tmp_dir}/unmoveable-list"

    find "${work_dir}/package-states" -maxdepth 1 -name "*.${suffix}" -printf '%f\n' | \
      sed 's|\.[^.]\+$||' | \
      sort -u > \
      "${tmp_dir}/moveable-list"

    cat "${tmp_dir}/moveable-list" "${tmp_dir}/unmoveable-list" | \
      sed '
        s|^|'"${work_dir}/package-infos/"'|
        s|$|.run-depends|
      ' | \
      # base is boring, so we ignore it (might give result "... can be unstaged" although it _will_not_ be unstaged!
      xargs -r grep -vHxF 'base' | \
      sed '
        s|^[^:]*/||
        s|\.run-depends:| |
      ' | \
      sort -k2,2 > \
      "${tmp_dir}/all-run-depends"

    cat "${tmp_dir}/moveable-list" "${tmp_dir}/unmoveable-list" | \
      sed '
        s|^|'"${work_dir}/package-infos/"'|
        s|$|.builds|
      ' | \
      # base is boring, so we ignore it (might give result "... can be unstaged" although it _will_not_ be unstaged!
      xargs -r grep -vHxF 'base' | \
      sed '
        s|^[^:]*/||
        s|\.builds:| |
      ' | \
      sort -k2,2 > \
      "${tmp_dir}/all-builds"

    for pkg in "$@"; do

      if ! state_file=$(
          grep '^'"$(str_to_regex "${pkg}")"'\(\.[^.]\+\)\{3\}$' "${tmp_dir}/moveable-list"
        ) || \
        [ ! -f "${work_dir}/package-states/${state_file}.${suffix}" ]; then
        printf '"%s" is not %s yet!\n' "${pkg}" "${suffix}"
        continue
      fi

      echo "${state_file}" > "${tmp_dir}/dependent.new"
      touch "${tmp_dir}/dependent"

      while [ -s "${tmp_dir}/dependent.new" ]; do
        cat "${tmp_dir}/dependent.new" "${tmp_dir}/dependent" | \
          sort -u | \
          sponge "${tmp_dir}/dependent"

        {
          sed '
            s|^|'"${work_dir}"'/package-infos/|
            s|$|.builds|
          ' "${tmp_dir}/dependent.new" | \
            xargs -r cat | \
            sort -u | \
            join -1 1 -2 2 -o 2.1 - "${tmp_dir}/all-run-depends"
          sed '
            s|^|'"${work_dir}"'/package-infos/|
            s|$|.run-depends|
          ' "${tmp_dir}/dependent.new" | \
            xargs -r cat | \
            sort -u | \
            join -1 1 -2 2 -o 2.1 - "${tmp_dir}/all-builds"
        } | \
          sort -u | \
          sponge "${tmp_dir}/dependent.new"

        cat "${tmp_dir}/dependent.new" "${tmp_dir}/dependent" "${tmp_dir}/dependent" | \
          sort | \
          uniq -u | \
          sponge "${tmp_dir}/dependent.new"
      done

      dependent_unmoveable=$(
        join -1 1 -2 1 -o 2.1 "${tmp_dir}/unmoveable-list" "${tmp_dir}/dependent"
      )

      if [ -n "${dependent_unmoveable}" ]; then
        printf 'The following packages are dependent on "%s", but cannot be %sd:\n' "${pkg}" "${action}"
        echo "${dependent_unmoveable}" | \
          while read -r sf; do
            printf '%s' "${sf}"
            if [ -f "${work_dir}/package-states/${sf}.testing" ]; then
              printf ' (not tested yet for %s days)' "$(( ($(date '+%s') - $(stat -c '%Y' "${work_dir}/package-states/${sf}.testing")) / 3600 / 24 ))"
            elif [ -f "${work_dir}/package-states/${sf}.done" ]; then
              printf ' (not unstaged yet for %s days)' "$(( ($(date '+%s') - $(stat -c '%Y' "${work_dir}/package-states/${sf}.done")) / 3600 / 24 ))"
            elif tr ' ' '.' < \
              "${work_dir}/build-list" | \
              grep -qxF "${sf}"; then
              printf ' (not built yet)'
            fi
            printf '\n'
          done
        printf '\n'
        continue
      fi

      printf 'Package "%s" can be %sd.\n' "${pkg}" "${action}"

    done

  ;;

  'keep'|'stubbornly_keep')

    find '/var/lib/pacman/sync' -name '*.db' -execdir bsdtar -tf '{}' \; | \
      sed -n '
        s|-[^-]\+-[^-]\+/$||
        T
        p
      ' | \
      sort -u > \
      "${tmp_dir}/upstream-packages"

    while read -r pkg; do

      if builds_file=$(
        find "${work_dir}/package-infos" -maxdepth 1 -printf '%f\n' | \
          grep -m1 '^'"$(str_to_regex "${pkg}")"'\(\.[^.]\+\)\{3\}\.builds$'
        ); then

        builds_file="${builds_file%.*}"
        prepo="${builds_file##*.}"
        builds_file="${builds_file%.*}"
        mod_rev="${builds_file##*.}"
        builds_file="${builds_file%.*}"
        rev="${builds_file##*.}"

      else

        found_PKGBUILD=false
        mod_rev=$(cat "${work_dir}/archlinux32.revision")
        for repo in ${repo_names}; do
          eval 'repo_path="${repo_paths__'"${repo}"'}"'
          rev=$(cat "${work_dir}/${repo}.revision")
          if [ "${repo}" = 'archlinux32' ]; then
            if git -C "${repo_path}" archive "${mod_rev}" | \
              grep -q '^[^/]\+/'"$(str_to_regex "${pkg}")"'/PKGBUILD$'; then
              prepo=$(
                git -C "${repo_path}" archive "${mod_rev}" | \
                  grep '^[^/]\+/'"$(str_to_regex "${pkg}")"'/PKGBUILD$' | \
                  cut -d/ -f1
              )
              found_PKGBUILD=true
              break
            fi
          else
            prepo=$(
              git -C "${repo_path}" archive "${rev}" -- "${pkg}/repos" 2>/dev/null | \
                tar -t 2> /dev/null | \
                grep '^[^/]\+/repos/[^/]\+/PKGBUILD$' | \
                grep -v -- '-i686/PKGBUILD$' | \
                grep -v -- '[-/]\(staging\|testing\|unstable\)-[^/]\+/PKGBUILD$' | \
                sed '
                  s|^[^/]\+/repos/\([^/]\+\)-[^/-]\+/PKGBUILD$|\1|
                ' | \
                head -n1
            )
            if [ -n "${prepo}" ]; then
              found_PKGBUILD=true
              break
            fi
          fi
        done
        if ! ${found_PKGBUILD}; then
          continue
        fi

        generate_package_metadata "${pkg}" "${rev}" "${mod_rev}" "${prepo}"

      fi

      sed "s|^|${pkg} builds |" "${work_dir}/package-infos/${pkg}.${rev}.${mod_rev}.${prepo}.builds"

    done < \
      "${work_dir}/deletion-list" > \
      "${tmp_dir}/deleted.builds"

    sort -k3,3 "${tmp_dir}/deleted.builds" | \
      sponge "${tmp_dir}/deleted.builds"

    for pkg in "$@"; do

      if ! grep -qxF "${pkg}" "${work_dir}/deletion-list"; then
        printf 'Package "%s" is not on the deletion list.\n' "${pkg}"
        continue
      fi

      if ! grep -qxF "${pkg}" "${tmp_dir}/upstream-packages"; then
        printf 'Package "%s" is not available upstream.\n' "${pkg}"
        if [ "${action}" = 'keep' ]; then
          continue
        fi
      fi

      if git -C "${repo_paths__archlinux32}" archive "$(cat "${work_dir}/archlinux32.revision")" -- blacklist | \
        tar -Ox 'blacklist' | \
        sed '
          s/\s*#.*$//
          /^\s*$/d
        ' | \
        grep -qxF "${pkg}"; then
        printf 'Package "%s" is explicitely blacklisted.\n' "${pkg}"
        continue
      fi

      if [ "lib32-${pkg#lib32-}" = "${pkg}" ]; then
        printf 'Package "%s" is a library from multilib.\n' "${pkg}"
        continue
      fi

      build_depends=$(
        find "${work_dir}/package-infos" -maxdepth 1 -name "${pkg}.*.build-depends" -exec cat {} \;
      )
      if [ -z "${build_depends}" ]; then
        printf 'Package "%s" was deleted in the git repositories.\n' "${pkg}"
        continue
      fi

      build_depends=$(
        echo "${build_depends}" | \
          sort -u
      )

      errors=$(
        {
          # shellcheck disable=SC2086
          printf '%s\n' ${build_depends}
          awk '{print $3}' "${tmp_dir}/deleted.builds" | \
            sort -u
        } | \
          sort | \
          uniq -d | \
          join -1 1 -2 3 -o 2.1,2.2,2.3 - "${tmp_dir}/deleted.builds"
      )
      if [ -n "${errors}" ]; then
        printf 'Package "%s" has dependencies on the deletion list:\n' "${pkg}"
        # shellcheck disable=SC2086,SC2183
        printf '%s %s %s\n' ${errors}
        printf '\n'
        continue
      fi

      printf 'It seems, package "%s" should not be deleted.\n' "${pkg}"

    done

  ;;

  *)
    >&2 printf 'unknown action "%s"\n' "${action}"
    exit 1

esac