summaryrefslogtreecommitdiff
path: root/bin/get-package-updates
blob: 5ff592f1b509312ccf82a256bdba2c31a09a59d6 (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
#!/bin/sh

# check for packages that need to be built
# Details:
#  https://github.com/archlinux32/builder/wiki/Build-system#get-package-updates

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

# TODO: Find out, why sometimes package updates are missed.

# TODO: correctly handle if pkgbase of a split package is renamed, e.g.:
# $a -> ($a,$b)  ==>  $b -> ($a,$b)

# 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 '  -b|--block:   If necessary, wait for lock blocking.'
  >&2 echo '  -h|--help:    Show this help and exit.'
  >&2 echo '  -n|--no-pull: Do not pull git repos, merely reorder build list.'
  >&2 echo '  -x|--test-exclusion $package:'
  >&2 echo '                Print additionally deleted/excluded packages if'
  >&2 echo '                "$package" would be black listed.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

eval set -- "$(
  getopt -o bhnx: \
    --long block \
    --long help \
    --long no-pull \
    --long test-exclusion: \
    -n "$(basename "$0")" -- "$@" || \
  echo usage
)"

block_flag='-n'
test_exclusion=''
pull=true

while true
do
  case "$1" in
    -b|--block)
      block_flag=''
    ;;
    -h|--help)
      usage 0
    ;;
    -n|--no-pull)
      pull=false
    ;;
    -x|--test-exclusion)
      shift
      if [ -n "${test_exclusion}" ]; then
        >&2 printf 'I already have --test-exclusion=%s and you gave me another one.\n' "${test_exclusion}"
        >&2 printf 'But I can only handle one exclusion test at a time.\n'
        exit 2
      fi
      test_exclusion="$1"
    ;;
    --)
      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 [ -s "${work_dir}/build-master-sanity" ]; then
  >&2 echo 'Build master is not sane.'
  exit
fi

# TODO: How should new deletion-list packages be handled?
# - packages deleted upstream should be marked as to-be-deleted if
#   existent and otherwise be ignored
# - packages deleted due to black listing should be marked as black
#   listed - and thus as to-be-deleted, but they should remain in the
#   database after deletion!

# delete_package package
# mark $package for deletion
delete_package() {
  # TODO: Once we want to rely on the database for test_exclusion, we
  # need to run the command below unconditionally, but with some
  # changes, so we can easily revert.
  if [ -z "${test_exclusion}" ]; then
    # shellcheck disable=SC2016
    {
      # packages from the build-list/to-be-decided go straight to the deletion-list
      printf 'UPDATE `binary_packages`'
      mysql_join_binary_packages_repositories
      mysql_join_binary_packages_build_assignments
      mysql_join_build_assignments_package_sources
      printf ' SET `binary_packages`.`repository`=(SELECT `repositories`.`id` FROM `repositories` WHERE `repositories`.`name`="deletion-list")'
      printf ' WHERE `repositories`.`name` in ("build-list","to-be-decided")'
      printf ' AND `package_sources`.`pkgbase`=from_base64("%s");\n' \
        "$(printf '%s' "$1" | base64 -w0)"
      # other packages are marked as `is_to_be_deleted`
      printf 'UPDATE `binary_packages`'
      mysql_join_binary_packages_repositories
      mysql_join_binary_packages_build_assignments
      mysql_join_build_assignments_package_sources
      printf ' SET `binary_packages`.`is_to_be_deleted`=1'
      printf ' WHERE `package_sources`.`pkgbase`=from_base64("%s");' \
        "$(printf '%s' "$1" | base64 -w0)"
    } | \
      mysql_run_query
  fi
}

something_new=false

for repo in ${repo_names}; do
  eval repo_path='"${repo_paths__'"${repo}"'}"'
  # Update git repositories (official packages, community packages and the repository of package customizations).
  git -C "${repo_path}" fetch origin master:master
  # read previous git revision numbers from database.
  # shellcheck disable=SC2016
  eval "old_repo_revisions__${repo}='$(
    {
      printf 'SELECT `git_repositories`.`head` 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
    eval "new_repo_revisions__${repo}='$(
      git -C "${repo_path}" rev-parse HEAD
    )'"
  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

# Create a lock file for build list.

exec 9> "${build_list_lock_file}"
if ! flock ${block_flag} 9; then
  >&2 echo 'come back (shortly) later - I cannot lock build list.'
  exit
