summaryrefslogtreecommitdiff
path: root/bin/modify-package-state
blob: b157d49875a45e33ef86375def2ad6c08d88d793 (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
#!/bin/sh

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

# shellcheck disable=SC2016
usage() {
  >&2 echo ''
  >&2 echo 'modify-package-state [options] packages-file:'
  >&2 echo ' modify state (file) of package(s).'
  >&2 echo ''
  >&2 echo 'possible options:'
  >&2 echo '  -b|--block:       Block package(s).'
  >&2 echo '  -f|--faulty:      Mark testing/tested package(s) as faulty.'
  >&2 echo '  -h|--help:        Show this help and exit.'
  >&2 echo '  -n|--no-report:   Do not report what packages were modified.'
  >&2 echo '  -t|--tested:      Mark package(s) as tested.'
  >&2 echo '  -u|--unblock:     Unblock package(s).'
  >&2 echo ''
  >&2 echo 'Exactly one of -b|-t|-u is needed for actual operation.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

# TODO: binary packages should not be identified by revisions, but by versions

eval set -- "$(
  getopt -o bfhntu \
    --long block \
    --long faulty \
    --long help \
    --long no-report \
    --long tested \
    --long unblock \
    -n "$(basename "$0")" -- "$@" || \
  echo usage
)"

action=''
report=true

while true
do
  case "$1" in
    -b|--block)
      if [ -n "${action}" ]; then
        >&2 echo 'Conflicting/redundand arguments.'
        usage
      fi
      action='block'
    ;;
    -f|--faulty)
      if [ -n "${action}" ]; then
        >&2 echo 'Conflicting/redundand arguments.'
        usage
      fi
      action='faulty'
    ;;
    -h|--help)
      usage 0
    ;;
    -n|--no-report)
      report=false
    ;;
    -t|--tested)
      if [ -n "${action}" ]; then
        >&2 echo 'Conflicting/redundand arguments.'
        usage
      fi
      action='tested'
    ;;
    -u|--unblock)
      if [ -n "${action}" ]; then
        >&2 echo 'Conflicting/redundand arguments.'
        usage
      fi
      action='unblock'
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo 'Whoops, forgot to implement option "'"$1"'" internally.'
      exit 42
    ;;
  esac
  shift
done

if [ -z "${action}" ]; then
  >&2 echo 'Expected -b|-f|-t|-u.'
  usage
fi

