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