fi

exec 8> "${sanity_check_lock_file}"
if ! flock -s ${block_flag} 8; then
  >&2 echo 'come back (shortly) later - sanity-check running.'
  exit
fi

trap mysql_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.

for repo in ${repo_names}; do
  eval repo_path='"${repo_paths__'"${repo}"'}"'
  eval old_repo_revision='"${old_repo_revisions__'"${repo}"'}"'
  eval new_repo_revision='"${new_repo_revisions__'"${repo}"'}"'
  {
    # if old revision unknown, mimic "git diff"-output
    # shellcheck disable=SC2154
    if [ "${old_repo_revision}" = "NONE" ]; then
      git -C "${repo_path}" archive --format=tar HEAD | \
        tar -t | \
        sed 's|^|A\t|'
    else
      git -C "${repo_path}" diff --no-renames --name-status "${old_repo_revision}" "${new_repo_revision}"
    fi
  } | \
    # only track changes in PKGBUILDs
    # TODO: However, packages which are already on the build list should
    # receive a git_revision bump if _any_ file changed.
    grep '/PKGBUILD$' | \
    if [ "${repo}" = "archlinux32" ]; then
    # modify the directory structure from the modifiaction-repository
    # to the one of an original source repository
      # shellcheck disable=SC2016
      sed 's|^\(.\t\)\([^/]\+\)/\([^/]\+\)/\(.\+\)$|\2 \1\3/repos/\2-x86_64/\4|' | \
        while read -r pkg_repo rest; do
          repo=$(find_git_repository_to_package_repository "${pkg_repo}")
          eval 'printf '"'"'%s %s %s\n'"'" \
            "$(printf '"${new_repo_revisions__%s}"' "${repo}")" \
            "$(printf '"${old_repo_revisions__%s}"' "${repo}")" \
            "'${rest}'"
        done
    else
      sed "s|^|${new_repo_revision} ${old_repo_revision} |"
    fi | \
    grep '^\S\+ \S\+ .\s[^/]\+/repos/[^/]\+/PKGBUILD$' | \
    # ignore i686
    grep -v -- '-i686/PKGBUILD$' | \
    # ignore staging and testing
    grep -v -- '[-/]\(staging\|testing\|unstable\)-[^/]\+/PKGBUILD$' | \
    sed 's|^\(\S\+ \S\+\) \(.\)\t\([^/]\+\)/repos/\([^/]\+\)-[^/-]\+/PKGBUILD$|\2 \3 \1 \4|'
done | \
  sort -u | \
  sed '
    s|^D\s|0 \0|
    t
    s|^[AM]\s|1 \0|
    t
    s|^|2 |
  ' | \
  sort -k1,1 | \
  sed 's|^[012] ||' | \
  while read -r mode package git_revision old_git_revision repository; do
    if [ "${mode}" = 'D' ]; then
      # deleted PKGBUILD
      # shellcheck disable=SC2154
      git_revision="${new_repo_revisions__archlinux32}"
      old_git_revision=''
      found_package=false
      for repository in ${repo_names}; do
        eval 'repo_path="${repo_paths__'"${repository}"'}"'
        if [ "${repository}" = "archlinux32" ]; then
          if git -C "${repo_path}" archive "${new_repo_revisions__archlinux32}" 2> /dev/null | \
            tar -t 2> /dev/null | \
            grep -q "/$(str_to_regex "${package}")/PKGBUILD$"; then
            found_package=true
            break;
          fi
        else
          # shellcheck disable=SC2154
          if eval 'git -C "${repo_path}" archive "${new_repo_revisions__'"${repository}"'}" -- "${package}/repos" 2> /dev/null | ' \
            'tar -t --wildcards "${package}/repos/*/PKGBUILD" 2> /dev/null | ' \
            'cut -d/ -f3 | ' \
            'grep -v '"'"'staging\|testing\|-unstable'"'"' | ' \
            'grep -vq -- '"'"'-i686$'"'"; then
            eval 'git_revision="${new_repo_revisions__'"${repository}"'}"'
            found_package=true
            break;
          fi
        fi
      done
      if ${found_package}; then
