Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/bin/get-package-updates
blob: b067233bf89668a6a2b7d68593bb848c5acda95d (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
#!/bin/sh

# check for packages that need to be built

# shellcheck disable=SC2119,SC2120

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

# TODO: keep database clean in case of abort

# shellcheck disable=SC2016
usage() {
  >&2 echo ''
  >&2 echo 'get-package-updates: check for packages that need to be built,'
  >&2 echo ' and build a list in the proper build order'
  >&2 echo ''
  >&2 echo 'possible options:'
  >&2 echo '  -d|--date $datetime:'
  >&2 echo '                Pull latest commit before $datetime'
  >&2 echo '                (yyyy-mm-ddThh:mm:ss). Conflicts -n.'
  >&2 echo '  -h|--help:    Show this help and exit.'
  >&2 echo '  -i|--ignore-insanity:'
  >&2 echo '                Do not abort when insane.'
  >&2 echo '  -m|--max-upstream-packages $number:'
  >&2 echo '                Do not update more than $number upstream packages.'
  >&2 echo '                Do not update git head of state repository.'
  >&2 echo '                USE WITH CAUTION: This may remove packages, if they'
  >&2 echo '                were moved. Always do a final run without -m.'
  >&2 echo '  -n|--no-pull: Do not pull git repos, merely reorder build list.'
  >&2 echo '                Conflicts -d.'
  >&2 echo '  -r|--recent-modifications:'
  >&2 echo '                Use the latest commit for the modifications'
  >&2 echo '                git repository (e.g. ignore -d for it).'
  >&2 echo '                Requires -d.'
  >&2 echo '  -w|--wait:    If necessary, wait for lock blocking.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

eval set -- "$(
  getopt -o d:him:nrw \
    --long date: \
    --long help \
    --long ignore-insanity \
    --long max-upstream-packages: \
    --long no-pull \
    --long recent-modifications \
    --long wait \
    -n "$(basename "$0")" -- "$@" || \
  echo usage
)"

block_flag='-n'
date_time=''
ignore_insanity=false
max_upstream_packages=''
pull=true
recent_modifications=false

while true
do
  case "$1" in
    -d|--date)
      shift
      date_time="$1"
    ;;
    -h|--help)
      usage 0
    ;;
    -i|--ignore-insanity)
      ignore_insanity=true
    ;;
    -m|--max-upstream-packages)
      shift
      max_upstream_packages="$1"
    ;;
    -n|--no-pull)
      pull=false
    ;;
    -r|--recent-modifications)
      recent_modifications=true
    ;;
    -w|--wait)
      block_flag=''
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo 'Whoops, forgot to implement option "'"$1"'" internally.'
      exit 42
    ;;
  esac
  shift
done

if [ $# -ne 0 ]; then
  >&2 echo 'Too many arguments.'
  usage
fi

if [ -n "${date_time}" ] && ! ${pull}; then
  >&2 printf -- '-d and -n are mutually exclusive.\n'
  usage
fi

if ${recent_modifications} && [ -z "${date_time}" ]; then
  >&2 printf -- '-r requires -d.\n'
  usage
fi

if [ -s "${work_dir}/build-master-sanity" ]; then
  >&2 echo 'Build master is not sane.'
  if ! ${ignore_insanity}; then
    exit
  fi
fi

# delete_package arch package repository
# mark $arch/$package for deletion

# shellcheck disable=SC3043
delete_package() {
  local architecture="$1"
  local pkgbase="$2"
  local repository="$3"
  >&2 printf 'delete_package %s %s %s\n' "${architecture}" "${pkgbase}" "${repository}"
  # shellcheck disable=SC2016
  query_delete_packages=$(
    printf '`architectures` AS `d_a`'
    printf ' JOIN `architecture_compatibilities` AS `a_c`'
    printf ' ON `a_c`.`runs_on`=`d_a`.`id`'
    printf ' AND `d_a`.`name`=from_base64("%s")' \
      "$(printf '%s' "${architecture}" | base64 -w0)"
    printf ' JOIN `build_assignments`'
    printf ' ON (`a_c`.`built_for`=`build_assignments`.`architecture`'
    # "any" references all architectures, but this is not represented
    # in `architecture_compatibilities`: If a package is not buildable
    # for "any", this means literally, that it is not buildable for
    # the _least_ architecture (e.g. it is not generic)
    printf ' OR `d_a`.`name`="any")'
    mysql_join_build_assignments_package_sources
    printf ' AND `package_sources`.`pkgbase`=from_base64("%s")' \
      "$(printf '%s' "${pkgbase}" | base64 -w0)"
    mysql_join_build_assignments_binary_packages
    mysql_join_binary_packages_binary_packages_in_repositories
    mysql_join_package_sources_upstream_repositories
    printf ' AND `upstream_repositories`.`name` LIKE from_base64("%s")' \
      "$(printf '%s' "${repository}" | base64 -w0)"
  )
  # shellcheck disable=SC2016
  {
    # packages from the build-list/to-be-decided go straight to the deletion-list
    # this happens in two steps, because we need to create one item per
    # target architecture
    printf 'INSERT IGNORE INTO `binary_packages_in_repositories` ('
      printf '`package`,'
      printf '`repository`,'
      printf '`last_moved`,'
      printf '`is_to_be_deleted`'
    printf ') SELECT'
    printf ' `binary_packages`.`id`,'
    printf '`d_r`.`id`,'
    printf 'NOW(),'
    printf '1'
    printf ' FROM'
    printf ' %s' "${query_delete_packages}"
    mysql_join_binary_packages_in_repositories_repositories
    # shellcheck disable=SC2154
    printf ' AND `repositories`.`stability` in (%s,%s)' \
      "${repository_stability_ids__unbuilt}" \
      "${repository_stability_ids__virtual}"
    mysql_join_build_assignments_architectures '' 'ba_a'
    printf ' JOIN `repositories` AS `d_r`'
    printf ' ON ('
      # arch-specific build_assignments must match exactly
      printf '`d_r`.`architecture`=`ba_a`.`id`'
      # "any" build_assignments build for all architectures
      printf ' OR `ba_a`.`name`="any"'
    printf ')'
    # shellcheck disable=SC2154
    printf ' AND `d_r`.`stability`=%s;\n' \
      "${repository_stability_ids__forbidden}"

    printf 'COMMIT;\n'

    # no need to remove the binary_package, because it is now on the
    # deletion-lists
    printf 'DELETE `binary_packages_in_repositories`'
    printf ' FROM %s' "${query_delete_packages}"
    mysql_join_binary_packages_in_repositories_repositories
    # shellcheck disable=SC2154
    printf ' AND `repositories`.`stability` in (%s,%s);\n' \
      "${repository_stability_ids__unbuilt}" \
      "${repository_stability_ids__virtual}"

    printf 'COMMIT;\n'

    # other packages are marked as `is_to_be_deleted`
    printf 'UPDATE %s' "${query_delete_packages}"
    printf ' SET `binary_packages_in_repositories`.`is_to_be_deleted`=1;\n'
  } | \
    mysql_run_query
}

something_new=false

for repo in ${repo_names}; do
  eval repo_path='"${repo_paths__'"${repo}"'}"'
  if [ "${repo}" = 'archlinux32' ]; then
    branch='master'
  else
    branch='main'
  fi
  # Update git repositories (upstream state and our package customizations).
  if [ -d "${repo_path}/.git" ]; then
    git -C "${repo_path}" pull --ff-only
  else
    git -C "${repo_path}" fetch origin "${branch}:${branch}"
  fi || \
    true
  # read previous git revision numbers from database.
  # shellcheck disable=SC2016
  eval "old_repo_revisions__${repo}='$(
    {
      printf 'SELECT `git_repositories`.`head`'
      printf ' FROM `git_repositories`'
      printf ' WHERE `git_repositories`.`name`=from_base64("%s");\n' \
        "$(printf '%s' "${repo}" | base64 -w0)"
    } | \
      mysql_run_query
  )'"
  # determine new git revision
  if ${pull}; then
    if ${recent_modifications} && \
      [ "${repo}" = 'archlinux32' ] || \
      [ -z "${date_time}" ]; then
      eval "new_repo_revisions__${repo}='$(
        git -C "${repo_path}" rev-parse HEAD
      )'"
    else
      new_rev=$(
        git -C "${repo_path}" rev-list -n1 --until "${date_time}" HEAD
      )
      eval 'old_rev="${old_repo_revisions__'"${repo}"'}"'
      # do not go backwards in time
      # shellcheck disable=SC2154
      if ! git -C "${repo_path}" merge-base --is-ancestor "${old_rev}" "${new_rev}"; then
        new_rev="${old_rev}"
      fi
      eval "new_repo_revisions__${repo}='${new_rev}'"
    fi
  else
    eval 'new_repo_revisions__'"${repo}"'="${old_repo_revisions__'"${repo}"'}"'
  fi
  if ! eval '[ "${new_repo_revisions__'"${repo}"'}" = "${old_repo_revisions__'"${repo}"'}" ]'; then
    something_new=true
  fi
done

if ${pull} && \
  ! ${something_new}; then
  >&2 echo 'Nothing changed.'
  exit
fi

exec 9> "${sanity_check_lock_file}"
# shellcheck disable=SC2086
if ! verbose_flock -s ${block_flag} 9; then
  >&2 echo 'come back (shortly) later - sanity-check running.'
  exit
fi

cleanup() {
  mysql_cleanup
  rm -rf --one-file-system "${tmp_dir:?}"
}
tmp_dir=$(mktemp -d 'tmp.get-package-updates.XXXXXXXXXX' --tmpdir)

trap cleanup EXIT

# shellcheck disable=SC2119
mysql_cleanup

echo 'Check modified packages from the last update, and put them to the build list.'

# Check modified packages from the last update, and put them to the build list.
# If a package is updated, but already on the rebuild list, then just update the git revision number.
# If a package is deleted, remove from the rebuild list, and add it to the deletion list.
# If a new package is added, then ensure that it's not on the deletion list.

{
  directories=$(
    # shellcheck disable=SC2154
    git -C "${repo_paths__state}" archive "${new_repo_revisions__state}" \
    | tar -t \
    | cut -d/ -f1 \
    | grep -vF -- '-testing-' \
    | grep -vF -- '-staging-' \
    | sort -u
  )
  # shellcheck disable=SC2016
  {
    printf 'SELECT DISTINCT `package_sources`.`pkgbase`,'
    printf '`package_sources`.`git_revision`,'
    printf '`upstream_repositories`.`name`'
    printf ' FROM `package_sources`'
    mysql_join_package_sources_upstream_repositories
    mysql_join_package_sources_build_assignments
    mysql_join_build_assignments_binary_packages
    mysql_join_binary_packages_binary_packages_in_repositories
    printf ' AND NOT `binary_packages_in_repositories`.`is_to_be_deleted`'
  } \
  | mysql_run_query \
  | tr '\t' ' ' \
  | sort -u \
  > "${tmp_dir}/mysql-packages"
  for directory in ${directories}; do
    # shellcheck disable=SC2154
    git -C "${repo_paths__state}" archive "${new_repo_revisions__state}" -- "${directory}" \
    | tar -Ox \
    | grep -v '^\S\+\( \S\+\)\1\1$' \
    | grep ' [0-9a-f]\{40\}$' \
    | cut -d' ' -f1,4 \
    | sed '
      s@$@ '"${directory%-*}"'@
    '
  done \
  | grep -v '^lib32-' \
  | sort -u \
  > "${tmp_dir}/upstream-packages"
  diff "${tmp_dir}/mysql-packages" "${tmp_dir}/upstream-packages" \
  | grep '^>' \
  | awk '{print $3 " " $2 " " $4}' \
  | sort -k2,2 -k3,3 \
  | uniq -uf1 \
  | while read -r git_revision pkgbase repository; do
    # shellcheck disable=SC2154
    if git -C "${repo_paths__archlinux32}" archive "${new_repo_revisions__archlinux32}" -- "${repository}/${pkgbase}" >/dev/null 2>&1; then
      mod_git_revision="${new_repo_revisions__archlinux32}"
    else
      mod_git_revision='0000000000000000000000000000000000000000'
    fi
    printf '%s %s %s %s\n' "${pkgbase}" "${repository}" "${git_revision}" "${mod_git_revision}"
  done \
  | if [ -n "${max_upstream_packages}" ]; then
    head -n"${max_upstream_packages}"
  else
    cat
  fi
  # shellcheck disable=SC2154
  git -C "${repo_paths__archlinux32}" diff "${old_repo_revisions__archlinux32}" "${new_repo_revisions__archlinux32}" --name-status \
  | tr '\t/' ' ' \
  | grep '^[AM]' \
  | cut -d' ' -f2,3 \
  | grep -v '^blacklist ' \
  | while read -r repository pkgbase; do
    git_revision=$(
      # shellcheck disable=SC2046
      git -C "${repo_paths__state}" archive "${new_repo_revisions__state}" -- $(
        printf '%s\n' "${directories}" \
        | grep '^'"${repository}"'-'
      ) \
      | tar -Ox \
      | sort -k1,1 \
      > "${tmp_dir}/git-revisions"
      echo "${pkgbase}" \
      | join -1 1 -2 1 -o 1.4 "${tmp_dir}/git-revisions" -
    )
    if [ -z "${git_revision}" ]; then
      git_revision='0000000000000000000000000000000000000000'
    fi
    if [ ${#git_revision} != 40 ] \
    || printf '%s\n' "${git_revision}" \
    | grep -vq '[0-9]'; then
      >&2 printf 'invalid git revision "%s"\n' "${git_revision}"
      exit 1
    fi
    printf '%s %s %s %s\n' "${pkgbase}" "${repository}" "${git_revision}" "${new_repo_revisions__archlinux32}"
  done
} \
| sort -u \
| sort -k1,1 -k2,2 \
> "${tmp_dir}/modified-packages"

errors=$(
  awk '{print $3 " " $4 " " $1 " " $2}' \
  < "${tmp_dir}/modified-packages" \
  | uniq -Df2
)
if [ -n "${errors}" ]; then
  >&2 printf 'Some packages are scheduled with different versions - this should not happen:\n'
  >&2 printf '%s\n' "${errors}"
  exit 1
fi

echo 'Mark to be deleted packages for removal'

cut -d' ' -f1,3 "${tmp_dir}/mysql-packages" \
| sort -u \
| sponge "${tmp_dir}/mysql-packages"

{
  cut -d' ' -f1,3 "${tmp_dir}/upstream-packages"
  git -C "${repo_paths__archlinux32}" archive "${new_repo_revisions__archlinux32}" \
  | tar -t \
  | grep -v '^blacklist/' \
  | tr '/' ' ' \
  | awk '{print $2 " " $1}'
} \
| sort -u \
| sponge "${tmp_dir}/upstream-packages"

diff "${tmp_dir}/mysql-packages" "${tmp_dir}/upstream-packages" \
| grep '^<' \
| cut -d' ' -f2,3 \
| while read -r pkgbase repository; do
  delete_package 'any' "${pkgbase}" "${repository}"
done

echo 'Insert updated packages'

while read -r pkgbase repository git_revision mod_git_revision; do
  if test "$repository" = "kde-unstable"; then
    continue
  fi
  if test "$repository" = "gnome-unstable"; then
    continue
  fi
  # shellcheck disable=SC2016
  {
    # delete old binary packages which are not yet built or on the
    # deletion list
    mysql_query_delete_packages \
      '`package_sources`.`pkgbase`=from_base64("'"$(
        printf '%s' "${pkgbase}" \
        | base64 -w0
      )"'")' \
      '`repositories`.`stability` IN ('"${repository_stability_ids__unbuilt}"','"${repository_stability_ids__forbidden}"')'
    # remove is-to-be-deleted marker from old binary packages
    printf 'UPDATE `binary_packages_in_repositories`'
    mysql_join_binary_packages_in_repositories_binary_packages
    mysql_join_binary_packages_build_assignments
    mysql_join_build_assignments_package_sources
    mysql_join_package_sources_upstream_repositories
    printf ' SET `binary_packages_in_repositories`.`is_to_be_deleted`=0'
    printf ' WHERE `package_sources`.`pkgbase`=from_base64("%s")' \
      "$(
        printf '%s' "${pkgbase}" \
        | base64 -w0
      )"
    printf ' AND `upstream_repositories`.`name`=from_base64("%s");\n' \
      "$(
        printf '%s' "${repository}" \
        | base64 -w0
      )"
  } \
  | mysql_run_query
  # shellcheck disable=SC2154
  >&2 printf '%s ' "${pkgbase}" "${git_revision}" "${mod_git_revision}" "${repository}"
  mysql_generate_package_metadata "${repository_ids__any_to_be_decided}" "${pkgbase}" "${git_revision}" "${mod_git_revision}" "${repository}"
  >&2 printf '\n'
