|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# gen-numpy-dep-graph.sh script is used to generate numpy dependency |
| 4 | +# graph for a given package. If you already don't have the package |
| 5 | +# sources available locally, you can use the script get-package |
| 6 | +# to download package sources before running this script. |
| 7 | +# The full dependency graph is stored as numpy-<pkgname>-dep.png file |
| 8 | +# in the specified output directory. Besides the full chart, for simplicity |
| 9 | +# this script trims specified package project dependency graph in dot format |
| 10 | +# Trimming is done such that only NumPy oriented dependencies are in focus |
| 11 | + |
| 12 | +# This script along wiht get-package.sh script has been used to create |
| 13 | +# NumPy dependency graphs for ehtim-imaging package, gwpy and cbPy packages. |
| 14 | +# It should work fine for any specified package that depends on NumPy. |
| 15 | +# Feedback is most welcome. |
| 16 | + |
| 17 | +# For more details on eht-imaging project visit: https://github.com/achael/eht-imaging |
| 18 | +# For more details on gwpy project visit: https://github.com/gwpy/gwpy |
| 19 | + |
| 20 | +display_help() { |
| 21 | + echo "-----------------------------------------------------------------------" |
| 22 | + echo "Usage:" |
| 23 | + echo " gen-numpy-dep-graph.sh pkgname workingdir reqfilepath outputdir [optional color]" |
| 24 | + echo "" |
| 25 | + echo "pkgname - the name of the package that uses numpy, use the same name |
| 26 | + echo "workingdir - the workingdir pathname where pkgname sources are available" |
| 27 | + that you specify to pip3 to install the package." |
| 28 | + echo "reqfilepath - directory containing latest source of eht-imaging requirements.txt file." |
| 29 | + echo "outputdir - dir or path where generated graphs will be stored." |
| 30 | + 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." |
| 31 | + echo "" |
| 32 | + echo "-----------------------------------------------------------------------" |
| 33 | +} |
| 34 | + |
| 35 | +check_pre_requisites() { |
| 36 | + pkgname=$1 |
| 37 | + vdir="$2/env-$pkgname/bin" |
| 38 | + ypkgname=`$vdir/pip3 list | grep "${pkgname}"` |
| 39 | + ygraphviz=`$vdir/pip3 list | grep graphviz` |
| 40 | + ypipdeptree=`$vdir/pip3 list | grep pipdeptree` |
| 41 | + if [[ ( -z "$ypkgname" ) || ( -z "$ygraphviz" ) || ( -z "$ypipdeptree" ) ]]; |
| 42 | + then |
| 43 | + echo "Error: Pre-requisite for generating dependency graph not fulfilled!" |
| 44 | + echo "The following packages must be installed:" |
| 45 | + echo " 1. $pkgname" |
| 46 | + echo " 2. graphviz" |
| 47 | + echo " 3. pipdeptree" |
| 48 | + exit |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +if [[ ($# -lt 3) || ($# -gt 5 ) || ( -z "$1" ) || (-z "$2") || (-z "$3") || ("$1" == "-h") || ("$1" == "--help") ]]; |
| 53 | +then |
| 54 | + display_help |
| 55 | + exit |
| 56 | +fi |
| 57 | + |
| 58 | +inputpkgname=$1 |
| 59 | +venvdir=$2 |
| 60 | +reqfilepath=$3 |
| 61 | +reqfile="$3/requirements.txt" |
| 62 | +if ! [ -f $reqfile ]; |
| 63 | +then |
| 64 | + echo "Error: Make sure you have latest $inputpkgname sources and requirements.txt file path is correct!!" |
| 65 | + display_help |
| 66 | + exit |
| 67 | +fi |
| 68 | + |
| 69 | +graphdir="$4" |
| 70 | +if [[ ( ! -d "$graphdir" ) || ( -f "$graphdir" ) ]]; |
| 71 | +then |
| 72 | + echo "Error: Make sure $graphdir is a valid directory to store generated graphs!" |
| 73 | + display_help |
| 74 | + exit |
| 75 | +fi |
| 76 | + |
| 77 | +check_pre_requisites $inputpkgname $venvdir |
| 78 | +echo "Using $reqfile to prune dependency graph of $inputpkgname." |
| 79 | +echo "Specified output directory is $graphdir." |
| 80 | +echo "Generating $inputpkgname dependency graphs..." |
| 81 | + |
| 82 | +inputfile=$graphdir/numpy-${inputpkgname}-dep.dot |
| 83 | +outputfile=$graphdir/numpy-${inputpkgname}-dep.png |
| 84 | +$venvdir/env-${inputpkgname}/bin/pipdeptree --graph-output dot > $graphdir/numpy-${inputpkgname}-dep.dot |
| 85 | +dot -T png $inputfile -o $outputfile |
| 86 | + |
| 87 | +awk '/^[0-9]*[.]+/ {printf(" %s\n", $0); found=1; next} !/^[0-9]*[.]+/ {printf((found==1)? "%s" : "\n%s", $0); found=0 } END {print ""}' $inputfile > $graphdir/cleanup.dot |
| 88 | + |
| 89 | +echo "Cleaning up requirements file...." |
| 90 | +reqfiletmp=$graphdir/tmpfile |
| 91 | +reqfiletmp1=$graphdir/tmpfile1 |
| 92 | + |
| 93 | +sed '/^#/ d' $reqfile > $reqfiletmp |
| 94 | +sed '/^$/ d' $reqfiletmp > $reqfiletmp1 |
| 95 | +awk -F '<|=|>|;' '{print $1}' $reqfiletmp1 > $graphdir/reqfileclean |
| 96 | + |
| 97 | +echo digraph { > $graphdir/cleanup_pkg.dot |
| 98 | + |
| 99 | +awk ' NR==FNR{a[$1]; next} {for (i in a) if ((index($0, i)&& $3 == "") || ($3 != "" && index($0, i) && index($3, i) )) print}' $graphdir/reqfileclean $graphdir/cleanup.dot >> $graphdir/cleanup_pkg.dot |
| 100 | + |
| 101 | +echo "Cleaning up temp files" |
| 102 | +\rm -rf $graphdir/tmpfile $graphdir/tmpfile1 $graphdir/reqfileclean |
| 103 | +echo } >> $graphdir/cleanup_pkg.dot |
| 104 | + |
| 105 | +dot -K twopi $graphdir/cleanup_pkg.dot -o $graphdir/numpy-clean.dot |
| 106 | +dot -T png $graphdir/numpy-clean.dot -o $graphdir/numpy-clean.png |
| 107 | + |
| 108 | +if [ -z "$5" ]; |
| 109 | +then |
| 110 | + #hcolor="crimson" |
| 111 | + hcolor="cyan" |
| 112 | +else |
| 113 | + hcolor="$5" |
| 114 | +fi |
| 115 | + |
| 116 | +echo "Highlighting numpy nodes using $hcolor." |
| 117 | + |
| 118 | +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 |
| 119 | + |
| 120 | +dot -T png $graphdir/numpy-clean-color.dot -o $graphdir/numpy-clean-color.png |
| 121 | + |
| 122 | +echo "-------------------------------------------------------------------" |
| 123 | +echo Done! |
0 commit comments