Advertisement
Guest User

Dependency tracker

a guest
Jun 27th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.19 KB | None | 0 0
  1. # This function accepts the task to be performed as its only argument.
  2. # 1) First, check if we have already added the task to the list. If true,
  3. #    bail out to avoid processing it twice.
  4. # 2) If a task does not depend on others, add a "self dependency" so that
  5. #    it will appear in the final list, even if no other tasks depend on
  6. #    it.
  7. # 3) If a task depends on another one, add the dependency and then call
  8. #    adddep() on the second task. That will recursively add all
  9. #    dependencies to the list.
  10. # Care should be taken not to introduce infinite cycles.
  11. adddep() {
  12.     TASKFOUND=`echo $DEPENDENCIES | grep -F "$1 " | wc -l`
  13.     if [ $TASKFOUND -eq 1 ]; then return; fi
  14.     case "$1" in
  15.         task1)
  16.             DEPENDENCIES="${DEPENDENCIES}task1 task1\n"
  17.             ;;
  18.         task2)
  19.             DEPENDENCIES="${DEPENDENCIES}task2 task1\n"
  20.             adddep task1
  21.             ;;
  22.         task3)
  23.             DEPENDENCIES="${DEPENDENCIES}task3 task2\n"
  24.             adddep task2
  25.             ;;
  26.         task4)
  27.             DEPENDENCIES="${DEPENDENCIES}task4 task1\n"
  28.             adddep task1
  29.             ;;
  30.         task5)
  31.             DEPENDENCIES="${DEPENDENCIES}task5 task5\n"
  32.             ;;
  33.     esac
  34. }
  35.  
  36. # Get the list of tasks to be performed. The "3>&1 1>&2 2>&3" at the end
  37. # will switch stdout and stderr (needed because whiptail prints its
  38. # output to stderr).
  39. TASKS=$(whiptail --title "Choose the tasks" --nocancel --separate-output --checklist "Choose the tasks you want to perform from the list below." 20 70 10 task1 "Task 1" 0 task2 "Task 2 (depends on Task 1)" 0 task3 "Task 3 (depends on Task 2)" 0 task4 "Task 4 (depends on Task 1)" 0 task5 "Task 5" 0 3>&1 1>&2 2>&3)
  40. # Build a dependency graph (an edge goes from A to B iff performing the
  41. # task A requires the task B to be performed first).
  42. DEPENDENCIES=""
  43. for i in $TASKS; do
  44.     adddep $i
  45. done
  46. # DEPENDENCIES now contains a list of all edges we need to keep track of,
  47. # topologically sort the graph to get the lists of tasks to perform in
  48. # reverse order (via "tsort") and reverse the order (via "tac").
  49. ORDEREDTASKS=`echo $DEPENDENCIES | tsort | tac`
  50. # Perform the tasks.
  51. for i in $ORDEREDTASKS; do
  52.     $i
  53. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement