source: BLFS/libs/func_dependencies@ 7f9fa78

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

Typos and minor fixes

  • Property mode set to 100644
File size: 8.3 KB
Line 
1#!/bin/bash
2#
3# $Id$
4#
5
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# 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*. #
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:
26# A string of spaces for indenting:
27declare -a spaceSTR=" "
28declare -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
35# put dependency_n as a dependency of parent and build
36# the subtree from there. In this case, the triplet
37# above shall contain (parent dependency_0 dependency_n)
38
39#----------------------------#
40generate_dependency_tree() { #
41#----------------------------#
42: <<inline_doc
43 function: Create a subtree of the dependency tree
44 (recursive function)
45 input vars: $1 : file with a list of targets (child nodes)
46 the first line of the file is the link
47 externals: vars: BLFS_XML
48 DEP_LEVEL
49 returns: 0 if the tree has been successfully created
50 1 if we are backing up to the parent of a circular dep
51 modifies: vars: exchange_triplet contains the triplet when return is 1
52 output: files: for each <pkg> with dependencies in $1,
53 a file <pkg>.dep and its dependencies
54 on error: nothing
55 on success: nothing
56inline_doc
57
58local DepFile=$1
59local -a rootlink
60local -a otherlink
61local -i depth
62local -i count=0
63local id_of_dep
64local parent
65local lines_to_remove=
66local srootlink
67local dep_level
68
69{
70# We use fd number 6 for input from DepFile, because we need 0 for user input
71read -u6 -a rootlink
72depth=${#rootlink[*]}
73dep_level=$DEP_LEVEL
74# For now, process only optional deps for the root packages.
75if (( $DEP_LEVEL > 2 )) && (( $depth > 1 )); then dep_level=2; fi
76srootlink="${rootlink[*]} "
77# start of Depfile
78echo -en "\nNode: $depth${spaceSTR:0:$depth}${RED}$DepFile${OFF}"
79
80while 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)
93 if [[ ${srootlink#"${otherlink[*]} "} != ${srootlink} ]]; then # cir. dep
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 \
102of ${BOLD}${parent}${OFF}"
103 echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${DepFile%.dep}${OFF} is a dependency \
104of ${BOLD}${id_of_dep}${OFF}"
105 echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${id_of_dep}${OFF} is a dependency \
106of ${BOLD}${DepFile%.dep}${OFF}"
107# we propose exchange always
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:
111# set triplet and return 1
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
117 else # not circular: prune
118 lines_to_remove="$lines_to_remove $id_of_dep"
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.
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
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
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
142 if [[ ${exchange_triplet} == ${DepFile%.dep} ]] # we are the parent
143 then tree_erase ${id_of_dep}.dep
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.
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]}
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
154# echo backing up to ${exchange_triplet} at ${DepFile%.dep}
155 return 1
156 fi
157 ;;
158 esac
159 else # id_of_dep has no dependencies, just record the link in a file
160 # and print
161 echo "${rootlink[*]} $count" > ${id_of_dep}.dep
162 echo -en "\nLeaf: $(($depth+1))${spaceSTR:0:$(($depth+1))}${CYAN}${id_of_dep}${OFF}"
163 fi
164 done
165done
166echo -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
171# the dependency appeared before). A simple sed /$line/d
172# destroys all the lines. We should instead remove
173# only one for each appearance of it in lines_to_remove.
174# so first get the position of last line and then delete
175# that line
176for line in $lines_to_remove
177 do lineno=$(sed -n /^$line\$/= $DepFile | tail -n1)
178 sed -i ${lineno}d $DepFile
179done
180return 0
181}
182
183#---------------#
184tree_browse() { #
185#---------------#
186local file=$1
187local f
188
189#echo file=$file
190for 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
194 fi
195 echo $f
196done
197}
198
199#--------------#
200tree_erase() { #
201#--------------#
202local file=$1
203local f
204local -a rootlink
205local -a rootlink2
206
207#echo file=$file
208rootlink=($(head -n1 $file))
209for 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
217done
218rm -f $file
219}
Note: See TracBrowser for help on using the repository browser.