[3c10176] | 1 | #!/bin/bash
|
---|
| 2 | #
|
---|
| 3 | # $Id$
|
---|
| 4 | #
|
---|
| 5 |
|
---|
[2140f22] | 6 | #-----------------------------------------------------------------------------#
|
---|
| 7 | # This is a set of (recursive) functions for manipulating a dependency graph. #
|
---|
| 8 | # We use algorithms and definitions from chapter 4 (mainly section 4.2) of #
|
---|
| 9 | # https://algs4.cs.princeton.edu/. The graph we manipulate is the directed #
|
---|
| 10 | # graph of the dependencies: nodes are packages in the BLFS book. A node A is #
|
---|
| 11 | # connected to a node B if package A depends on B. A topological order (rev- #
|
---|
| 12 | # erted) is exactly what we want for a build order. But a topological order #
|
---|
| 13 | # only exists if the graph is acyclic. We'll therefore have to remove cycles. #
|
---|
| 14 | # There are a number of other features we want to consider: #
|
---|
| 15 | # - edges are weighted according to the dependency requirement: #
|
---|
| 16 | # 1 for required #
|
---|
| 17 | # 2 for recommended #
|
---|
| 18 | # 3 for optional #
|
---|
| 19 | # 4 for external #
|
---|
| 20 | # We should consider only edges with weight lower or equal to that #
|
---|
| 21 | # specified by the user, but see below. #
|
---|
| 22 | # - we do not want to build the whole book. The user specifies a set of #
|
---|
| 23 | # packages, and we have to consider only nodes reachable from this set #
|
---|
| 24 | # using edges of weight not exceeding the specified weight. #
|
---|
| 25 | # - when doing the topological sort, we want to consider all the edges and #
|
---|
| 26 | # not only those not exceeding the specified weight: If a package A in the #
|
---|
| 27 | # reachable subgraph depends optionally on another package B in the same #
|
---|
| 28 | # subgraph, we want to build B before A. But this means we'll have to #
|
---|
| 29 | # remove cycles for all weights. #
|
---|
| 30 | # - dependencies have another qualifier: before or after. The problem: if a #
|
---|
| 31 | # package A depends on B with an "after" qualifier, and a package C depends #
|
---|
| 32 | # on A with a "before" qualifier, C may need B to be able to use A. So the #
|
---|
| 33 | # only safe way to consider "after" qualifiers is to consider that they are #
|
---|
| 34 | # "before" deps for any parent of the packages considered. #
|
---|
| 35 | # We'll therefore have a 3 pass procedure. First build the set of nodes #
|
---|
| 36 | # reachable from the root set. Second, remove dangling edges (those pointing #
|
---|
| 37 | # to packages outside the node set), and move "after" edges to "before" edges #
|
---|
| 38 | # originating from the parents. Third remove cycles and generate a #
|
---|
| 39 | # topological sort. #
|
---|
| 40 | # #
|
---|
| 41 | # TODO: document each pass
|
---|
| 42 | # Data layout: #
|
---|
| 43 | # TODO: needs also to document the .tree files, and the "f" qualifier
|
---|
| 44 | # #
|
---|
| 45 | # A node of the tree is represented by a text file <nodeName>.dep. Each edge #
|
---|
| 46 | # starting from this node is represented by a line in this file. We keep #
|
---|
| 47 | # those files in the same directory. We introduce a special node named root, #
|
---|
| 48 | # whose edges point to the list of nodes requested by the user. Each line #
|
---|
| 49 | # contains three fields: #
|
---|
| 50 | # - the weight of the edge #
|
---|
| 51 | # - the "before" (b) or "after" (a) qualifier #
|
---|
| 52 | # - the name of the destination of the edge #
|
---|
[7dc8595] | 53 | # #
|
---|
[2140f22] | 54 | # TODO: The following is obsolete
|
---|
[d7c07e0] | 55 | # Circular dependencies: #
|
---|
[7dc8595] | 56 | # #
|
---|
[d7c07e0] | 57 | # In case we find a cirdular dependency, it has the form : #
|
---|
| 58 | # parent->dependency_0->...->dependency_n->dependency_0 #
|
---|
| 59 | # If we want to build dependency_n before dependency_0, no problem: #
|
---|
| 60 | # we just prune the tree at dependency_n. If we want to build first #
|
---|
| 61 | # dependency_0, we need to put dependency_n as a dependency of parent, #
|
---|
| 62 | # then erase and rebuild the subtree from there. Now, we may have met #
|
---|
| 63 | # another circular dependency in the subtree, and erasing the tree makes #
|
---|
| 64 | # us forget the decision which was made. So, after first generating the #
|
---|
| 65 | # list of dependencies from packages.xml, we keep the generated list in #
|
---|
| 66 | # a file <nodeName>.odep, which we modify according to the decision which #
|
---|
| 67 | # was made. #
|
---|
[e4b1293] | 68 | #---------------------------------------------------------------------------#
|
---|
| 69 |
|
---|
| 70 | # Global variables:
|
---|
[e576789] | 71 | # A string of spaces for indenting:
|
---|
[3c10176] | 72 | declare -a spaceSTR=" "
|
---|
[2140f22] | 73 | # When we are backing up from a circular dependency, `parentNode'
|
---|
| 74 | # contains the node which has an edge entering the cycle
|
---|
| 75 | declare parentNode
|
---|
[3c10176] | 76 |
|
---|
[2140f22] | 77 | #---------------------#
|
---|
| 78 | generate_subgraph() { #
|
---|
| 79 | #---------------------#
|
---|
[3c10176] | 80 | : <<inline_doc
|
---|
[2140f22] | 81 | function: Create a subgraph of all the nodes reachable from the node
|
---|
| 82 | represented by the file whose name is $1. The edges considered
|
---|
| 83 | are those with maximal weight DEP_LEVEL (recursive function).
|
---|
| 84 | input vars: $1 : file name corresponding to the node whose edges will be
|
---|
| 85 | followed for the DFS
|
---|
| 86 | $2 : weight of the edge leading to this node
|
---|
| 87 | $3 : depth (root is 1)
|
---|
| 88 | $4 : qualifier (a for after, b for before)
|
---|
[7ac11f0] | 89 | externals: vars: DEP_LEVEL contains 1 if we want to build the
|
---|
| 90 | tree only for required dependencies,
|
---|
| 91 | 2 if we want also recommended ones,
|
---|
[2140f22] | 92 | 3 if we want also optional ones, but only
|
---|
| 93 | for the requested packages,
|
---|
| 94 | 4 if we want all the dependencies
|
---|
[7ac11f0] | 95 | MAIL_SERVER contains the name of the MTA we want to use.
|
---|
| 96 | files: ../xsl/dependencies.xsl: stylesheet for creating the
|
---|
| 97 | .dep files
|
---|
[e91a15d] | 98 | ../packages.xml: File containing packages id
|
---|
[7ac11f0] | 99 | and dependencies
|
---|
[2140f22] | 100 | returns: 0 if the tree has been successfully created
|
---|
| 101 | output: files: for each node reachable from $1, a file <node>.dep.
|
---|
| 102 | on error: nothing
|
---|
| 103 | on success: nothing
|
---|
| 104 | inline_doc
|
---|
| 105 |
|
---|
| 106 | local depFile=$1
|
---|
| 107 | local -i weight=$2
|
---|
| 108 | local -i depth=$3
|
---|
| 109 | local qualifier=$4
|
---|
| 110 | local -i spacing=0
|
---|
| 111 | local priostring
|
---|
| 112 | local buildstring
|
---|
| 113 | local id_of_dep
|
---|
| 114 | local prio_of_dep
|
---|
| 115 | local build_of_dep
|
---|
| 116 | local dep_level
|
---|
| 117 |
|
---|
| 118 | if (( depth < 10 )); then spacing=1; fi
|
---|
| 119 | case $weight in
|
---|
| 120 | 1) priostring=required ;;
|
---|
| 121 | 2) priostring=recommended ;;
|
---|
| 122 | 3) priostring=optional ;;
|
---|
| 123 | esac
|
---|
| 124 | case $qualifier in
|
---|
| 125 | a) buildstring=runtime ;;
|
---|
| 126 | b) buildstring= ;;
|
---|
| 127 | esac
|
---|
| 128 | dep_level=$DEP_LEVEL
|
---|
| 129 | if [ "$dep_level" = 3 ] && [ "$depth" -gt 2 ]; then dep_level=2; fi
|
---|
| 130 | if [ "$dep_level" -gt 3 ]; then dep_level=3; fi
|
---|
| 131 | echo -en "\nNode: $depth${spaceSTR:0:$(( depth + spacing ))}${RED}${depFile%.dep}${OFF} $priostring $buildstring"
|
---|
| 132 |
|
---|
| 133 | depth=$(( depth + 1 ))
|
---|
| 134 | if (( depth < 10 )); then spacing=1; else spacing=0; fi
|
---|
| 135 | # Start of loop
|
---|
| 136 | {
|
---|
| 137 | while read prio_of_dep build_of_dep id_of_dep; do
|
---|
| 138 | case $prio_of_dep in
|
---|
| 139 | 1) priostring=required ;;
|
---|
| 140 | 2) priostring=recommended ;;
|
---|
| 141 | 3) priostring=optional ;;
|
---|
| 142 | 4) priostring=external ;;
|
---|
| 143 | esac
|
---|
| 144 | case $build_of_dep in
|
---|
| 145 | a) buildstring=runtime ;;
|
---|
| 146 | b) buildstring= ;;
|
---|
| 147 | esac
|
---|
| 148 | # Has this entry already been seen
|
---|
| 149 | if [ -f ${id_of_dep}.dep ] ; then
|
---|
| 150 | # Just display it and proceed.
|
---|
| 151 | echo -en "\nEdge: $depth${spaceSTR:0:$((depth + spacing))}${MAGENTA}${id_of_dep}${OFF} $priostring $buildstring"
|
---|
| 152 | continue
|
---|
| 153 | fi
|
---|
| 154 | # Is the weight higher than requested?
|
---|
| 155 | if [ "$prio_of_dep" -gt $dep_level ]; then
|
---|
| 156 | # Just display it and proceed.
|
---|
| 157 | echo -en "\n Out: $depth${spaceSTR:0:$((depth + spacing))}${YELLOW}${id_of_dep}${OFF} $priostring $buildstring"
|
---|
| 158 | continue
|
---|
| 159 | fi
|
---|
| 160 | # Otherwise, let's build the corresponding subgraph.
|
---|
| 161 | xsltproc --stringparam idofdep "$id_of_dep" \
|
---|
| 162 | --stringparam MTA "$MAIL_SERVER" \
|
---|
| 163 | -o ${id_of_dep}.dep \
|
---|
| 164 | ../xsl/dependencies.xsl ../packages.xml
|
---|
| 165 |
|
---|
| 166 | if [[ -s ${id_of_dep}.dep ]]; then # this dependency has dependencies
|
---|
| 167 | generate_subgraph ${id_of_dep}.dep $prio_of_dep $depth $build_of_dep
|
---|
| 168 | else # id_of_dep has no dependencies, just touch the file and display
|
---|
| 169 | touch ${id_of_dep}.dep
|
---|
| 170 | echo -en "\nLeaf: $depth${spaceSTR:0:$((depth + spacing))}${CYAN}${id_of_dep}${OFF} $priostring $buildstring"
|
---|
| 171 | fi
|
---|
| 172 | done
|
---|
| 173 | } <$depFile
|
---|
| 174 | depth=$(( depth - 1 ))
|
---|
| 175 | if (( depth < 10 )); then spacing=1; else spacing=0; fi
|
---|
| 176 | echo -en "\n End: $depth${spaceSTR:0:$((depth + spacing))}${GREEN}${depFile%.dep}${OFF}"
|
---|
| 177 | return 0
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | #-----------#
|
---|
| 181 | path_to() { #
|
---|
| 182 | #-----------#
|
---|
| 183 | : <<inline_doc
|
---|
| 184 | function: check whether there is a path from $1 to $2 on the graph
|
---|
| 185 | input vars: $1 contains the filename of the starting node.
|
---|
| 186 | $2 contains the name of the node to reach
|
---|
| 187 | $3 contains the weight above which we do not want to
|
---|
| 188 | follow an edge
|
---|
| 189 | "seen" (global) contains the list of already seen nodes
|
---|
| 190 | returns: 0 if the node has been found
|
---|
| 191 | 1 if not
|
---|
| 192 | on error: nothing
|
---|
| 193 | on success: nothing
|
---|
| 194 | inline_doc
|
---|
| 195 | local start=$1
|
---|
| 196 | local seek=$2
|
---|
| 197 | local prio=$3
|
---|
| 198 | local prio_of_dep
|
---|
| 199 | local build_of_dep
|
---|
| 200 | local id_of_dep
|
---|
| 201 | local r
|
---|
| 202 |
|
---|
| 203 | if test "${start%.dep}" = "$seek"; then return 0; fi
|
---|
[59afe73] | 204 | seen="$seen${start%.dep} "
|
---|
[5afaf7c] | 205 | if test -s $start; then
|
---|
[2140f22] | 206 | {
|
---|
| 207 | while read prio_of_dep build_of_dep id_of_dep; do
|
---|
| 208 | if test "$prio" -lt "$prio_of_dep"; then continue; fi
|
---|
[5afaf7c] | 209 | if ! test "${seen% $id_of_dep *}" = "$seen"; then continue; fi
|
---|
[2140f22] | 210 | if path_to ${id_of_dep}.dep $seek $prio; then return 0; fi
|
---|
| 211 | done
|
---|
| 212 | } < $start
|
---|
| 213 | fi
|
---|
| 214 | return 1
|
---|
| 215 | }
|
---|
| 216 | #------------------#
|
---|
| 217 | clean_subgraph() { #
|
---|
| 218 | #------------------#
|
---|
| 219 | : <<inline_doc
|
---|
| 220 | function: Remove dangling edges and create groups of deps for "after"
|
---|
| 221 | deps: A-before->B-after->C becomes:
|
---|
| 222 | A -before-> Bgroupxx -before-> B
|
---|
| 223 | \
|
---|
| 224 | -before-> C
|
---|
| 225 | the name of the group is chosen so that it is unlikely as
|
---|
| 226 | a package name (so that it is removed when building the
|
---|
| 227 | xml book).
|
---|
| 228 | input vars: None
|
---|
| 229 | files: <node>.dep files containing dangling edges and
|
---|
| 230 | "after" qualifiers
|
---|
| 231 | returns: 0
|
---|
| 232 | output: files: <node>.dep files containing no dangling edges and
|
---|
| 233 | no "after" qualifiers
|
---|
| 234 | on error: nothing
|
---|
| 235 | on success: nothing
|
---|
| 236 | inline_doc
|
---|
| 237 |
|
---|
| 238 | local node
|
---|
| 239 | local id_of_dep
|
---|
| 240 | local prio_of_dep
|
---|
| 241 | local build_of_dep
|
---|
| 242 | local lines_to_remove
|
---|
| 243 | local lines_to_change
|
---|
| 244 | local parent
|
---|
| 245 | local p
|
---|
| 246 | local b
|
---|
| 247 | local start
|
---|
| 248 | local seen
|
---|
| 249 |
|
---|
| 250 | for node in $(ls *.dep); do
|
---|
| 251 | if test $node = root.dep; then continue; fi
|
---|
[28546ae] | 252 | echo Cleaning $node
|
---|
[2140f22] | 253 | lines_to_remove=
|
---|
| 254 | {
|
---|
| 255 | while read prio_of_dep build_of_dep id_of_dep; do
|
---|
| 256 | if ! test -f ${id_of_dep}.dep; then
|
---|
| 257 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
| 258 | continue
|
---|
| 259 | fi
|
---|
| 260 | done
|
---|
| 261 | } <$node
|
---|
| 262 | for id_of_dep in $lines_to_remove; do
|
---|
| 263 | sed "/\ $id_of_dep\$/d" -i $node
|
---|
| 264 | done
|
---|
| 265 | done
|
---|
| 266 | for node in $(grep -l ' a ' *.dep); do
|
---|
| 267 | lines_to_remove=
|
---|
[28546ae] | 268 | echo Process "runtime" deps in $node
|
---|
[2140f22] | 269 | if ! [ -e ${node%.dep}groupxx.dep ]; then
|
---|
[f1edc8e] | 270 | b=0
|
---|
[2140f22] | 271 | for parent in $(grep -l ${node%.dep}\$ *); do
|
---|
| 272 | p=0
|
---|
| 273 | for start in $(grep ' a ' $node | cut -d' ' -f3); do
|
---|
| 274 | seen=" "
|
---|
| 275 | if path_to ${start}.dep ${parent%.dep} 3; then p=1; break; fi
|
---|
| 276 | done
|
---|
| 277 | if test $p = 0; then
|
---|
[f1edc8e] | 278 | b=1
|
---|
[2140f22] | 279 | sed -i "s/\ ${node%.dep}\$/&groupxx/" $parent
|
---|
| 280 | fi
|
---|
| 281 | done
|
---|
| 282 | echo "1 b ${node%.dep}" > ${node%.dep}groupxx.dep
|
---|
[f1edc8e] | 283 | if test $b = 0; then echo "1 b ${node%.dep}groupxx" >> root.dep; fi
|
---|
[2140f22] | 284 | fi
|
---|
| 285 | {
|
---|
| 286 | while read prio_of_dep build_of_dep id_of_dep; do
|
---|
| 287 | if test $build_of_dep = a; then
|
---|
| 288 | if ! grep -q ${id_of_dep} ${node%.dep}groupxx.dep; then
|
---|
| 289 | echo "$prio_of_dep b ${id_of_dep}" >> ${node%.dep}groupxx.dep
|
---|
| 290 | fi
|
---|
| 291 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
| 292 | fi
|
---|
| 293 | done
|
---|
| 294 | } <$node
|
---|
| 295 | for id_of_dep in $lines_to_remove; do
|
---|
| 296 | sed "/\ $id_of_dep\$/d" -i $node
|
---|
| 297 | done
|
---|
| 298 | done
|
---|
| 299 | for node in $(grep -l ' f ' *); do
|
---|
[28546ae] | 300 | echo Process "first" deps in $node
|
---|
[2140f22] | 301 | lines_to_change=
|
---|
| 302 | {
|
---|
| 303 | while read prio_of_dep build_of_dep id_of_dep; do
|
---|
| 304 | if test $build_of_dep = f; then
|
---|
| 305 | if ! test -f ${id_of_dep}-pass1.dep; then
|
---|
| 306 | cp ${id_of_dep}{,-pass1}.dep;
|
---|
| 307 | fi
|
---|
| 308 | lines_to_change="$lines_to_change $id_of_dep"
|
---|
| 309 | unset lr
|
---|
| 310 | {
|
---|
| 311 | while read p b start; do
|
---|
| 312 | seen=" "
|
---|
| 313 | if path_to ${start}.dep ${node%.dep} $p; then
|
---|
| 314 | lr="$lr $start"
|
---|
| 315 | fi
|
---|
| 316 | done
|
---|
| 317 | } < ${id_of_dep}-pass1.dep
|
---|
| 318 | for p in $lr; do
|
---|
| 319 | sed "/\ $p\$/d" -i ${id_of_dep}-pass1.dep
|
---|
| 320 | done
|
---|
| 321 | fi
|
---|
| 322 | done
|
---|
| 323 | } <$node
|
---|
| 324 | for id_of_dep in $lines_to_change; do
|
---|
[d80302b] | 325 | sed "/\ $id_of_dep\$/"'{s/[[:digit:]] f /1 b /;s/$/-pass1/}' -i $node
|
---|
[2140f22] | 326 | done
|
---|
| 327 | done
|
---|
| 328 | } # End clean_subgraph
|
---|
| 329 |
|
---|
| 330 | #----------------------------#
|
---|
| 331 | generate_dependency_tree() { #
|
---|
| 332 | #----------------------------#
|
---|
| 333 | : <<inline_doc
|
---|
| 334 | function: Create a subtree of the dependency tree
|
---|
| 335 | (recursive function)
|
---|
| 336 | input vars: $1 : file with a list of targets (child nodes)
|
---|
| 337 | the first line of the file is the link
|
---|
| 338 | $2 : priority (1=req, 2=rec, 3=opt)
|
---|
[e4b1293] | 339 | returns: 0 if the tree has been successfully created
|
---|
| 340 | 1 if we are backing up to the parent of a circular dep
|
---|
[2140f22] | 341 | and there are only required deps in the cycle
|
---|
| 342 | 2 if we are backing up to the parent of a circular dep
|
---|
| 343 | and there are recommended deps and no optional deps in the
|
---|
| 344 | cycle
|
---|
| 345 | 3 if we are backing up to the parent of a circular dep
|
---|
| 346 | and there are optiional deps in the cycle
|
---|
| 347 | modifies: vars: ParentNode is set when return is not 0
|
---|
[e4b1293] | 348 | output: files: for each <pkg> with dependencies in $1,
|
---|
[2140f22] | 349 | a file <pkg>.tree and its dependencies
|
---|
[3c10176] | 350 | on error: nothing
|
---|
| 351 | on success: nothing
|
---|
| 352 | inline_doc
|
---|
| 353 |
|
---|
[2140f22] | 354 | local depFile=$1
|
---|
[7dc8595] | 355 | local priority=$2
|
---|
[e576789] | 356 | local -a rootlink
|
---|
[7dc8595] | 357 | local -a priolink
|
---|
[e576789] | 358 | local -a otherlink
|
---|
| 359 | local -i depth
|
---|
| 360 | local -i count=0
|
---|
| 361 | local id_of_dep
|
---|
[2140f22] | 362 | local build_of_dep
|
---|
| 363 | local prio_of_dep
|
---|
[e576789] | 364 | local parent
|
---|
| 365 | local lines_to_remove=
|
---|
| 366 | local srootlink
|
---|
[7dc8595] | 367 | local priostring
|
---|
| 368 | local dpriostring
|
---|
| 369 | local i
|
---|
[e576789] | 370 |
|
---|
| 371 | {
|
---|
[2140f22] | 372 | read -a rootlink
|
---|
[e576789] | 373 | depth=${#rootlink[*]}
|
---|
[2140f22] | 374 | read -a priolink
|
---|
[e576789] | 375 | srootlink="${rootlink[*]} "
|
---|
[7dc8595] | 376 | case $priority in
|
---|
| 377 | 1) priostring=required ;;
|
---|
| 378 | 2) priostring=recommended ;;
|
---|
| 379 | 3) priostring=optional ;;
|
---|
| 380 | esac
|
---|
[2140f22] | 381 | # start of depFile
|
---|
| 382 | echo -en "\nNode: $depth${spaceSTR:0:$depth}${RED}${depFile%.tree}${OFF} $priostring"
|
---|
[e576789] | 383 |
|
---|
[2140f22] | 384 | while read prio_of_dep build_of_dep id_of_dep; do
|
---|
| 385 | case $prio_of_dep in
|
---|
[7dc8595] | 386 | 1) dpriostring=required ;;
|
---|
| 387 | 2) dpriostring=recommended ;;
|
---|
| 388 | 3) dpriostring=optional ;;
|
---|
[2140f22] | 389 | esac
|
---|
[e576789] | 390 | # count entries in file
|
---|
| 391 | (( count++ ))
|
---|
| 392 | # Has this entry already been seen?
|
---|
[2140f22] | 393 | if [ -f ${id_of_dep}.tree ]; then # found ${id_of_dep}.tree already in tree
|
---|
| 394 | otherlink=($(head -n 1 ${id_of_dep}.tree))
|
---|
[2e1c1c3] | 395 | if [ -z "${otherlink[*]}" ]
|
---|
[2140f22] | 396 | then echo otherlink empty for $id_of_dep.tree
|
---|
[e576789] | 397 | echo This should not happen, but happens to happen...
|
---|
| 398 | exit 1
|
---|
| 399 | fi
|
---|
[2140f22] | 400 | #Do not use "${rootlink[*]}" =~ "${otherlink[*]}": case rootlink=(1 11)
|
---|
[e576789] | 401 | # and otherlink=(1 1)
|
---|
[e4b1293] | 402 | if [[ ${srootlink#"${otherlink[*]} "} != ${srootlink} ]]; then # cir. dep
|
---|
[7dc8595] | 403 | echo -en "\nCirc: $((depth+1))${spaceSTR:0:$((depth+1))}${YELLOW}${id_of_dep}${OFF} $dpriostring"
|
---|
[2140f22] | 404 | # Find lowest priority in branch from parent to depFile:
|
---|
[7dc8595] | 405 | p2=0
|
---|
| 406 | for (( i=${#otherlink[*]}; i < $depth ; i++ )) ; do
|
---|
| 407 | if (( ${priolink[i]} > $p2 )); then p2=${priolink[i]}; fi
|
---|
| 408 | done
|
---|
| 409 | if (( $prio_of_dep >= $p2 )); then # prune
|
---|
[e4b1293] | 410 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
[2140f22] | 411 | sed -i "/$id_of_dep/d" ${depFile/.tree/.dep}
|
---|
| 412 | else # find and set parent, then return lowest priority
|
---|
| 413 | # The parent has the same link without the last entry.
|
---|
| 414 | # We do not need otherlink anymore so just destroy the last element
|
---|
| 415 | unset otherlink[-1]
|
---|
| 416 | parentNode=$(grep ^"${otherlink[*]}"\$ -l *)
|
---|
| 417 | return $p2
|
---|
[e4b1293] | 418 | fi
|
---|
[2140f22] | 419 | else # not circular: prune tree (but not .dep, since it may happen that
|
---|
[7ac11f0] | 420 | # the tree is destroyed and rebuilt in another order)
|
---|
[e576789] | 421 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
[e4b1293] | 422 | fi # circular or not
|
---|
| 423 | continue # this dependency has already been seen, and the associated
|
---|
| 424 | # subtree computed. We are done
|
---|
| 425 | fi # Has this entry already been seen?
|
---|
[2140f22] | 426 | # So, this entry has not already been seen. Let's build the corresponding
|
---|
| 427 | # subtree. First check there is a subtree...
|
---|
| 428 | # Use -s, because it may happen that after removing lines, .dep exists
|
---|
[d7c07e0] | 429 | # but is empty.
|
---|
[2140f22] | 430 | if [[ -s ${id_of_dep}.dep ]]; then # this dependency has dependencies
|
---|
| 431 | sed "1i${rootlink[*]} $count\\
|
---|
| 432 | ${priolink[*]} $prio_of_dep" < ${id_of_dep}.dep \
|
---|
| 433 | > ${id_of_dep}.tree # add link and priolink
|
---|
| 434 | generate_dependency_tree ${id_of_dep}.tree $prio_of_dep
|
---|
[e576789] | 435 | # Test return value, in case we exchange dependencies
|
---|
[2140f22] | 436 | p2=$?
|
---|
| 437 | case $p2 in
|
---|
| 438 | 0) # Normal return
|
---|
| 439 | ;;
|
---|
| 440 | $prio_of_dep) # we remove this dep, but since it may become unreachable,
|
---|
| 441 | # move it to be built later (as a dep of parent).
|
---|
| 442 | tree_erase ${id_of_dep}.tree
|
---|
| 443 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
| 444 | sed -i "/${id_of_dep}/d" ${depFile/.tree/.dep}
|
---|
| 445 | echo "$prio_of_dep b $id_of_dep" >> $parentNode
|
---|
| 446 | # must be added to .dep in case parentNode is destroyed when erasing
|
---|
| 447 | # the tree
|
---|
| 448 | echo "$prio_of_dep b $id_of_dep" >> ${parentNode/.tree/.dep}
|
---|
| 449 | continue
|
---|
| 450 | ;;
|
---|
| 451 | *) # We are backing up
|
---|
| 452 | return $p2
|
---|
| 453 | ;;
|
---|
| 454 | esac
|
---|
| 455 | else # id_of_dep has no dependencies, just record the link in a file
|
---|
| 456 | # and print
|
---|
| 457 | echo "${rootlink[*]} $count" > ${id_of_dep}.tree
|
---|
| 458 | echo -en "\nLeaf: $(($depth+1))${spaceSTR:0:$(($depth+1))}${CYAN}${id_of_dep}${OFF} $dpriostring"
|
---|
| 459 | fi
|
---|
[e576789] | 460 | done
|
---|
[2140f22] | 461 | echo -en "\n End: $depth${spaceSTR:0:$depth}${GREEN}${depFile%.tree}${OFF}"
|
---|
| 462 | } <$depFile
|
---|
[e576789] | 463 | # It may happen that a file is created with several times
|
---|
| 464 | # the same line. Normally, all those lines but one
|
---|
| 465 | # would be flagged to be removed (or all of them if
|
---|
[e4b1293] | 466 | # the dependency appeared before). A simple sed /$line/d
|
---|
[e576789] | 467 | # destroys all the lines. We should instead remove
|
---|
| 468 | # only one for each appearance of it in lines_to_remove.
|
---|
[e4b1293] | 469 | # so first get the position of last line and then delete
|
---|
[e576789] | 470 | # that line
|
---|
| 471 | for line in $lines_to_remove
|
---|
[2140f22] | 472 | do lineno=$(sed -n /^[[:digit:]]\ b\ $line\$/= $depFile | tail -n1)
|
---|
| 473 | sed -i ${lineno}d $depFile
|
---|
[e576789] | 474 | done
|
---|
| 475 | return 0
|
---|
| 476 | }
|
---|
[3c10176] | 477 |
|
---|
[2140f22] | 478 |
|
---|
[e576789] | 479 | #---------------#
|
---|
| 480 | tree_browse() { #
|
---|
| 481 | #---------------#
|
---|
| 482 | local file=$1
|
---|
| 483 | local f
|
---|
| 484 |
|
---|
| 485 | #echo file=$file
|
---|
[7dc8595] | 486 | for f in $(grep '[^0-9 ]' $file | sed 's/.* //'); do
|
---|
[e576789] | 487 | # echo f=$f
|
---|
[2140f22] | 488 | if grep -q '[^0-9 ]' ${f}.tree ; then
|
---|
| 489 | tree_browse ${f}.tree
|
---|
[3c10176] | 490 | fi
|
---|
[e576789] | 491 | echo $f
|
---|
| 492 | done
|
---|
| 493 | }
|
---|
[3c10176] | 494 |
|
---|
[e576789] | 495 | #--------------#
|
---|
| 496 | tree_erase() { #
|
---|
| 497 | #--------------#
|
---|
| 498 | local file=$1
|
---|
| 499 | local f
|
---|
[6c8095a] | 500 | local rootlink
|
---|
[f37d08b] | 501 | local rootlink2
|
---|
[e576789] | 502 |
|
---|
| 503 | #echo file=$file
|
---|
[6c8095a] | 504 | rootlink="$(head -n1 $file) "
|
---|
[7dc8595] | 505 | for f in $(grep '[^0-9 ]' $file | sed 's/.* //'); do
|
---|
[2140f22] | 506 | if [ -f ${f}.tree ]; then
|
---|
| 507 | rootlink2="$(head -n1 ${f}.tree) "
|
---|
[6c8095a] | 508 | # We want two things:
|
---|
| 509 | # i) do not erase the file if it is in another branch
|
---|
| 510 | # ii) do not erase the file if there is a circular dependency
|
---|
| 511 | # for case i), we test that rootlink is contained in rootlink2
|
---|
| 512 | # for case ii), we test that rootlink2 is not contained in
|
---|
| 513 | # rootlink.
|
---|
[f37d08b] | 514 | # See comment above about srootlink
|
---|
[6c8095a] | 515 | if [[ ${rootlink2#${rootlink}} != ${rootlink2} &&
|
---|
| 516 | ${rootlink#${rootlink2}} == ${rootlink} ]] ; then
|
---|
[2140f22] | 517 | tree_erase ${f}.tree
|
---|
[e576789] | 518 | fi
|
---|
| 519 | fi
|
---|
| 520 | done
|
---|
| 521 | rm -f $file
|
---|
[3c10176] | 522 | }
|
---|