Created
September 13, 2011 13:37
-
-
Save son-link/1213816 to your computer and use it in GitHub Desktop.
Ejecuantondo 2 threads a la vez
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# -*- coding: utf-8 -*- | |
import threading, time, json | |
from urllib import urlretrieve, urlopen | |
from sys import stdout | |
from commands import getoutput | |
i = 0 | |
def getAUR(): | |
aursearch = 'https://aur.archlinux.org/rpc.php?type=info&arg=' | |
porcentaje = porcion*i*100 | |
# partimos pkg en 2, una con el nombre del programa y otro con la versión | |
pkg = pkgs[i].split() | |
# imprimimos en pantalla el progreso | |
#stdout.write('\rDescargando información de los paquetes desde AUR %i (%i): %s' % (i, porcentaje, pkg[0])) | |
#stdout.flush() | |
print 'Descargando', pkg | |
# Abrimos la url para solicitar a AUR que nos de los datos del programa | |
raw = urlopen(aursearch+pkg[0]).read() | |
# Y lo pasamos por json para poder obtener los datos | |
data = json.loads(raw) | |
# Si el paquete existe en AUR type valdra info, en caso contrario vale error | |
if data['type'] == 'info': | |
# Comprobamos que la versión de AUR es nueva | |
isnew = getoutput('vercmp "%s" "%s"' % (data['results']['Version'], pkg[1])) | |
#print isnew | |
if isnew == '1': | |
# si la versión de AUR es mas nueva que la local se añade a la lista | |
aurupdates.append((data['results']['Name'], pkg[1], data['results']['Version'])) | |
class MiThread(threading.Thread): | |
def __init__(self, evento): | |
threading.Thread.__init__(self) | |
self.evento = evento | |
def run(self): | |
getAUR() | |
#pkgs = ['bleachbit', 'haguichi', 'pacsyu', 'dolphin-emu-svn'] | |
pkgs = getoutput('pacman -Qm').split('\n') | |
print pkgs | |
aurupdates = [] | |
porcion = 1.0 / len(pkgs) | |
evento = threading.Event() | |
while i <= len(pkgs): | |
t1 = MiThread(evento) | |
t1.start() | |
i += 1 | |
t2 = MiThread(evento) | |
t2.start() | |
t2.join() | |
i += 1 | |
# Esperamos un poco | |
time.sleep(1) | |
evento.set() | |
print aurupdates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment