summaryrefslogtreecommitdiff
path: root/bin/nit-picker
blob: 37b032197f31e89fece3c8a93d8f41f3e642ce11 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
#!/bin/sh

# shellcheck disable=SC2119

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

# this script shall host all the tests that are too slow to be run on
# the buildmaster (thus, it runs asynchronously and must therefor be
# resilient against transient states of the buildmaster):
# - check sanity of git
# - check expiration of package and master keys
# TODO:
# - check for differences of dependencies between mysql and git
# - check for differences of dependencies between mysql and packages
# - check for installability of packages
# - check if our packages are still available upstream
# - complain about packages residing too long in [staging]/[testing]

if [ $# -ge 1 ] && [ "x$1" = 'x-n' ]; then
  >&2 echo 'not joining irc'
  irc=false
  repair_dependencies=false
  shift
elif [ $# -ge 1 ] && [ "x$1" = 'x-r' ]; then
  >&2 echo 'repair all wrong dependencies (remove superfluid, add missing)'
  irc=false
  repair_dependencies=true
  shift
else
  irc=true
  repair_dependencies=false
  # shellcheck disable=SC2016
  if [ $# -ne 0 ]; then
    >&2 echo 'usage: nit-picker [-n|-r [$single_test]]'
    >&2 echo '  -n: do not join irc'
    >&2 echo '  -r: remove superfluid dependencies in the database'
    >&2 echo '  $single_test: only execute the given test'
    exit 1
  fi
fi

clean_up() {
  rm -rf --one-file-system "${tmp_dir}"
}

tmp_dir=$(mktemp -d 'tmp.nit-picker.XXXXXXXXXX' --tmpdir)
trap 'clean_up' EXIT

if ${irc}; then
  if pgrep -x ii; then
    >&2 echo 'ii is already running - this will not work'
    exit 1
  fi

  rm -rf --one-file-system "${irc_dir}"

  ii -s irc.freenode.net -n nit-picker -f nit-picker >/dev/null 2>&1 &
  ii_pid=$!

  clean_up() {
    rm -rf --one-file-system "${tmp_dir}"
    kill "${ii_pid}"
  }

  # wait for nickserv complaint
  while ! grep -qF 'This nickname is registered. Please choose a different nickname' "${irc_dir}/nickserv/out"; do
    sleep 1
  done

  # register
  printf 'identify %s\n' "${irc_password}" | \
    sponge "${irc_dir}/nickserv/in"

  # wait for registering to succeed
  while ! grep -qF 'You are now identified for' "${irc_dir}/nickserv/out"; do
    sleep 1
  done

  # join channel
  echo '/j #archlinux32' | \
    sponge "${irc_dir}/in"

  while [ ! -f "${irc_dir}/#archlinux32/out" ]; do
    sleep 1
  done

fi

# shellcheck disable=SC2120
local_irc_say() {
  if ${irc}; then
    if printf 'SHOW STATUS LIKE "Slave_running"' | mysql_run_query | cut -f2 | grep -qxF 'ON'; then
      irc_say "$@"
    else
      printf 'The replication slave is not running.\n' \
      | irc_say 'deep42thought'
      irc_say 'deep42thought'
    fi
  else
    sed 's/^/irc: /'
  fi
}

mysql_load_min_and_max_versions

last_once_a_day_check=0

while pgrep -x ii >/dev/null \
|| ! ${irc}; do
  if [ "$(date +%s)" -gt "$((last_once_a_day_check + 60*60*24))" ]; then
    do_once_a_day_checks=true
    last_once_a_day_check=$(date +%s)
  else
    do_once_a_day_checks=false
  fi
  if [ $# -eq 0 ]; then
    # shellcheck disable=SC2016
    {
      if ! "${repair_dependencies}"; then
        printf 'SELECT DISTINCT'
        printf ' "commit",'
        printf '`git_repositories`.`name`,'
        printf '`git_repositories`.`head`,'
        printf '`package_sources`.`git_revision`'
        printf ' FROM `package_sources`'
        mysql_join_package_sources_upstream_repositories
        mysql_join_upstream_repositories_git_repositories
        printf ';\n'

        printf 'SELECT DISTINCT'
        printf ' "commit",'
        printf '"archlinux32",'
        # shellcheck disable=SC2154
        printf '"%s",' \
          "${repo_heads__archlinux32}"
        printf '`package_sources`.`mod_git_revision`'
        printf ' FROM `package_sources`'
        printf ';\n'

        printf 'SELECT DISTINCT'
        printf ' "binary-signature",'
        mysql_package_name_query
        printf ' FROM `binary_packages`'
        mysql_join_binary_packages_architectures
        printf ' LEFT'
        mysql_join_binary_packages_compressions
        mysql_join_binary_packages_binary_packages_in_repositories
        mysql_join_binary_packages_in_repositories_repositories
        printf ' WHERE `repositories`.`is_on_master_mirror`'
        printf ';\n'

        if "${do_once_a_day_checks}"; then
          printf 'SELECT DISTINCT'
          printf ' "keyring",'
          mysql_package_name_query
          printf ' FROM `binary_packages`'
          mysql_join_binary_packages_architectures
          printf ' LEFT'
          mysql_join_binary_packages_compressions
          mysql_join_binary_packages_binary_packages_in_repositories
          mysql_join_binary_packages_in_repositories_repositories
          printf ' WHERE `repositories`.`is_on_master_mirror`'
          printf ' AND `binary_packages`.`pkgname` IN ('
            printf '"archlinux32-keyring",'
            printf '"archlinux32-keyring-transition"'
          printf ');\n'

          printf 'SELECT'
          printf ' "build-duration",'
          printf '`build_slaves`.`name`'
          printf ' FROM `build_slaves`'
          printf ';\n'

          printf 'SELECT DISTINCT'
          printf ' "package-blob",'
          printf 'CONCAT('
            printf '`binary_packages`.`build_assignment`,"-",'
            printf '`binary_packages`.`epoch`,":",'
            printf '`binary_packages`.`pkgver`,"-",'
            printf '`binary_packages`.`pkgrel`,".",'
            printf '`binary_packages`.`sub_pkgrel`'
          printf '),'
          printf '`repositories`.`architecture`'
          printf ' FROM `binary_packages`'
          mysql_join_binary_packages_binary_packages_in_repositories
          mysql_join_binary_packages_in_repositories_repositories
          printf ' AND `repositories`.`is_on_master_mirror`;\n'

          printf 'SELECT "repository-duration",CONCAT('
            printf '`architectures`.`name`,'
            printf '"/",'
            printf '`repositories`.`name`'
          printf '),'
          printf '`repositories`.`stability`'
          printf ' FROM `repositories`'
          mysql_join_repositories_architectures
          # shellcheck disable=SC2154
          printf ' WHERE `repositories`.`stability` IN (%s,%s);\n' \
            "${repository_stability_ids__testing}" \
            "${repository_stability_ids__staging}"

          printf 'SELECT DISTINCT'
          printf ' "upstream-availability",'
          printf '`binary_packages`.`pkgname`'
          printf ' FROM `binary_packages`'
          mysql_join_binary_packages_binary_packages_in_repositories
          printf ' AND NOT `binary_packages_in_repositories`.`is_to_be_deleted`'
          mysql_join_binary_packages_in_repositories_repositories
          printf ' AND ('
            printf '`repositories`.`is_on_master_mirror`'
            printf ' OR `repositories`.`id`=%s' \
              "${repository_ids__any_build_list}"
          printf ')'
          mysql_join_binary_packages_build_assignments
          mysql_join_build_assignments_package_sources
          printf ' AND `package_sources`.`uses_upstream`;\n'
        fi
      fi

      printf 'SELECT DISTINCT'
      printf ' "binary-dependencies",'
      mysql_package_name_query
      printf ' FROM `binary_packages`'
      mysql_join_binary_packages_architectures
      printf ' LEFT'
      mysql_join_binary_packages_compressions
      mysql_join_binary_packages_binary_packages_in_repositories
      mysql_join_binary_packages_in_repositories_repositories
      printf ' WHERE `repositories`.`is_on_master_mirror`'
      printf ';\n'
    } | \
      mysql_run_query | \
      tr '\t' ' ' | \
      shuf
  else
    printf '%s\n' "$*"
  fi | \
    while read -r action parameters; do
      if ${irc} && ! pgrep -x ii >/dev/null; then
        break
      fi
      case "${action}" in
        'commit') # check whether a given commit is present in the git repo
          git_repo="${parameters%% *}"
          git_rev="${parameters#${git_repo} }"
          git_head="${git_rev%% *}"
          git_rev="${git_rev#${git_head} }"
          # shellcheck disable=SC2016
          eval "$(
            printf 'git_dir="${repo_paths__%s}"\n' \
              "${git_repo}"
          )"
          # shellcheck disable=SC2154
          if ! git -C "${git_dir}" cat-file -t "${git_rev}" 2> /dev/null | \
            grep -qxF 'commit'; then
            git -C "${git_dir}" fetch --all -p >/dev/null 2>&1
            if ! git -C "${git_dir}" cat-file -t "${git_rev}" 2> /dev/null | \
              grep -qxF 'commit'; then
              printf 'commit %s is missing from repository %s\n' \
                "${git_rev}" \
                "${git_repo}" \
              | local_irc_say
            fi
          fi
          # shellcheck disable=SC2154
          if ! git -C "${git_dir}" cat-file -t "${git_head}" 2> /dev/null | \
            grep -qxF 'commit'; then
            git -C "${git_dir}" fetch --all -p >/dev/null 2>&1
            if ! git -C "${git_dir}" cat-file -t "${git_head}" 2> /dev/null | \
              grep -qxF 'commit'; then
              printf 'commit %s is missing from repository %s\n' \
                "${git_head}" \
                "${git_repo}" \
              | local_irc_say
            fi
          fi
          # shellcheck disable=SC2154
          if ! git -C "${git_dir}" merge-base --is-ancestor "${git_rev}" "${git_head}" 2> /dev/null; then
            current_git_head=$(
              # shellcheck disable=SC2016
              {
                printf 'SELECT DISTINCT'
                printf ' `git_repositories`.`head`'
                printf ' FROM `git_repositories`'
                printf ' WHERE `git_repositories`.`name`=from_base64("%s");\n' \
                  "$(
                    printf '%s' "${git_repo}" \
                    | base64 -w0
                  )"
              } \
              | mysql_run_query
            )
            if ! git -C "${git_dir}" merge-base --is-ancestor "${git_rev}" "${current_git_head}" 2> /dev/null; then
              git -C "${git_dir}" fetch --all -p >/dev/null 2>&1
              if ! git -C "${git_dir}" merge-base --is-ancestor "${git_rev}" "${current_git_head}" 2> /dev/null; then
                printf 'commit %s is not an ancestor of HEAD %s in repository %s\n' \
                  "${git_rev}" \
                  "${current_git_head}" \
                  "${git_repo}" \
                | local_irc_say
              fi
            fi
          fi
        ;;
        'binary-dependencies')
          if ! ${master_mirror_rsync_command} \
            "${master_mirror_rsync_directory}/pool/${parameters}" \
            "${tmp_dir}/"; then
            rm -f "${tmp_dir}/${parameters}"
            continue
          fi
          extract_dependencies_from_package \
            "${tmp_dir}/${parameters}" \
          > "${tmp_dir}/pkg-deps"
          # shellcheck disable=SC2016
          {
            printf 'SELECT'
            printf ' `dependency_types`.`name`,'
            printf '`install_targets`.`name`,'
            printf '`dependencies`.`version_relation`,'
            printf '`versions`.`epoch`,'
            printf '`versions`.`version`'
            printf ' FROM `binary_packages`'
            mysql_join_binary_packages_dependencies
            mysql_join_binary_packages_architectures
            printf ' LEFT'
            mysql_join_binary_packages_compressions
            mysql_join_dependencies_dependency_types
            mysql_join_dependencies_versions
            mysql_join_dependencies_install_targets
            printf ' WHERE '
            mysql_package_name_query
            printf '="%s"' \
              "${parameters}"
            printf ' AND `dependency_types`.`name` IN ("run","make","check")'
            printf ';\n'
          } \
          | mysql_run_query \
          | tr '\t' ' ' \
          | sort -u \
          > "${tmp_dir}/db-deps"
          if ! diff -q "${tmp_dir}/db-deps" "${tmp_dir}/pkg-deps"; then
            if "${repair_dependencies}"; then
              cat "${tmp_dir}/db-deps" "${tmp_dir}/pkg-deps" "${tmp_dir}/pkg-deps" \
              | sort \
              | uniq -u \
              | sed 's@^@'"${parameters}"' @' \
              | tr ' ' '\t' \
              >> "${tmp_dir}/remove-those-dependencies"
              cat "${tmp_dir}/db-deps" "${tmp_dir}/db-deps" "${tmp_dir}/pkg-deps" \
              | sort \
              | uniq -u \
              | sed 's@^@'"${parameters}"' @' \
              | tr ' ' '\t' \
              >> "${tmp_dir}/add-those-dependencies"
            else
              build_date=$(
                bsdtar -Oxf "${tmp_dir}/${parameters}" '.PKGINFO' \
                | sed '
                  s/^builddate = //
                  t
                  d
                '
              )
              build_date=$(
                date -I -d@"${build_date}"
              )
              printf 'dependencies of %s (built on %s) differ between the package and our database\n' \
                "${parameters}" \
                "${build_date}" \
              | local_irc_say
              if ! ${irc}; then
                diff -u --color "${tmp_dir}/db-deps" "${tmp_dir}/pkg-deps" || true
              fi
              if [ $# -eq 0 ]; then
                sleep 60
              fi
            fi
          fi
          rm \
            "${tmp_dir}/${parameters}" \
            "${tmp_dir}/db-deps" \
            "${tmp_dir}/pkg-deps"
        ;;
        'binary-signature')
          if ! ${master_mirror_rsync_command} \
            "${master_mirror_rsync_directory}/pool/${parameters}" \
            "${master_mirror_rsync_directory}/pool/${parameters}.sig" \
            "${tmp_dir}/"; then
            rm -f "${tmp_dir}/${parameters}" "${tmp_dir}/${parameters}.sig"
            continue
          fi
          unset error_message
          if ! gpg_output=$(
            gpg --batch --status-fd 1 -q --homedir /etc/pacman.d/gnupg \
              --verify "${tmp_dir}/${parameters}.sig" "${tmp_dir}/${parameters}" \
              2>/dev/null
          ); then
            sleep 1
            if ! gpg_output=$(
              gpg --batch --status-fd 1 -q --homedir /etc/pacman.d/gnupg \
                --verify "${tmp_dir}/${parameters}.sig" "${tmp_dir}/${parameters}" \
                2>/dev/null
            ); then
              error_message="package ${parameters} has an invalid signature."
            fi
          fi
          if [ -z "${error_message}" ]; then
            gpg_key=$(
              printf '%s\n' "${gpg_output}" \
              | sed '
                s/^\[GNUPG:] KEY_CONSIDERED \([0-9A-F]\{40\}\) 0$/\1/
                t
                d
              ' \
              | sort -u
            )
            if [ -z "${gpg_key}" ]; then
              error_message="cannot find pgp_key of package ${parameters}."
            fi
          fi
          if [ -z "${error_message}" ]; then
            for expiration in $(
              gpg --batch --homedir /etc/pacman.d/gnupg --with-colons --list-keys "0x${gpg_key}" \
              2>/dev/null \
              | grep '^\(sub\|pub\):' \
              | cut -d: -f7
            ); do
              expiration_days=$(((expiration - $(date +%s))/24/60/60))
              if [ ${expiration_days} -lt 100 ]; then
                error_message=$(
                  printf 'signing key %s (from %s) for package %s expires on %s (in %s < 100 days).\n' \
                  "${gpg_key}" \
                  "$(
                    gpg --batch --homedir /etc/pacman.d/gnupg --with-colons --list-keys "0x${gpg_key}" \
                    2>/dev/null \
                    | grep '^\(uid\):' \
                    | cut -d: -f10
                  )" \
                  "${parameters}" \
                  "$(date -I -d@"${expiration}")" \
                  "${expiration_days}"
                )
                break
              fi
            done
          fi
          if [ -n "${error_message}" ]; then
            printf '%s\n' "${error_message}" \
            | local_irc_say
            if [ $# -eq 0 ]; then
              sleep 60
            fi
          fi
          rm \
            "${tmp_dir}/${parameters}" \
            "${tmp_dir}/${parameters}.sig"
        ;;
        'build-duration')
          infos=$(
            # shellcheck disable=SC2016
            {
              printf 'SELECT'
              printf '`persons`.`name`,'
              printf '`build_slaves`.`name`,'
              printf '`architectures`.`name`,'
              printf '`package_sources`.`pkgbase`,'
              printf 'UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP('
                printf 'MAX('
                  printf 'IF('
                    printf '`ssh_log`.`action`="get-assignment",'
                    printf '`ssh_log`.`date`,'
                    printf 'NULL'
                  printf ')'
                printf ')'
              printf ')'
              printf ' FROM `build_slaves`'
              mysql_join_build_slaves_build_assignments
              mysql_join_build_assignments_architectures
              mysql_join_build_assignments_package_sources
              mysql_join_build_slaves_ssh_keys
              mysql_join_ssh_keys_persons
              mysql_join_build_slaves_ssh_log
              printf ' WHERE `ssh_log`.`date`>=ADDDATE(NOW(),"-7 00:00:00")'
              printf ' AND `build_slaves`.`name`=from_base64("%s")' \
                "$(
                  printf '%s' "${parameters}" \
                  | base64 -w0
                )"
              printf ' GROUP BY `build_slaves`.`id`'
              printf ';\n'
            } \
            | mysql_run_query \
            | tr '\t' ' '
          )
          if [ -z "${infos}" ]; then
            continue
          fi
          if [ "${infos##* }" -lt $((60*60*24)) ]; then
            continue
          fi
          printf '%s %s\n' \
            "${infos% *}" \
            "$(
              date -d"@${infos##* }" +'%-j %H:%M:%S'
            )" \
          | awk '{print $1 ": your slave " $2 " builds " $3 "/" $4 " for more than a day, now (" ($5-1) " day(s) " $6 ")"}' \
          | local_irc_say
          if [ $# -eq 0 ]; then
            sleep 60
          fi
        ;;
        'keyring')
          if ! ${master_mirror_rsync_command} \
            "${master_mirror_rsync_directory}/pool/${parameters}" \
            "${tmp_dir}/"; then
            rm -f "${tmp_dir}/${parameters}"
            continue
          fi
          mkdir "${tmp_dir}/pkg" "${tmp_dir}/gpg-home"
          bsdtar -C "${tmp_dir}/pkg" -xf "${tmp_dir}/${parameters}" --strip-components=4 'usr/share/pacman/keyrings'

          gpg --no-permission-warning --quiet --homedir "${tmp_dir}/gpg-home" --import \
          < "${tmp_dir}/pkg/archlinux32.gpg"
          cut -d: -f1 "${tmp_dir}/pkg/archlinux32-trusted" \
          | while read -r gpg_key; do
            gpg --no-permission-warning --homedir "${tmp_dir}/gpg-home" --with-colons --list-keys "0x${gpg_key}" \
            | grep '^pub:\|^sub:' \
            | cut -d: -f7 \
            | grep -vxF '' \
            | sort -u \
            | while read -r expiration; do
              expiration_days=$(((expiration - $(date +%s))/24/60/60))
              if [ ${expiration_days} -lt 100 ]; then
                printf 'key %s (from %s) in package %s expires on %s (in %s < 100 days).\n' \
                "${gpg_key}" \
                "$(
                  gpg --batch --homedir "${tmp_dir}/gpg-home" --with-colons --list-keys "0x${gpg_key}" \
                  2>/dev/null \
                  | grep '^\(uid\):' \
                  | cut -d: -f10
                )" \
                "${parameters}" \
                "$(date -I -d@"${expiration}")" \
                "${expiration_days}" \
                | local_irc_say
                if [ $# -eq 0 ]; then
                  sleep 60
                fi
              fi
            done
          done

          rm "${tmp_dir}/${parameters}"
          rm -rf --one-file-system "${tmp_dir}/gpg-home" "${tmp_dir}/pkg"
        ;;
        'package-blob')
          infos=$(
            # shellcheck disable=SC2016
            {
              printf 'SELECT DISTINCT'
              printf ' CONCAT('
                printf '`architectures`.`name`,'
                printf '"/",'
                printf '`package_sources`.`pkgbase`'
              printf '),'
              printf '`repositories`.`name`'
              printf ' FROM `binary_packages`'
              mysql_join_binary_packages_build_assignments
              printf ' AND CONCAT('
                printf '`binary_packages`.`build_assignment`,"-",'
                printf '`binary_packages`.`epoch`,":",'
                printf '`binary_packages`.`pkgver`,"-",'
                printf '`binary_packages`.`pkgrel`,".",'
                printf '`binary_packages`.`sub_pkgrel`'
              printf ')=from_base64("%s")' \
                "$(
                  printf '%s' "${parameters% *}" \
                  | base64 -w0
                )"
              mysql_join_build_assignments_package_sources
              mysql_join_binary_packages_binary_packages_in_repositories
              mysql_join_binary_packages_in_repositories_repositories
              printf ' AND `repositories`.`is_on_master_mirror`'
              printf ' AND `repositories`.`architecture`=%s' \
                "${parameters#* }"
              mysql_join_repositories_architectures
              printf ';\n'
            } \
            | mysql_run_query \
            | tr '\t' ' '
          )
          # 0 is ok, too - then the package was removed in the meantime
          if [ "$(printf '%s\n' "${infos}" | wc -l)" -gt 1 ]; then
            printf '%s\n' \
              "${infos}" \
            | sed '
              :a
                $! N
                s/\n\S\+ /, /
                ta
              s/^\S\+ /parts of \0are in different repositories: /
            ' \
            | local_irc_say
            if [ $# -eq 0 ]; then
              sleep 60
            fi
          fi
        ;;
        'upstream-availability')
          upstream_mirror=$(
            # shellcheck disable=SC2016
            sed '
              s/^\s*Server\s*=\s*\(\S\+\)\$arch\(\S*\)$/\1x86_64\2/
              t
              d
            ' '/etc/pacman.d/mirrorlist' \
            | head -n1
          )
          if [ -n "$(
            # shellcheck disable=SC2154
            for repo in ${upstream_repository_names}; do
              if [ "${repo}" = 'build-support' ]; then
                continue
              fi
              if curl -s "$(
                # shellcheck disable=SC2016
                printf '%s\n' "${upstream_mirror}" \
                | sed '
                  s/\(\$repo\)/'"${repo}"'/
                  s@/$@@
                  s@$@/'"${repo}"'.db.tar.gz@
                '
              )" 2>/dev/null \
              | tar -tzf - 2>/dev/null \
              | grep '^'"$(str_to_regex "${parameters}")"'-[^-]\+-[^-]\+/desc$'; then
                break
              fi
            done
          )" ]; then
            continue
          fi
          # shellcheck disable=SC2016
          if {
            printf 'SELECT'
            printf ' COUNT(1)'
            printf ' FROM `binary_packages`'
            mysql_join_binary_packages_binary_packages_in_repositories
            printf ' AND `binary_packages`.`pkgname`=from_base64("%s")' \
              "$(
                printf '%s\n' "${parameters}" \
                | base64 -w0
              )"
            printf ' AND NOT `binary_packages_in_repositories`.`is_to_be_deleted`'
            mysql_join_binary_packages_in_repositories_repositories
            printf ' AND ('
              printf '`repositories`.`is_on_master_mirror`'
              printf ' OR `repositories`.`id`=%s' \
                "${repository_ids__any_build_list}"
            printf ')'
            mysql_join_binary_packages_build_assignments
            mysql_join_build_assignments_package_sources
            printf ' AND `package_sources`.`uses_upstream`;\n'
          } \
          | mysql_run_query \
          | grep -qxF 0; then
            continue
          fi
          printf 'Package %s is still in our repositories or on the build-list, but is not available upstream.\n' \
            "${parameters}" \
          | local_irc_say
          if [ $# -eq 0 ]; then
            sleep 60
          fi
        ;;
        'repository-duration')
          case "${parameters#* }" in
            "${repository_stability_ids__testing}")
              duration=$((max_testing_duration+3))
            ;;
            "${repository_stability_ids__staging}")
              duration=3
            ;;
            *)
              >&2 printf 'unknown stability: %s\n' "${parameters#* }"
              continue
            ;;
          esac
          errors=$(
            ${master_mirror_rsync_command} "${master_mirror_rsync_directory}/${parameters% *}/" \
            | sed '
              s/^l\(\S\+\s\+\)\{2\}\(\(\S\+\s\+\)\{2\}\S\+\.pkg\.tar\S*\)\.sig -> \S\+$/\2/
              t
              d
            ' \
            | {
              while read -r date time file; do
                printf '%s %s\n' \
                  "$(
                    date +%s -d"${date} ${time}"
                  )" \
                  "${file}"
              done
              printf '%s ####CUT_HERE####\n' \
                "$((
                  $(date +%s) - duration*60*60*24
                ))"
            } \
            | sort -k1n,1 \
            | sed '/ ####CUT_HERE####$/q' \
            | sed '$d' \
            | while read -r time file; do
              printf '%s: since %s (%s days)\n' \
                "${file}" \
                "$(
                  date -d"@${time}" -I
                )" \
                "$((
                  ($(date +%s) - time)/24/60/60
                ))"
            done
          )
          if [ -z "${errors}" ]; then
            continue
          fi
          {
            printf 'The following packages are longer than %s days in %s:\n' \
              "${duration}" \
              "${parameters% *}"
            printf '%s\n' "${errors}" \
            | head -n5
            if [ "$(
              printf '%s\n' "${errors}" \
              | wc -l
            )" -gt 5 ]; then
              printf '... (%s total)\n' \
                "$(
                  printf '%s\n' "${errors}" \
                  | wc -l
                )"
            fi
          } \
          | local_irc_say
          if [ $# -eq 0 ]; then
            sleep 60
          fi
        ;;
        *)
          >&2 printf 'action "%s" is not yet implemented ...\n' "${action}"
        ;;
      esac
    done
  if [ $# -ge 1 ] || "${repair_dependencies}"; then
    break
  fi
  sleep 120
done

{
  if [ -s "${tmp_dir}/remove-those-dependencies" ]; then
    # shellcheck disable=SC2016
    {
      printf 'CREATE TEMPORARY TABLE `rd`('
        printf '`pf` VARCHAR(128),'
        printf '`dt` VARCHAR(32),'
        printf '`it` VARCHAR(128),'
        printf '`vr` VARCHAR(2),'
        printf '`e` MEDIUMINT,'
        printf '`v` VARCHAR(64)'
      printf ');\n'
      printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `rd`(`pf`,`dt`,`it`,`vr`,`e`,`v`);\n' \
        "${tmp_dir}/remove-those-dependencies"
      printf 'SELECT `d`.`id` FROM `binary_packages`'
      mysql_join_binary_packages_architectures
      printf ' LEFT'
      mysql_join_binary_packages_compressions
      printf ' JOIN `rd`'
      printf ' ON `rd`.`pf`='
      mysql_package_name_query
      mysql_join_binary_packages_dependencies '' 'd'
      printf ' AND `d`.`version_relation`=`rd`.`vr`'
      mysql_join_dependencies_install_targets 'd' 'it'
      printf ' AND `it`.`name`=`rd`.`it`'
      mysql_join_dependencies_versions 'd' 'v'
      printf ' AND `v`.`epoch`=`rd`.`e`'
      printf ' AND `v`.`version`=`rd`.`v`'
      mysql_join_dependencies_dependency_types 'd' 'dt'
      printf ' AND `dt`.`name`=`rd`.`dt`'
      printf ';\n'
    } \
    | mysql_run_query \
    | sed '
      1 i DELETE `dependencies` FROM `dependencies` WHERE `dependencies`.`id` IN (
      $! s/$/,/
      $ a );
    '
  fi
  # shellcheck disable=SC2016
  if [ -s "${tmp_dir}/add-those-dependencies" ]; then
    rsync "${tmp_dir}/add-those-dependencies" 'buildmaster:/tmp/add-those-dependencies' >/dev/null 2>&1

    printf 'CREATE TEMPORARY TABLE `ad`('
      printf '`pf` VARCHAR(128),'
      printf '`dt` VARCHAR(32),'
      printf '`it` VARCHAR(128),'
      printf '`vr` VARCHAR(2),'
      printf '`e` MEDIUMINT,'
      printf '`v` VARCHAR(64)'
    printf ');\n'

    printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `ad`(`pf`,`dt`,`it`,`vr`,`e`,`v`);\n' \
      '/tmp/add-those-dependencies'

    printf 'INSERT IGNORE INTO `install_targets`('
      printf '`name`'
    printf ')'
    printf ' SELECT'
    printf ' `ad`.`it`'
    printf ' FROM `ad`;\n'

    printf 'INSERT IGNORE INTO `versions`('
      printf '`epoch`,'
      printf '`version`'
    printf ')'
    printf ' SELECT'
    printf ' `ad`.`e`,'
    printf '`ad`.`v`'
    printf ' FROM `ad`;\n'

    printf 'INSERT IGNORE INTO `dependencies`('
      printf '`dependent`,'
      printf '`depending_on`,'
      printf '`dependency_type`,'
      printf '`version`,'
      printf '`version_relation`'
    printf ')'
    printf 'SELECT'
    printf ' `binary_packages`.`id`,'
    printf '`install_targets`.`id`,'
    printf '`dependency_types`.`id`,'
    printf '`versions`.`id`,'
    printf '`ad`.`vr`'
    printf ' FROM `binary_packages`'
    mysql_join_binary_packages_architectures
    printf ' LEFT'
    mysql_join_binary_packages_compressions
    printf ' JOIN `ad`'
    printf ' ON `ad`.`pf`='
    mysql_package_name_query
    printf ' JOIN `install_targets`'
    printf ' ON `install_targets`.`name`=`ad`.`it`'
    printf ' JOIN `versions`'
    printf ' ON `versions`.`epoch`=`ad`.`e`'
    printf ' AND `versions`.`version`=`ad`.`v`'
    printf ' JOIN `dependency_types`'
    printf ' ON `dependency_types`.`name`=`ad`.`dt`'
    printf ';\n'
  fi
} \
| ifne ssh buildmaster 'mysql buildmaster
  rm -f "/tmp/add-those-dependencies"
'