|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to trace WDL task dependencies |
| 4 | +# Usage: ./trace_task_dependencies.sh <task_name> |
| 5 | +# Example: ./trace_task_dependencies.sh GetTodayDate |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +TASK_NAME="$1" |
| 10 | +WDL_DIR="./wdl" |
| 11 | +VISITED=() |
| 12 | +CHAIN=() |
| 13 | + |
| 14 | +# Function to check if a value exists in an array |
| 15 | +contains_element() { |
| 16 | + local element="$1" |
| 17 | + shift |
| 18 | + local array=("$@") |
| 19 | + for item in "${array[@]}"; do |
| 20 | + [[ "$item" == "$element" ]] && return 0 |
| 21 | + done |
| 22 | + return 1 |
| 23 | +} |
| 24 | + |
| 25 | +# Function to add visited file to avoid cycles |
| 26 | +add_visited() { |
| 27 | + local file="$1" |
| 28 | + VISITED+=("$file") |
| 29 | +} |
| 30 | + |
| 31 | +# Function to check if file was already visited |
| 32 | +is_visited() { |
| 33 | + local file="$1" |
| 34 | + if [[ ${#VISITED[@]} -eq 0 ]]; then |
| 35 | + return 1 |
| 36 | + fi |
| 37 | + contains_element "$file" "${VISITED[@]}" |
| 38 | +} |
| 39 | + |
| 40 | +# Function to find the file containing a task definition |
| 41 | +find_task_definition() { |
| 42 | + local task="$1" |
| 43 | + # Search for task definition pattern: "^task TaskName {" |
| 44 | + find "${WDL_DIR}" -name "*.wdl" -exec grep -l "^task ${task}\s*{" {} \; 2>/dev/null || true |
| 45 | +} |
| 46 | + |
| 47 | +# Function to find workflows/tasks that call a specific task |
| 48 | +find_callers() { |
| 49 | + local task="$1" |
| 50 | + local calling_files=() |
| 51 | + |
| 52 | + # Search for "call TaskName" or "call Namespace.TaskName" patterns |
| 53 | + while IFS= read -r -d '' file; do |
| 54 | + if grep -q "call.*${task}" "$file" 2>/dev/null; then |
| 55 | + calling_files+=("$file") |
| 56 | + fi |
| 57 | + done < <(find "${WDL_DIR}" -name "*.wdl" -print0) |
| 58 | + |
| 59 | + # Only print if array has elements to avoid "unbound variable" error |
| 60 | + if [[ ${#calling_files[@]} -gt 0 ]]; then |
| 61 | + printf '%s\n' "${calling_files[@]}" |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +# Function to get workflow/task name from file |
| 66 | +get_workflow_or_task_name() { |
| 67 | + local file="$1" |
| 68 | + # Look for workflow or task definition at the top level |
| 69 | + local name="" |
| 70 | + name=$(grep -E "^(workflow|task) [0-9A-Za-z_-]+" "$file" | head -1 | awk '{print $2}' | sed 's/{.*$//') |
| 71 | + echo "$name" |
| 72 | +} |
| 73 | + |
| 74 | +# Function to trace dependencies recursively |
| 75 | +trace_dependencies() { |
| 76 | + local current_task="$1" |
| 77 | + local depth="${2:-0}" |
| 78 | + local indent=$(printf "%*s" $((depth * 2)) "") |
| 79 | + |
| 80 | + echo "${indent}Tracing callers of task: $current_task" |
| 81 | + |
| 82 | + # Find the task definition file |
| 83 | + local task_file |
| 84 | + task_file=$(find_task_definition "$current_task") |
| 85 | + |
| 86 | + if [[ -z "$task_file" ]]; then |
| 87 | + echo "${indent} Task '$current_task' definition not found" |
| 88 | + return |
| 89 | + fi |
| 90 | + |
| 91 | + echo "${indent} Task defined in: $task_file" |
| 92 | + |
| 93 | + # Find all files that call this task |
| 94 | + local callers=() |
| 95 | + while IFS= read -r line; do |
| 96 | + [[ -n "$line" ]] && callers+=("$line") |
| 97 | + done < <(find_callers "$current_task") |
| 98 | + |
| 99 | + if [[ ${#callers[@]} -eq 0 ]]; then |
| 100 | + echo "${indent} No callers found for task '$current_task'" |
| 101 | + return |
| 102 | + fi |
| 103 | + |
| 104 | + echo "${indent} Found ${#callers[@]} caller(s):" |
| 105 | + |
| 106 | + for caller_file in "${callers[@]}"; do |
| 107 | + # Skip if already visited to avoid cycles |
| 108 | + if is_visited "$caller_file"; then |
| 109 | + echo "${indent} $caller_file (already visited, skipping to avoid cycle)" |
| 110 | + continue |
| 111 | + fi |
| 112 | + |
| 113 | + add_visited "$caller_file" |
| 114 | + |
| 115 | + local caller_name |
| 116 | + caller_name=$(get_workflow_or_task_name "$caller_file") |
| 117 | + |
| 118 | + echo "${indent} $caller_file" |
| 119 | + echo "${indent} Contains workflow/task: $caller_name" |
| 120 | + |
| 121 | + # Add to current chain |
| 122 | + CHAIN+=("$caller_name -> $caller_file") |
| 123 | + |
| 124 | + # Recursively trace callers of this workflow/task |
| 125 | + if [[ -n "$caller_name" && "$caller_name" != "$current_task" ]]; then |
| 126 | + trace_dependencies "$caller_name" $((depth + 1)) |
| 127 | + fi |
| 128 | + done |
| 129 | +} |
| 130 | + |
| 131 | +# Function to print the full dependency chain |
| 132 | +print_chain() { |
| 133 | + echo "" |
| 134 | + echo "=== DEPENDENCY CHAIN SUMMARY ===" |
| 135 | + if [[ ${#CHAIN[@]} -eq 0 ]]; then |
| 136 | + echo "No dependency chain found for task '$TASK_NAME'" |
| 137 | + else |
| 138 | + echo "Dependency chain for task '$TASK_NAME':" |
| 139 | + for item in "${CHAIN[@]}"; do |
| 140 | + echo " $item" |
| 141 | + done |
| 142 | + fi |
| 143 | +} |
| 144 | + |
| 145 | +# Main execution |
| 146 | +main() { |
| 147 | + if [[ $# -eq 0 ]]; then |
| 148 | + echo "Usage: $0 <task_name>" |
| 149 | + echo "Example: $0 GetTodayDate" |
| 150 | + exit 0 |
| 151 | + fi |
| 152 | + |
| 153 | + echo "==================================" |
| 154 | + echo "=== WDL TASK DEPENDENCY TRACER ===" |
| 155 | + echo "==================================" |
| 156 | + echo -e "Searching for workflows that call task: \n\t$TASK_NAME" |
| 157 | + echo "WDL directory: $WDL_DIR" |
| 158 | + echo "" |
| 159 | + |
| 160 | + # Check if WDL directory exists |
| 161 | + if [[ ! -d "$WDL_DIR" ]]; then |
| 162 | + echo "Error: WDL directory '$WDL_DIR' not found" |
| 163 | + exit 1 |
| 164 | + fi |
| 165 | + |
| 166 | + # Start tracing |
| 167 | + trace_dependencies "$TASK_NAME" |
| 168 | + |
| 169 | + # Print summary |
| 170 | + print_chain |
| 171 | +} |
| 172 | + |
| 173 | +main "$@" |
0 commit comments