done \
< "${tmp_dir}/modified-packages"

echo 'remove blacklisted packages'

{
  git -C "${repo_paths__archlinux32}" archive "${new_repo_revisions__archlinux32}" -- 'blacklist' \
  | tar -t 'blacklist' \
  | sed '
    s@^blacklist/\([^/]\+\)/[^/]\+/\([^/]\+\)$@\1 \2@
    t
    d
  ' \
  | sort -k2,2 \
  | join -1 1 -2 2 -o 2.1,2.2 "${tmp_dir}/modified-packages" -
  git -C "${repo_paths__archlinux32}" diff --name-status "${old_repo_revisions__archlinux32}" "${new_repo_revisions__archlinux32}" -- 'blacklist' \
  | sed '
    s@^[AM]\sblacklist/\([^/]\+\)/[^/]\+/\([^/]\+\)$@\1 \2@
    t
    d
  '
} \
| sort -u \
| while read -r arch pkgbase; do
  delete_package "${arch}" "${pkgbase}" '%'
done

echo 'Done - mark decisions as final.'

# shellcheck disable=SC2016
{
  # save blacklist into database
  printf 'CREATE TEMPORARY TABLE `blacklist` (`arch` VARCHAR(16), `pkgbase` VARCHAR(64), `reason` TEXT);\n'
  git -C "${repo_paths__archlinux32}" archive "${new_repo_revisions__archlinux32}" -- 'blacklist' | \
    tar -x --to-command 'sed "s@^@$TAR_FILENAME @"' 'blacklist' | \
    sed '
      s@^blacklist/\([^/[:space:]]\+\)/\S\+/\([^/[:space:]]\+\) @\1 \2 @
      t
      d
    ' | \
    while read -r arch pkgbase reason; do
      printf '(from_base64("%s"),from_base64("%s"),from_base64("%s")),\n' \
        "$(printf '%s' "${arch}" | base64 -w0)" \
        "$(printf '%s' "${pkgbase}" | base64 -w0)" \
        "$(printf '%s' "${reason}" | base64 -w0)"
    done | \
    sed '
      1 i INSERT IGNORE INTO `blacklist` (`arch`,`pkgbase`,`reason`) VALUES
      $ s/,$/;/
    '
  printf 'UPDATE `build_assignments`'
  printf ' SET `build_assignments`.`is_black_listed`=NULL;\n'
  printf 'UPDATE `blacklist`'
  printf ' JOIN `architectures`'
  printf ' ON `architectures`.`name`=`blacklist`.`arch`'
  printf ' JOIN `package_sources`'
  printf ' ON `blacklist`.`pkgbase`=`package_sources`.`pkgbase`'
  mysql_join_package_sources_build_assignments
  printf ' JOIN `architecture_compatibilities`'
  printf ' ON `build_assignments`.`architecture`=`architecture_compatibilities`.`built_for`'
  printf ' AND ('
    printf '`architectures`.`id`=`architecture_compatibilities`.`runs_on`'
    # shellcheck disable=SC2154
    printf ' OR `architectures`.`id`=%s' \
      "${architecture_ids__any}"
  printf ')'
  printf ' SET `build_assignments`.`is_black_listed`=`blacklist`.`reason`;\n'
  printf 'DROP TEMPORARY TABLE `blacklist`;\n'
  printf 'COMMIT;\n'

  # update hashes of repositories in mysql database
  for repo in ${repo_names}; do
    if [ -n "${max_upstream_packages}" ] \
    && [ "${repo}" = 'state' ]; then
      continue
    fi
    printf 'UPDATE `git_repositories`'
    printf ' SET `git_repositories`.`head`=from_base64("%s")' \
      "$(eval 'printf '"'"'%s'"'"' "${new_repo_revisions__'"${repo}"'}"' | base64 -w0)"
    printf ' WHERE `git_repositories`.`name`=from_base64("%s");\n' \
      "$(printf '%s' "${repo}" | base64 -w0)"
  done
  # move binary_packages from "to-be-decided" to "build-list"
  printf 'UPDATE `binary_packages_in_repositories`'
  mysql_join_binary_packages_in_repositories_binary_packages
  printf ' SET `binary_packages_in_repositories`.`repository`=%s' \
    "${repository_ids__any_build_list}"
  printf ' WHERE `binary_packages_in_repositories`.`repository`=%s;\n' \
    "${repository_ids__any_to_be_decided}"
} | \
  mysql_run_query

echo 'Aftermath - sort versions.'

mysql_sort_versions

echo 'Aftermath - find assignment loops.'

# update loop list in database (beware, the packages are expected to be in "build-list",
# not "to-be-decided", so we need to run this after moving the packages from "to-be-decided" to the "build-list".
mysql_find_build_assignment_loops

echo 'Aftermath - remove duplicate binary_packages.'

# remove duplicate binary_packages from "build-list"
mysql_query_remove_old_binary_packages_from_build_list | \
  mysql_run_query 'unimportant'