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 #
|
---|
12 | # priorities. This is why we keep with each node a record of the path #
|
---|
13 | # from the root to the node, which we call *link* and a record of the #
|
---|
14 | # successive priorities which we call *priolink*. #
|
---|
15 | # #
|
---|
16 | # Layout of the tree: #
|
---|
17 | # #
|
---|
18 | # A node of the tree is represented by a file <nodeName>.dep. We keep all #
|
---|
19 | # those files in the same directory. The root node file is "root.dep". #
|
---|
20 | # Files <nodeName>.dep have the following layout: #
|
---|
21 | # - the first line is the link: the link is an array of numbers #
|
---|
22 | # (n1 n2 ... nN), describing the path from the root to <nodeName>: n1 #
|
---|
23 | # is the position of the first node of the path in root.dep, n2 is the #
|
---|
24 | # position of the second node of the path in <node1>.dep and so on. The #
|
---|
25 | # link is not needed for normal tree operations (building a subtree or #
|
---|
26 | # browsing the tree), but it allows to check whether a dependency is #
|
---|
27 | # circular, and to find its parent. #
|
---|
28 | # - the second line is an array of priorities (p1 p2 ... pN), giving the #
|
---|
29 | # priority (1=required, 2=recommended, 3=optional) of each dependency #
|
---|
30 | # in the link. #
|
---|
31 | # - each subsequent line is of the form "p <depname>", where p is the #
|
---|
32 | # priority as above, and <depname> is the name of the dependency. The #
|
---|
33 | # position which is recorded in the link is the number of the line #
|
---|
34 | # minus 2. #
|
---|
35 | # #
|
---|
36 | # Circular dependencies: #
|
---|
37 | # #
|
---|
38 | # In case we find a cirdular dependency, it has the form : #
|
---|
39 | # parent->dependency_0->...->dependency_n->dependency_0 #
|
---|
40 | # If we want to build dependency_n before dependency_0, no problem: #
|
---|
41 | # we just prune the tree at dependency_n. If we want to build first #
|
---|
42 | # dependency_0, we need to put dependency_n as a dependency of parent, #
|
---|
43 | # then erase and rebuild the subtree from there. Now, we may have met #
|
---|
44 | # another circular dependency in the subtree, and erasing the tree makes #
|
---|
45 | # us forget the decision which was made. So, after first generating the #
|
---|
46 | # list of dependencies from packages.xml, we keep the generated list in #
|
---|
47 | # a file <nodeName>.odep, which we modify according to the decision which #
|
---|
48 | # was made. #
|
---|
49 | #---------------------------------------------------------------------------#
|
---|
50 |
|
---|
51 | # Global variables:
|
---|
52 | # A string of spaces for indenting:
|
---|
53 | declare -a spaceSTR=" "
|
---|
54 | # When we are backing up from a circular dependency, `exchange_triplet'
|
---|
55 | # shall contain (parent dependency_0 dependency_n):
|
---|
56 | declare -a exchange_triplet
|
---|
57 |
|
---|
58 | #----------------------------#
|
---|
59 | generate_dependency_tree() { #
|
---|
60 | #----------------------------#
|
---|
61 | : <<inline_doc
|
---|
62 | function: Create a subtree of the dependency tree
|
---|
63 | (recursive function)
|
---|
64 | input vars: $1 : file with a list of targets (child nodes)
|
---|
65 | the first line of the file is the link
|
---|
66 | $2 : priority (1=req, 2=rec, 3=opt)
|
---|
67 | externals: vars: DEP_LEVEL contains 1 if we want to build the
|
---|
68 | tree only for required dependencies,
|
---|
69 | 2 if we want also recommended ones,
|
---|
70 | and 3 if we want optional ones too.
|
---|
71 | MAIL_SERVER contains the name of the MTA we want to use.
|
---|
72 | files: ../xsl/dependencies.xsl: stylesheet for creating the
|
---|
73 | .dep files
|
---|
74 | ../packages.xml: Files containing packages id
|
---|
75 | and dependencies
|
---|
76 | returns: 0 if the tree has been successfully created
|
---|
77 | 1 if we are backing up to the parent of a circular dep
|
---|
78 | modifies: vars: exchange_triplet contains the triplet when return is 1
|
---|
79 | output: files: for each <pkg> with dependencies in $1,
|
---|
80 | a file <pkg>.dep and its dependencies
|
---|
81 | on error: nothing
|
---|
82 | on success: nothing
|
---|
83 | inline_doc
|
---|
84 |
|
---|
85 | local DepFile=$1
|
---|
86 | local priority=$2
|
---|
87 | local -a rootlink
|
---|
88 | local -a priolink
|
---|
89 | local -a otherlink
|
---|
90 | local -i depth
|
---|
91 | local -i count=0
|
---|
92 | local id_of_dep
|
---|
93 | local parent
|
---|
94 | local lines_to_remove=
|
---|
95 | local srootlink
|
---|
96 | local dep_level
|
---|
97 | local priostring
|
---|
98 | local dpriostring
|
---|
99 | local i
|
---|
100 |
|
---|
101 | {
|
---|
102 | # We use fd number 6 for input from DepFile, because we need 0 for user input
|
---|
103 | read -u6 -a rootlink
|
---|
104 | depth=${#rootlink[*]}
|
---|
105 | read -u6 -a priolink
|
---|
106 | dep_level=$DEP_LEVEL
|
---|
107 | # For now, process optional deps only for the root packages.
|
---|
108 | if (( $DEP_LEVEL > 2 )) && (( $depth > 1 )); then dep_level=2; fi
|
---|
109 | srootlink="${rootlink[*]} "
|
---|
110 | case $priority in
|
---|
111 | 1) priostring=required ;;
|
---|
112 | 2) priostring=recommended ;;
|
---|
113 | 3) priostring=optional ;;
|
---|
114 | esac
|
---|
115 | # start of DepFile
|
---|
116 | echo -en "\nNode: $depth${spaceSTR:0:$depth}${RED}$DepFile${OFF} $priostring"
|
---|
117 |
|
---|
118 | while read -u6 prio_of_dep id_of_dep; do
|
---|
119 | case $prio_of_dep in
|
---|
120 | 1) dpriostring=required ;;
|
---|
121 | 2) dpriostring=recommended ;;
|
---|
122 | 3) dpriostring=optional ;;
|
---|
123 | esac
|
---|
124 | # count entries in file
|
---|
125 | (( count++ ))
|
---|
126 | # Has this entry already been seen?
|
---|
127 | if [ -f ${id_of_dep}.dep ]; then # found ${id_of_dep}.dep already in tree
|
---|
128 | otherlink=($(head -n 1 ${id_of_dep}.dep))
|
---|
129 | if [ -z "${otherlink[*]}" ]
|
---|
130 | then echo otherlink empty for $id_of_dep.dep
|
---|
131 | echo This should not happen, but happens to happen...
|
---|
132 | exit 1
|
---|
133 | fi
|
---|
134 | # Do not use "${rootlink[*]}" =~ "${otherlink[*]}": case rootlink=(1 11)
|
---|
135 | # and otherlink=(1 1)
|
---|
136 | if [[ ${srootlink#"${otherlink[*]} "} != ${srootlink} ]]; then # cir. dep
|
---|
137 | echo -en "\nCirc: $((depth+1))${spaceSTR:0:$((depth+1))}${YELLOW}${id_of_dep}${OFF} $dpriostring"
|
---|
138 | # First look for the other parent of this dependency.
|
---|
139 | # The parent has the same link without the last entry.
|
---|
140 | # We do not need otherlink anymore so just destroy the last element
|
---|
141 | unset otherlink[-1]
|
---|
142 | parent=$(grep ^"${otherlink[*]}"\$ -l *)
|
---|
143 | parent=${parent%.dep}
|
---|
144 | # Find lowest priority in branch from parent to DepFile:
|
---|
145 | p2=0
|
---|
146 | for (( i=${#otherlink[*]}; i < $depth ; i++ )) ; do
|
---|
147 | if (( ${priolink[i]} > $p2 )); then p2=${priolink[i]}; fi
|
---|
148 | done
|
---|
149 | if (( $prio_of_dep >= $p2 )); then # prune
|
---|
150 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
151 | sed -i "/$id_of_dep/d" ${DepFile/.dep/.odep}
|
---|
152 | else #backup with prio priority
|
---|
153 | exchange_triplet=($parent $id_of_dep ${DepFile%.dep})
|
---|
154 | return $priority
|
---|
155 | fi
|
---|
156 | else # not circular: prune tree (but not .odep, since it may happen that
|
---|
157 | # the tree is destroyed and rebuilt in another order)
|
---|
158 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
159 | fi # circular or not
|
---|
160 | continue # this dependency has already been seen, and the associated
|
---|
161 | # subtree computed. We are done
|
---|
162 | fi # Has this entry already been seen?
|
---|
163 | # So, this entry has not already been seen. Let's build the corresponding
|
---|
164 | # subtree. Since decisions about circular deps can lead us to start again
|
---|
165 | # dependencies, we restart until the flag is false.
|
---|
166 | flag=true
|
---|
167 | while [ $flag = true ]; do
|
---|
168 | flag=false
|
---|
169 | if [ ! -f "${id_of_dep}.odep" ]; then
|
---|
170 | xsltproc --stringparam dependencies ${dep_level} \
|
---|
171 | --stringparam idofdep $id_of_dep \
|
---|
172 | --stringparam MTA $MAIL_SERVER \
|
---|
173 | -o ${id_of_dep}.odep \
|
---|
174 | ../xsl/dependencies.xsl ../packages.xml
|
---|
175 | fi
|
---|
176 |
|
---|
177 | # Use -s, because it may happen that after removing lines, .odep exists
|
---|
178 | # but is empty.
|
---|
179 | if [[ -s ${id_of_dep}.odep ]]; then # this dependency has dependencies
|
---|
180 | sed "1i${rootlink[*]} $count\\
|
---|
181 | ${priolink[*]} $prio_of_dep" < ${id_of_dep}.odep \
|
---|
182 | > ${id_of_dep}.dep # add link and priolink
|
---|
183 | generate_dependency_tree ${id_of_dep}.dep $prio_of_dep
|
---|
184 | # Test return value, in case we exchange dependencies
|
---|
185 | p2=$?
|
---|
186 | case $p2 in
|
---|
187 | 0) # Normal return
|
---|
188 | ;;
|
---|
189 | [123]) # We are backing up to parent
|
---|
190 | if [[ ${exchange_triplet} == ${DepFile%.dep} ]] ; then
|
---|
191 | # We are the parent!
|
---|
192 | # First, we have to find the parent of our new direct dep, and remove
|
---|
193 | # the new direct dep from the parent.odep:
|
---|
194 | otherlink=($(head -n1 ${exchange_triplet[2]}.dep))
|
---|
195 | unset otherlink[-1]
|
---|
196 | parent=$(grep -l ^"${otherlink[*]}"\$ *.dep)
|
---|
197 | sed -i /[[:digit:]]\ ${exchange_triplet[2]}\$/d ${parent/.dep/.odep}
|
---|
198 | tree_erase ${id_of_dep}.dep
|
---|
199 | # We want that our direct dep be ${exchange_triplet[2]} and that id_of_dep
|
---|
200 | # be pulled in as an indirect dep, so exchange.
|
---|
201 | # Just doing a sed -i "s@${id_of_dep}@${exchange_triplet[2]}@" $DepFile
|
---|
202 | # is not good if $DepFile contains several times the same line
|
---|
203 | # so first find the first line and then sed
|
---|
204 | lineno=$(sed -n /${id_of_dep}/= $DepFile | head -n1)
|
---|
205 | sed -i "${lineno}s@${id_of_dep}@${exchange_triplet[2]}@" $DepFile
|
---|
206 | # Do the same for the odep file:
|
---|
207 | local OdepFile=${DepFile/.dep/.odep}
|
---|
208 | lineno=$(sed -n /${id_of_dep}/= $OdepFile | head -n1)
|
---|
209 | sed -i "${lineno}s@${id_of_dep}@${exchange_triplet[2]}@" $OdepFile
|
---|
210 | id_of_dep=${exchange_triplet[2]}
|
---|
211 | flag=true # we have to regenerate the tree for the new dependency
|
---|
212 | else
|
---|
213 | # We are not the parent. If our priority is greater than p2
|
---|
214 | # we have to change the triplet and return priority, else return current p2.
|
---|
215 | # echo (DEBUG) backing up to ${exchange_triplet} at ${DepFile%.dep}
|
---|
216 | if (( $priority > $p2 )); then
|
---|
217 | exchange_triplet[2]=${DepFile%.dep}
|
---|
218 | return $priority
|
---|
219 | else
|
---|
220 | return $p2
|
---|
221 | fi
|
---|
222 | fi
|
---|
223 | ;;
|
---|
224 | esac
|
---|
225 | else # id_of_dep has no dependencies, just record the link in a file
|
---|
226 | # and print
|
---|
227 | echo "${rootlink[*]} $count" > ${id_of_dep}.dep
|
---|
228 | echo -en "\nLeaf: $(($depth+1))${spaceSTR:0:$(($depth+1))}${CYAN}${id_of_dep}${OFF} $dpriostring"
|
---|
229 | fi
|
---|
230 | done
|
---|
231 | done
|
---|
232 | echo -en "\n End: $depth${spaceSTR:0:$depth}${GREEN}$DepFile${OFF}"
|
---|
233 | } 6<$DepFile
|
---|
234 | # It may happen that a file is created with several times
|
---|
235 | # the same line. Normally, all those lines but one
|
---|
236 | # would be flagged to be removed (or all of them if
|
---|
237 | # the dependency appeared before). A simple sed /$line/d
|
---|
238 | # destroys all the lines. We should instead remove
|
---|
239 | # only one for each appearance of it in lines_to_remove.
|
---|
240 | # so first get the position of last line and then delete
|
---|
241 | # that line
|
---|
242 | for line in $lines_to_remove
|
---|
243 | do lineno=$(sed -n /^[[:digit:]]\ $line\$/= $DepFile | tail -n1)
|
---|
244 | sed -i ${lineno}d $DepFile
|
---|
245 | done
|
---|
246 | return 0
|
---|
247 | }
|
---|
248 |
|
---|
249 | #---------------#
|
---|
250 | tree_browse() { #
|
---|
251 | #---------------#
|
---|
252 | local file=$1
|
---|
253 | local f
|
---|
254 |
|
---|
255 | #echo file=$file
|
---|
256 | for f in $(grep '[^0-9 ]' $file | sed 's/.* //'); do
|
---|
257 | # echo f=$f
|
---|
258 | if grep -q '[^0-9 ]' ${f}.dep ; then
|
---|
259 | tree_browse ${f}.dep
|
---|
260 | fi
|
---|
261 | echo $f
|
---|
262 | done
|
---|
263 | }
|
---|
264 |
|
---|
265 | #--------------#
|
---|
266 | tree_erase() { #
|
---|
267 | #--------------#
|
---|
268 | local file=$1
|
---|
269 | local f
|
---|
270 | local -a rootlink
|
---|
271 | local -a rootlink2
|
---|
272 |
|
---|
273 | #echo file=$file
|
---|
274 | rootlink=($(head -n1 $file))
|
---|
275 | for f in $(grep '[^0-9 ]' $file | sed 's/.* //'); do
|
---|
276 | # echo " f"=$f
|
---|
277 | if [ -f ${f}.dep ]; then
|
---|
278 | rootlink2=($(head -n1 ${f}.dep))
|
---|
279 | if [[ "${rootlink2[*]}" =~ "${rootlink[*]}" ]] ; then
|
---|
280 | tree_erase ${f}.dep
|
---|
281 | fi
|
---|
282 | fi
|
---|
283 | done
|
---|
284 | rm -f $file
|
---|
285 | }
|
---|