summaryrefslogtreecommitdiff
path: root/bin/get-package-updates
blob: de58eb23811e93f108d3e7340954bfb8f0d208fb (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
#!/bin/bash

# check for packages that need to be built, and build a list in the proper build order
# Details:
#  https://github.com/archlinux32/builder/wiki/Build-system#get-package-updates

# TODO:
#  include repository of package customizations

. "${0%/*}/../conf/default.conf"

# Create a lock file for build list.

exec 9> "${lock_file}"
flock -n 9 || exit

# Update git repositories (official packages, community packages and the repository of package customizations).

for repo in "${repo_paths[@]}"; do
  git -C "${repo}" clean -df
  git -C "${repo}" checkout master
  git -C "${repo}" pull
done

# Read previous git revision numbers from files.

declare -A old_repo_revisions

for repo in "${!repo_paths[@]}"; do
  old_repo_revisions["${repo}"]="$(
    cat "${work_dir}/${repo}.revision" 2> /dev/null || \
      echo NONE
  )"
  git -C "${repo_paths["${repo}"]}" rev-parse HEAD > \
    "${work_dir}/${repo}.revision.new"
done

echo 'Check modified packages from the last update, and put them to the build list.'

# Check modified packages from the last update, and put them to the build list.
# If a package is updated, but already on the rebuild list, then just update the git revision number.
# If a package is deleted, remove from the rebuild list, and add it to the deletion list.
# If a new package is added, then ensure that it's not on the deletion list.

cp "${work_dir}/build-list"{,.new}
cp "${work_dir}/deletion-list"{,.new}

(
  for repo in "${!repo_paths[@]}"; do
    current_HEAD="$(cat "${work_dir}/${repo}.revision.new")"
    (
      if [ "${old_repo_revisions["${repo}"]}" == "NONE" ]; then
        git -C "${repo_paths["${repo}"]}" archive --format=tar HEAD | \
          tar -t | \
          sed 's|^|A\t|'
      else
        git -C "${repo_paths["${repo}"]}" diff --no-renames --name-status "${old_repo_revisions["${repo}"]}" HEAD
      fi
    ) | \
      grep '^.\s[^/]\+/repos/[^/]\+/PKGBUILD$' | \
      grep -v -- '-i686/PKGBUILD$' | \
      sed 's|^\(.\)\t\([^/]\+\)/repos/\([^/]\+\)-[^/-]\+/PKGBUILD$|\1 \2 '"${current_HEAD}"' \3|' | \
      grep -v '\(staging\|testing\)$'
  done | \
    sort -u
  sed "s|^\(.*\)\$|D \1 $(cat "${work_dir}/archlinux32.revision.new") archlinux32|" "${repo_paths["archlinux32"]}/blacklist"
) | \
  while read -r mode package git_revision repository; do
    case "${mode}" in

      "A"|"M")
        sed -i "/^${package} /d" "${work_dir}/build-list"
        echo "${package} ${git_revision} ${repository}" >> \
          "${work_dir}/build-list.new"
        sed -i "/^${package}\$/d" "${work_dir}/deletion-list.new"
      ;;

      "D")
        echo "${package}" >> \
          "${work_dir}/deletion-list.new"
        sed -i "/^${package} /d" "${work_dir}/build-list.new"
      ;;

      *)
        >&2 echo "unknown git diff mode '${mode}'"
        exit 1
      ;;

    esac
  done

echo 'Extract dependencies of packages.'

# Put the list in the proper build order.
# First, we extract the dependencies of each package.
# TODO:
#  package modifications need to be included here

mkdir -p "${work_dir}/package-infos"

rm -f "${work_dir}/build-order"
touch "${work_dir}/build-order"

while read -r package git_revision repository; do
  PKGBUILD="$(find_pkgbuild "${package}" "${repository}")"

  if [ ! -r "${PKGBUILD}" ]; then
    echo "can't find PKGBUILD to package '${package}' from repository '${repository}': '${PKGBUILD}'"
    exit 1
  fi

  ls -1 "${work_dir}/package-infos" | \
    grep "^${package}"'\.[0-9a-f]\{40\}\.\(builds\|needs\)$' | \
    grep -v "^${package}\.${git_revision}"'\.\(builds\|needs\)$' | \
    while read file; do
      echo rm "${work_dir}/package-infos/${file}"
    done

  if [ ! -e "${work_dir}/package-infos/${package}.${git_revision}.builds" ] || \
    [ ! -e "${work_dir}/package-infos/${package}.${git_revision}.needs" ]; then

    (
      cd "${PKGBUILD%/*}"
      mksrcinfo -o "${work_dir}/package-infos/${package}.${git_revision}.SRCINFO"
    )

    grep '^\('$'\t''provides\|pkgname\) = ' "${work_dir}/package-infos/${package}.${git_revision}.SRCINFO" | \
      cut -d= -f2 | \
      sed 's|^\s\+||; s|[<>]$||' | \
      sort -u > \
      "${work_dir}/package-infos/${package}.${git_revision}.builds"

    (
      (
        sed -n '/^pkgname = /q;/^'$'\t''depends = /p' "${work_dir}/package-infos/${package}.${git_revision}.SRCINFO"
        grep '^'$'\t''\(makedepends\|checkdepends\) = ' "${work_dir}/package-infos/${package}.${git_revision}.SRCINFO"
      ) | \
        cut -d= -f2 | \
        sed 's|^\s\+||; s|[<>]$||' | \
        sort -u
      cat "${work_dir}/package-infos/${package}.${git_revision}.builds" | \
        sed 'p'
    ) | \
      sort | \
      uniq -u > \
      "${work_dir}/package-infos/${package}.${git_revision}.needs"

    rm "${work_dir}/package-infos/${package}.${git_revision}.SRCINFO"

  fi

  while read target; do
    echo "${package} ${target}" >> \
      "${work_dir}/build-order"
  done < "${work_dir}/package-infos/${package}.${git_revision}.builds"

  while read dependency; do
    echo "${dependency} ${package}" >> \
      "${work_dir}/build-order"
  done < "${work_dir}/package-infos/${package}.${git_revision}.needs"

done < "${work_dir}/build-list.new"

echo 'Now actually sort it.'

(
  tsort "${work_dir}/build-order" 2> "${work_dir}/tsort.error" | \
    nl -ba | \
    awk '{print $1 " not-git whatever " $2}'
  awk '{print "0 " $2 " " $3 " " $1}' < \
    "${work_dir}/build-list.new"
) | \
  sort -k4,4 -k1nr | \
  uniq -f3 -D | \
  sed '/^0 /d;N;s|\n| |' | \
  sort -k1n,1 | \
  awk '{print $4 " " $6 " " $7}' > \
  "${work_dir}/build-list.new.new"

if [ -s "${work_dir}/tsort.error" ]; then
  >&2 echo 'WARNING: There is a dependency cycle!'
  >&2 cat "${work_dir}/tsort.error"
  >&2 echo
  >&2 echo 'I will continue anyway.'
else
  rm "${work_dir}/tsort.error"
fi

# Move the .new-files to the actual files

(
  echo "build-list.new" "build-list" "deletion-list"
  echo "${!repo_paths[@]}" | \
    sed 's@\( \|$\)@.revision\1@g'
) | \
  tr ' ' '\n' | \
  while read -r file; do
    mv "${work_dir}/${file}.new" "${work_dir}/${file}"
  done

# Remove the lock file

rm -f "${lock_file}"