Advertisement
anjishnu

apt-aria2

Sep 15th, 2011
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.13 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## apt-aria2: To help download packages faster via aria2, instead of wget.
  4. ## Author: Anjishnu Sarkar
  5. ## Version: 0.5
  6. ## Acknowledgement: This script is a rewrite of the apt-fast script by
  7. ## Matt Parnell (admin@mattparnell.com) (http://www.mattparnell.com)
  8. ## Usage: Same as apt-get. Using the option "-y" always.
  9.  
  10. ## BUG:
  11. ## *) If this script is interuppted, then next time aria2 starts downloading
  12. ## the same from the begining. Can be solved - something to do with .st file.
  13.  
  14. ## TODO:
  15. ## *) Start installing via apt-get as soon as first package is downloaded
  16. ##    and also keep downloading at the same time. This however might lead
  17. ##    to dependencies not being satisfied.
  18.  
  19. ## Initialization(s):
  20. Download="False"
  21. Install="True"
  22. Confirm="True"
  23. UniqueName="$RANDOM"
  24. Options="$@"
  25.  
  26. ## Checking for commands which requires download
  27. while test -n "${1}"
  28. do
  29.     case "${1}" in
  30.       install|upgrade|dist-upgrade|source|build-dep)
  31.         ## Download
  32.         Download="True"
  33.         ;;
  34.       update|remove|autoremove|purge|dselect-upgrade|clean|autoclean|check)
  35.         ## Anything other than download
  36.         Download="False"
  37.         ;;
  38.      -d) ## Download only (don't install)
  39.         Install="False"
  40.         ;;
  41.      -y) ## No need to ask for confirmation
  42.         Confirm="False"
  43.         ;;
  44.       *)
  45.         ## Nothing to be done. If any wrong options/commands are given then
  46.         ## let apt-get handle it.
  47.         ;;
  48.     esac
  49.     shift
  50. done
  51.  
  52. ## In case download is true
  53. if [ "$Download" == "True" ];then
  54.  
  55.     ## Installing pre-requisite(s): aria2
  56.     if ! which aria2c > /dev/null; then
  57.         echo "Aria2 not installed. Installing aria2 first via apt-get"
  58.         apt-get -y --force-yes install aria2
  59.     fi
  60.  
  61.     ArchiveDir=/var/cache/apt/archives/
  62.     cd ${ArchiveDir}/partial
  63.  
  64.     PrintUris=$(apt-get --yes --print-uris ${Options})
  65.     if [ $? -ne 0 ];then
  66.         echo "Aborting."
  67.         exit 1
  68.     fi
  69.  
  70.     PackageInfo=$(echo "$PrintUris" | awk '/Reading package/,/After this operation/')
  71. #     echo "$PrintUris" | grep ^\' | cut -d\' -f2 > "$UniqueName"-uris.txt
  72.     echo "$PrintUris" | grep "http:" | cut -d\' -f2 > "$UniqueName"-uris.txt
  73.     NumberOfPackages=$(wc -l "$UniqueName"-uris.txt | awk '{print $1}')
  74.  
  75.     ## Print info
  76.     echo "$PackageInfo"
  77.     echo "Number of packages to be downloaded: $NumberOfPackages"
  78.  
  79.     ## Check whether package has already been installed or not
  80.     InstallUpgradeMsg=$(echo "$PackageInfo" | grep \
  81.         -e "The following NEW packages will be installed:" \
  82.         -e "The following packages will be upgraded:")
  83.     if [ -z "$InstallUpgradeMsg" ];then
  84.         rm -f "$UniqueName"-uris.txt
  85.         exit 0
  86.     fi
  87.     ## In $InstallUpgradeMsg is not null, then proceed...
  88.  
  89.     ## If confirm is true
  90.     if [ "$Confirm" == "True" ];then
  91.         echo -n "Do you want to continue [y|n]? "
  92.         read Ans
  93.  
  94.         case "$Ans" in
  95.             y|yes|"")   ;;
  96.  
  97.             n|no|*)     echo "Abort."
  98.                         rm -f "$UniqueName"-uris.txt
  99.                         exit 1 ;;
  100.         esac
  101.     fi
  102.  
  103.     if [ $NumberOfPackages -ne 0 ];then
  104.         ## Downloading the packages
  105.         echo "Proceeding with downloading ..."
  106.         while read DebUrl
  107.         do
  108.             DebName=$(basename "$DebUrl")
  109.             echo "$DebName"
  110.  
  111.             AptConf="/etc/apt/apt.conf"
  112.             if [ -f "$AptConf" ];then
  113.                 http_proxy=$(grep -i "http::proxy" "$AptConf" | cut -d \" -f2)
  114.             fi
  115.  
  116.             if [ -n "$http_proxy" ];then
  117.                 echo "Using proxy..."
  118.                 aria2c -c -s 10 -j 10 --http-proxy=$http_proxy "$DebUrl"
  119.             else
  120.                 echo "Not using proxy..."
  121.                 aria2c -c -s 10 -j 10 "$DebUrl"
  122.             fi
  123.             if [ $? -eq 0 ];then
  124.                 mv $DebName ${ArchiveDir}
  125.             fi
  126.         done < "$UniqueName"-uris.txt
  127.     fi
  128.     rm -f "$UniqueName"-uris.txt
  129.  
  130. #     echo "Installing..."
  131.     if [ "$Install" == "True" ];then
  132.         apt-get -y --force-yes ${Options}
  133.     fi
  134. else
  135. ## Cases when download is false
  136.     apt-get ${Options}
  137. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement