Wiki Ubuntu-it

Indice
Partecipa
FAQ
Wiki Blog
------------------
Ubuntu-it.org
Forum
Chiedi
Chat
Cerca
Planet
  • Pagina non alterabile
  • Informazioni
  • Allegati
  • Differenze per "SimoneFittabile/IDP/Script"
Differenze tra le versioni 2 e 3
Versione 2 del 04/02/2007 17.47.23
Dimensione: 3996
Commento:
Versione 3 del 05/02/2007 03.09.04
Dimensione: 3999
Commento:
Le cancellazioni sono segnalate in questo modo. Le aggiunte sono segnalate in questo modo.
Linea 5: Linea 5:
[[BR]]''brutto codice, lo so me lo perdoni chi può''[[BR]] [[BR]]''brutto codice, lo so me lo corregga chi può'':)[[BR]]

'Requisiti':apt,python,wget,coreutils,sed,grep,gzip

IDP.py

BRbrutto codice, lo so me lo corregga chi può:)BR

   1 #!/usr/bin/python
   2 import sys,commands,math
   3 
   4 global urlbase,wget,col
   5 #urlbase="http://localhost/websites/popcon-ubuntu/popcon.ubuntu.com/"
   6 urlbase="http://popcon.ubuntu.com/"
   7 #urlbase="http://popcon.debian.org/"
   8 urlbase="http://popcon.ubuntu.com/"
   9 wget='wget -qNr -O '
  10 col=int(commands.getoutput('export COLUMNS; echo $COLUMNS'))
  11 
  12 def main():
  13         i=init()
  14         s=scarica(i[0],i[1],i[2],i[3],i[4],0)
  15         e=elabora(i[0],s[0],i[5])
  16         calcola(e[0],e[1],e[2],e[3])
  17         sys.exit(0)
  18         
  19 def init():
  20         l=len(sys.argv) #parole in linea di comando
  21         if (l!=2):
  22                 if (l==1):
  23                         print "Inserire un nome di pacchetto\n es: $ sudo ./idp.py htop"
  24                         sys.exit(1)
  25                 elif (l>2):
  26                         print "Attenzione: non accetto piu' di un pacchetto per volta! Solo il primo verra' considerato"
  27         p=sys.argv[1]
  28         print ""
  29         h=len(p)+2
  30         h=h*"="
  31         print h.center(col)
  32         print p.upper().center(col)
  33         print h.center(col)     
  34         gfile='by_vote.gz'
  35         sez=commands.getoutput('apt-cache show -q=2 '+p+' |grep -m 1 \"Section: \" |sed -e \'s/Section:\s//g\'')
  36         commands.getoutput('echo \"'+sez+'\" > tf~')
  37         r=commands.getoutput('wc -l tf~ |sed -e \'s/\stf~//g \'')
  38         r=int(r)
  39         if (r!=1):
  40                 print "Errore: pacchetto inesistente o errore nel db locale,"
  41                 print "prova ad aggiornare il db pacchetti con 'sudo apt-get update'"
  42                 sys.exit(1)
  43         #se non esiste /usr/bin/wget: FIXME
  44                 #print "Installa wget e riprova, grazie!"
  45                 #sys.exit(1)
  46         if (sez.count('/')==0): #
  47                 ramo='main'
  48                 cat=sez
  49         else:
  50                 sez=sez.rsplit('/') #
  51                 ramo=sez[0]
  52                 cat=sez[1]
  53         print "Ramo: "+ramo
  54         print "Categoria: "+cat.strip('/')
  55         return [gfile,urlbase,wget,ramo,cat,p]
  56 
  57 def scarica(gfile,urlbase,wget,ramo,cat,unkn):
  58         catfile=ramo+'_'+cat.strip('/')+'_'+gfile
  59         if (unkn!=1):
  60                 cat+='/'
  61                 print "Recupero graduatoria globale   ("+urlbase+gfile+")..."
  62                 commands.getoutput(wget+gfile+' '+urlbase+gfile) #scarica graduatoria globale ordinata per voto
  63                 #se by_vote.gz esiste FIXME
  64                 print "OK"
  65         print "Recupero graduatoria di categoria   ("+urlbase+ramo+"/"+cat+gfile+")..."
  66         commands.getoutput(wget+catfile+' '+urlbase+ramo+'/'+cat+gfile) #scarica graduatoria della categoria ordinata per voto
  67         #se catfile esiste FIXME
  68         print "OK"
  69         return [catfile]
  70 
  71 def elabora(gfile,catfile,p):
  72         commands.getoutput('zcat '+catfile+' | grep -m 1 \"'+p+'   \" > tf~')
  73         test=commands.getoutput('cat tf~')
  74         if (test=='' or test.isspace()==1): #qualcosa va storto non c'e' il pacchetto nella graduat.di categoria
  75                 print "Maledizione, il pacchetto e' in 'unknown', devo riscaricare la categoria!" # :-)
  76                 unkn=1
  77                 s=scarica(gfile,urlbase,wget,'unknown','',1)
  78                 return elabora(gfile,s[0],p)
  79         c_rank=float(commands.getoutput('sed -e \'1!d\' tf~').rsplit(' ')[0]) #rank in graduatoria della categoria
  80         #print c_rank
  81         commands.getoutput('zcat '+catfile+' | grep \"Total   \" > tf~')
  82         c_tot=int(commands.getoutput('sed -e \'1!d\' tf~').rsplit(' ')[0]) #totale entry in graduatoria della categoria
  83         #print c_tot
  84         commands.getoutput('zcat '+gfile+' | grep \"Total   \" > tf~')
  85         g_tot=int(commands.getoutput('sed -e \'1!d\' tf~').rsplit(' ')[0]) #totale entry in graduatoria globale
  86         #print g_tot
  87         grep_tmp=commands.getoutput('zcat '+gfile+' | grep -m 1 \"'+p+'   \" > tf~')
  88         g_rank=float(commands.getoutput('sed -e \'1!d\' tf~').rsplit(' ')[0]) #rank in graduatoria globale
  89         #print g_rank
  90         commands.getoutput('rm -f *.gz tf~') #rimuovo file temporanei
  91         return [g_tot,g_rank,c_tot,c_rank]
  92         
  93 def calcola(g_tot,g_rank,c_tot,c_rank):
  94         idp=(((g_tot - g_rank) / g_tot) + ((c_tot - c_rank) / c_tot)) * 50 #calcolo I.D.P.
  95         if (idp-round(int(idp),1) < 0.5): #arrotondamento all'intero
  96                 idp=math.floor(idp) 
  97         else:
  98                 idp=math.ceil(idp)
  99         print ""
 100         idp="I.D.P.="+str(int(idp))+"%"
 101         print idp.center(col)
 102         print ""
 103         
 104 main()

DOWNLOAD-SCARICA attachment:idp.py BRS.