# TODO: moving packages between community64 and packages64 is currently broken (?)
        mode='M'
        repository=$(
          find_package_repository_to_package "${package}" "${repository}" "${git_revision}"
        )
      else
        delete_package "${package}"
        continue
      fi
    fi
    if [ "${mode}" = 'A' ] || [ "${mode}" = 'M' ]; then
      # shellcheck disable=SC2016
      {
        # delete old build assignment and associated binary packages
        # which are not yet built or on the deletion list
        printf 'DELETE `build_assignments`,`binary_packages`'
        printf ' FROM `binary_packages`'
        mysql_join_binary_packages_build_assignments
        mysql_join_build_assignments_package_sources
        mysql_join_binary_packages_repositories
        printf ' WHERE `package_sources`.`pkgbase`=from_base64("%s")' \
          "$(
            printf '%s' "${package}" | \
              base64 -w0
          )"
        printf ' AND (`repositories`.`name`="build-list" OR `repositories`.`name`="deletion-list");\n'
        # remove is-to-be-deleted marker from old binary packages
        printf 'UPDATE `binary_packages`'
        mysql_join_binary_packages_build_assignments
        mysql_join_build_assignments_package_sources
        mysql_join_binary_packages_repositories
        printf ' SET `is_to_be_deleted`=0'
        printf ' WHERE `package_sources`.`pkgbase`=from_base64("%s");\n' \
          "$(
            printf '%s' "${package}" | \
              base64 -w0
          )"
      } | \
        mysql_run_query
      # shellcheck disable=SC2154
      printf '%s ' "${package}" "${git_revision}" "${new_repo_revisions__archlinux32}" "${repository}" >&2
      mysql_generate_package_metadata 'to-be-decided' "${package}" "${git_revision}" "${old_git_revision}" "${new_repo_revisions__archlinux32}" "${repository}"
      printf '\n' >&2
      continue
    fi

    >&2 echo "unknown git diff mode '${mode}'"
    exit 1
  done

echo 'apply blacklisting'
# ignore blacklisted packages and dependent packages
# this is the first time when all the information is available and up to date

black_listed=''
black_listed_new=$(
  {
    {
      git -C "${repo_paths__archlinux32}" archive "${new_repo_revisions__archlinux32}" -- 'blacklist' | \
        tar -Ox 'blacklist' | \
        sed '
          s/\s*#.*$//
          /^\s*$/d
        '
      if [ -n "${test_exclusion}" ]; then
        echo "${test_exclusion}"
      fi
    } | \
      base64_encode_each
    # shellcheck disable=SC2016
    {
      printf 'SELECT DISTINCT replace(to_base64(`package_sources`.`pkgbase`),"\\n","")'
      printf ' FROM `package_sources`'
      printf ' WHERE `package_sources`.`pkgbase` LIKE "lib32-%%"'
    } | \
      mysql_run_query
  } | \
    sort -u
)

while [ -n "${black_listed_new}" ]; do
  black_listed=$(
    printf '%s\n' "${black_listed}" "${black_listed_new}" | \
      grep -vxF '' | \
      sort -u
  )
  black_listed_new=$(
    {
      printf '%s\n' "${black_listed}" "${black_listed}"

      # shellcheck disable=SC2016
      {
        printf 'CREATE TEMPORARY TABLE `bl` (`pkgbase` VARCHAR(64));\n'
        printf 'INSERT INTO `bl` (`pkgbase`) VALUES '
        printf '%s\n' "${black_listed}" | \
          sort -u | \
          sed '
            s/^/(from_base64("/
            s/$/")),/
            $ s/,$/;/
          '
        printf 'SELECT replace(to_base64(`a_ps`.`pkgbase`),"\\n","")'
        printf ' FROM `package_sources` AS `a_ps`'
        mysql_join_package_sources_build_assignments 'a_ps' 'a_ba'
        mysql_join_build_assignments_binary_packages 'a_ba' 'a_bp'
        mysql_join_binary_packages_dependencies 'a_bp'
        printf ' WHERE NOT EXISTS ('
          printf ' SELECT *'
          printf ' FROM `install_target_providers`'
          printf ' WHERE NOT EXISTS ('
            printf ' SELECT *'
            printf ' FROM `bl`'
            printf ' JOIN `package_sources` AS `b_ps` ON `bl`.`pkgbase`=`b_ps`.`pkgbase`'
            mysql_join_package_sources_build_assignments 'b_ps' 'b_ba'
            mysql_join_build_assignments_binary_packages 'b_ba' 'b_bp'
            printf ' WHERE `install_target_providers`.`package`=`b_bp`.`id`'
          printf ')'
          printf ' AND `install_target_providers`.`install_target`=`dependencies`.`depending_on`'
        printf ') AND EXISTS ('
        # TODO: This should be corrected at the root: automatic install targets, which are bogus should
        #       not be added in the first place - but how do we detect that?
          printf ' SELECT *'
          printf ' FROM `install_target_providers`'
          printf ' WHERE `install_target_providers`.`install_target`=`dependencies`.`depending_on`'
        printf ');\n'
      } | \
        mysql_run_query | \
        sort -u
    } | \
      grep -vxF '' | \
      sort | \
      uniq -u
  )
done

black_listed=$(
  printf '%s\n' "${black_listed}" | \
  while read -r line; do
    printf '%s' \
      "${line}" | \
      base64 -d
    printf '\n'
  done
)

deletion_list_count=$(
  # shellcheck disable=SC2086
  printf '%s\n' ${black_listed} | \
    wc -l
)
if [ "${deletion_list_count}" -gt 1000 ]; then
  >&2 printf 'There are %s > 1000 packages on the deletion-list. This does not seem right.\n' \
    "${deletion_list_count}"
  if [ ! -s "${work_dir}/told-irc-about-too-many-deletion-list-packages" ]; then
    {
      printf '\001ACTION refuses to blacklist %s packages.\001\n' "${deletion_list_count}"
      for repo_name in ${repo_names}; do
        eval 'old_revision="${old_repo_revisions__'"${repo_name}"'}"'
        eval 'new_revision="${new_repo_revisions__'"${repo_name}"'}"'
        # shellcheck disable=SC2154
        if [ "${old_revision}" != "${new_revision}" ]; then
          printf '%s: %s -> %s\n' \
            "${repo_name}" \
            "${old_revision}" \
            "${new_revision}"
        fi
      done
    } | \
      tee "${work_dir}/told-irc-about-too-many-deletion-list-packages" | \
      irc_say
  fi
  exit 3
fi
rm -f "${work_dir}/told-irc-about-too-many-deletion-list-packages"

echo "${black_listed}" | \
  while read -r package; do
    if [ -z "${package}" ]; then
      continue
    fi
    delete_package "${package}"
  done


if [ -n "${test_exclusion}" ]; then
  # TODO: reimplement test_exclusion with information from the database
  >&2 echo 'sry, not yet done.'
  exit 0
fi

echo 'Done - mark decisions as final.'

# shellcheck disable=SC2016
{
  # save blacklist into database
  printf 'CREATE TEMPORARY TABLE `bl` (`pkgbase` VARCHAR(64), `reason` TEXT);\n'
  git -C "${repo_paths__archlinux32}" archive "${new_repo_revisions__archlinux32}" -- 'blacklist' | \
    tar -Ox 'blacklist' | \
    sed -n '
      s/^\(\S\+\)\s*#\s*/\1 /
      T
      p
    ' | \
    while read -r pkgbase reason; do
      printf '(from_base64("%s"),from_base64("%s")),\n' \
        "$(printf '%s' "${pkgbase}" | base64 -w0)" \
        "$(printf '%s' "${reason}" | base64 -w0)"
    done | \
    sed '
      1 i INSERT INTO `bl` (`pkgbase`,`reason`) VALUES
      $ s/,$/;/
    '
  printf 'UPDATE `build_assignments`'
  mysql_join_build_assignments_package_sources
  printf ' LEFT JOIN `bl` ON `package_sources`.`pkgbase`=`bl`.`pkgbase`'
  printf ' SET `build_assignments`.`is_black_listed`=`bl`.`reason`;\n'
  printf 'DROP TEMPORARY TABLE `bl`;\n'

  # update hashes of repositories in mysql database
  for repo in ${repo_names}; do
    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`'
  mysql_join_binary_packages_repositories '' 'from_repo'
  printf ' SET `repository`=('
    printf 'SELECT `to_repo`.`id`'
    printf ' FROM `repositories` AS `to_repo`'
    printf ' WHERE `to_repo`.`name`="build-list"'
  printf ')'
  printf ' WHERE `from_repo`.`name`="to-be-decided";\n'
} | \
  mysql_run_query

# 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

# Remove the lock file

rm -f "${build_list_lock_file}"