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

# shellcheck disable=SC2039,SC2119,SC2120

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

# shellcheck disable=SC2016
usage() {
  >&2 echo ''
  >&2 echo 'modify-package-state [options] packages-file:'
  >&2 echo ' modify state 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 '  -w|--wait:        Wait for lock if necessary.'
  >&2 echo ''
  >&2 echo 'Exactly one of -b|-f|-t|-u is needed for actual operation.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

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

action=''
report=true
wait_for_lock='-n'

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'
    ;;
    -w|--wait)
      wait_for_lock=''
    ;;
    --)
      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

input_file="$1"
if ${report}; then
  if ! [ -w "${input_file}" ]; then
    >&2 printf \
      'Cannot open file "%s" for writing.\n' \
      "${input_file}"
    exit 2
  fi
  move_output() {
    cat "${output_file}" > "${input_file}"
    rm -f "${output_file}"
  }
  output_file=$(mktemp 'tmp.modify-package-state.XXXXXXXXXX' --tmpdir)
  trap 'move_output' EXIT
else
  output_file='/dev/null'
fi

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

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

exec 8> "${package_database_lock_file}"
if ! verbose_flock ${wait_for_lock} 8; then
  >&2 echo 'Cannot get package-database lock.'
  exit
fi

while read -r a b; do
  case "${action}" in
    'faulty'|'tested')
      # we expect a sha512sum and binary package identifier
      # (pkgname-epoch:pkgver-pkgrel.sub_pkgrel-arch[".pkg.tar.xz"])
      # and we will only operate on packages in "testing" repositories
      # shellcheck disable=SC2016
      combiner=$(
        printf '`binary_packages`'
        mysql_join_binary_packages_binary_packages_in_repositories
        mysql_join_binary_packages_in_repositories_repositories
        mysql_join_repositories_repository_stabilities
        mysql_join_binary_packages_architectures
      )
      # shellcheck disable=SC2016,SC2031
      selector=$(
        extract_pkgname_epoch_pkgver_pkgrel_sub_pkgrel_arch_from_package_name "${b}"
        printf 'WHERE `repository_stabilities`.`name`="testing"'
        printf ' AND `binary_packages`.`%s`=from_base64("%s")' \
          'pkgname'    "$(printf '%s' "${pkgname}" | base64 -w0)" \
          'epoch'      "$(printf '%s' "${epoch}" | base64 -w0)" \
          'pkgver'     "$(printf '%s' "${pkgver}" | base64 -w0)" \
          'pkgrel'     "$(printf '%s' "${pkgrel}" | base64 -w0)" \
          'sub_pkgrel' "$(printf '%s' "${sub_pkgrel}" | base64 -w0)" \
          'sha512sum'  "$(printf '%s' "${a}" | base64 -w0)"
        printf ' AND `architectures`.`name`=from_base64("%s")' \
          "$(printf '%s' "${arch}" | base64 -w0)"
      )
    ;;
    'block'|'unblock')
      # we expect a package source identifier (pkgbase.git_revision.mod_git_revision.repository.architecture)
      # and we will only operate on packages in "unbuilt" repositories
      # shellcheck disable=SC2016
      combiner=$(
        printf '`build_assignments`'
        printf ' JOIN `architecture_compatibilities`'
        printf ' ON `architecture_compatibilities`.`runs_on`=`build_assignments`.`architecture`'
        printf ' AND `architecture_compatibilities`.`fully_compatible`'
        printf ' JOIN `architectures`'
        printf ' ON `architecture_compatibilities`.`built_for`=`architectures`.`id`'
        mysql_join_build_assignments_package_sources
        mysql_join_package_sources_upstream_repositories
        mysql_join_build_assignments_binary_packages
        mysql_join_binary_packages_binary_packages_in_repositories
        mysql_join_binary_packages_in_repositories_repositories
        mysql_join_repositories_repository_stabilities
      )
      # shellcheck disable=SC2016
      selector=$(
        arch="${a##*.}"
        pkgbase="${a%.*}"
        repository="${pkgbase##*.}"
        pkgbase="${pkgbase%.*}"
        mod_git_revision="${pkgbase##*.}"
        pkgbase="${pkgbase%.*}"
        git_revision="${pkgbase##*.}"
        pkgbase="${pkgbase%.*}"
        printf 'WHERE `repository_stabilities`.`name`="unbuilt"'
        printf ' AND `upstream_repositories`.`name`=from_base64("%s")' \
          "$(printf '%s' "${repository}" | base64 -w0)"
        printf ' AND `architectures`.`name`=from_base64("%s")' \
          "$(printf '%s' "${arch}" | base64 -w0)"
        printf ' AND `package_sources`.`%s`=from_base64("%s")' \
          'pkgbase'          "$(printf '%s' "${pkgbase}" | base64 -w0)" \
          'git_revision'     "$(printf '%s' "${git_revision}" | base64 -w0)" \
          'mod_git_revision' "$(printf '%s' "${mod_git_revision}" | base64 -w0)"
      )
    ;;
    *)
      >&2 printf 'Whooops, action "%s" not implemented yet.\n' "${action}"
      exit 42
    ;;
  esac
  case "${action}" in
    'block')
      if [ -z "${b}" ]; then
        >&2 printf 'No reason is given for blocking package "%s".\n' "${a}"
        exit 2
      fi
      tester='1'
      # shellcheck disable=SC2016
      modifier=$(
        printf '`build_assignments`.`is_blocked`=from_base64("%s")' \
          "$(printf '%s' "${b}" | base64 -w0)"
      )
    ;;
    'unblock')
      # shellcheck disable=SC2016
      tester='NOT `build_assignments`.`is_blocked` IS NULL'
      # shellcheck disable=SC2016
      modifier='`build_assignments`.`is_blocked`=NULL'
    ;;
    'faulty')
      # shellcheck disable=SC2016
      tester='`binary_packages`.`has_issues`=0'
      # shellcheck disable=SC2016
      modifier='`binary_packages`.`is_tested`=0,`binary_packages`.`has_issues`=1'
    ;;
    'tested')
      # shellcheck disable=SC2016
      tester='`binary_packages`.`is_tested`=0'
      # shellcheck disable=SC2016
      modifier='`binary_packages`.`is_tested`=1'
    ;;
    *)
      >&2 printf 'Whooops, action "%s" not implemented yet.\n' "${action}"
      exit 42
    ;;
  esac
  if {
    printf 'SELECT 1'
    printf ' FROM %s %s AND %s LIMIT 1' "${combiner}" "${selector}" "${tester}"
  } | \
    mysql_run_query | \
    grep -qxF '1'; then
    # shellcheck disable=SC2016
    {
      printf 'UPDATE %s SET %s %s' "${combiner}" "${modifier}" "${selector}"
      printf ';\n'
    } | \
      mysql_run_query
    printf '%s %s\n' "${action}" "${a}"
  fi
done \
  < "${input_file}" \
  > "${output_file}"