2006-07-16 12:29:37 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
|
2018-01-28 18:56:55 +01:00
|
|
|
#-----------------------------------------------------------------------------#
|
|
|
|
# This is a set of (recursive) functions for manipulating a dependency graph. #
|
|
|
|
# We use algorithms and definitions from chapter 4 (mainly section 4.2) of #
|
|
|
|
# https://algs4.cs.princeton.edu/. The graph we manipulate is the directed #
|
|
|
|
# graph of the dependencies: nodes are packages in the BLFS book. A node A is #
|
|
|
|
# connected to a node B if package A depends on B. A topological order (rev- #
|
|
|
|
# erted) is exactly what we want for a build order. But a topological order #
|
|
|
|
# only exists if the graph is acyclic. We'll therefore have to remove cycles. #
|
|
|
|
# There are a number of other features we want to consider: #
|
|
|
|
# - edges are weighted according to the dependency requirement: #
|
|
|
|
# 1 for required #
|
|
|
|
# 2 for recommended #
|
|
|
|
# 3 for optional #
|
|
|
|
# 4 for external #
|
|
|
|
# We should consider only edges with weight lower or equal to that #
|
|
|
|
# specified by the user, but see below. #
|
|
|
|
# - we do not want to build the whole book. The user specifies a set of #
|
|
|
|
# packages, and we have to consider only nodes reachable from this set #
|
|
|
|
# using edges of weight not exceeding the specified weight. #
|
|
|
|
# - when doing the topological sort, we want to consider all the edges and #
|
|
|
|
# not only those not exceeding the specified weight: If a package A in the #
|
|
|
|
# reachable subgraph depends optionally on another package B in the same #
|
|
|
|
# subgraph, we want to build B before A. But this means we'll have to #
|
|
|
|
# remove cycles for all weights. #
|
|
|
|
# - dependencies have another qualifier: before or after. The problem: if a #
|
|
|
|
# package A depends on B with an "after" qualifier, and a package C depends #
|
|
|
|
# on A with a "before" qualifier, C may need B to be able to use A. So the #
|
|
|
|
# only safe way to consider "after" qualifiers is to consider that they are #
|
|
|
|
# "before" deps for any parent of the packages considered. #
|
|
|
|
# We'll therefore have a 3 pass procedure. First build the set of nodes #
|
|
|
|
# reachable from the root set. Second, remove dangling edges (those pointing #
|
|
|
|
# to packages outside the node set), and move "after" edges to "before" edges #
|
|
|
|
# originating from the parents. Third remove cycles and generate a #
|
|
|
|
# topological sort. #
|
|
|
|
# #
|
|
|
|
# TODO: document each pass
|
|
|
|
# Data layout: #
|
|
|
|
# TODO: needs also to document the .tree files, and the "f" qualifier
|
|
|
|
# #
|
|
|
|
# A node of the tree is represented by a text file <nodeName>.dep. Each edge #
|
|
|
|
# starting from this node is represented by a line in this file. We keep #
|
|
|
|
# those files in the same directory. We introduce a special node named root, #
|
|
|
|
# whose edges point to the list of nodes requested by the user. Each line #
|
|
|
|
# contains three fields: #
|
|
|
|
# - the weight of the edge #
|
|
|
|
# - the "before" (b) or "after" (a) qualifier #
|
|
|
|
# - the name of the destination of the edge #
|
2015-11-17 21:22:04 +01:00
|
|
|
# #
|
2018-01-28 18:56:55 +01:00
|
|
|
# TODO: The following is obsolete
|
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=" "
|
2018-01-28 18:56:55 +01:00
|
|
|
# When we are backing up from a circular dependency, `parentNode'
|
|
|
|
# contains the node which has an edge entering the cycle
|
|
|
|
declare parentNode
|
2006-07-16 12:29:37 +02:00
|
|
|
|
2018-01-28 18:56:55 +01:00
|
|
|
#---------------------#
|
|
|
|
generate_subgraph() { #
|
|
|
|
#---------------------#
|
2006-07-16 12:29:37 +02:00
|
|
|
: <<inline_doc
|
2018-01-28 18:56:55 +01:00
|
|
|
function: Create a subgraph of all the nodes reachable from the node
|
|
|
|
represented by the file whose name is $1. The edges considered
|
|
|
|
are those with maximal weight DEP_LEVEL (recursive function).
|
|
|
|
input vars: $1 : file name corresponding to the node whose edges will be
|
|
|
|
followed for the DFS
|
|
|
|
$2 : weight of the edge leading to this node
|
|
|
|
$3 : depth (root is 1)
|
|
|
|
$4 : qualifier (a for after, b for before)
|
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,
|
2018-01-28 18:56:55 +01:00
|
|
|
3 if we want also optional ones, but only
|
|
|
|
for the requested packages,
|
|
|
|
4 if we want all the dependencies
|
2015-11-23 15:14:23 +01:00
|
|
|
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
|
2018-01-28 18:56:55 +01:00
|
|
|
returns: 0 if the tree has been successfully created
|
|
|
|
output: files: for each node reachable from $1, a file <node>.dep.
|
|
|
|
on error: nothing
|
|
|
|
on success: nothing
|
|
|
|
inline_doc
|
|
|
|
|
|
|
|
local depFile=$1
|
|
|
|
local -i weight=$2
|
|
|
|
local -i depth=$3
|
|
|
|
local qualifier=$4
|
|
|
|
local -i spacing=0
|
|
|
|
local priostring
|
|
|
|
local buildstring
|
|
|
|
local id_of_dep
|
|
|
|
local prio_of_dep
|
|
|
|
local build_of_dep
|
|
|
|
local dep_level
|
|
|
|
|
|
|
|
if (( depth < 10 )); then spacing=1; fi
|
|
|
|
case $weight in
|
|
|
|
1) priostring=required ;;
|
|
|
|
2) priostring=recommended ;;
|
|
|
|
3) priostring=optional ;;
|
|
|
|
esac
|
|
|
|
case $qualifier in
|
|
|
|
a) buildstring=runtime ;;
|
|
|
|
b) buildstring= ;;
|
|
|
|
esac
|
|
|
|
dep_level=$DEP_LEVEL
|
|
|
|
if [ "$dep_level" = 3 ] && [ "$depth" -gt 2 ]; then dep_level=2; fi
|
|
|
|
if [ "$dep_level" -gt 3 ]; then dep_level=3; fi
|
|
|
|
echo -en "\nNode: $depth${spaceSTR:0:$(( depth + spacing ))}${RED}${depFile%.dep}${OFF} $priostring $buildstring"
|
|
|
|
|
|
|
|
depth=$(( depth + 1 ))
|
|
|
|
if (( depth < 10 )); then spacing=1; else spacing=0; fi
|
|
|
|
# Start of loop
|
|
|
|
{
|
|
|
|
while read prio_of_dep build_of_dep id_of_dep; do
|
2018-02-19 17:01:00 +01:00
|
|
|
case $prio_of_dep in
|
2018-01-28 18:56:55 +01:00
|
|
|
1) priostring=required ;;
|
|
|
|
2) priostring=recommended ;;
|
|
|
|
3) priostring=optional ;;
|
|
|
|
4) priostring=external ;;
|
2018-02-19 17:01:00 +01:00
|
|
|
esac
|
|
|
|
case $build_of_dep in
|
2018-01-28 18:56:55 +01:00
|
|
|
a) buildstring=runtime ;;
|
|
|
|
b) buildstring= ;;
|
2018-02-19 17:01:00 +01:00
|
|
|
esac
|
|
|
|
# Has this entry already been seen?
|
|
|
|
# We have a special case here: if the entry has been seen at depth > 2
|
|
|
|
# and now depth=2 and DEP_LEVEL=3, optional deps have not been processed.
|
|
|
|
# If this is the case, just consider it has not been seen.
|
2018-01-28 18:56:55 +01:00
|
|
|
if [ -f ${id_of_dep}.dep ] ; then
|
2018-02-19 17:01:00 +01:00
|
|
|
case $depth$DEP_LEVEL in
|
|
|
|
23) ;;
|
|
|
|
*)
|
2018-01-28 18:56:55 +01:00
|
|
|
# Just display it and proceed.
|
2018-02-19 17:01:00 +01:00
|
|
|
echo -en "\nEdge: $depth${spaceSTR:0:$((depth + spacing))}${MAGENTA}${id_of_dep}${OFF} $priostring $buildstring"
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
esac
|
2018-01-28 18:56:55 +01:00
|
|
|
fi
|
|
|
|
# Is the weight higher than requested?
|
|
|
|
if [ "$prio_of_dep" -gt $dep_level ]; then
|
|
|
|
# Just display it and proceed.
|
|
|
|
echo -en "\n Out: $depth${spaceSTR:0:$((depth + spacing))}${YELLOW}${id_of_dep}${OFF} $priostring $buildstring"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
# Otherwise, let's build the corresponding subgraph.
|
|
|
|
xsltproc --stringparam idofdep "$id_of_dep" \
|
|
|
|
--stringparam MTA "$MAIL_SERVER" \
|
|
|
|
-o ${id_of_dep}.dep \
|
|
|
|
../xsl/dependencies.xsl ../packages.xml
|
|
|
|
|
|
|
|
if [[ -s ${id_of_dep}.dep ]]; then # this dependency has dependencies
|
|
|
|
generate_subgraph ${id_of_dep}.dep $prio_of_dep $depth $build_of_dep
|
|
|
|
else # id_of_dep has no dependencies, just touch the file and display
|
|
|
|
touch ${id_of_dep}.dep
|
|
|
|
echo -en "\nLeaf: $depth${spaceSTR:0:$((depth + spacing))}${CYAN}${id_of_dep}${OFF} $priostring $buildstring"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
} <$depFile
|
|
|
|
depth=$(( depth - 1 ))
|
|
|
|
if (( depth < 10 )); then spacing=1; else spacing=0; fi
|
|
|
|
echo -en "\n End: $depth${spaceSTR:0:$((depth + spacing))}${GREEN}${depFile%.dep}${OFF}"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------#
|
|
|
|
path_to() { #
|
|
|
|
#-----------#
|
|
|
|
: <<inline_doc
|
|
|
|
function: check whether there is a path from $1 to $2 on the graph
|
|
|
|
input vars: $1 contains the filename of the starting node.
|
|
|
|
$2 contains the name of the node to reach
|
|
|
|
$3 contains the weight above which we do not want to
|
|
|
|
follow an edge
|
|
|
|
"seen" (global) contains the list of already seen nodes
|
|
|
|
returns: 0 if the node has been found
|
|
|
|
1 if not
|
|
|
|
on error: nothing
|
|
|
|
on success: nothing
|
|
|
|
inline_doc
|
|
|
|
local start=$1
|
|
|
|
local seek=$2
|
|
|
|
local prio=$3
|
|
|
|
local prio_of_dep
|
|
|
|
local build_of_dep
|
|
|
|
local id_of_dep
|
|
|
|
local r
|
|
|
|
|
|
|
|
if test "${start%.dep}" = "$seek"; then return 0; fi
|
2018-02-02 22:06:49 +01:00
|
|
|
seen="$seen${start%.dep} "
|
2018-02-02 18:55:29 +01:00
|
|
|
if test -s $start; then
|
2018-01-28 18:56:55 +01:00
|
|
|
{
|
|
|
|
while read prio_of_dep build_of_dep id_of_dep; do
|
|
|
|
if test "$prio" -lt "$prio_of_dep"; then continue; fi
|
2018-02-02 18:55:29 +01:00
|
|
|
if ! test "${seen% $id_of_dep *}" = "$seen"; then continue; fi
|
2018-01-28 18:56:55 +01:00
|
|
|
if path_to ${id_of_dep}.dep $seek $prio; then return 0; fi
|
|
|
|
done
|
|
|
|
} < $start
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
#------------------#
|
|
|
|
clean_subgraph() { #
|
|
|
|
#------------------#
|
|
|
|
: <<inline_doc
|
|
|
|
function: Remove dangling edges and create groups of deps for "after"
|
|
|
|
deps: A-before->B-after->C becomes:
|
|
|
|
A -before-> Bgroupxx -before-> B
|
|
|
|
\
|
|
|
|
-before-> C
|
|
|
|
the name of the group is chosen so that it is unlikely as
|
|
|
|
a package name (so that it is removed when building the
|
|
|
|
xml book).
|
|
|
|
input vars: None
|
|
|
|
files: <node>.dep files containing dangling edges and
|
|
|
|
"after" qualifiers
|
|
|
|
returns: 0
|
|
|
|
output: files: <node>.dep files containing no dangling edges and
|
|
|
|
no "after" qualifiers
|
|
|
|
on error: nothing
|
|
|
|
on success: nothing
|
|
|
|
inline_doc
|
|
|
|
|
|
|
|
local node
|
|
|
|
local id_of_dep
|
|
|
|
local prio_of_dep
|
|
|
|
local build_of_dep
|
|
|
|
local lines_to_remove
|
|
|
|
local lines_to_change
|
|
|
|
local parent
|
|
|
|
local p
|
|
|
|
local b
|
|
|
|
local start
|
|
|
|
local seen
|
|
|
|
|
|
|
|
for node in $(ls *.dep); do
|
|
|
|
if test $node = root.dep; then continue; fi
|
2018-02-09 10:24:55 +01:00
|
|
|
echo Cleaning $node
|
2018-01-28 18:56:55 +01:00
|
|
|
lines_to_remove=
|
|
|
|
{
|
|
|
|
while read prio_of_dep build_of_dep id_of_dep; do
|
|
|
|
if ! test -f ${id_of_dep}.dep; then
|
|
|
|
lines_to_remove="$lines_to_remove $id_of_dep"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
} <$node
|
|
|
|
for id_of_dep in $lines_to_remove; do
|
|
|
|
sed "/\ $id_of_dep\$/d" -i $node
|
|
|
|
done
|
|
|
|
done
|
|
|
|
for node in $(grep -l ' a ' *.dep); do
|
|
|
|
lines_to_remove=
|
2018-02-09 10:24:55 +01:00
|
|
|
echo Process "runtime" deps in $node
|
2018-01-28 18:56:55 +01:00
|
|
|
if ! [ -e ${node%.dep}groupxx.dep ]; then
|
2018-02-02 22:12:30 +01:00
|
|
|
b=0
|
2018-01-28 18:56:55 +01:00
|
|
|
for parent in $(grep -l ${node%.dep}\$ *); do
|
|
|
|
p=0
|
|
|
|
for start in $(grep ' a ' $node | cut -d' ' -f3); do
|
|
|
|
seen=" "
|
|
|
|
if path_to ${start}.dep ${parent%.dep} 3; then p=1; break; fi
|
|
|
|
done
|
|
|
|
if test $p = 0; then
|
2018-02-02 22:12:30 +01:00
|
|
|
b=1
|
2018-01-28 18:56:55 +01:00
|
|
|
sed -i "s/\ ${node%.dep}\$/&groupxx/" $parent
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "1 b ${node%.dep}" > ${node%.dep}groupxx.dep
|
2018-02-02 22:12:30 +01:00
|
|
|
if test $b = 0; then echo "1 b ${node%.dep}groupxx" >> root.dep; fi
|
2018-01-28 18:56:55 +01:00
|
|
|
fi
|
|
|
|
{
|
|
|
|
while read prio_of_dep build_of_dep id_of_dep; do
|
|
|
|
if test $build_of_dep = a; then
|
|
|
|
if ! grep -q ${id_of_dep} ${node%.dep}groupxx.dep; then
|
|
|
|
echo "$prio_of_dep b ${id_of_dep}" >> ${node%.dep}groupxx.dep
|
|
|
|
fi
|
|
|
|
lines_to_remove="$lines_to_remove $id_of_dep"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
} <$node
|
|
|
|
for id_of_dep in $lines_to_remove; do
|
|
|
|
sed "/\ $id_of_dep\$/d" -i $node
|
|
|
|
done
|
|
|
|
done
|
|
|
|
for node in $(grep -l ' f ' *); do
|
2018-02-09 10:24:55 +01:00
|
|
|
echo Process "first" deps in $node
|
2018-01-28 18:56:55 +01:00
|
|
|
lines_to_change=
|
|
|
|
{
|
|
|
|
while read prio_of_dep build_of_dep id_of_dep; do
|
|
|
|
if test $build_of_dep = f; then
|
|
|
|
if ! test -f ${id_of_dep}-pass1.dep; then
|
|
|
|
cp ${id_of_dep}{,-pass1}.dep;
|
|
|
|
fi
|
|
|
|
lines_to_change="$lines_to_change $id_of_dep"
|
|
|
|
unset lr
|
|
|
|
{
|
|
|
|
while read p b start; do
|
|
|
|
seen=" "
|
|
|
|
if path_to ${start}.dep ${node%.dep} $p; then
|
|
|
|
lr="$lr $start"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
} < ${id_of_dep}-pass1.dep
|
|
|
|
for p in $lr; do
|
|
|
|
sed "/\ $p\$/d" -i ${id_of_dep}-pass1.dep
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
} <$node
|
|
|
|
for id_of_dep in $lines_to_change; do
|
2018-02-02 22:10:16 +01:00
|
|
|
sed "/\ $id_of_dep\$/"'{s/[[:digit:]] f /1 b /;s/$/-pass1/}' -i $node
|
2018-01-28 18:56:55 +01:00
|
|
|
done
|
|
|
|
done
|
|
|
|
} # End clean_subgraph
|
|
|
|
|
|
|
|
#----------------------------#
|
|
|
|
generate_dependency_tree() { #
|
|
|
|
#----------------------------#
|
|
|
|
: <<inline_doc
|
|
|
|
function: Create a subtree of the dependency tree
|
|
|
|
(recursive function)
|
|
|
|
input vars: $1 : file with a list of targets (child nodes)
|
|
|
|
the first line of the file is the link
|
|
|
|
$2 : priority (1=req, 2=rec, 3=opt)
|
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
|
2018-01-28 18:56:55 +01:00
|
|
|
and there are only required deps in the cycle
|
|
|
|
2 if we are backing up to the parent of a circular dep
|
|
|
|
and there are recommended deps and no optional deps in the
|
|
|
|
cycle
|
|
|
|
3 if we are backing up to the parent of a circular dep
|
|
|
|
and there are optiional deps in the cycle
|
|
|
|
modifies: vars: ParentNode is set when return is not 0
|
2015-11-03 18:17:15 +01:00
|
|
|
output: files: for each <pkg> with dependencies in $1,
|
2018-01-28 18:56:55 +01:00
|
|
|
a file <pkg>.tree and its dependencies
|
2006-07-16 12:29:37 +02:00
|
|
|
on error: nothing
|
|
|
|
on success: nothing
|
|
|
|
inline_doc
|
|
|
|
|
2018-01-28 18:56:55 +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
|
2018-01-28 18:56:55 +01:00
|
|
|
local build_of_dep
|
|
|
|
local prio_of_dep
|
2013-10-29 16:42:03 +01:00
|
|
|
local parent
|
|
|
|
local lines_to_remove=
|
|
|
|
local srootlink
|
2015-11-17 21:22:04 +01:00
|
|
|
local priostring
|
|
|
|
local dpriostring
|
|
|
|
local i
|
2013-10-29 16:42:03 +01:00
|
|
|
|
|
|
|
{
|
2018-01-28 18:56:55 +01:00
|
|
|
read -a rootlink
|
2013-10-29 16:42:03 +01:00
|
|
|
depth=${#rootlink[*]}
|
2018-01-28 18:56:55 +01:00
|
|
|
read -a priolink
|
2013-10-29 16:42:03 +01:00
|
|
|
srootlink="${rootlink[*]} "
|
2015-11-17 21:22:04 +01:00
|
|
|
case $priority in
|
|
|
|
1) priostring=required ;;
|
|
|
|
2) priostring=recommended ;;
|
|
|
|
3) priostring=optional ;;
|
|
|
|
esac
|
2018-01-28 18:56:55 +01:00
|
|
|
# start of depFile
|
|
|
|
echo -en "\nNode: $depth${spaceSTR:0:$depth}${RED}${depFile%.tree}${OFF} $priostring"
|
2013-10-29 16:42:03 +01:00
|
|
|
|
2018-01-28 18:56:55 +01:00
|
|
|
while read prio_of_dep build_of_dep id_of_dep; do
|
|
|
|
case $prio_of_dep in
|
2015-11-17 21:22:04 +01:00
|
|
|
1) dpriostring=required ;;
|
|
|
|
2) dpriostring=recommended ;;
|
|
|
|
3) dpriostring=optional ;;
|
2018-01-28 18:56:55 +01:00
|
|
|
esac
|
2013-10-29 16:42:03 +01:00
|
|
|
# count entries in file
|
|
|
|
(( count++ ))
|
|
|
|
# Has this entry already been seen?
|
2018-01-28 18:56:55 +01:00
|
|
|
if [ -f ${id_of_dep}.tree ]; then # found ${id_of_dep}.tree already in tree
|
|
|
|
otherlink=($(head -n 1 ${id_of_dep}.tree))
|
2018-01-13 10:08:56 +01:00
|
|
|
if [ -z "${otherlink[*]}" ]
|
2018-01-28 18:56:55 +01:00
|
|
|
then echo otherlink empty for $id_of_dep.tree
|
2013-10-29 16:42:03 +01:00
|
|
|
echo This should not happen, but happens to happen...
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-01-28 18:56:55 +01:00
|
|
|
#Do not use "${rootlink[*]}" =~ "${otherlink[*]}": case rootlink=(1 11)
|
2013-10-29 16:42:03 +01:00
|
|
|
# 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"
|
2018-01-28 18:56:55 +01:00
|
|
|
# Find lowest priority in branch from parent to depFile:
|
2015-11-17 21:22:04 +01:00
|
|
|
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"
|
2018-01-28 18:56:55 +01:00
|
|
|
sed -i "/$id_of_dep/d" ${depFile/.tree/.dep}
|
|
|
|
else # find and set parent, then return lowest priority
|
|
|
|
# The parent has the same link without the last entry.
|
|
|
|
# We do not need otherlink anymore so just destroy the last element
|
|
|
|
unset otherlink[-1]
|
|
|
|
parentNode=$(grep ^"${otherlink[*]}"\$ -l *)
|
|
|
|
return $p2
|
2015-11-03 18:17:15 +01:00
|
|
|
fi
|
2018-01-28 18:56:55 +01:00
|
|
|
else # not circular: prune tree (but not .dep, 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?
|
2018-01-28 18:56:55 +01:00
|
|
|
# So, this entry has not already been seen. Let's build the corresponding
|
|
|
|
# subtree. First check there is a subtree...
|
|
|
|
# Use -s, because it may happen that after removing lines, .dep exists
|
2015-11-07 12:36:11 +01:00
|
|
|
# but is empty.
|
2018-01-28 18:56:55 +01:00
|
|
|
if [[ -s ${id_of_dep}.dep ]]; then # this dependency has dependencies
|
|
|
|
sed "1i${rootlink[*]} $count\\
|
|
|
|
${priolink[*]} $prio_of_dep" < ${id_of_dep}.dep \
|
|
|
|
> ${id_of_dep}.tree # add link and priolink
|
|
|
|
generate_dependency_tree ${id_of_dep}.tree $prio_of_dep
|
2013-10-29 16:42:03 +01:00
|
|
|
# Test return value, in case we exchange dependencies
|
2018-01-28 18:56:55 +01:00
|
|
|
p2=$?
|
|
|
|
case $p2 in
|
|
|
|
0) # Normal return
|
|
|
|
;;
|
|
|
|
$prio_of_dep) # we remove this dep, but since it may become unreachable,
|
|
|
|
# move it to be built later (as a dep of parent).
|
|
|
|
tree_erase ${id_of_dep}.tree
|
|
|
|
lines_to_remove="$lines_to_remove $id_of_dep"
|
|
|
|
sed -i "/${id_of_dep}/d" ${depFile/.tree/.dep}
|
|
|
|
echo "$prio_of_dep b $id_of_dep" >> $parentNode
|
|
|
|
# must be added to .dep in case parentNode is destroyed when erasing
|
|
|
|
# the tree
|
|
|
|
echo "$prio_of_dep b $id_of_dep" >> ${parentNode/.tree/.dep}
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
*) # We are backing up
|
|
|
|
return $p2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
else # id_of_dep has no dependencies, just record the link in a file
|
|
|
|
# and print
|
|
|
|
echo "${rootlink[*]} $count" > ${id_of_dep}.tree
|
|
|
|
echo -en "\nLeaf: $(($depth+1))${spaceSTR:0:$(($depth+1))}${CYAN}${id_of_dep}${OFF} $dpriostring"
|
|
|
|
fi
|
2013-10-29 16:42:03 +01:00
|
|
|
done
|
2018-01-28 18:56:55 +01:00
|
|
|
echo -en "\n End: $depth${spaceSTR:0:$depth}${GREEN}${depFile%.tree}${OFF}"
|
|
|
|
} <$depFile
|
2013-10-29 16:42:03 +01:00
|
|
|
# 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
|
2018-01-28 18:56:55 +01:00
|
|
|
do lineno=$(sed -n /^[[:digit:]]\ b\ $line\$/= $depFile | tail -n1)
|
|
|
|
sed -i ${lineno}d $depFile
|
2013-10-29 16:42:03 +01:00
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
2006-07-16 12:29:37 +02:00
|
|
|
|
2018-01-28 18:56:55 +01: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
|
2018-01-28 18:56:55 +01:00
|
|
|
if grep -q '[^0-9 ]' ${f}.tree ; then
|
|
|
|
tree_browse ${f}.tree
|
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
|
2018-01-28 18:56:55 +01:00
|
|
|
if [ -f ${f}.tree ]; then
|
|
|
|
rootlink2="$(head -n1 ${f}.tree) "
|
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
|
2018-01-28 18:56:55 +01:00
|
|
|
tree_erase ${f}.tree
|
2013-10-29 16:42:03 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
rm -f $file
|
2006-07-16 12:29:37 +02:00
|
|
|
}
|