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

# contains functions used by more than one script

# TODO:

#  mangle $arch in PKBUILDs to contain i486, i586, i686

# find_pkgbuilds package repository git_repository git_revision mod_git_revision
# find the PKGBUILD and modification of $package from $repository
# sets $PKGBUILD and $PKGBUILD_mod

find_pkgbuilds() {

  local package="$1"
  local repository="$2"
  local git_repository="$3"
  local git_revision="$4"
  local mod_git_revision="$5"

  PKGBUILD="$(
    eval git -C "$(printf '"${repo_paths__%s}"' "${git_repository}")" 'archive "${git_revision}" -- "${package}/repos/"' 2> /dev/null | \
      tar -t 2> /dev/null | \
      grep "^$(str_to_regex "${package}/repos/${repository}")"'-.*/PKGBUILD$' | \
      grep -v -- '-i686/PKGBUILD$' | \
      grep -v -- '[-/]\(staging\|testing\)-[^/]\+/PKGBUILD$' | \
      sort | \
      tail -n1
  )"

  PKGBUILD_mod="$(
    git -C "${repo_paths__archlinux32}" archive "${mod_git_revision}" 2> /dev/null | \
      tar -t "${repository}/${package}/PKGBUILD" 2> /dev/null
  )" || true

  if [ -z "${PKGBUILD}" ] && \
    [ -z "${PKGBUILD_mod}" ]; then
    >&2 printf 'Neither PKGBUILD nor modification of PKGBUILD found for package "%s" from %s (%s), revisions %s and %s.\n' \
      "${package}" \
      "${repository}" \
      "${git_repository}" \
      "${git_revision}" \
      "${mod_git_revision}"
    return 1
  fi

}

# find_repository_with_commit commit
# find the repository which has $commit

find_repository_with_commit() {

  local repository

  for repository in ${repo_names}; do
    if [ "$(eval git -C "$(printf '"${repo_paths__%s}"' "${repository}")" cat-file -t '"$1"' 2> /dev/null)" = "commit" ]; then
      echo "${repository}"
      return 0
    fi
  done
  >&2 printf 'find_repository_with_commit: Cannot find repository with commit "%s"\n' "$1"
  exit 1

}

# find_git_repository_to_package_repository repository
# find the git repository which tracks the package repository $repository

find_git_repository_to_package_repository() {

  local repository

  for repository in ${repo_names}; do
    if [ "${repository}" = "archlinux32" ]; then
      continue
    fi
    if [ -n "$(
      (
        eval ls "$(printf '"${repo_paths__%s}"' "${repository}")/"*"/repos" | \
          grep -v ':$' | \
          sed 's|-[^-]\+$||' | \
          sort -u
        echo "$1"
      ) | \
        sort | \
        uniq -d
      )" ]; then
      echo "${repository}"
      return 0
    fi
  done
  >&2 echo "can't find git repository with package repository '$1'"
  exit 1

}

# package_locked_or_blocked package git_revision mod_git_revision repository
# return if package - of given repository and revisions - is [locked or blocked]

package_locked_or_blocked() {
  [ -f "${work_dir}/package-states/$1.$2.$3.$4.locked" ] || \
    [ -f "${work_dir}/package-states/$1.$2.$3.$4.blocked" ]
}

# generate_package_metadata $package $git_revision $mod_git_revision $repository
# or
# generate_package_metadata $package.$git_revision.$mod_git_revision.$repository
# generate the meta data files of a package (dependencies, built packages, ...)

generate_package_metadata() {

  local package="$1"
  local git_revision="$2"
  local mod_git_revision="$3"
  local repository="$4"
  local file_prefix
  local file
  local PKGBUILD

  if [ $# -eq 1 ]; then
    # second form
    repository="${package##*.}"
    package="${package%.*}"
    mod_git_revision="${package##*.}"
    package="${package%.*}"
    git_revision="${package##*.}"
    package="${package%.*}"
  fi

  file_prefix="${work_dir}/package-infos/${package}.${git_revision}.${mod_git_revision}"

  if [ -e "${file_prefix}.builds" ] && \
    [ -e "${file_prefix}.depends" ] && \
    [ -e "${file_prefix}.needs" ] && \
    [ -e "${file_prefix}.packages" ]; then
    return 0
  fi

  if ! make_source_info "${package}" "${repository}" "${git_revision}" "${mod_git_revision}" "${file_prefix}.SRCINFO"; then
    echo "make_source_info failed."
    exit 1
  fi
  if [ ! -f "${file_prefix}.SRCINFO" ]; then
    >&2 printf '"%s" not created by "make_source_info" - eh, what?' "${file_prefix}.SRCINFO"
    exit 1
  fi

  # otherwise this just calls for trouble
  sed -i '/=\s*$/d' "${file_prefix}.SRCINFO"

  # extract "builds" = provides \cup pkgname
  grep "$(printf '^\\(\tprovides\\|pkgname\\) = ')" "${file_prefix}.SRCINFO" | \
    cut -d= -f2 | \
    sed 's|^\s\+||; s|[<>]$||' | \
    sort -u > \
    "${file_prefix}.builds"

  # extract "packages" = pkgname
  grep '^pkgname = ' "${file_prefix}.SRCINFO" | \
    cut -d= -f2 | \
    sed 's|^\s\+||; s|[<>]$||' | \
    sort -u > \
    "${file_prefix}.packages"

  # extract "depends" = makedepends \cup checkdepends \cup depends
  (
    sed -n "$(printf '/^pkgname = /q;/^\tdepends = /p')" "${file_prefix}.SRCINFO"
    grep "$(printf '^\t\\(makedepends\\|checkdepends\\) = ')" "${file_prefix}.SRCINFO"
  ) | \
    cut -d= -f2 | \
    sed 's|^\s\+||; s|[<>]$||' | \
    sort -u > \
    "${file_prefix}.depends"

  # extract "needs" = "depends" \setminus "builds"
  (
    cat "${file_prefix}.depends"
    sed 'p' "${file_prefix}.builds"
  ) | \
    sort | \
    uniq -u > \
    "${file_prefix}.needs"

  rm "${file_prefix}.SRCINFO"

}

# delete_old_metadata
# delete old (=unneeded) meta data of packages

delete_old_metadata() {

  (
    ls -1 "${work_dir}/package-infos" | \
      sed '
        s|\.\([^.]\+\)\.\([^.]\+\)\.[^.]\+$| \1 \2|
      ' | \
      sort -u
    ls -1 "${work_dir}/package-states" | \
      sed '
        s|\.\([^.]\+\)\.\([^.]\+\)\(\.[^.]\+\)\{2\}$| \1 \2|
      ' | \
      sort -u | \
      sed 'p'
    cut -d' ' -f1,2,3 "${work_dir}/build-list" | \
      sed 'p'
  ) | \
    sort | \
    uniq -u | \
    while read -r pkg rev mod_rev; do
      rm -f "${work_dir}/package-infos/${pkg}.${rev}.${mod_rev}".*
    done
}

# repository_of_package $package.$repo_revision.$mod_repo_revision.$repository
# print which (stable) repository a package belongs to

repository_of_package() {
  local package="$1"
  local repository="${package##*.}"
  package="${package%.*}"
  local a32_rev="${package##*.}"
  package="${package%.*.*}"

  case "${repository}" in
    'multilib')
      if git -C "${repo_paths__archlinux32}" archive --format=tar "${a32_rev}" -- 'extra-from-multilib' | \
        tar -Ox | \
        grep -qFx "${package%.*.*.*}"; then
        echo 'extra'
      else
        echo 'community'
      fi
      ;;
    *)
      echo "${repository}"
  esac
}

# official_or_community $package.$repo_revision.$mod_repo_revision.$repository $ending
# print wether the specified package is an official package (print
# $ending) or a community package (print 'community-$ending') or a
# build-suppor package (print 'build-support')

official_or_community() {
  local prepo
  prepo="$(repository_of_package "$1")"

  if [ "${prepo}" = 'community' ]; then
    echo 'community-'"$2"
  elif [ "${prepo}" = 'build-support' ]; then
    echo 'build-support'
  else
    echo "$2"
  fi
}

# ls_master_mirror $path
# list content of $path on the master mirror (via rsync)

ls_master_mirror() {

  local path="$1"

  ${master_mirror_rsync_command} \
    "${master_mirror_rsync_directory}/${path}/" | \
    grep -v '\s\.$' | \
    awk '{print $5}'

}

# remove_old_package_versions $directory $package_file
# removes all other versions of $package_file in $directory on the master mirror

remove_old_package_versions() {

  local directory="$1"
  local package="$2"

  local pkgname="${package%-*-*-*.pkg.tar.xz}"

  ${master_mirror_rsync_command} \
    --recursive \
    --delete \
    $( \
      ls_master_mirror "${directory}" | \
        grep "^$(str_to_regex "${pkgname}")\(-[^-]\+\)\{3\}\.pkg\.tar\.xz\(\.sig\)\?\$" | \
        grep -v "^$(str_to_regex "${package}")\(\.sig\)\?\$" | \
        sed 's|^|--include=|'
    ) \
    '--exclude=*' \
    ./ \
    "${master_mirror_rsync_directory}/${directory}/"

}

# wait_some_time $minimum $diff
# wait between minimum and minimum+diff seconds (diff defaults to 30)

