source: BLFS/libs/func_dependencies@ d7c07e0

2.4 ablfs-more legacy new_features trunk
Last change on this file since d7c07e0 was d7c07e0, checked in by Pierre Labastie <pierre@…>, 9 years ago

Memorize answers for circular dependencies. Still, we need a way to
automaitcally answer when competition is between for example optional
and recommended dep.

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