if [ $# -ne 1 ]; then
  >&2 echo 'Too few or too many arguments.'
  usage
fi

clean_up() {
  rm -rf --one-file-system "${tmp_dir}"
}
tmp_dir=$(mktemp -d 'tmp.modify-package-state.XXXXXXXXXX' --tmpdir)
trap clean_up EXIT

input_file="$1"
if ${report}; then
  if ! [ -w "${input_file}" ]; then
    >&2 printf \
      'Cannot open file "%s" for writing.' \
      "${input_file}"
    exit 2
  fi
  move_output() {
    cat "${output_file}" > "${input_file}"
    clean_up
  }
  output_file="${tmp_dir}/output-file"
  touch "${output_file}"
  trap 'move_output' EXIT
else
  output_file='/dev/null'
fi

if ! [ -r "${input_file}" ]; then
  >&2 printf \
    'Cannot open file "%s" for reading.' \
    "${input_file}"
  exit 2
fi

exec 9> "${sanity_check_lock_file}"
if ! flock -s -n 9; then
  >&2 echo 'Cannot get sanity-check lock.'
  exit
fi

if [ "${action}" = 'tested' ]; then
  receive_buglist 'Testing' > \
    "${tmp_dir}/package-bug-titles"
fi

{
  err=0
  while read -r package reason; do
    if echo "${package}" | \
      grep -q '\.pkg\.tar\.xz$'; then
      package=$(
        find "${work_dir}/package-states" -maxdepth 1 \( -name '*.tested' -o -name '*.testing' \) \
          -exec grep -qxF "${package}" '{}' \; \
          -printf '%f\n' | \
          sed 's|\.[^.]\+$||' | \
          sort -u
      )
      if [ -z "${package}" ]; then
        continue
      fi
    fi
    repo=$(
      printf '%s' "${package##*.}" | \
        base64 -w0
    )
    pkgbase="${package%.*}"
    mod_git_rev=$(
      printf '%s' "${pkgbase##*.}" | \
        base64 -w0
    )
    pkgbase="${pkgbase%.*}"
    git_rev=$(
      printf '%s' "${pkgbase##*.}" | \
        base64 -w0
    )
    pkgbase=$(
      printf '%s' "${pkgbase%.*}" | \
        base64 -w0
    )
    if [ "${action}" = 'block' ] || \
      [ "${action}" = 'unblock' ]; then
      # these packages need to be on the build list
      if ! tr ' ' '.' < \
        "${work_dir}/build-list" | \
        grep -qxF "${package}"; then
        >&2 printf 'Package "%s" is not on the build-list.\n' "${package}"
        err=2
        continue
      fi
    fi
    case "${action}" in
      'block')
        if [ -z "${reason}" ]; then
          >&2 printf 'No reason is given for blocking package "%s".\n' "${package}"
          err=2
        else
          echo "${reason}" > \
            "${work_dir}/package-states/${package}.blocked"
          # shellcheck disable=SC2016
          {
            printf 'UPDATE `build_assignments`'
            printf ' JOIN `package_sources` ON `build_assignments`.`package_source`=`package_sources`.`id`'
            printf ' JOIN `upstream_repositories` ON `package_sources`.`upstream_package_repository`=`upstream_repositories`.`id`'
            printf ' SET `build_assignments`.`is_blocked`=from_base64("%s")' \
              "$(printf '%s' "${reason}" | base64 -w0)"
            printf ' WHERE'
            printf ' `package_sources`.`%s`=from_base64("%s") AND' \
              'pkgbase' "${pkgbase}" \
              'git_revision' "${git_rev}" \
              'mod_git_revision' "${mod_git_rev}"
            printf ' `upstream_repositories`.`name`=from_base64("%s");\n' \
              "${repo}"
          } | tee /dev/stderr | \
            ${mysql_command}
          printf '%s %s\n' "${package}" "${reason}"
        fi
      ;;
      'faulty')
        if [ -f "${work_dir}/package-states/${package}.tested" ]; then
          mv \
            "${work_dir}/package-states/${package}.tested" \
            "${work_dir}/package-states/${package}.testing"
          # shellcheck disable=SC2016
          {
            printf 'UPDATE `binary_packages`'
            printf ' JOIN `%s` ON `%s`.`id`=`%s`.`%s`' \
              'build_assignments' 'build_assignments' 'binary_packages' 'build_assignment' \
              'package_sources' 'package_sources' 'build_assignments' 'package_source' \
              'upstream_repositories' 'upstream_repositories' 'package_sources' 'upstream_package_repository'
            printf ' SET `binary_packages`.`has_issues`=1, `binary_packages`.`is_tested`=0'
            printf ' WHERE'
            printf ' `package_sources`.`%s`=from_base64("%s") AND' \
              'pkgbase' "${pkgbase}" \
              'git_revision' "${git_rev}" \
              'mod_git_revision' "${mod_git_rev}"
            printf ' `upstream_repositories`.`name`=from_base64("%s");\n' \
              "${repo}"
          } | \
            ${mysql_command}
          printf '%s\n' "${package}"
        fi
      ;;
      'tested')
        if [ -f "${work_dir}/package-states/${package}.testing" ] && \
          ! grep -qF "[${package%.*.*.*}]" "${tmp_dir}/package-bug-titles"; then
          mv \
            "${work_dir}/package-states/${package}.testing" \
            "${work_dir}/package-states/${package}.tested"
          # shellcheck disable=SC2016
          {
            printf 'UPDATE `binary_packages`'
            printf ' JOIN `%s` ON `%s`.`id`=`%s`.`%s`' \
              'build_assignments' 'build_assignments' 'binary_packages' 'build_assignment' \
              'package_sources' 'package_sources' 'build_assignments' 'package_source' \
              'upstream_repositories' 'upstream_repositories' 'package_sources' 'upstream_package_repository'
            printf ' SET `binary_packages`.`is_tested`=1'
            printf ' WHERE'
            printf ' `package_sources`.`%s`=from_base64("%s") AND' \
              'pkgbase' "${pkgbase}" \
              'git_revision' "${git_rev}" \
              'mod_git_revision' "${mod_git_rev}"
            printf ' `upstream_repositories`.`name`=from_base64("%s");\n' \
              "${repo}"
          } | \
            ${mysql_command}
          printf '%s\n' "${package}"
        fi
      ;;
      'unblock')
        if [ -f "${work_dir}/package-states/${package}.blocked" ]; then
          rm "${work_dir}/package-states/${package}.blocked"
          # shellcheck disable=SC2016
          {
            printf 'UPDATE `build_assignments`'
            printf ' JOIN `package_sources` ON `build_assignments`.`package_source`=`package_sources`.`id`'
            printf ' JOIN `upstream_repositories` ON `package_sources`.`upstream_package_repository`=`upstream_repositories`.`id`'
            printf ' SET `build_assignments`.`is_blocked`=NULL'
            printf ' WHERE'
            printf ' `package_sources`.`%s`=from_base64("%s") AND' \
              'pkgbase' "${pkgbase}" \
              'git_revision' "${git_rev}" \
              'mod_git_revision' "${mod_git_rev}"
            printf ' `upstream_repositories`.`name`=from_base64("%s");\n' \
              "${repo}"
          } | \
            ${mysql_command}
          printf '%s\n' "${package}"
        fi
      ;;
      *)
        >&2 printf 'Whooops, action "%s" not implemented yet.\n' "${action}"
        exit 42
      ;;
    esac
  done > \
    "${output_file}"

  exit ${err}
} < \
  "${input_file}"