[3c10176] | 1 | #!/bin/bash
|
---|
| 2 | #
|
---|
| 3 | # $Id$
|
---|
| 4 | #
|
---|
| 5 |
|
---|
[e4b1293] | 6 | #---------------------------------------------------------------------------#
|
---|
| 7 | # This is a set of (recursive) functions for manipulating a dependency #
|
---|
| 8 | # tree. Everything would be "simple" without circular dependencies. We #
|
---|
| 9 | # would just have to build the tree using the packages.xml file, and to #
|
---|
| 10 | # a function for browsing it. But we need to be able to detect circular #
|
---|
| 11 | # dependencies and to possibly change the tree depending on the user #
|
---|
| 12 | # decision. This is why we keep with each node a record of the path from #
|
---|
| 13 | # the root to the node, which we call *link*. #
|
---|
| 14 | # Layout of the tree: #
|
---|
| 15 | # Each node is a file <nodeName>.dep, which contains the names of the #
|
---|
| 16 | # child nodes, one per line, except the first line is the *link*: #
|
---|
| 17 | # the link is a series of numbers (n1 n2 ... nN), describing the path from #
|
---|
| 18 | # the root to <nodeName>: n1 is the position of the first node of the path #
|
---|
| 19 | # in root.dep, n2 is the position of the second node of the path in #
|
---|
| 20 | # <node1>.dep and so on. The link is not needed for normal tree operations #
|
---|
| 21 | # (building a subtree or browsing the tree), but it allows to #
|
---|
| 22 | # check whether a dependency is circular, and to find its parent. #
|
---|
| 23 | #---------------------------------------------------------------------------#
|
---|
| 24 |
|
---|
| 25 | # Global variables:
|
---|
[e576789] | 26 | # A string of spaces for indenting:
|
---|
[3c10176] | 27 | declare -a spaceSTR=" "
|
---|
[e576789] | 28 | declare -a exchange_triplet
|
---|
| 29 |
|
---|
| 30 | # In case we find a cirdular dependency, it has the form :
|
---|
| 31 | # parent->dependency_0->...->dependency_n->dependency_0
|
---|
| 32 | # If we want to build dependency_n before dependency_0,
|
---|
| 33 | # no problem: we just prune the tree at dependency_n.
|
---|
| 34 | # If we want to build first dependency_0, we need to
|
---|
[e4b1293] | 35 | # put dependency_n as a dependency of parent and build
|
---|
| 36 | # the subtree from there. In this case, the triplet
|
---|
[e576789] | 37 | # above shall contain (parent dependency_0 dependency_n)
|
---|
[3c10176] | 38 |
|
---|
| 39 | #----------------------------#
|
---|
| 40 | generate_dependency_tree() { #
|
---|
| 41 | #----------------------------#
|
---|
| 42 | : <<inline_doc
|
---|
[e576789] | 43 | function: Create a subtree of the dependency tree
|
---|
| 44 | (recursive function)
|
---|
| 45 | input vars: $1 : file with a list of targets
|
---|
[e4b1293] | 46 | the first line of the file is the link
|
---|
[e576789] | 47 | externals: vars: BLFS_XML
|
---|
[3c10176] | 48 | DEP_LEVEL
|
---|
[e4b1293] | 49 | modifies: vars: exchange_triplet cointains the triplet when return is 1
|
---|
| 50 | returns: 0 if the tree has been successfully created
|
---|
| 51 | 1 if we are backing up to the parent of a circular dep
|
---|
| 52 | output: files: for each <pkg> with dependencies in $1,
|
---|
| 53 | a file <pkg>.dep and its dependencies
|
---|
[3c10176] | 54 | on error: nothing
|
---|
| 55 | on success: nothing
|
---|
| 56 | inline_doc
|
---|
| 57 |
|
---|
[e576789] | 58 | local DepFile=$1
|
---|
| 59 | local -a rootlink
|
---|
| 60 | local -a otherlink
|
---|
| 61 | local -i depth
|
---|
| 62 | local -i count=0
|
---|
| 63 | local id_of_dep
|
---|
| 64 | local parent
|
---|
| 65 | local lines_to_remove=
|
---|
| 66 | local srootlink
|
---|
| 67 | local dep_level
|
---|
| 68 |
|
---|
| 69 | {
|
---|
| 70 | # We use fd number 6 for input from DepFile, because we need 0 for user input
|
---|
| 71 | read -u6 -a rootlink
|
---|
| 72 | depth=${#rootlink[*]}
|
---|
| 73 | dep_level=$DEP_LEVEL
|
---|
[e4b1293] | 74 | # For now, process only optional deps for the root packages.
|
---|
[e576789] | 75 | if (( $DEP_LEVEL > 2 )) && (( $depth > 1 )); then dep_level=2; fi
|
---|
| 76 | srootlink="${rootlink[*]} "
|
---|
| 77 | # start of Depfile
|
---|
| 78 | echo -en "\nNode: $depth${spaceSTR:0:$depth}${RED}$DepFile${OFF}"
|
---|
| 79 |
|
---|
| 80 | while read -u6 id_of_dep; do
|
---|
| 81 | # count entries in file
|
---|
| 82 | (( count++ ))
|
---|
| 83 | # Has this entry already been seen?
|
---|
| 84 | if [ -f ${id_of_dep}.dep ]; then # found ${id_of_dep}.dep already in tree
|
---|
| 85 | otherlink=($(head -n 1 ${id_of_dep}.dep))
|
---|
| 86 | if [ -z "${otherlink[*]}" ]
|
---|
| 87 | then echo otherlink empty for $id_of_dep.dep
|
---|
| 88 | echo This should not happen, but happens to happen...
|
---|
| 89 | exit 1
|
---|
| 90 | fi
|
---|
| 91 | # Do not use "${rootlink[*]}" =~ "${otherlink[*]}": case rootlink=(1 11)
|
---|
| 92 | # and otherlink=(1 1)
|
---|
[e4b1293] | 93 | if [[ ${srootlink#"${otherlink[*]} "} != ${srootlink} ]]; then # cir. dep
|
---|
[e576789] | 94 | # First look for the other parent of this dependency.
|
---|
| 95 | # The parent has the same link without the last entry.
|
---|
| 96 | # We do not need otherlink anymore so just destroy the last element
|
---|
| 97 | unset otherlink[${#otherlink[*]}-1]
|
---|
| 98 | parent=$(grep ^"${otherlink[*]}"\$ -l *)
|
---|
| 99 | parent=${parent%.dep}
|
---|
| 100 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BLUE}Circular dependency detected:${OFF}"
|
---|
| 101 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${id_of_dep}${OFF} is a dependency \
|
---|
| 102 | of ${BOLD}${parent}${OFF}"
|
---|
| 103 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${DepFile%.dep}${OFF} is a dependency \
|
---|
| 104 | of ${BOLD}${id_of_dep}${OFF}"
|
---|
| 105 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${id_of_dep}${OFF} is a dependency \
|
---|
| 106 | of ${BOLD}${DepFile%.dep}${OFF}"
|
---|
| 107 | # we propose exchange always
|
---|
[e4b1293] | 108 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}Do you want to build ${id_of_dep} first? yes/no (no):"
|
---|
| 109 | read ANSWER
|
---|
| 110 | if [ x$ANSWER = "xyes" ] ; then # exchange:
|
---|
[e576789] | 111 | # set triplet and return 1
|
---|
[e4b1293] | 112 | exchange_triplet=($parent $id_of_dep ${DepFile%.dep})
|
---|
| 113 | return 1
|
---|
| 114 | else # no exchange: prune
|
---|
| 115 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
| 116 | fi
|
---|
[e576789] | 117 | else # not circular: prune
|
---|
| 118 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
[e4b1293] | 119 | fi # circular or not
|
---|
| 120 | continue # this dependency has already been seen, and the associated
|
---|
| 121 | # subtree computed. We are done
|
---|
| 122 | fi # Has this entry already been seen?
|
---|
| 123 | # So, this entry has not already been seen. Let's build the corresponding
|
---|
| 124 | # subtree. Since decisions about circular deps can lead us to start again
|
---|
| 125 | # dependencies, we restart until the flag is false.
|
---|
[e576789] | 126 | flag=true
|
---|
| 127 | while [ $flag = true ]; do
|
---|
| 128 | flag=false
|
---|
| 129 | xsltproc --stringparam dependencies ${dep_level} \
|
---|
| 130 | --stringparam idofdep $id_of_dep \
|
---|
| 131 | -o ${id_of_dep}.dep \
|
---|
| 132 | ../xsl/dependencies.xsl ../packages.xml
|
---|
| 133 |
|
---|
[e4b1293] | 134 | if [[ -f ${id_of_dep}.dep ]]; then # this dependency has dependencies
|
---|
| 135 | sed -i "1i${rootlink[*]} $count" ${id_of_dep}.dep # add the link
|
---|
[e576789] | 136 | generate_dependency_tree ${id_of_dep}.dep
|
---|
| 137 | # Test return value, in case we exchange dependencies
|
---|
| 138 | case $? in
|
---|
| 139 | 0) # Normal return
|
---|
| 140 | ;;
|
---|
| 141 | 1) # We are backing up to parent
|
---|
[e4b1293] | 142 | if [[ ${exchange_triplet} == ${DepFile%.dep} ]] # we are the parent
|
---|
[e576789] | 143 | then tree_erase ${id_of_dep}.dep
|
---|
[e4b1293] | 144 | # We want that our direct dep be ${exchange_triplet[2]} and that id_of_dep
|
---|
| 145 | # be pulled in as an indirect dep, so exchange.
|
---|
[e576789] | 146 | # Just doing a sed -i "s@${id_of_dep}@${exchange_triplet[2]}@" $DepFile
|
---|
| 147 | # is not good if $DepFile contains several times the same line
|
---|
| 148 | # so first find the first line and then sed
|
---|
| 149 | lineno=$(sed -n /${id_of_dep}/= $DepFile | head -n1)
|
---|
| 150 | sed -i "${lineno}s@${id_of_dep}@${exchange_triplet[2]}@" $DepFile
|
---|
| 151 | id_of_dep=${exchange_triplet[2]}
|
---|
[e4b1293] | 152 | flag=true # we have to regenerate the tree for the new dependency
|
---|
| 153 | else # we are not the parent. let's just back up one step
|
---|
[e576789] | 154 | # echo backing up to ${exchange_triplet} at ${DepFile%.dep}
|
---|
| 155 | return 1
|
---|
| 156 | fi
|
---|
| 157 | ;;
|
---|
[3c10176] | 158 | esac
|
---|
[e4b1293] | 159 | else # id_of_dep has no dependencies, just record the link in a file
|
---|
| 160 | # and print
|
---|
[e576789] | 161 | echo "${rootlink[*]} $count" > ${id_of_dep}.dep
|
---|
[e4b1293] | 162 | echo -en "\nLeaf: $(($depth+1))${spaceSTR:0:$(($depth+1))}${CYAN}${id_of_dep}${OFF}"
|
---|
[3c10176] | 163 | fi
|
---|
| 164 | done
|
---|
[e576789] | 165 | done
|
---|
| 166 | echo -en "\n End: $depth${spaceSTR:0:$depth}${GREEN}$DepFile${OFF}"
|
---|
| 167 | } 6<$DepFile
|
---|
| 168 | # It may happen that a file is created with several times
|
---|
| 169 | # the same line. Normally, all those lines but one
|
---|
| 170 | # would be flagged to be removed (or all of them if
|
---|
[e4b1293] | 171 | # the dependency appeared before). A simple sed /$line/d
|
---|
[e576789] | 172 | # destroys all the lines. We should instead remove
|
---|
| 173 | # only one for each appearance of it in lines_to_remove.
|
---|
[e4b1293] | 174 | # so first get the position of last line and then delete
|
---|
[e576789] | 175 | # that line
|
---|
| 176 | for line in $lines_to_remove
|
---|
[bd07ef5] | 177 | do lineno=$(sed -n /^$line\$/= $DepFile | tail -n1)
|
---|
[e576789] | 178 | sed -i ${lineno}d $DepFile
|
---|
| 179 | done
|
---|
| 180 | return 0
|
---|
| 181 | }
|
---|
[3c10176] | 182 |
|
---|
[e576789] | 183 | #---------------#
|
---|
| 184 | tree_browse() { #
|
---|
| 185 | #---------------#
|
---|
| 186 | local file=$1
|
---|
| 187 | local f
|
---|
| 188 |
|
---|
| 189 | #echo file=$file
|
---|
| 190 | for f in $(grep '[^0-9 ]' $file); do
|
---|
| 191 | # echo f=$f
|
---|
| 192 | if grep -q '[^0-9 ]' ${f}.dep ; then
|
---|
| 193 | tree_browse ${f}.dep
|
---|
[3c10176] | 194 | fi
|
---|
[e576789] | 195 | echo $f
|
---|
| 196 | done
|
---|
| 197 | }
|
---|
[3c10176] | 198 |
|
---|
[e576789] | 199 | #--------------#
|
---|
| 200 | tree_erase() { #
|
---|
| 201 | #--------------#
|
---|
| 202 | local file=$1
|
---|
| 203 | local f
|
---|
| 204 | local -a rootlink
|
---|
| 205 | local -a rootlink2
|
---|
| 206 |
|
---|
| 207 | #echo file=$file
|
---|
| 208 | rootlink=($(head -n1 $file))
|
---|
| 209 | for f in $(grep '[^0-9 ]' $file); do
|
---|
| 210 | # echo " f"=$f
|
---|
| 211 | if [ -f ${f}.dep ]; then
|
---|
| 212 | rootlink2=($(head -n1 ${f}.dep))
|
---|
| 213 | if [[ "${rootlink2[*]}" =~ "${rootlink[*]}" ]] ; then
|
---|
| 214 | tree_erase ${f}.dep
|
---|
| 215 | fi
|
---|
| 216 | fi
|
---|
| 217 | done
|
---|
| 218 | rm -f $file
|
---|
[3c10176] | 219 | }
|
---|