summaryrefslogtreecommitdiff
path: root/bin/delete-packages
blob: 124e4035c6f9d9bd25ab1aaab596663b7938a04e (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
#!/bin/sh

# delete obsolete binary packages

# 1] Condition for deleting a package A is that:
#   a) nothing on the build-list (make|check|)depends on A and
#   b) no built package B which is not being deleted depends on A

# "Package x depends on package y" means, that something needed by x
# is provided by y and no other package which will not be deleted.

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

# shellcheck disable=SC2016
usage() {
  >&2 echo ''
  >&2 echo 'delete-packages [options]:'
  >&2 echo ' delete obsolete binary packages.'
  >&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-action:   Only print what would be deleted.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

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

block_flag='-n'
no_action=false

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

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

tmp_dir=$(mktemp -d "${work_dir}/tmp.XXXXXX")
trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT

# Create a lock file and a trap.

if ! ${no_action}; then

  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

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

fi

clean_up_lock_file() {
  if ! ${no_action}; then
    rm -f "${package_database_lock_file}" "${build_list_lock_file}"
  fi
  rm -rf --one-file-system "${tmp_dir}"
}

trap clean_up_lock_file EXIT

cp \
  "${work_dir}/deletion-list" \
  "${work_dir}/build-list" \
  "${tmp_dir}/"

all_repos="${standalone_package_repositories} ${stable_package_repositories} ${testing_package_repositories} ${staging_package_repositories}"
all_repos="core"

for repo in ${all_repos}; do
  mkdir "${tmp_dir}/${repo}"
  ${master_mirror_rsync_command} \
    "${master_mirror_rsync_directory}/i686/${repo}/${repo}.db."* \
    "${master_mirror_rsync_directory}/i686/${repo}/${repo}.files."* \
    "${tmp_dir}/${repo}/"
done

for repo in ${all_repos}; do
  tar -C "${tmp_dir}/${repo}" -xzf "${tmp_dir}/${repo}/${repo}.db.tar.gz"
done

find "${tmp_dir}" -mindepth 3 -maxdepth 3 -name 'desc' -exec \
  sed '
    /^%DEPENDS%$/{
      s/.*//
      :dep_loop
      N
      /\n$/{
        s@\(^\|\n\)\(.\)@\1depends \2@g
        bend
      }
      bdep_loop
    }
    /^%PROVIDES%$/{
      s/.*//
      :pro_loop
      N
      /\n$/{
        s@\(^\|\n\)\(.\)@\1provides \2@g
        bend
      }
      bpro_loop
    }
    d
    :end
    s/[<>=]\S*\($\|\n\)/\1/g
    s#\(^\|\n\)\(.\)#\1{} \2#g
  ' {} \; \
  -printf '%p provides %p\n' | \
  sed '
    /^$/d
    s|^\S\+/\([^/ ]\+\)/\([^/ ]\+\)\(-[^-/ ]\+\)\{2\}/desc |\1 \2 |
    s| \S\+/\([^/]\+\)\(-[^/-]\+\)\{2\}/desc$| \1|
  ' | \
  awk '{print $3 " " $1 " " $2 " " $4}' | \
  sed -n '
    /^provides /{
      s/^provides //
      w '"${tmp_dir}"'/db.provides
      b
    }
    /^depends /{
      s/^depends //
      w '"${tmp_dir}"'/db.depends
      b
    }
  '



echo 'OK' >&2
exit 42

# # sanity check
#
# for ending in 'done' 'testing'; do
#   if [ "${ending}" = 'testing' ] && \
#     [ -z "${packages_to_stabilize}" ]; then
#     # if nothing is to be untested, we don't care about duplicate
#     # testing packages (and maybe an unstaging fixes this anyway)
#     continue
#   fi
#   if [ -n "$(
#     find "${work_dir}/package-states" -name "*.${ending}" -printf '%f\n' | \
#       sed 's|\(\.[^.]\+\)\{4\}$||' | \
#       sort | \
#       uniq -d
#     )" ]; then
#     >&2 echo 'Removing duplicates not yet implemented:'
#     find "${work_dir}/package-states" -name "*.${ending}" -printf '%f\n' | \
#       sed 's|\(\.[^.]\+\)\{4\}$||' | \
#       sort | \
#       uniq -d
#     exit 42
#   fi
# done
#
# # packages which are done
#
# find "${work_dir}/package-states" -maxdepth 1 -type f -name '*.done' -printf '%f\n' | \
#   sed '
#     s|\.done$||
#   ' | \
#   sort -u > \
#   "${tmp_dir}/done-packages"
#
# # packages still on the build-list
#
# tr ' ' '.' < \
#   "${work_dir}/build-list" | \
#   sort -u > \
#   "${tmp_dir}/keep-packages"
#
# find "${work_dir}/package-infos" -name '*.groups' \
#   -exec grep -qx 'base\(-devel\)\?' {} \; \
#   -printf '%f\n' | \
#   sed '
#     s|\.groups$||
#   ' | \
#   sort -u > \
#   "${tmp_dir}/base-packages"
#
# # no base / base-devel packages on the build list?
# if [ -z "$(
#     join -j 1 \
#       "${tmp_dir}/base-packages" \
#       "${tmp_dir}/keep-packages"
#   )" ]; then
#   # unstage all base / base-devel packages from staging
#   cat "${tmp_dir}/base-packages" "${tmp_dir}/base-packages" "${tmp_dir}/keep-packages" | \
#     sort | \
#     uniq -u | \
#     sponge "${tmp_dir}/keep-packages"
# fi
#
# # find all dependencies of the unstageable packages
# mv \
#   "${tmp_dir}/keep-packages" \
#   "${tmp_dir}/new-keep-packages"
# touch "${tmp_dir}/keep-packages"
#
# while [ -s "${tmp_dir}/new-keep-packages" ]; do
#
#   cat "${tmp_dir}/new-keep-packages" "${tmp_dir}/keep-packages" | \
#     sort -u | \
#     sponge "${tmp_dir}/keep-packages"
#
#   sed '
#     s|^|'"${work_dir}"'/package-infos/|
#     s|$|.depends|
#   ' "${tmp_dir}/keep-packages" | \
#     xargs -r grep -HF '' | \
#     sed '
#       s|^.*/||
#       s|\.depends:| |
#     ' | \
#     sort -u | \
#     sort -k2,2 > \
#     "${tmp_dir}/keep-packages.depends"
#
#   sed '
#     s|^|'"${work_dir}"'/package-infos/|
#     s|$|.builds|
#   ' "${tmp_dir}/done-packages" | \
#     xargs -r grep -HF '' | \
#     sed '
#       s|^.*/||
#       s|\.builds:| |
#     ' | \
#     sort -u | \
#     sort -k2,2 > \
#     "${tmp_dir}/done-packages.builds"
#
#   join -j 2 -o 1.1 \
#     "${tmp_dir}/done-packages.builds" \
#     "${tmp_dir}/keep-packages.depends" | \
#     sort -u > \
#     "${tmp_dir}/new-keep-packages"
#
#   # "new" is only what has not been there before
#   cat "${tmp_dir}/keep-packages" "${tmp_dir}/keep-packages" "${tmp_dir}/new-keep-packages" | \
#     sort | \
#     uniq -u | \
#     sponge "${tmp_dir}/new-keep-packages"
#
# done
#
# # unlock build list
#
# rm -f "${build_list_lock_file}"
# flock -u 9
#
# clean_up_lock_file() {
#   rm -rf --one-file-system "${tmp_dir}"
#   rm -f "${package_database_lock_file}"
# }
#
# # calculate unstageable packages from keep_packages and done_packages
#
# done_packages=$(
#   cat \
#     "${tmp_dir}/keep-packages" \
#     "${tmp_dir}/keep-packages" \
#     "${tmp_dir}/done-packages" | \
#     sort | \
#     uniq -u
# )
#
# # move packages in packages_to_stabilize from *testing/ to the stable repos
#
# # shellcheck disable=SC2046
# move_packages $(
#
#   for package in ${packages_to_stabilize}; do
#
#     if [ -z "${package}" ]; then
#       continue
#     fi
#
#     printf '%s/%s/%s\n' \
#       "${package}" \
#       "$(official_or_community "${package}" 'testing')" \
#       "$(repository_of_package "${package}")"
#
#   done
#
# )
#
# # move packages from *staging to *testing
#
# # shellcheck disable=SC2046
# move_packages $(
#
#   for package in ${done_packages}; do
#
#     if [ -z "${package}" ]; then
#       continue
#     fi
#
#     printf '%s/%s/%s\n' \
#       "${package}" \
#       "$(official_or_community "${package}" 'staging')" \
#       "$(official_or_community "${package}" 'testing')"
#
#   done
#
# )
#
# clean_up_lock_file