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

# check for packages that need to be built, and build a list in the proper build order
# Details:
#  https://github.com/archlinux32/builder/wiki/Build-system#get-package-updates

# TODOs:

#  be more secure in case of update while build(s) is/are still in progress
#   -> (stale) lock files, moving (or changing content of) loop lock files

#  (might be connected to above): find out who deletes loop-files which
#   should not be deleted

#  remove extra whitelist cases

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

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.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

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

block_flag='-n'
pull=true

while true
do
  case "$1" in
    -b|--block)
      block_flag=''
    ;;
    -h|--help)
      usage 0
    ;;
    -n|--no-pull)
      pull=false
    ;;
    --)
      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 1
fi

# delete_package package
# mark $package for deletion
delete_package() {
  echo "$1" >> \
    "${work_dir}/deletion-list.new"
  sed -i "/^$(str_to_regex "${1}") /d" "${work_dir}/build-list.new"
}

# Update git repositories (official packages, community packages and the repository of package customizations).

for repo in ${repo_names}; do
  eval repo_path='"${repo_paths__'"${repo}"'}"'
  # TODO:
  #  this is somewhat redundant and slow -- improve it!
  git -C "${repo_path}" checkout -f master
  git -C "${repo_path}" clean -xdf
  git -C "${repo_path}" fetch
  if ${pull}; then
    git -C "${repo_path}" reset --hard origin/master
  fi
done

# Read previous git revision numbers from files.

something_new=false

for repo in ${repo_names}; do
  eval "old_repo_revisions__${repo}='$(
    cat "${work_dir}/${repo}.revision" 2> /dev/null || \
      echo NONE
  )'"
  eval repo_path='"${repo_paths__'"${repo}"'}"'
  eval "new_repo_revisions__${repo}='$(
    git -C "${repo_path}" rev-parse HEAD | \
      tee "${work_dir}/${repo}.revision.new"
  )'"
  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

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.

grep -vxF 'break_loops' \
  "${work_dir}/build-list" > \
  "${work_dir}/build-list.new" || \
  true
cp \
  "${work_dir}/deletion-list" \
  "${work_dir}/deletion-list.new"

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
    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
          eval "printf '%s %s\n'" \
            "$(printf '"${new_repo_revisions__%s}"' "$(find_git_repository_to_package_repository "${pkg_repo}")")" \
            "'${rest}'"
        done
    else
      sed "s|^|${new_repo_revision} |"
    fi | \
    grep '^\S\+ .\s[^/]\+/repos/[^/]\+/PKGBUILD$' | \
    # ignore i686
    grep -v -- '-i686/PKGBUILD$' | \
    # ignore staging and testing
    grep -v -- '[-/]\(staging\|testing\|unstable\)-[^/]\+/PKGBUILD$' | \
    sed '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 repository; do
    case "${mode}" in

      # new or modified PKGBUILD
      "A"|"M")
        sed -i "/^$(str_to_regex "${package}") /d" "${work_dir}/build-list.new"
        # shellcheck disable=SC2154
        echo "${package} ${git_revision} ${new_repo_revisions__archlinux32} ${repository}" >> \
          "${work_dir}/build-list.new"
        sed -i "/^$(str_to_regex "${package}")\$/d" "${work_dir}/deletion-list.new"
      ;;

      # deleted PKGBUILD
      "D")
        delete_package "${package}"
      ;;

      *)
        >&2 echo "unknown git diff mode '${mode}'"
        exit 1
      ;;

    esac
  done

sort -u "${work_dir}/deletion-list.new" | \
  sponge "${work_dir}/deletion-list.new"

echo 'Extract dependencies of packages.'

# First, we extract the dependencies of each package.

mkdir -p "${work_dir}/package-infos"

while read -r package git_revision mod_git_revision repository; do

  generate_package_metadata "${package}" "${git_revision}" "${mod_git_revision}" "${repository}"

