summaryrefslogtreecommitdiff
path: root/bin/prioritize-build-list
blob: 04a9d4f4276bd17a25ac6f3dc9afdc9b1a3b54f2 (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
#!/bin/sh

# shellcheck disable=SC2119,SC2120

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

usage() {
  >&2 echo ''
  >&2 echo 'Usage: prioritize-build-list [options] pkg-regex-file'
  >&2 echo '  moves packages matching any pkg-regex in pkg-regex-file'
  >&2 echo '  to front of build list'
  >&2 echo ''
  >&2 echo 'possible options:'
  >&2 echo '  -d|--dependencies'
  >&2 echo '    Set priority of dependencies not-lower as priority of'
  >&2 echo '    dependent packages.'
  >&2 echo '  -h|--help:'
  >&2 echo '    Show this help and exit.'
  >&2 echo '  -w|--wait:'
  >&2 echo '    Wait for lock if necessary.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

eval set -- "$(
  getopt -o dhw \
    --long dependencies \
    --long help \
    --long wait \
    -n "$(basename "$0")" -- "$@" || \
    echo usage
  )"

dependencies=false
wait_for_lock='-n'

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

if [ $# -ne 1 ]; then
  >&2 echo 'No package-regex-list was given.'
  usage
fi

if [ ! -r "$1" ]; then
  >&2 printf 'Package-list "%s" is not readable.\n' "$1"
  usage
fi

# Create a lock file for build list.

exec 9> "${sanity_check_lock_file}"
# shellcheck disable=SC2086
verbose_flock -s ${wait_for_lock} 9

exec 8> "${build_list_lock_file}"
# shellcheck disable=SC2086
verbose_flock ${wait_for_lock} 8

updated_rows=$(
  # shellcheck disable=SC2016
  {
    printf 'UPDATE `build_assignments`'
    mysql_join_build_assignments_package_sources
    mysql_join_build_assignments_binary_packages
    mysql_join_binary_packages_binary_packages_in_repositories
    printf ' SET `build_assignments`.`priority`=('
      printf 'SELECT COALESCE(MAX(`all_priorities`.`priority`),0)+1'
      printf ' FROM ('
        printf 'SELECT `others`.`priority`'
        printf ' FROM `build_assignments` AS `others`'
      printf ') AS `all_priorities`'
    printf ')'
    printf ' WHERE ('
    tr '[:space:]' '\n' < "$1" | \
      base64_encode_each | \
      sed '
        s/^/`package_sources`.`pkgbase` REGEXP from_base64("/
        s/$/") OR /
      '
    printf '0) AND `binary_packages_in_repositories`.`repository`=%s;\n' \
      "${repository_ids__any_build_list}"
    printf 'SELECT row_count();\n'
  } | \
    mysql_run_query
)

if ${dependencies}; then
  new_updated_rows=1
  updated_rows=$((updated_rows-1))
  while [ ${new_updated_rows} -ne 0 ]; do
    updated_rows=$((updated_rows+new_updated_rows))
    new_updated_rows=$(
      # shellcheck disable=SC2016
      {
        printf 'UPDATE `build_assignments`'
        printf ' JOIN ('
          printf 'SELECT'
          printf ' `s_ba`.`id` AS `build_assignment`,'
          printf 'MAX(`g_ba`.`priority`) AS `priority`'
          printf ' FROM `build_assignments` AS `g_ba`'
          mysql_join_build_assignments_binary_packages 'g_ba' 'g_bp'
          mysql_join_binary_packages_binary_packages_in_repositories 'g_bp' 'g_bpir'
          printf ' AND `g_bpir`.`repository`=%s' \
            "${repository_ids__any_build_list}"
          mysql_join_binary_packages_dependencies 'g_bp'
          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 '' 's_bp'
          mysql_join_binary_packages_binary_packages_in_repositories 's_bp' 's_bpir'
          printf ' AND `s_bpir`.`repository`=%s' \
            "${repository_ids__any_build_list}"
          printf ' JOIN `architecture_compatibilities` AS `ac_a`'
          printf ' ON `ac_a`.`fully_compatible`'
          printf ' AND `ac_a`.`built_for`=`g_bp`.`architecture`'
          printf ' JOIN `architecture_compatibilities` AS `ac_b`'
          printf ' ON `ac_b`.`fully_compatible`'
          printf ' AND `ac_b`.`built_for`=`s_bp`.`architecture`'
          printf ' AND `ac_a`.`runs_on`=`ac_b`.`runs_on`'
          mysql_join_binary_packages_build_assignments 's_bp' 's_ba'
          printf ' GROUP BY `s_ba`.`id`'
        printf ') AS `sub_q`'
        printf ' ON `sub_q`.`build_assignment`=`build_assignments`.`id`'
        printf ' SET `build_assignments`.`priority`=`sub_q`.`priority`'
        printf ' WHERE `build_assignments`.`priority`<`sub_q`.`priority`'
        printf ' AND NOT `build_assignments`.`is_broken`'
        printf ';\n'
        printf 'SELECT row_count();\n'
      } | \
        mysql_run_query
    )
  done
fi

if [ -w "$1" ] && \
  [ -f "$1" ]; then
  printf '%s\n' "${updated_rows}" >"$1"
else
  printf '%s\n' "${updated_rows}" >&2
fi