Advertisement
Guest User

pacspeed revamp

a guest
Sep 11th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.28 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # architettura definita automaticamente
  4. arch=$(uname -m)
  5.  
  6. # Configurazione di aria2c
  7. ariaconf="--max-concurrent-downloads=44
  8. --file-allocation=none
  9. --continue
  10. --split=4
  11. --max-connection-per-server=4
  12. --max-tries=2
  13. --min-split-size=1M
  14. --allow-overwrite=true
  15. --summary-interval=0
  16. --log-level=error
  17. --no-conf
  18. --remote-time=true
  19. --timeout=5
  20. --dir=/var/cache/pacman/pkg
  21. --uri-selector=inorder"
  22.  
  23.  
  24. version="0.1b4"
  25. PROGNAME=$0
  26. # Si scegli se optare per confermare l' installazione da pacman
  27. NOCONFIRM=''
  28. #dichiaro un vettore
  29. declare -a args
  30. #ci butto dentro i miei argomenti
  31. args=("$@")
  32.  
  33. print_help() {
  34.     echo "Utilizzo: $PROGNAME <operazione> [...]"
  35.     echo ""
  36.     echo "<operazioni>:"
  37.     echo -e "\t $PROGNAME -S [opzioni] {pacchetto/i}"
  38.     echo -e "\t $PROGNAME -{R,U,Q,D,T,V} [opzioni] {pacchetto/i}"
  39.     echo -e "\t $PROGNAME -v"
  40.     echo ""
  41. }
  42.  
  43. #funzione di creazione lista "liscia"
  44. update_mirror() {
  45.     sed -e '/^#/d' -e '/^$/d' -e 's/Server = //' /etc/pacman.d/mirrorlist > /tmp/stripped_mirrorlist
  46. }
  47.  
  48. #funzione di generazione lista pacchetti
  49. gen_pkglist() {
  50.     declare -a argsnoupdate
  51.     index=0
  52.  
  53.     for i in "${args[@]}"
  54.     do
  55.         if [[ $i = "--refresh" ]]; then
  56.             :
  57.         elif [[ $i = "-y" ]]; then
  58.             :
  59.         elif [[ $i =~ ^"--" ]]; then
  60.             argsnoupdate[$index]=$i
  61.             let index+=1
  62.         elif [[ $i =~ ^-*y* ]]; then
  63.             argsnoupdate[$index]=$(echo $i |sed 's/y//')
  64.             let index+=1
  65.         else
  66.             argsnoupdate[$index]=$i
  67.             let index+=1
  68.         fi
  69.     done
  70.     pacman ${argsnoupdate[*]} -p --print-format %%s | sed '/.pkg.tar.[gx]z/!d' >/tmp/pkglist
  71.     gawk -F"/" '{print $(NF)}' /tmp/pkglist | gawk -F"-" '{NF--; print}' | sed 's/ /-/g' | \
  72.         sed ':a;N;$!ba;s/\n/  /g' | fold -s --width=$(tput cols)
  73. echo ""
  74. }
  75.  
  76. # funzione di generazione lista degli url creata dall'unione di lista mirror + lista pacchetti
  77. gen_addresslist() {
  78.     cat /dev/null > /tmp/addresslist
  79.     total_size=0
  80.     while read pkg
  81.     do
  82.         repo=$(echo $pkg|gawk -F"/" '{print $(NF-3)}')
  83.         pack=$(echo $pkg|gawk -F[/£] '{print $(NF-1)}')
  84.         shuf /tmp/stripped_mirrorlist |sed -e 's/$repo/'${repo}'/' | sed -e 's/$arch/'$arch'/' | \
  85.             sed -e 's/$/\/'${pack}'/' | sed -e '$!{:a;N;s/\n/\t/;ta}' >> /tmp/addresslist
  86.         size=${pkg#*£}
  87.         let total_size+=$size
  88.     done < /tmp/pkglist
  89.     total_size_h=$(($total_size/1048576))
  90.     echo "Dimensione totale dei file da scaricare:" $total_size "Byte"  "(~"$total_size_h "MiB)"
  91.     echo ""
  92. }
  93.  
  94. #funzione di download della lista in tmp
  95. download() {
  96.     if [[ $total_size != 0 ]]; then
  97.         while true; do
  98.         read -p "Vuoi eseguire il download? [S/n] " Sn
  99.         case $Sn in
  100.  
  101.         ""|[Ss]) nice_print ; break;;
  102.                 [Nn]) exit;;
  103.                 *) echo "Type S or N"
  104.             esac
  105.         done
  106.     fi
  107.  
  108. }
  109.  
  110. nice_print() {
  111.     log=$(mktemp /tmp/aria2-out.XXXXX)
  112.     trap "rm -f $log" SIGINT SIGTERM EXIT
  113.     aria2c -i /tmp/addresslist $ariaconf &>"$log" &
  114.     aria2_pid=$!
  115.     while (ps $aria2_pid &>/dev/null && [[ -f $log ]]); do
  116.         if grep -q "ERR|." "$log"; then
  117.             break
  118.         fi
  119.         #calcolo la larghezza della console, per cancellare lo stampato del
  120.         # ciclo precedente. Calcolo dinamico, per ovviare al caso
  121.         # di ridimensionamento della console
  122.         TCOLS=$(stty -a | tr -s ';' '\n' | grep "column" | sed s/'[^[:digit:]]'//g)
  123.         cout=$(sed -n '/[\[0-9\]* SIZE:.*ETA:/p' "$log" | tail -n1)
  124.         #numero di spazi meno la lunghezza dell' output di aria2
  125.         spaces='' ; NUMspace=$(($TCOLS-${#cout}))
  126.         for((j=0; j<$NUMspace; j++)); do
  127.             spaces="$spaces "
  128.         done
  129.         echo -en "\r$cout$spaces"
  130.     done
  131.     if [[ ! -f "$log" ]]; then
  132.         echo -e "\nError: Interrupted"
  133.         return 1
  134.     fi
  135.     #gli errori verranno ripresi da pacman durante la verifica dei pacchetti
  136.     if grep -q "ERR|.*$fname" "$log"; then
  137.         echo -e "\nError: Download Failure"
  138.         return 1
  139.     fi
  140.     if grep -q "errorCode=" "$log"; then
  141.         echo "\nError: something went wrong"
  142.         return 2
  143.     fi
  144.     rm -f $log >/dev/null
  145.     return 0
  146. }
  147.  
  148. #from yaourt
  149. explode_args() {
  150.     unset OPTS
  151.     local args=$1
  152.     while [[ $args ]]; do
  153.         [[ $args = "--" ]] && OPTS+=("$@") && break;
  154.         if [[ ${args:0:1} = "-" && ${args:1:1} != "-" ]]; then
  155.             OPTS+=("-${args:1:1}")
  156.             (( ${#args} > 2 )) && args="-${args:2}" || { shift; args=$1; }
  157.         else
  158.             OPTS+=("$args"); shift; args=$1
  159.         fi
  160.     done
  161. }
  162. action() {
  163.     # generazione lista pacchetti
  164.     gen_pkglist
  165.     # preparazione la lista degli url
  166.     gen_addresslist
  167.     download
  168.     rm -f /tmp/addresslist >/dev/null
  169.     rm -f /tmp/stripped_mirrorlist >/dev/null
  170.     rm -f /tmp/pkglist >/dev/null
  171. }
  172. pacman_sync() {
  173.     if [ $(whoami) != "root" ] ; then
  174.         echo "Devi essere root per continuare!"
  175.         exit 1
  176.     fi
  177.     if [[ $UPGRADE="--upgrades" || $DOWNONLY="--downloadonly" ]] ; then
  178.         PACSPEED="1"
  179.     else
  180.         PACSPEED="0"
  181.     fi
  182.  
  183.     A=${IGNOREPKG[*]}
  184.     echo $A
  185.     # creo mirrorlist "liscia"
  186.     update_mirror
  187.     #Se c'è un -S
  188.     if [ $SYNC == "1" ] ; then
  189.     #Se c'è la possibilità di accelerare il download
  190.         if [ $PACSPEED == "1" ] ;then
  191.             #Se c'è un -y, prima cosa che faccio è aggiornare la lista
  192.  
  193.             if [[ ${REFRESH[0]} = "--refresh" ]] ; then
  194.                 pacman -S ${REFRESH[*]} $NOPBAR
  195.             fi
  196.             #Se c'è un -w
  197.             if [[ $DOWNONLY = "--downloadonly" ]] ; then
  198.                 action
  199.             fi
  200.             #Se c'è un -u
  201.             if [[ $UPGRADE="--upgrades" ]] ; then
  202.             #controllo se c'è qualche ignore
  203.                 if [[ $IS_IGNOREPKG = "1" && $IS_IGNOREGRP = "1" ]] ; then
  204.                     echo 1
  205.                     while [[ $IGNOREGRP ]] ; do IGNOREARRAY+="--ignoregroup ${IGNOREGRP[0]}" ; shift ; done
  206.                     while [[ $IGNOREPKG ]] ; do IGNOREARRAY+="--ignore ${IGNOREPKG[0]}" ; shift ; done
  207.                 elif    [[ $IS_IGNOREPKG = "1" && $IS_IGNOREGRP != "1" ]] ; then
  208.                     echo 2
  209.                     while [[ ${IGNOREPKG[*]} ]] ; do IGNOREARRAY+="--ignore ${IGNOREPKG[0]}" ; echo ${IGNOREARRAY[*]} ; shift ; done
  210.                 elif [[ $IS_IGNOREPKG != "1" && $IS_IGNOREGRP = "1" ]] ; then
  211.                     echo 3
  212.                     while [[ $IGNOREGRP ]] ; do IGNOREARRAY+="--ignoregroup ${IGNOREGRP[0]}" ; shift ; done
  213.                 fi
  214.                 #Se c'è un --print continua normale
  215.                 #sennò accelero il download
  216.                 if [[ $PRINT != "--print" ]] ; then # TODO mancano il -S -Sg e basta
  217.                     action
  218.                     pacman -Su $NOCCONFIRM $NOPBAR $FORCE ${NODEPS[*]} ${OTHER[*]} ${IGNOREARRAY[*]}
  219.                 else
  220.                     pacman ${args[*]}
  221.                 fi
  222.             fi
  223.         fi
  224.     fi
  225. }
  226.  
  227. #from yaourt
  228. # Explode arguments (-Su -> -S -u)
  229. ARGSANS=("$@")
  230. explode_args "$@"
  231. set -- "${OPTS[@]}"
  232. unset OPTS
  233.  
  234. #from yaourt, only -S section
  235. while [[ $1 ]]; do
  236.     case "$1" in
  237.             -S|--sync) SYNC=1 ;;
  238.             -p|--print) PRINT="--print" ;;
  239.             -f|--force) FORCE="--force" ;;
  240.             -g|--groups) GROUP="--groups" ;;
  241.             -u|--upgrades) UPGRADE="--upgrades" ;;
  242.             -y|--refresh) REFRESH+=("--refresh") ;;
  243.             -w|--downloadonly) DOWNLONLY="--downloadonly" ;;
  244.             ##-d) ((DEPENDS++)); program_arg $((A_PS | A_M | A_PQ)) $1 ;;
  245.             -d) NODEPS+=("--nodeps") ;;
  246.             --noprogressbar|--noscriptlet) NOPBAR="--noprogressbar" ;;
  247.             --ignore) shift; IGNOREPKG+=("$1") ; IS_IGNOREPKG=1 ;;
  248.             --ignoregroup) shift; IGNOREGRP+=("$1") ; IS_IGNOREGRP=1 ;;
  249.             ##--config|--dbpath|-r|--root) program_arg $((A_PC | A_PKC)) "$1" "$2"; shift ;;
  250.             ##--cachedir|--logfile|--arch) program_arg $A_PC "$1" "$2"; shift ;;
  251.             --noconfirm) NOCONFIRM="--noconfirm" ;;
  252.             --*) ;;
  253.             -*) ;;
  254.             *) OTHER+=("$1") ;;
  255.     esac
  256.     shift
  257. done
  258.  
  259. #scarico pacchetti con le seguenti combinazioni:
  260. # -Su
  261. # -Syu
  262. # -Sw
  263. # -Sf
  264. # -S
  265.  
  266. case ${args[0]} in
  267.     -h|--help) print_help
  268.     ;;
  269.     -S*) pacman_sync
  270.     ;;
  271.     -v|--version) echo "$version"-"$arch"
  272.     ;;
  273.     *) pacman ${args[*]}
  274.     ;;
  275. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement