2006-07-16 12:29:37 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
|
2015-11-03 18:17:15 +01:00
|
|
|
#---------------------------------------------------------------------------#
|
|
|
|
# This is a set of (recursive) functions for manipulating a dependency #
|
|
|
|
# tree. Everything would be "simple" without circular dependencies. We #
|
|
|
|
# would just have to build the tree using the packages.xml file, and to #
|
2015-11-03 22:53:34 +01:00
|
|
|
# provide a function for browsing it. But we need to be able to detect #
|
2015-11-17 21:22:04 +01:00
|
|
|
# circular dependencies and to possibly change the tree depending on #
|
|
|
|
# priorities. This is why we keep with each node a record of the path #
|
|
|
|
# from the root to the node, which we call *link* and a record of the #
|
|
|
|
# successive priorities which we call *priolink*. #
|
|
|
|
# #
|
2015-11-03 18:17:15 +01:00
|
|
|
# Layout of the tree: #
|
2015-11-17 21:22:04 +01:00
|
|
|
# #
|
|
|
|
# A node of the tree is represented by a file <nodeName>.dep. We keep all #
|
|
|
|
# those files in the same directory. The root node file is "root.dep". #
|
|
|
|
# Files <nodeName>.dep have the following layout: #
|
|
|
|
# - the first line is the link: the link is an array of numbers #
|
|
|
|
# (n1 n2 ... nN), describing the path from the root to <nodeName>: n1 #
|
|
|
|
# is the position of the first node of the path in root.dep, n2 is the #
|
|
|
|
# position of the second node of the path in <node1>.dep and so on. The #
|
|
|
|
# link is not needed for normal tree operations (building a subtree or #
|
|
|
|
# browsing the tree), but it allows to check whether a dependency is #
|
|
|
|
# circular, and to find its parent. #
|
|
|
|
# - the second line is an array of priorities (p1 p2 ... pN), giving the #
|
|
|
|
# priority (1=required, 2=recommended, 3=optional) of each dependency #
|
|
|
|
# in the link. #
|
|
|
|
# - each subsequent line is of the form "p <depname>", where p is the #
|
|
|
|
# priority as above, and <depname> is the name of the dependency. The #
|
|
|
|
# position which is recorded in the link is the number of the line #
|
|
|
|
# minus 2. #
|
|
|
|
# #
|
2015-11-07 12:36:11 +01:00
|
|
|
# Circular dependencies: #
|
2015-11-17 21:22:04 +01:00
|
|
|
# #
|
2015-11-07 12:36:11 +01:00
|
|
|
# In case we find a cirdular dependency, it has the form : #
|
|
|
|
# parent->dependency_0->...->dependency_n->dependency_0 #
|
|
|
|
# If we want to build dependency_n before dependency_0, no problem: #
|
|
|
|
# we just prune the tree at dependency_n. If we want to build first #
|
|
|
|
# dependency_0, we need to put dependency_n as a dependency of parent, #
|
|
|
|
# then erase and rebuild the subtree from there. Now, we may have met #
|
|
|
|
# another circular dependency in the subtree, and erasing the tree makes #
|
|
|
|
# us forget the decision which was made. So, after first generating the #
|
|
|
|
# list of dependencies from packages.xml, we keep the generated list in #
|
|
|
|
# a file <nodeName>.odep, which we modify according to the decision which #
|
|
|
|
# was made. #
|
2015-11-03 18:17:15 +01:00
|
|
|
#---------------------------------------------------------------------------#
|
|
|
|
|
|
|
|
# Global variables:
|
2013-10-29 16:42:03 +01:00
|
|
|
# A string of spaces for indenting:
|
2006-07-16 12:29:37 +02:00
|
|
|
declare -a spaceSTR=" "
|
2015-11-07 12:36:11 +01:00
|
|
|
# When we are backing up from a circular dependency, `exchange_triplet'
|
2015-11-17 21:22:04 +01:00
|
|
|
# shall contain (parent dependency_0 dependency_n):
|
|
|
|
declare -a exchange_triplet
|
2006-07-16 12:29:37 +02:00
|
|
|
|
|
|
|
#----------------------------#
|
|
|
|
generate_dependency_tree() { #
|
|
|
|
#----------------------------#
|
|
|
|
: <<inline_doc
|
2013-10-29 16:42:03 +01:00
|
|
|
function: Create a subtree of the dependency tree
|
|
|
|
(recursive function)
|
2015-11-03 22:53:34 +01:00
|
|
|
input vars: $1 : file with a list of targets (child nodes)
|
2015-11-03 18:17:15 +01:00
|
|
|
the first line of the file is the link
|
2015-11-17 21:22:04 +01:00
|
|
|
$2 : priority (1=req, 2=rec, 3=opt)
|
2015-11-23 15:14:23 +01:00
|
|
|
externals: vars: DEP_LEVEL contains 1 if we want to build the
|
|
|
|
tree only for required dependencies,
|
|
|
|
2 if we want also recommended ones,
|
|
|
|
and 3 if we want optional ones too.
|
|
|
|
MAIL_SERVER contains the name of the MTA we want to use.
|
|
|
|
files: ../xsl/dependencies.xsl: stylesheet for creating the
|
|
|
|
.dep files
|
2015-11-23 15:22:48 +01:00
|
|
|
../packages.xml: File containing packages id
|
2015-11-23 15:14:23 +01:00
|
|
|
and dependencies
|
2015-11-03 18:17:15 +01:00
|
|
|
returns: 0 if the tree has been successfully created
|
|
|
|
1 if we are backing up to the parent of a circular dep
|
2015-11-03 22:53:34 +01:00
|
|
|
modifies: vars: exchange_triplet contains the triplet when return is 1
|
2015-11-03 18:17:15 +01:00
|
|
|
output: files: for each <pkg> with dependencies in $1,
|
|
|
|
a file <pkg>.dep and its dependencies
|
2006-07-16 12:29:37 +02:00
|
|
|
on error: nothing
|
|
|
|
on success: nothing
|
|
|
|
inline_doc
|
|
|
|
|
2013-10-29 16:42:03 +01:00
|
|
|
local DepFile=$1
|
2015-11-17 21:22:04 +01:00
|
|
|
local priority=$2
|
2013-10-29 16:42:03 +01:00
|
|
|
local -a rootlink
|
2015-11-17 21:22:04 +01:00
|
|
|
local -a priolink
|
2013-10-29 16:42:03 +01:00
|
|
|
local -a otherlink
|
|
|
|
local -i depth
|
|
|
|
local -i count=0
|
|
|
|
local id_of_dep
|
|
|
|
local parent
|
|
|
|
local lines_to_remove=
|
|
|
|
local srootlink
|
|
|
|
local dep_level
|
2015-11-17 21:22:04 +01:00
|
|
|
local priostring
|
|
|
|
local dpriostring
|
|
|
|
local i
|
2013-10-29 16:42:03 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
# We use fd number 6 for input from DepFile, because we need 0 for user input
|
|
|
|
read -u6 -a rootlink
|
|
|
|
depth=${#rootlink[*]}
|
2015-11-17 21:22:04 +01:00
|
|
|
read -u6 -a priolink
|
2013-10-29 16:42:03 +01:00
|
|
|
dep_level=$DEP_LEVEL
|
2015-11-04 10:26:58 +01:00
|
|
|
# For now, process optional deps only for the root packages.
|
2013-10-29 16:42:03 +01:00
|
|
|
if (( $DEP_LEVEL > 2 )) && (( $depth > 1 )); then dep_level=2; fi
|
|
|
|
srootlink="${rootlink[*]} "
|
2015-11-17 21:22:04 +01:00
|
|
|
case $priority in
|
|
|
|
1) priostring=required ;;
|
|
|
|
2) priostring=recommended ;;
|
|
|
|
3) priostring=optional ;;
|
2015-11-23 15:22:48 +01:00
|
|
|
4) priostring=external ;;
|
2015-11-17 21:22:04 +01:00
|
|
|
esac
|
2015-11-07 12:36:11 +01:00
|
|
|
# start of DepFile
|
2015-11-17 21:22:04 +01:00
|
|
|
echo -en "\nNode: $depth${spaceSTR:0:$depth}${RED}$DepFile${OFF} $priostring"
|
2013-10-29 16:42:03 +01:00
|
|
|
|
2015-11-17 21:22:04 +01:00
|
|
|
while read -u6 prio_of_dep id_of_dep; do
|
|
|
|
case $prio_of_dep in
|
|
|
|
1) dpriostring=required ;;
|
|
|
|
2) dpriostring=recommended ;;
|
|
|
|
3) dpriostring=optional ;;
|
2015-11-23 15:22:48 +01:00
|
|
|
4) dpriostring=external ;;
|
2015-11-17 21:22:04 +01:00
|
|
|
esac
|
2013-10-29 16:42:03 +01:00
|
|
|
# count entries in file
|
|
|
|
(( count++ ))
|
|
|
|
# Has this entry already been seen?
|
|
|
|
if [ -f ${id_of_dep}.dep ]; then # found ${id_of_dep}.dep already in tree
|
|
|
|
otherlink=($(head -n 1 ${id_of_dep}.dep))
|
2018-01-13 10:08:56 +01:00
|
|
|
if [ -z "${otherlink[*]}" ]
|
2013-10-29 16:42:03 +01:00
|
|
|
then echo otherlink empty for $id_of_dep.dep
|
|
|
|
echo This should not happen, but happens to happen...
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
# Do not use "${rootlink[*]}" =~ "${otherlink[*]}": case rootlink=(1 11)
|
|
|
|
# and otherlink=(1 1)
|
2015-11-03 18:17:15 +01:00
|
|
|
if [[ ${srootlink#"${otherlink[*]} "} != ${srootlink} ]]; then # cir. dep
|
2015-11-17 21:22:04 +01:00
|
|
|
echo -en "\nCirc: $((depth+1))${spaceSTR:0:$((depth+1))}${YELLOW}${id_of_dep}${OFF} $dpriostring"
|
2013-10-29 16:42:03 +01:00
|
|
|
# First look for the other parent of this dependency.
|
|
|
|
# The parent has the same link without the last entry.
|
|
|
|
# We do not need otherlink anymore so just destroy the last element
|
2015-11-17 21:22:04 +01:00
|
|
|
unset otherlink[-1]
|
2013-10-29 16:42:03 +01:00
|
|
|
parent=$(grep ^"${otherlink[*]}"\$ -l *)
|
|
|
|
parent=${parent%.dep}
|
2015-11-17 21:22:04 +01:00
|
|
|
# Find lowest priority in branch from parent to DepFile:
|
|
|
|
p2=0
|
|
|
|
for (( i=${#otherlink[*]}; i < $depth ; i++ )) ; do
|
|
|
|
if (( ${priolink[i]} > $p2 )); then p2=${priolink[i]}; fi
|
|
|
|
done
|
|
|
|
if (( $prio_of_dep >= $p2 )); then # prune
|
2015-11-03 18:17:15 +01:00
|
|
|
lines_to_remove="$lines_to_remove $id_of_dep"
|
2015-11-07 12:36:11 +01:00
|
|
|
sed -i "/$id_of_dep/d" ${DepFile/.dep/.odep}
|
2015-11-17 21:22:04 +01:00
|
|
|
else #backup with prio priority
|
|
|
|
exchange_triplet=($parent $id_of_dep ${DepFile%.dep})
|
|
|
|
return $priority
|
2015-11-03 18:17:15 +01:00
|
|
|
fi
|
2015-11-17 21:22:04 +01:00
|
|
|
else # not circular: prune tree (but not .odep, since it may happen that
|
2015-11-23 15:14:23 +01:00
|
|
|
# the tree is destroyed and rebuilt in another order)
|
2013-10-29 16:42:03 +01:00
|
|
|
lines_to_remove="$lines_to_remove $id_of_dep"
|
2015-11-03 18:17:15 +01:00
|
|
|
fi # circular or not
|
|
|
|
continue # this dependency has already been seen, and the associated
|
|
|
|
# subtree computed. We are done
|
|
|
|
fi # Has this entry already been seen?
|
2015-11-23 15:22:48 +01:00
|
|
|
# So, this entry has not already been seen.
|
|
|
|
# If this is an external dep, just display it and go to next dep:
|
|
|
|
if [ "$prio_of_dep" -eq 4 ]; then
|
|
|
|
echo "${rootlink[*]} $count" > ${id_of_dep}.dep
|
|
|
|
echo -en "\nLeaf: $(($depth+1))${spaceSTR:0:$(($depth+1))}${CYAN}${id_of_dep}${OFF} $dpriostring"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
# Otherwise, let's build the corresponding
|
2015-11-03 18:17:15 +01:00
|
|
|
# subtree. Since decisions about circular deps can lead us to start again
|
|
|
|
# dependencies, we restart until the flag is false.
|
2013-10-29 16:42:03 +01:00
|
|
|
flag=true
|
|
|
|
while [ $flag = true ]; do
|
|
|
|
flag=false
|
2015-11-07 12:36:11 +01:00
|
|
|
if [ ! -f "${id_of_dep}.odep" ]; then
|
|
|
|
xsltproc --stringparam dependencies ${dep_level} \
|
2013-10-29 16:42:03 +01:00
|
|
|
--stringparam idofdep $id_of_dep \
|
2015-11-23 15:14:23 +01:00
|
|
|
--stringparam MTA $MAIL_SERVER \
|
2015-11-07 12:36:11 +01:00
|
|
|
-o ${id_of_dep}.odep \
|
2013-10-29 16:42:03 +01:00
|
|
|
../xsl/dependencies.xsl ../packages.xml
|
2015-11-07 12:36:11 +01:00
|
|
|
fi
|
2013-10-29 16:42:03 +01:00
|
|
|
|
2015-11-07 12:36:11 +01:00
|
|
|
# Use -s, because it may happen that after removing lines, .odep exists
|
|
|
|
# but is empty.
|
|
|
|
if [[ -s ${id_of_dep}.odep ]]; then # this dependency has dependencies
|
2015-11-17 21:22:04 +01:00
|
|
|
sed "1i${rootlink[*]} $count\\
|
|
|
|
${priolink[*]} $prio_of_dep" < ${id_of_dep}.odep \
|
|
|
|
> ${id_of_dep}.dep # add link and priolink
|
|
|
|
generate_dependency_tree ${id_of_dep}.dep $prio_of_dep
|
2013-10-29 16:42:03 +01:00
|
|
|
# Test return value, in case we exchange dependencies
|
2015-11-17 21:22:04 +01:00
|
|
|
p2=$?
|
|
|
|
case $p2 in
|
2013-10-29 16:42:03 +01:00
|
|
|
0) # Normal return
|
|
|
|
;;
|
2015-11-17 21:22:04 +01:00
|
|
|
[123]) # We are backing up to parent
|
|
|
|
if [[ ${exchange_triplet} == ${DepFile%.dep} ]] ; then
|
|
|
|
# We are the parent!
|
|
|
|
# First, we have to find the parent of our new direct dep, and remove
|
|
|
|
# the new direct dep from the parent.odep:
|
|
|
|
otherlink=($(head -n1 ${exchange_triplet[2]}.dep))
|
|
|
|
unset otherlink[-1]
|
|
|
|
parent=$(grep -l ^"${otherlink[*]}"\$ *.dep)
|
|
|
|
sed -i /[[:digit:]]\ ${exchange_triplet[2]}\$/d ${parent/.dep/.odep}
|
|
|
|
tree_erase ${id_of_dep}.dep
|
2015-11-03 18:17:15 +01:00
|
|
|
# We want that our direct dep be ${exchange_triplet[2]} and that id_of_dep
|
|
|
|
# be pulled in as an indirect dep, so exchange.
|
2013-10-29 16:42:03 +01:00
|
|
|
# Just doing a sed -i "s@${id_of_dep}@${exchange_triplet[2]}@" $DepFile
|
|
|
|
# is not good if $DepFile contains several times the same line
|
|
|
|
# so first find the first line and then sed
|
|
|
|
lineno=$(sed -n /${id_of_dep}/= $DepFile | head -n1)
|
|
|
|
sed -i "${lineno}s@${id_of_dep}@${exchange_triplet[2]}@" $DepFile
|
2015-11-07 12:36:11 +01:00
|
|
|
# Do the same for the odep file:
|
|
|
|
local OdepFile=${DepFile/.dep/.odep}
|
|
|
|
lineno=$(sed -n /${id_of_dep}/= $OdepFile | head -n1)
|
|
|
|
sed -i "${lineno}s@${id_of_dep}@${exchange_triplet[2]}@" $OdepFile
|
2013-10-29 16:42:03 +01:00
|
|
|
id_of_dep=${exchange_triplet[2]}
|
2015-11-03 18:17:15 +01:00
|
|
|
flag=true # we have to regenerate the tree for the new dependency
|
2015-11-17 21:22:04 +01:00
|
|
|
else
|
|
|
|
# We are not the parent. If our priority is greater than p2
|
|
|
|
# we have to change the triplet and return priority, else return current p2.
|
|
|
|
# echo (DEBUG) backing up to ${exchange_triplet} at ${DepFile%.dep}
|
|
|
|
if (( $priority > $p2 )); then
|
|
|
|
exchange_triplet[2]=${DepFile%.dep}
|
|
|
|
return $priority
|
|
|
|
else
|
|
|
|
return $p2
|
|
|
|
fi
|
2013-10-29 16:42:03 +01:00
|
|
|
fi
|
|
|
|
;;
|
2006-07-16 12:29:37 +02:00
|
|
|
esac
|
2015-11-03 18:17:15 +01:00
|
|
|
else # id_of_dep has no dependencies, just record the link in a file
|
|
|
|
# and print
|
2013-10-29 16:42:03 +01:00
|
|
|
echo "${rootlink[*]} $count" > ${id_of_dep}.dep
|
2015-11-17 21:22:04 +01:00
|
|
|
echo -en "\nLeaf: $(($depth+1))${spaceSTR:0:$(($depth+1))}${CYAN}${id_of_dep}${OFF} $dpriostring"
|
2006-07-16 12:29:37 +02:00
|
|
|
fi
|
|
|
|
done
|
2013-10-29 16:42:03 +01:00
|
|
|
done
|
|
|
|
echo -en "\n End: $depth${spaceSTR:0:$depth}${GREEN}$DepFile${OFF}"
|
|
|
|
} 6<$DepFile
|
|
|
|
# It may happen that a file is created with several times
|
|
|
|
# the same line. Normally, all those lines but one
|
|
|
|
# would be flagged to be removed (or all of them if
|
2015-11-03 18:17:15 +01:00
|
|
|
# the dependency appeared before). A simple sed /$line/d
|
2013-10-29 16:42:03 +01:00
|
|
|
# destroys all the lines. We should instead remove
|
|
|
|
# only one for each appearance of it in lines_to_remove.
|
2015-11-03 18:17:15 +01:00
|
|
|
# so first get the position of last line and then delete
|
2013-10-29 16:42:03 +01:00
|
|
|
# that line
|
|
|
|
for line in $lines_to_remove
|
2015-11-17 21:22:04 +01:00
|
|
|
do lineno=$(sed -n /^[[:digit:]]\ $line\$/= $DepFile | tail -n1)
|
2013-10-29 16:42:03 +01:00
|
|
|
sed -i ${lineno}d $DepFile
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
2006-07-16 12:29:37 +02:00
|
|
|
|
2013-10-29 16:42:03 +01:00
|
|
|
#---------------#
|
|
|
|
tree_browse() { #
|
|
|
|
#---------------#
|
|
|
|
local file=$1
|
|
|
|
local f
|
|
|
|
|
|
|
|
#echo file=$file
|
2015-11-17 21:22:04 +01:00
|
|
|
for f in $(grep '[^0-9 ]' $file | sed 's/.* //'); do
|
2013-10-29 16:42:03 +01:00
|
|
|
# echo f=$f
|
|
|
|
if grep -q '[^0-9 ]' ${f}.dep ; then
|
|
|
|
tree_browse ${f}.dep
|
2006-07-16 12:29:37 +02:00
|
|
|
fi
|
2013-10-29 16:42:03 +01:00
|
|
|
echo $f
|
|
|
|
done
|
|
|
|
}
|
2006-07-16 12:29:37 +02:00
|
|
|
|
2013-10-29 16:42:03 +01:00
|
|
|
#--------------#
|
|
|
|
tree_erase() { #
|
|
|
|
#--------------#
|
|
|
|
local file=$1
|
|
|
|
local f
|
2017-08-22 11:00:49 +02:00
|
|
|
local rootlink
|
2016-02-28 19:07:52 +01:00
|
|
|
local rootlink2
|
2013-10-29 16:42:03 +01:00
|
|
|
|
|
|
|
#echo file=$file
|
2017-08-22 11:00:49 +02:00
|
|
|
rootlink="$(head -n1 $file) "
|
2015-11-17 21:22:04 +01:00
|
|
|
for f in $(grep '[^0-9 ]' $file | sed 's/.* //'); do
|
2013-10-29 16:42:03 +01:00
|
|
|
if [ -f ${f}.dep ]; then
|
2016-02-28 19:07:52 +01:00
|
|
|
rootlink2="$(head -n1 ${f}.dep) "
|
2017-08-22 11:00:49 +02:00
|
|
|
# We want two things:
|
|
|
|
# i) do not erase the file if it is in another branch
|
|
|
|
# ii) do not erase the file if there is a circular dependency
|
|
|
|
# for case i), we test that rootlink is contained in rootlink2
|
|
|
|
# for case ii), we test that rootlink2 is not contained in
|
|
|
|
# rootlink.
|
2016-02-28 19:07:52 +01:00
|
|
|
# See comment above about srootlink
|
2017-08-22 11:00:49 +02:00
|
|
|
if [[ ${rootlink2#${rootlink}} != ${rootlink2} &&
|
|
|
|
${rootlink#${rootlink2}} == ${rootlink} ]] ; then
|
2013-10-29 16:42:03 +01:00
|
|
|
tree_erase ${f}.dep
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
rm -f $file
|
2006-07-16 12:29:37 +02:00
|
|
|
}
|