Merge pull request #1106 from AladW/depends-reqby-dict
[aurutils.git] / lib / aur-graph
blob2ec6d60c833e522c6d93bcdda75c6a0630f683bc
1 #!/bin/awk -f
2 # aur-graph - print package/dependency pairs
4 # < 0 : if ver1 < ver2
5 # = 0 : if ver1 == ver2
6 # > 0 : if ver1 > ver2
7 function get_vercmp(ver1, ver2, op) {
8 command = ("vercmp " ver1 " " ver2)
10 if (op == "-" || ver2 == "-") {
11 return "true" # dependency is unversioned
12 } else if (op == "=") {
13 return (ver1 == ver2) # common case
14 } else if (op == "<") {
15 command | getline
16 close(command)
17 return ($1 < 0)
18 } else if (op == ">") {
19 command | getline
20 close(command)
21 return ($1 > 0)
22 } else if (op == "<=") {
23 command | getline
24 close(command)
25 return ($1 <= 0)
26 } else if (op == ">=") {
27 command | getline
28 close(command)
29 return ($1 >= 0)
30 } else {
31 printf("invalid operation\n") > "/dev/stderr"
32 exit 1
36 /pkgbase/ {
37 in_split_pkg = 0
38 pkgbase = $3
39 pkgver = ""
41 # track both the pkgbases themselves and their number of deps
42 dep_counts[pkgbase] = 0
45 /^\tpkgver/ {
46 if (!in_split_pkg) {
47 pkgver = $3
51 /^\tdepends/ {
52 if ((!length(DEPENDS) || DEPENDS == 1) && !in_split_pkg)
53 pkg_deps[pkgbase, ++dep_counts[pkgbase]] = $3 # versioned
56 /^\tmakedepends/ {
57 if ((!length(MAKEDEPENDS) || MAKEDEPENDS == 1) && !in_split_pkg)
58 pkg_deps[pkgbase, ++dep_counts[pkgbase]] = $3
61 /^\tcheckdepends/ {
62 if ((!length(CHECKDEPENDS) || CHECKDEPENDS == 1) && !in_split_pkg)
63 pkg_deps[pkgbase, ++dep_counts[pkgbase]] = $3
66 /^\toptdepends/ {
67 if (OPTDEPENDS == 1 && !in_split_pkg) {
68 pkg_deps[pkgbase, ++dep_counts[pkgbase]] = $3
72 /^$/ {
73 in_split_pkg = 1
76 /pkgname/ {
77 pkg_map[$3] = pkgbase # node
78 ver_map[$3] = pkgver # weight
81 /^\tprovides/ {
82 split($3, prov, "=")
84 # if provider is unversioned, use pkgver
85 if ("2" in prov)
86 ver_map[prov[1]] = prov[2]
87 else
88 ver_map[prov[1]] = pkgver
90 # append node
91 pkg_map[prov[1]] = pkgbase
94 END {
95 _vercmp_exit = 0
97 for (pkgbase in dep_counts) {
98 # add a loop to isolated nodes (#402)
99 printf("%s\t%s\n", pkgbase, pkgbase)
101 for (dep = 1; dep <= dep_counts[pkgbase]; dep++) {
102 if (length(PRINTALL) || PRINTALL == 1) {
103 printf("%s\t%s\n", pkgbase, pkg_deps[pkgbase, dep])
104 continue
106 dep_op = "-" # unversioned / no comparison
108 # valid operators (important: <= before <)
109 split("<=|>=|<|=|>", cmp, "|")
111 # split: fourth argument is gawk extension
112 for (i in cmp) {
113 split(pkg_deps[pkgbase, dep], dep_split, cmp[i])
115 if ("2" in dep_split) {
116 dep_op = cmp[i]
117 break
121 if ("1" in dep_split)
122 dep_pkgname = dep_split[1]
123 else
124 exit 2
126 if ("2" in dep_split)
127 dep_pkgver = dep_split[2]
128 else
129 dep_pkgver = "-"
131 # only print dependency if it was encountered before
132 if (dep_pkgname in pkg_map == 0)
133 continue
135 # run vercmp with provider and versioned dependency
136 if (get_vercmp(ver_map[dep_pkgname], dep_pkgver, dep_op)) {
137 printf("%s\t%s\n", pkgbase, pkg_map[dep_pkgname])
138 } else {
139 printf("invalid node: %s %s (required: %s%s)\n",
140 dep_pkgname, ver_map[dep_pkgname], dep_op, dep_pkgver) > "/dev/stderr"
141 close("/dev/stderr")
143 # delay mismatches to loop end
144 _vercmp_exit = 1
149 if (_vercmp_exit)
150 exit _vercmp_exit