summaryrefslogtreecommitdiff
path: root/bin/why-dont-you
blob: 61eae34781ccdd9bcec42e938a3df73aed378267 (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
#!/bin/sh

# usage: why_dont_you $action $package1 $package2 ...

# shellcheck disable=SC2119,SC2120

# investigate, why a certain operation is not done with certain packages

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

# TODO: reintrocude "keep", "stubbornly_keep", "stabilize" and "unstage"
# using information from the database.

action="$1"
shift

tmp_dir=$(mktemp -d 'tmp.why-dont-you.XXXXXXXXXX' --tmpdir)
trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT

case "${action}" in

  'build')

    # shellcheck disable=SC2016
    {
      printf 'CREATE TEMPORARY TABLE `searches` (`search` VARCHAR(64));\n'
      printf 'INSERT INTO `searches` VALUES '
      # shellcheck disable=SC2046
      printf '(from_base64("%s")),' \
        $(
          printf '%s\n' "$@" | \
            base64_encode_each
        ) | \
        sed 's/,$/;\n/'
      # we select everything which is possibly of any interest:
      # - search term
      # - id (to see if it actually is on the build-list)
      # - build_assignments.architecture.name
      # - to_build.is_broken
      # - failed_builds_count
      # - to_build.is_blocked
      # - deps.pkgbase (any dependency pending?)
      # - build_slaves.name (is anyone building this?)
      # - pkgbase
      printf 'SELECT DISTINCT replace(to_base64(`searches`.`search`),"\\n",""),'
      printf '`to_build`.`ba_id`,'
      printf '`to_build`.`baa_name`,'
      printf 'If(`to_build`.`is_broken`,1,0),'
      printf '('
        printf 'SELECT COUNT(1)'
        printf ' FROM `failed_builds`'
        printf 'WHERE `failed_builds`.`build_assignment`=`to_build`.`ba_id`'
      printf ')'
      printf ',replace(to_base64(`%s`.`%s`),"\\n","")' \
        'to_build' 'is_blocked' \
        'deps' 'pkgbase' \
        'build_slaves' 'name' \
        'to_build' 'pkgbase'
      # at least one row for each given `search`
      printf ' FROM `searches`'
      printf ' LEFT JOIN '
        printf '('
        # join the tables for the to-be-built packages:
        # package_source, build_assignment, binary_package, repostory
        printf 'SELECT DISTINCT '
        printf '`tb_ps`.`pkgbase`,'
        printf '`tb_bin`.`id` AS `bin_id`,'
        printf '`tb_ba`.`id` AS `ba_id`,'
        printf '`tb_ba`.`is_blocked`,'
        printf '`tb_ba`.`is_broken`,'
        printf '`tb_baa`.`name` AS `baa_name`'
        printf ' FROM `package_sources` AS `tb_ps`'
        mysql_join_package_sources_build_assignments 'tb_ps' 'tb_ba'
        mysql_join_build_assignments_architectures 'tb_ba' 'tb_baa'
        mysql_join_build_assignments_binary_packages 'tb_ba' 'tb_bin'
        mysql_join_binary_packages_binary_packages_in_repositories 'tb_bin' 'tb_bir'
        printf ' WHERE `tb_bir`.`repository`=%s' \
          "${repository_ids__any_build_list}"
        printf ') AS `to_build`'
      printf ' ON ('
        printf '`to_build`.`pkgbase`=`searches`.`search`'
        printf ' OR `searches`.`search`=CONCAT(`to_build`.`baa_name`,"/",`to_build`.`pkgbase`)'
      printf ') LEFT JOIN '
        printf '('
        # same join as above, but with different names - for the
        # potential dependencies
        printf 'SELECT DISTINCT `dep_ps`.`pkgbase`,`dependencies`.`dependent`'
        printf ' FROM `package_sources` AS `dep_ps`'
        mysql_join_package_sources_build_assignments 'dep_ps' 'dep_ba'
        mysql_join_build_assignments_binary_packages 'dep_ba' 'dep_bin'
        mysql_join_binary_packages_binary_packages_in_repositories 'dep_bin' 'dep_bir'
        # now we have some (=3) additional joins,
        # because we are interested in dependency relations to `to_build`
        mysql_join_binary_packages_install_target_providers 'dep_bin'
        mysql_join_install_target_providers_dependencies_with_versions
        mysql_join_dependencies_dependency_types
        printf ' WHERE `dep_bir`.`repository`=%s' \
          "${repository_ids__any_build_list}"
        printf ' AND `dependency_types`.`relevant_for_building`'
        printf ') AS `deps`'
      printf ' ON `deps`.`dependent`=`to_build`.`bin_id`'
      # now we join with build slaves to see if someone builds this
      printf ' LEFT JOIN `build_slaves`'
      printf ' ON `build_slaves`.`currently_building`=`to_build`.`ba_id`'
      printf ';\n'
    } | \
      mysql_run_query 'unimportant' | \
      tr '\t' ' ' | \
      sort -k9,9 -k8,8 -k7,7 | \
      sed '
        / NULL \S\+$/ b multi-dep
        :multi-slave
          $!N
          s/^\(\(\S\+ \)\{6\}\)\(\S\+\)\( \S\+\)\n\(\S\+ \)\{6\}\(\S\+\)\4/\1\3,\6\4/
          t multi-slave
          P
          D
        :multi-dep
          / NULL\( \S\+\)\{3\}$/! b
          $!N
          s/^\(\(\S\+ \)\{5\}\)\(\S\+\)\(\( \S\+\)\{2\}\)\n\(\S\+ \)\{5\}\(\S\+\)\4/\1\3,\7\4/
          t multi-dep
          P
          D
      ' | \
      sed '
        s/NULL,//g
      ' | \
      while read -r search id ba_arch is_broken trials is_blocked dependency slave pkgbase; do
        search=$(
          printf '%s' "${search}" | \
            base64 -d
        )
        pkgbase=$(
          printf '%s' "${pkgbase}" | \
            base64 -d
        )
        if [ "${id}" = 'NULL' ]; then
          >&2 printf '"%s" is not on the build list.\n' \
            "${search}"
          continue
        fi
        if [ "${slave}" != 'NULL' ]; then
          # beware: A slave named "5BË" will look exactly like this!
          printf '"%s/%s" is locked by %s.\n' \
            "${ba_arch}" \
            "${pkgbase}" \
            "$(
              printf '%s\n' "${slave}" | \
                tr ',' '\n' | \
                while read -r line; do
                  printf '%s\n' "${line}" | \
                    base64 -d
                  printf ','
                done | \
                sed 's/,$//'
            )"
          continue
        fi
        if [ "${is_blocked}" != 'NULL' ]; then
          # beware: A block-reason "5BË" will look exactly like this!
          printf '"%s/%s" is blocked: "%s".\n' \
            "${ba_arch}" \
            "${pkgbase}" \
            "$(
              printf '%s' "${is_blocked}" | \
                base64 -d
            )"
          continue
        fi
        if [ "${dependency}" != 'NULL' ]; then
          printf '"%s/%s" has unmet dependencies:\n' \
            "${ba_arch}" \
            "${pkgbase}"
          printf '%s\n' "${dependency}" | \
            tr ',' '\n' | \
            while read -r line; do
              printf '  '
              printf '%s\n' "${line}" | \
                base64 -d
              printf '\n'
            done | \
            sort -u
          continue
        fi
        if [ "${is_broken}" = '1' ]; then
          printf '"%s/%s" is broken (%sx built), but would be built.\n' \
            "${ba_arch}" \
            "${pkgbase}" \
            "${trials}"
          continue
        fi
        printf '"%s/%s" would be built.\n' \
          "${ba_arch}" \
          "${pkgbase}"
      done

  ;;

  'stabilize'|'unstage')

    if [ "${action}" = 'stabilize' ]; then
      # shellcheck disable=SC2154
      from_stability=${repository_stability_ids__testing}
      # shellcheck disable=SC2016
      test_filter='`binary_packages`.`is_tested`'
    else
      # shellcheck disable=SC2154
      from_stability=${repository_stability_ids__staging}
      test_filter='1'
    fi

    for pkgname in "$@"; do
      # shellcheck disable=SC2016
      {
        printf 'SELECT'
        printf ' `binary_packages_in_repositories`.`id`,'
        printf ' IF(%s,1,0),' "${test_filter}"
        printf ' IF(`binary_packages`.`has_issues`,1,0)'
        printf ' FROM `binary_packages_in_repositories`'
        mysql_join_binary_packages_in_repositories_repositories
        mysql_join_repositories_architectures
        mysql_join_binary_packages_in_repositories_binary_packages
        printf ' WHERE `repositories`.`stability`=%s' \
          "${from_stability}"
        printf ' AND ('
          printf '`binary_packages`.`pkgname`=from_base64("%s")' \
            "$(
              printf '%s' "${pkgname}" | \
                base64 -w0
            )"
          printf 'OR CONCAT('
            printf '`architectures`.`name`,"/",'
            printf '`binary_packages`.`pkgname`'
          printf ')=from_base64("%s")' \
            "$(
              printf '%s' "${pkgname}" | \
                base64 -w0
            )"
        printf ');\n'
      } \
      | mysql_run_query 'unimportant' \
      | while read -r bpir tested issues; do
        if [ "${tested}" = '0' ]; then
          printf '"%s" is not tested.\n' \
            "${pkgname}"
          continue
        fi
        if [ "${issues}" = '1' ]; then
          printf 'There are unresolved bugs reported against "%s".\n' \
            "${pkgname}"
          continue
        fi
        printf '%s\n' "${bpir}" \
        >> "${tmp_dir}/packages"
      done
    done

    if [ ! -s "${tmp_dir}/packages" ]; then
      exit
    fi

    # shellcheck disable=SC2016
    {
      for copy in '' '_2'; do
        printf 'CREATE TEMPORARY TABLE `bpirs%s` (' \
          "${copy}"
          printf '`id` BIGINT,'
          printf 'UNIQUE KEY `id`(`id`)'
        printf ');\n'
      done
      printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `bpirs`;\n' \
        "${tmp_dir}/packages"
      printf 'INSERT INTO `bpirs_2`(`id`)'
      printf ' SELECT '
      printf '`bpirs`.`id`'
      printf ' FROM `bpirs`;\n'

      printf 'SELECT CONCAT('
        printf '"\\"",'
        printf '`sub_q`.`label`,'
        printf '"\\" depends on ",'
        printf 'GROUP_CONCAT('
          printf 'CONCAT('
            printf '"\\"",'
            printf '`sub_q`.`dependency`,'
            printf '"\\""'
          printf ')'
        printf '),'
        printf '" which must be moved simultanously"'
      printf ') FROM ('
        printf 'SELECT DISTINCT '
        printf 'CONCAT(`architectures`.`name`,"/",`itp_bp`.`pkgname`) AS `dependency`,'
        printf '`bpirs`.`id`,'
        printf 'CONCAT(`architectures`.`name`,"/",`binary_packages`.`pkgname`) AS `label`'
        printf ' FROM `bpirs`'
        printf ' JOIN `binary_packages_in_repositories`'
        printf ' ON `binary_packages_in_repositories`.`id`=`bpirs`.`id`'
        mysql_join_binary_packages_in_repositories_repositories
        mysql_join_repositories_architectures
        mysql_join_binary_packages_in_repositories_binary_packages
        mysql_join_binary_packages_dependencies
        mysql_join_dependencies_dependency_types
        printf ' AND `dependency_types`.`relevant_for_binary_packages`'
        mysql_join_dependencies_install_target_providers_with_versions
        mysql_join_install_target_providers_binary_packages '' 'itp_bp'
        mysql_join_binary_packages_binary_packages_in_repositories 'itp_bp' 'itp_bpir'
        mysql_join_binary_packages_in_repositories_repositories 'itp_bpir' 'itp_r'
        printf ' AND `itp_r`.`architecture`=`repositories`.`architecture`'
        printf ' AND `repositories`.`stability`=`itp_r`.`stability`'
        printf ' LEFT JOIN `bpirs_2`'
        printf ' ON `bpirs_2`.`id`=`itp_bpir`.`id`'
        printf ' WHERE `bpirs_2`.`id` IS NULL'
      printf ') AS `sub_q`'
      printf ' GROUP BY `sub_q`.`id`;\n'

      printf 'SELECT CONCAT('
        printf '"\\"",'
        printf '`sub_q`.`label`,'
        printf '"\\" replaces dependencies of ",'
        printf 'GROUP_CONCAT('
          printf 'CONCAT('
            printf '"\\"",'
            printf '`sub_q`.`dependent`,'
            printf '"\\""'
          printf ')'
        printf '),'
        printf '"\\" which must be replaced simulatanously"'
      printf ') FROM ('
        printf 'SELECT DISTINCT '
        printf 'CONCAT(`architectures`.`name`,"/",`d_bp`.`pkgname`) AS `dependent`,'
        printf '`bpirs`.`id`,'
        printf 'CONCAT(`architectures`.`name`,"/",`binary_packages`.`pkgname`) AS `label`'
        printf ' FROM `bpirs`'
        printf ' JOIN `binary_packages_in_repositories`'
        printf ' ON `binary_packages_in_repositories`.`id`=`bpirs`.`id`'
        mysql_join_binary_packages_in_repositories_repositories
        mysql_join_repositories_architectures
        mysql_join_binary_packages_in_repositories_binary_packages
        printf ' JOIN `binary_packages` AS `r_bp`'
        printf ' ON `binary_packages`.`pkgname`=`r_bp`.`pkgname`'
        mysql_join_binary_packages_binary_packages_in_repositories 'r_bp' 'r_bpir'
        printf ' JOIN `repository_moves`'
        printf ' ON `repository_moves`.`from_repository`=`repositories`.`id`'
        printf ' AND `repository_moves`.`to_repository`=`r_bpir`.`repository`'
        mysql_join_binary_packages_install_target_providers 'r_bp' 'r_itp'
        mysql_join_install_target_providers_dependencies_with_versions 'r_itp'
        mysql_join_dependencies_dependency_types
        printf ' AND `dependency_types`.`relevant_for_binary_packages`'
        mysql_join_dependencies_binary_packages '' 'd_bp'
        mysql_join_binary_packages_binary_packages_in_repositories 'd_bp' 'd_bpir'
        mysql_join_binary_packages_in_repositories_repositories 'd_bpir' 'd_r'
        printf ' AND `d_r`.`architecture`=`repositories`.`architecture`'
        printf ' JOIN `repository_stability_relations`'
        printf ' ON `repository_stability_relations`.`more_stable`=`d_r`.`stability`'
        printf ' AND `repository_stability_relations`.`less_stable`=`repositories`.`stability`'
      printf ') AS `sub_q`'
      printf ' GROUP BY `sub_q`.`id`;\n'
    } \
    | mysql_run_query 'unimportant'

  ;;

  'keep'|'stubbornly_keep')

    printf 'Sry, "why-dont-you %s" is unavailable, until someone recodes it to look into the database.\n' "${action}"

  ;;

  *)
    >&2 printf 'unknown action "%s" - known actions are:' "${action}"
    >&2 printf ' "%s"' \
      'build' \
      'stabilize' \
      'unstage' \
      'keep' \
      'stubbornly_keep'
    >&2 printf '\n'
    exit 1

esac