jump to navigation

Chromium updater 21 Agosto 2009

Posted by fripp in Browser, Chrome, Chromium, Informatica, Mac OS X, Programmazione, Python, Scripting, Sistemi Operativi, Unix.
Tags: , , , , , , , , ,
trackback

Per passatempo ho scritto in questi giorni un piccolissimo script python che controlla se esiste una nuova versione per il browser Chromium per Mac OS X; in tal caso scarica il file .zip da http://build.chromium.org/buildbot/snapshots/chromium-rel-mac, lo scompatta e salva l’applicazione Chromium.app sul Desktop, in modo che successivamente l’utente possa copiarla dove meglio crede.

Ecco il codice

#!/usr/bin/env python
'''
Created on 18/ago/2009

@author: feanor
'''
import urllib
import string
import shelve
import os
import sys
import shutil
import commands

class LatestVersionRetriever(object):

 def get_latest_version(self):
 '''Ottiene dal web l'ultima versione di Chromium per Mac e ritorna il numero di version'''
 version =  None
 tuple = urllib.urlretrieve("http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/LATEST")
 filename = tuple[0]

 with open(filename,"r") as file:
 version = file.read()

 return version

class ChromeDownloader(object):
 def __init__(self,latest_version):
 self.latest_version = latest_version
 self.current_version = None

 def compare_versions(self):
 home = os.getenv('HOME')
 path = os.path.join(home,".chromium_version")

 s = shelve.open(path)

 self.current_version = s['current_version']

 if self.current_version != self.latest_version:
 s['current_version'] = self.latest_version
 s.close()

 return True

 else:
 s.close()
 return False

 def download(self):
 home = os.getenv('HOME')
 if self.compare_versions():
 print "A new version of Chromium is available: " + self.latest_version
 print "You have version: " + self.current_version

 #Crea l'url da cui scaricare l'ultima versione di chromium
 url = string.join(["http://build.chromium.org/buildbot/snapshots/chromium-rel-mac",self.latest_version,"chrome-mac.zip"], "/")

 #Si scarica l'archivio .zip in un file temporaneo
 tmpfile = urllib.urlretrieve(url,reporthook = self.dlProgress)[0]

 print "\nDownload completed"

 #Si scompatta l'archivio usando il comando unzip
 #Ho provato ad utilizzare il modulo zipfile della libreria standard Python, ma non riesco a capire
 #come mai il file estratto non viene eseguito dal sistema
 commands.getstatusoutput("unzip " + tmpfile + " -d " + os.path.dirname(tmpfile))

 #Spostiamo il file .app appena estratto sul Desktop
 shutil.move(os.path.join(os.path.dirname(tmpfile),"chrome-mac","Chromium.app"), os.path.join(home,"Desktop"))

 #Cancelliamo le directory create da unzip
 shutil.rmtree(os.path.join(os.path.dirname(tmpfile),"chrome-mac"))

 else:
 print "Chromium is up to date"

 def dlProgress(self,count, blockSize, totalSize):
 percent = int(count*blockSize*100/totalSize)

 sys.stdout.write("\r" + "Download" + "...%d%%" % percent)
 sys.stdout.flush()

def first_version():
 #Inizializza per la prima volta il database dell'ultima versione installata
 home = os.getenv('HOME')

 path = os.path.join(home,".chromium_version")

 if not os.path.exists(path):
 s = shelve.open(path)
 s['current_version'] = "23600"

 s.close()

def checkenviron():
 if not sys.version.startswith('2.6'):
 print "You have to install python version >= 2.6 in order to use this software"
 return False

 ret = commands.getstatusoutput("unzip")[0]

 if ret is not 0:
 print "You have to install unzip in order to use this software"
 return False

 return True

if __name__ == '__main__':

 if checkenviron():

 first_version()

 r = LatestVersionRetriever()

 latest_version = r.get_latest_version()

 cd = ChromeDownloader(latest_version)

 cd.download()

Per eseguire correttamente il programma occorre avere installato sul proprio Mac Python 2.6 e unzip

Commenti»

1. kususe - 4 Settembre 2009

perdona l’ignoranza, ma questo è Phyton?
E’ davvero così versatile come dicono??
quanto può servire una buona conoscenza di c++??

grazie per l’attenzione
saluti

2. fripp - 4 Settembre 2009

Esatto, è proprio Python e ti posso dire che è veramente versatile. Puoi farci praticamente di tutto.Per quanto riguarda la conoscenza del C++, posso dirti per esperienza personale che non serve. Basta avere buone conoscenze dei paradgmi della programmazione ad oggetti e di quella strutturata e/o funzionale

3. kususe - 6 Settembre 2009

qualche giuda/libro in particolare da consigliarmi per capire un po di che si tratta??

4. fripp - 7 Settembre 2009

Per iniziare ti consiglio:
Dive into Python http://diveintopython.org/
Learning Python http://www.rmi.net/~lutz/about-lp3e.html