summaryrefslogtreecommitdiff
path: root/bin/db-update
blob: f8205dcc59a4dbe68da7751a208de94b61a8bc55 (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
#!/bin/sh

# move binary packages from staging to testing (if possible [1]) and
# additionally all packages specified on the command line from testing
# to the respective stable repository

# 1] Condition for moving a package A from staging to testing is that:
#   a) nothing on the build-list depends on A and
#   b) no done package B which is not being moved depends on A

# TODO:

#  the meta data used to evaluate condition "b" might be for a different
#    version

#  correctly handle if multiple versions of a single package are marked
#    as "done"

. "${0%/*}/../conf/default.conf"

usage() {
  >&2 echo ''
  >&2 echo 'db-update [options] [packages]:'
  >&2 echo ' move possible packages from staging to testing.'
  >&2 echo ' move packages on the command line from testing to stable.'
  >&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.'
  [ -z "$1" ] && exit 1 || exit $1
}

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

block_flag='-n'

while true
do
  case "$1" in
    -b|--block)
      block_flag=''
    ;;
    -h|--help)
      usage 0
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo 'Whoops, forgot to implement option "'"$1"'" internally.'
      exit 42
    ;;
  esac
  shift
done

packages_to_stabilize=${@}

for package in ${packages_to_stabilize}; do
  # some sanity checks
  if [ ! -f "${work_dir}/package-states/${package}.testing" ]; then
    >&2 echo "Package '${package}' is not in testing!"
    exit 2
  fi
done

updated_package_database=false

# Create a lock file and a trap.

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

exec 8> "${package_database_lock_file}"
if ! flock ${block_flag} 8; then
  >&2 echo 'come back (shortly) later - I cannot lock package database.'
  exit 1
fi

tmp_dir="$(mktemp -d)"

clean_up_lock_file() {
  rm -f "${package_database_lock_file}" "${build_list_lock_file}"
  rm -rf --one-file-system "${tmp_dir}"
}

trap clean_up_lock_file EXIT

# sanity check

if [ -n "$(
  ls "${work_dir}/package-states" | \
    grep '\.done$' | \
    sed 's|\(\.[^.]\+\)\{4\}$||' | \
    sort | \
    uniq -d)" ]; then
  >&2 echo 'Removing duplicates not yet implemented!'
  exit 42
fi

# packages which can't be un-staged because they're still dependencies
# of any job on the build-list

while read -r pkg pkg_rev mod_rev repo; do
  cat "${work_dir}/package-infos/${pkg}.${pkg_rev}.${mod_rev}.depends"
done < \
  "${work_dir}/build-list" | \
  sort -u > \
  "${tmp_dir}/keep_packages"

# packages which are done

ls "${work_dir}/package-states" | \
  grep '\.done$' | \
  sed 's|^\(.*\)\(\(\.[^.]\+\)\{3\}\)\.done$|\1 \1\2|' | \
  sort > \
  "${tmp_dir}/done_packages"

# remove packages not yet done from keep-packages list

keep_packages="$(
  join -1 1 -2 1 -o 2.2 \
    "${tmp_dir}/keep_packages" \
    "${tmp_dir}/done_packages"
)"
printf '%s\n' "${keep_packages}" > \
  "${tmp_dir}/keep_packages"

# packages which can't be unstaged
mv \
  "${tmp_dir}/keep_packages" \
  "${tmp_dir}/new_keep_packages"
keep_packages=''

while [ -s "${tmp_dir}/new_keep_packages" ]; do

  while read -r package; do
    generate_package_metadata "${package}"
  done < "${tmp_dir}/new_keep_packages"

  keep_packages="$(
    (
      printf '%s\n' "${keep_packages}"
      cat "${tmp_dir}/new_keep_packages"
    ) | \
      sort -u | \
      grep -v '^$'
  )"

  new_keep_packages="$(
    while read -r package; do
      cat "${work_dir}/package-infos/${package%.*}.depends"
    done < \
      "${tmp_dir}/new_keep_packages" | \
      sort -u
  )"
  printf '%s\n' "${new_keep_packages}" > \
    "${tmp_dir}/new_keep_packages"

  new_keep_packages="$(
    join -1 1 -2 1 -o 2.2 \
      "${tmp_dir}/new_keep_packages" \
      "${tmp_dir}/done_packages"
  )"

  # "new" is only what has not been there before
  printf '%s\n' "${keep_packages}" "${keep_packages}" "${new_keep_packages}" | \
    sort | \
    uniq -u > \
    "${tmp_dir}/new_keep_packages"

done

done_packages="$(cat "${tmp_dir}/done_packages")"

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

# unlock build list

rm -f "${build_list_lock_file}"
flock -u 9

clean_up_lock_file() {
  rm -f "${package_database_lock_file}"
}

# calculate unstageable packages from keep_packages and done_packages

done_packages="$(
  echo "${done_packages}" | \
    cut -d' ' -f2
)"

done_packages="$(
  (
    printf '%s\n' "${done_packages}" "${keep_packages}"
  ) | \
    sort | \
    uniq -u
)"

unset keep_packages

for package in ${done_packages}; do
  parts=$(cat "${work_dir}/package-states/${package}.done")
  parts_names=$(
    printf '%s\n' ${parts} | \
      sed 's|\(-[^-]\+\)\{3\}\.pkg\.tar\.xz$||'
  )
  is_community="$(official_or_community "${package}")"
  cd "${master_mirror_directory}/i686/${is_community}staging"
  mkdir -p "../${is_community}testing"
  repo-remove "${is_community}staging.db.tar.gz" ${parts_names}
  for part in ${parts}; do
    mv "${part}" "${part}.sig" "../${is_community}testing/"
    updated_package_database=true
  done
  cd "${master_mirror_directory}/i686/${is_community}testing"
  repo-add "${is_community}testing.db.tar.gz" ${parts}
  for part in ${parts}; do
    remove_old_package_versions "${part}"
  done

  mv \
    "${work_dir}/package-states/${package}.done" \
    "${work_dir}/package-states/${package}.testing"
done

# move packages in packages_to_stabilize from *testing/ to the stable repos
for package in ${packages_to_stabilize}; do
  parts=$(cat "${work_dir}/package-states/${package}.testing")
  parts_names=$(
    printf '%s\n' ${parts} | \
      sed 's|\(-[^-]\+\)\{3\}\.pkg\.tar\.xz$||'
  )
  source="$(official_or_community ${package})testing"
  destination="${package##*.}"
  cd "${master_mirror_directory}/i686/${source}"
  repo-remove "${source}.db.tar.gz" ${parts_names}
  for part in ${parts}; do
    mv "${part}" "${part}.sig" "../${destination}/"
    updated_package_database=true
  done
  cd "${master_mirror_directory}/i686/${destination}"
  repo-add "${destination}.db.tar.gz" ${parts}
  for part in ${parts}; do
    remove_old_package_versions "${part}"
  done

  rm "${work_dir}/package-states/${package}.testing"
done

if ${updated_package_database}; then
  date '+%s' > \
    "${master_mirror_directory}/lastupdate"
fi