done < "${work_dir}/build-list.new"

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 "$(cat "${work_dir}/archlinux32.revision")" -- 'blacklist' | \
      tar -Ox 'blacklist'
    find "${work_dir}/package-infos" -maxdepth 1 -name 'lib32-*' -printf '%f\n' | \
      sed 's|\(\.[^.]\+\)\{4\}$||' | \
      uniq
  ) | \
    sort -u
)

find "${work_dir}/package-infos/" -maxdepth 1 -name '*.builds' -printf '%f\n' | \
  grep '\(\.[0-9a-f]\{40\}\)\{2\}\.[^.]\+\.builds$' | \
  sed '
    s|^\(.*\)\.\([^.]\+\)\.\([^.]\+\)\.\([^.]\+\)\.[^.]\+$|\2 \3 \4 \1|
  ' | \
  sort -k4,4 | \
  uniq -f3 --group=append | \
  while read -r rev mod_rev repo pkg; do
    if [ -z "${rev}" ] && \
      [ -z "${mod_rev}" ] && \
      [ -z "${repo}" ] && \
      [ -z "${pkg}" ]; then
      # shellcheck disable=SC2031
      printf '%s %s.%s.%s.%s\n' \
        "${opkg}" \
        "${opkg}" \
        "$(
          # shellcheck disable=SC2086
          printf '%s\n' ${revs} | \
            sort -u | \
            find_newest_of_git_revisions
        )" \
        "$(
          # shellcheck disable=SC2086
          printf '%s\n' ${mod_revs} | \
            sort -u | \
            find_newest_of_git_revisions
        )" \
        "${orepo}" | \
        grep '\(\.[0-9a-f]\{40\}\)\{2\}\.[^.]\+$'
      revs=''
      mod_revs=''
      continue
    fi
    revs=$(
      printf '%s\n' \
        ${revs} \
        "${rev}"
    )
    mod_revs=$(
      printf '%s\n' \
        ${mod_revs} \
        "${mod_rev}"
    )
    opkg="${pkg}"
    orepo="${repo}"
  done | \
  sort -k1,1 > \
  "${work_dir}/newest-revisions"

sed "
  s|^\S\+ |${work_dir}/package-infos/|
  s|\$|.builds|
" "${work_dir}/newest-revisions" | \
  xargs -r cat | \
  sort | \
  uniq -c > \
  "${work_dir}/built-packages"

sed "
  s|^\S\+ |${work_dir}/package-infos/|
  s|\$|.build-depends|
" "${work_dir}/newest-revisions" | \
  xargs -r grep -HF '' | \
  sed '
    s|\(\.[^.]\+\)\{4\}:\([^:]\+\)$| \2|
    s|^.*/||
  ' | \
  sort -k2,2 > \
  "${work_dir}/newest-dependencies"

while [ -n "${black_listed_new}" ]; do
  black_listed=$(
    # shellcheck disable=SC2086
    printf '%s\n' ${black_listed} ${black_listed_new} | \
      sort -u
  )
  black_listed_new=$(
    (
      # shellcheck disable=SC2086
      printf '%s\n' ${black_listed} | \
        sort -k1,1 | \
        join -j1 -o 2.2 - "${work_dir}/newest-revisions" | \
        sed "
          s|^|${work_dir}/package-infos/|
          s|\$|.builds|
        " | \
        xargs -r cat | \
        sort | \
        uniq -c
      cat "${work_dir}/built-packages"
    ) | \
      sort | \
      uniq -d | \
      awk '{print $2}' | \
      sort -k2,2 | \
      join -1 1 -2 2 -o 2.1 - "${work_dir}/newest-dependencies" | \
      while read -r pkg; do
        if [ "${pkg}" = 'dev86' ] || \
          [ "${pkg}" = 'luxblend25' ] || \
          [ "${pkg}" = 'python-pysocks' ] || \
          [ "${pkg}" = 'wine' ] || \
          [ "${pkg}" = 'wine-staging' ] || \
          [ "${pkg}" = 'wine-staging-nine' ]; then
          >&2 printf 'blacklisting: %s would be black listed, but is ignored.\n' "${pkg}"
        else
          echo "${pkg}"
        fi
      done | \
      sort -u
  )
  black_listed_new=$(
    # shellcheck disable=SC2086
    printf '%s\n' ${black_listed} ${black_listed} ${black_listed_new} | \
      sort | \
      uniq -u
  )
