summaryrefslogtreecommitdiff
path: root/dependency-graph
blob: 0f38f002714d8de6b5a65276c7da8d69a137f13d (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
#!/bin/bash

if [ $# -eq 2 ]; then
  cd "${1}"
  if [ ! -f '.SRCINFO' ] ||
    [ "$(stat -c%Y PKGBUILD)" -gt "$(stat -c%Y .SRCINFO)" ]; then
    makepkg --printsrcinfo > .SRCINFO
  fi
  sed '
    /^\S/ b
    /^\s\+\(make\|check\|\)depends = \S/ {
      s/[<>=]\S\+$//
      b
    }
    /^$/ b
    d
  ' '.SRCINFO' \
  > "${2}"
  all=$(
    sed -n '
      /^pkgbase = /,/^$/ {
        /^\s/ p
      }
    ' "${2}" \
    | sed '
      $! s/$/\\/
    '
  )
  sed -i '
    /^pkgbase = /,/^$/ d
    /^pkgname = / a\
'"${all}"'
  ' "${2}"
  sed -i '
    /^\s*$/d
  ' "${2}"
  exit
fi

if [ $# -ne 0 ]; then
  >&2 echo 'usage error'
  >&2 printf '"%s" ' "$@"
  >&2 printf '\n'
  exit 1
fi

tmp_dir=$(mktemp -d)
trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT

find "$(pwd)" -maxdepth 2 -mindepth 2 -name 'PKGBUILD' -type f \
| sed -n '
  s@/[^/]\+$@@
  T
  s@/[^/]\+$@\0\n'"${tmp_dir%/}"'\0.srcinfo@
  T
  p
' \
| parallel -j0 -L2 "$0"

find "${tmp_dir}" -type f -name '*.srcinfo' \
| while read -r f; do
  while read -r a b c; do
    if [ "x${b}" != 'x=' ]; then
      >&2 printf 'Syntax error: "%s %s %s"\n' "${a}" "${b}" "${c}"
      exit 1
    fi
    case "${a}" in
      'pkgname')
        pkgname="${c}"
      ;;
      'depends'|'makedepends'|'checkdepends')
        printf '%s %s %s\n' \
          "${a}" \
          "${c}" \
          "${pkgname}"
      ;;
      *)
        >&2 printf 'Semantic error: "%s %s %s"\n' "${a}" "${b}" "${c}"
        exit 1
      ;;
    esac
  done \
  < "${f}"
done \
| sort -u \
| sed '
  s/^depends /0 \0/
  t
  s/^/1 /
' \
| sort -k3,4 -k1n,1 -k2,2 \
| uniq -f2 \
| sort -k3,3 \
> "${tmp_dir}/dependencies"

awk '{print $4}' "${tmp_dir}/dependencies" \
| sort -u \
| join -1 1 -2 3 -o 2.2,2.3,2.4 - "${tmp_dir}/dependencies" \
| sponge "${tmp_dir}/dependencies"

{
  printf 'digraph dependencies {\n'
  sed '
    s/^depends \(\S\+\) \(\S\+\)$/  "\1" -> "\2";/
    t
    s/^makedepends \(\S\+\) \(\S\+\)$/  "\1" -> "\2" [color="#0000ff"];/
    t
    s/^checkdepends \(\S\+\) \(\S\+\)$/  "\1" -> "\2" [color="#0000ff", style=dotted];/
    t
    w /dev/stderr
    d
  ' "${tmp_dir}/dependencies"
  printf '}\n'
} \
| dot -Tpng -o"${0%/*}/dependency-graph.png"