wait_some_time() {
  local minimum=$1
  local diff=$2
  local random

  if [ -z "${diff}" ]; then
    diff=30
  fi

  random="$(
    dd if='/dev/urandom' count=1 2> /dev/null | \
      cksum | \
      cut -d' ' -f1
  )"

  sleep $((${minimum} + ${random} % ${diff}))
}

# str_to_regex $string
# escape dots for use in regex

str_to_regex() {
  echo "$1" | \
    sed 's|\.|\\.|g'
}

# make_source_info $package $repository $git_revision $mod_git_revision $output
# create .SRCINFO from PKGBUILD within git repositories, output to $output

make_source_info() {

  local package="$1"
  local repository="$2"
  local git_revision="$3"
  local mod_git_revision="$4"
  local output="$5"

  local git_repo="$(find_repository_with_commit "${git_revision}")"
  local PKGBUILD
  local PKGBUILD_mod
  local content

  if [ -z "${git_repo}" ]; then
    return 1
  fi

  find_pkgbuilds "${package}" "${repository}" "${git_repo}" "${git_revision}" "${mod_git_revision}"

  if [ -n "${PKGBUILD}" ]; then
    content="$(
      eval git -C "$(printf '"${repo_paths__%s}"' "${git_repo}")" 'archive "${git_revision}" -- "${PKGBUILD}"' | \
        tar -Ox
    )"
  else
    unset content
  fi

  if [ -n "${PKGBUILD_mod}" ]; then
    content="$(
      printf '%s\n' \
        "${content}" \
        "$(
          git -C "${repo_paths__archlinux32}" archive "${mod_git_revision}" -- "${PKGBUILD_mod}" | \
            tar -Ox
        )"
    )"
  fi

  bail_out() {
    err=$?
    recursively_umount_and_rm "${overlays_dir}"
    exit ${err}
  }

  overlays_dir="$(mktemp -d "${work_dir}/tmp.XXXXXX")"

  sudo mount -t tmpfs none "${overlays_dir}" || \
    bail_out

  mkdir \
    "${overlays_dir}/lower" \
    "${overlays_dir}/upper" \
    "${overlays_dir}/work" \
    "${overlays_dir}/merged"

  echo "${content}" | \
    sed '/^\$Id\$$/d' > \
    "${overlays_dir}/lower/PKGBUILD"

  mkdir "${overlays_dir}/lower/etc"
  hexdump -n 16 -e '4/4 "%08x" 1 "\n"' /dev/urandom > \
    "${overlays_dir}/lower/etc/machine-id"

  sudo mount -t overlay overlay -o lowerdir="${overlays_dir}/lower":"/",upperdir="${overlays_dir}/upper",workdir="${overlays_dir}/work" "${overlays_dir}/merged" || \
    bail_out

  sudo systemd-nspawn -q \
    -D "${overlays_dir}/merged" \
    --register=no \
    ${base_dir}/bin/mksrcinfo || \
    bail_out

  sudo umount -l "${overlays_dir}/merged" || \
    bail_out

  mv \
    "${overlays_dir}/upper/.SRCINFO" \
    "${output}"

  sudo umount -l "${overlays_dir}" || \
    bail_out
  rmdir "${overlays_dir}"

}

# recursively_umount_and_rm $dir
# umount all mountpoints in $dir which are also in $dir's
#  filesystem, possibly also $dir itself and then
#  rm -rf --one-file-system $dir

recursively_umount_and_rm() {
  local dir="$1"

  if [ -z "${dir}" ]; then
    >&2 echo 'ERROR: recursively_umount_and_rm requires an argument'
    exit 42
  fi

  find "${dir}" \
    -xdev -depth -type d \
    -exec 'mountpoint' '-q' '{}' ';' \
    -exec 'sudo' 'umount' '-l' '{}' ';'
  rm -rf --one-file-system "${dir}"
}

# insert_break_loops_orders $file
# inserts the correct number of 'break_loops' order into $file

insert_break_loops_orders() {
  local file="$1"
  local looped_packages
  local line

  looped_packages="$(
    ls "${work_dir}/build-list.loops" | \
      grep '^loop_[0-9]\+$' | \
      sed "s|^|${work_dir}/build-list.loops/|" | \
      xargs -r cat | \
      sort -u | \
      wc -l
  )"

  (
    while read -r line; do
      if [ "${line}" = 'break_loops' ]; then
        if [ ${looped_packages} -eq 0 ]; then
          continue
        fi
        looped_packages=$((${looped_packages}-1))
      fi
      printf '%s\n' "${line}"
    done < \
      "${file}"
    while [ ${looped_packages} -gt 0 ]; do
      looped_packages=$((${looped_packages}-1))
      echo 'break_loops'
    done
  ) | \
    sponge "${file}"
}