|
| 1 | +#!/bin/bash |
| 2 | +# Script to trim eht-imaging project (M87 blackhole study) dependency graph in dot format |
| 3 | +# Trimming is done such that only NumPy oriented dependencies are in focus |
| 4 | +# For more details on eht-imaging project visit: |
| 5 | +# https://github.com/achael/eht-imaging |
| 6 | +# https://achael.github.io/_pages/imaging/ |
| 7 | + |
| 8 | +display_help() { |
| 9 | + |
| 10 | + echo "Usage:" |
| 11 | + echo " numpy-ehtim-dep-graph.sh reqfilepath outputdir [optional color]" |
| 12 | + echo "" |
| 13 | + echo "reqfilepath - directory containing latest source of eht-imaging requirements.txt file." |
| 14 | + echo "outputdir - dir or path where generated graphs will be stored." |
| 15 | + echo "optional color. For e.g., cyan, crimson or any of the https://graphviz.gitlab.io/_pages/doc/info/colors.html specifications for graphviz. Default cyan." |
| 16 | + echo "" |
| 17 | +} |
| 18 | + |
| 19 | +check_pre_requisites() { |
| 20 | + yehtim=`pip list | grep ehtim` |
| 21 | + ygraphviz=`pip list | grep graphviz` |
| 22 | + ypipdeptree=`pip list | grep pipdeptree` |
| 23 | + if [[ ( -z "$yehtim" ) || ( -z "$ygraphviz" ) || ( -z "$ypipdeptree" ) ]]; |
| 24 | + then |
| 25 | + echo "Error: Pre-requisite for generating dependency graph not fulfilled!" |
| 26 | + echo "The following packages must be installed:" |
| 27 | + echo " 1. ehtim" |
| 28 | + echo " 2. graphviz" |
| 29 | + echo " 3. pipdeptree" |
| 30 | + exit |
| 31 | + fi |
| 32 | +} |
| 33 | + |
| 34 | +if [[ ($# -lt 2) || ($# -gt 3 ) || ( -z "$1" ) || (-z "$2") || ("$1" == "-h") || ("$1" == "--help") ]]; |
| 35 | +then |
| 36 | + display_help |
| 37 | + exit |
| 38 | +fi |
| 39 | + |
| 40 | +reqfilepath=$1 |
| 41 | +reqfile="$1/requirements.txt" |
| 42 | +if ! [ -f $reqfile ]; |
| 43 | +then |
| 44 | + echo "Error: Make sure you have latest eht-imaging sources and requirements.txt file path is correct!!" |
| 45 | + display_help |
| 46 | + exit |
| 47 | +fi |
| 48 | + |
| 49 | +graphdir="$2" |
| 50 | +if [[ ( ! -d "$graphdir" ) || ( -f "$graphdir" ) ]]; |
| 51 | +then |
| 52 | + echo "Error: Make sure $graphdir is a valid directory to store generated graphs!" |
| 53 | + display_help |
| 54 | + exit |
| 55 | +fi |
| 56 | + |
| 57 | +check_pre_requisites |
| 58 | +echo "Using $reqfile to prune dependency graph of ehtim." |
| 59 | +echo "Specified output directory is $graphdir." |
| 60 | +echo "Generating ehtim dependency graphs..." |
| 61 | + |
| 62 | +pipdeptree --graph-output dot > $graphdir/numpy-ehtim-dep.dot |
| 63 | +dot -T png $graphdir/numpy-ehtim-dep.dot -o $graphdir/numpy-ehtim-dep.png |
| 64 | + |
| 65 | +awk '/^[0-9]*[.]+/ {printf(" %s\n", $0); found=1; next} !/^[0-9]*[.]+/ {printf((found==1)? "%s" : "\n%s", $0); found=0 } END {print ""}' $graphdir/numpy-ehtim-dep.dot > $graphdir/cleanup.dot |
| 66 | +echo digraph { > $graphdir/cleanup_pkg.dot |
| 67 | + |
| 68 | +awk ' NR==FNR{a[$1]; next} {for (i in a) if ((index($0, i)&& $3 == "") || ($3 != "" && index($0, i) && index($3, i) )) print}' $reqfile $graphdir/cleanup.dot >> $graphdir/cleanup_pkg.dot |
| 69 | + |
| 70 | +echo } >> $graphdir/cleanup_pkg.dot |
| 71 | + |
| 72 | +dot -K twopi $graphdir/cleanup_pkg.dot -o $graphdir/numpy-clean.dot |
| 73 | +dot -T png $graphdir/numpy-clean.dot -o $graphdir/numpy-clean.png |
| 74 | + |
| 75 | +if [ -z "$3" ]; |
| 76 | +then |
| 77 | + #hcolor="crimson" |
| 78 | + hcolor="cyan" |
| 79 | +else |
| 80 | + hcolor="$3" |
| 81 | +fi |
| 82 | + |
| 83 | +echo "Highlighting numpy nodes using $hcolor." |
| 84 | + |
| 85 | +awk -v ac=${hcolor} '{ if ($1 == "numpy") {print $0, "color=",ac,", style=filled," } else { if ($2 == "->" && $3 == "numpy") {print $0, "color=",ac,","} else {print}}}' $graphdir/numpy-clean.dot > $graphdir/numpy-clean-color.dot |
| 86 | + |
| 87 | +dot -T png $graphdir/numpy-clean-color.dot -o $graphdir/numpy-clean-color.png |
| 88 | + |
| 89 | +echo "-------------------------------------------------------------------" |
| 90 | +echo Done! |
0 commit comments