Xtraeme

Locally Downloading All Reverse Dependencies with Apt-Get

Oct 21st, 2016
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.74 KB | None | 0 0
  1. #!/bin/bash
  2. export MAXPARAMETERS=255
  3.  
  4. function array_contains_find_index() {
  5.     local n=$#
  6.     local i=0
  7.     local value=${!n}
  8.  
  9.     for (( i=1; i < n; i++ )) {
  10.     if [ "${!i}" == "${value}" ]; then
  11.         echo "REMOVING $i: ${!i} = ${value}"
  12.         return $i
  13.     fi
  14.     }
  15.     return $MAXPARAMETERS
  16. }
  17.  
  18. function Pause() {
  19.     if [[ -z $1 ]]; then
  20.         read -n1 -r -p "continue..."
  21.     else
  22.         read -n1 -r -p "$1"
  23.     fi
  24. }
  25.  
  26. export IFS=$'\n'
  27. # Store all reverse dependencies in an indexed array and output them to STDOUT & a log file
  28. # for easy checking later
  29. LIST=( $( apt-rdepends $1 | grep -v "^ " ) )
  30. echo ${LIST[*]}
  31. echo ${LIST[*]} > getdepends.log.results
  32. Pause "... Packages that will be downloaded (Continue or CTRL+C) ..."
  33.  
  34. # Try to download the dependencies
  35. RESULTS=( $( apt-get download ${LIST[*]} |& cut -d' ' -f 8 ) )
  36.  
  37. # If RESULTS contains any items that means we have packages that are
  38. # problematic that need to be removed from LIST. (note: `|&` is shortform for `2>&1 |`)
  39.  
  40. LISTLEN=${#LIST[@]}                 #Array elements aren't removed so the size is constant
  41.  
  42. while [ ${#RESULTS[@]} -gt 0 ]; do
  43.     for (( i=0; i < $LISTLEN; i++ )); do
  44.         array_contains_find_index ${RESULTS[@]} ${LIST[$i]}
  45.         ret=$?
  46.  
  47.         if (( $ret != $MAXPARAMETERS )); then
  48.             unset LIST[$i]
  49.         fi
  50.     done
  51.    
  52.     FULLRESULTS=$( apt-get download ${LIST[*]} 2>&1  )
  53.     RESULTS=( $( echo $FULLRESULTS |& cut -d' ' -f 11 | sed -r "s/'(.*?):(.*$)/\1/g" ) )
  54.  
  55.     echo ${LIST[*]} > getdepends.list                   #Log of downloaded packages
  56.     echo ${FULLRESULTS[*]} >> getdepends.fullresults    #Verbose log of skipped packages
  57.     echo ${RESULTS[*]} >> getdepends.results            #Just the name of skipped packages
  58. done
  59.  
  60. apt-get download ${LIST[*]}
Add Comment
Please, Sign In to add comment