done
echo "${black_listed}" | \
  while read -r package; do
    [ -n "${package}" ] && \
      delete_package "${package}"
  done

sort -u "${work_dir}/deletion-list.new" | \
  sponge "${work_dir}/deletion-list.new"

# Now we create the partial order.

while read -r package git_revision mod_git_revision repository; do
  # add "$pkgname -> $build-target" to build-order list
  sed "s|^|${package} |" "${work_dir}/package-infos/${package}.${git_revision}.${mod_git_revision}.${repository}.builds"
  # add "$dependency -> $pkgname" to build-order list
  sed "s|\$| ${package}|" "${work_dir}/package-infos/${package}.${git_revision}.${mod_git_revision}.${repository}.build-depends"
  # add "base/base-devel -> $pkgname" to build-order list
  printf "%s ${package}\n" 'base' 'base-devel'
done \
  < "${work_dir}/build-list.new" \
  > "${work_dir}/build-order"

if grep -vq '^\S\+ \S\+$' "${work_dir}/build-order"; then
  >&2 echo 'Created partial order file "build-order" is invalid.'
  exit 2
fi

echo 'Now actually sort it.'

(
  # this part will have the correct build order, but all the infos are missing
  tsort "${work_dir}/build-order" 2> "${work_dir}/tsort.error" | \
    nl -ba | \
    awk '{print $1 " not-git also-not-git whatever " $2}'
  # this part has all the infos, but possibly the wrong order
  awk '{print "0 " $2 " " $3 " " $4 " " $1}' "${work_dir}/build-list.new"
) | \
  sort -k5,5 -k1nr | \
  # now, we have the correct order and the infos, but in adjacent lines
  uniq -f4 -D | \
  sed '/^0 /d;N;s|\n| |' | \
  # now in one line, each
  sort -k1n,1 | \
  awk '{print $5 " " $7 " " $8 " " $9}' > \
  "${work_dir}/build-list.new.new"

rm --one-file-system -rf "${work_dir}/build-list.loops.new"
mkdir "${work_dir}/build-list.loops.new"

if [ -s "${work_dir}/tsort.error" ]; then
  >&2 echo 'WARNING: There is a dependency cycle!'
  >&2 cat "${work_dir}/tsort.error"
  >&2 echo
  >&2 echo 'I will continue anyway.'
  # save loops in separate files each, so breaking them is easier
  awk '
    /^tsort: \S+: input contains a loop:$/{
      n++;
      getline
    }
    {
      print $2 >"'"${work_dir}"'/build-list.loops.new/loop_" n
    }
    ' "${work_dir}/tsort.error"

  # remove lines from loop files which are no packages
  find "${work_dir}/build-list.loops.new" -maxdepth 1 -printf '%f\n' | \
    grep '^loop_[0-9]\+$' | \
    while read -r loop; do
      (
        sort -u "${work_dir}/build-list.loops.new/${loop}"
        cut -d' ' -f1 "${work_dir}/build-list.new.new" | \
          sort -u
      ) | \
        sort | \
        uniq -d > \
        "${work_dir}/build-list.loops.new/${loop}.new"
      mv \
        "${work_dir}/build-list.loops.new/${loop}.new" \
        "${work_dir}/build-list.loops.new/${loop}"
    done

  # add orders to break loops to build list
  insert_break_loops_orders "${work_dir}/build-list.new.new"

else
  rm "${work_dir}/tsort.error"
fi

# Move the .new-files to the actual files

rm -rf --one-file-system "${work_dir}/build-list.loops"
(
  printf '%s\n' "build-list.loops" "build-list.new" "build-list" "deletion-list"
  # shellcheck disable=SC2086
  printf '%s.revision\n' ${repo_names}
) | \
  while read -r file; do
    mv "${work_dir}/${file}.new" "${work_dir}/${file}"
  done

# Remove the lock file

rm -f "${build_list_lock_file}"