2010-05-28 6 views
1

J'essaie d'extraire les nouvelles révisions de Chromium.app à partir de leurs instantanés, et je peux télécharger le fichier correctement, mais quand il s'agit de l'extraire, ZipFile extrait le dossier chrome-mac en tant que fichier, dit que les répertoires n'existent pas, etc. Je suis très nouveau sur python, donc ces erreurs ont peu de sens pour moi. Voici ce que j'ai jusqu'ici.Extraire un fichier .app à partir d'un fichier zip en Python, en utilisant ZipFile

import urllib2 
response = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/LATEST') 
latestRev = response.read() 
print latestRev 

# we have the revision, now we need to download the zip and extract it 
latestZip = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/%i/chrome-mac.zip' % (int(latestRev)), '~/Desktop/ChromiumUpdate/%i-update' % (int(latestRev))) 
#declare some vars that hold paths n shit 
workingDir = '/Users/slehan/Desktop/ChromiumUpdate/' 
chromiumZipPath = '%s%i-update.zip' % (workingDir, (int(latestRev))) 
chromiumAppPath = 'chrome-mac/' #the path of the chromium executable within the zip file 
chromiumAppExtracted = '%s/Chromium.app' % (workingDir) # path of the extracted executable 

output = open(chromiumZipPath, 'w') #delete any current file there 
output.write(latestZip.read()) 
output.close() 

# we have the .zip now we need to extract the Chromium.app file, it's in ziproot/chrome-mac/Chromium.app 
import zipfile, os 
zippedFile = open(chromiumZipPath) 
zippedChromium = zipfile.ZipFile(zippedFile, 'r') 
zippedChromium.extract(chromiumAppPath, workingDir) 
#print zippedChromium.namelist() 

zippedChromium.close() 
#zippedChromium.close() 

Des idées?

Répondre

4

Il semble que vous ayez rencontré un bug in Python. Ce other question détaille le problème et les solutions de contournement. Vous pouvez choisir d'utiliser l'une de ces solutions ou mettre à jour vers Python 2.6.5 ou 2.7b2. L'une des solutions de contournement suggère de copier le patched zipfile.py module à partir du correctif Python.

Bonne chance!

+0

Eh bien beurrer mon cul et appelez-moi Susan, j'ai mis à jour et ça marche maintenant. – skylerl

0

Cela semble fonctionner pour moi:

import os 
import urllib2 
import zipfile 
from StringIO import StringIO 

response = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/LATEST') 
latestRev = response.read() 
print 'getting revision', latestRev 

# we have the revision, now we need to download the zip and extract it 
locRef='http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/%i/chrome-mac.zip' % (int(latestRev)) 
latestZip = StringIO(urllib2.urlopen(locRef).read()) 

# we have the .zip now we need to extract the Chromium.app file, it's in chrome-mac/Chromium.app/ 
zippedChromium = zipfile.ZipFile(latestZip) 
# find all zip members in chrome-mac/Chromium.app 
members = [m for m in zippedChromium.namelist() if m.startswith('chrome-mac/Chromium.app/')] 
#zippedChromium.extract(chromiumAppPath, workingDir) 
target = 'chromium-%s' % latestRev 
if os.path.isdir(target): 
    print 'destination already exists, exiting' 
    raise SystemExit(1) 
os.makedirs(target) 
zippedChromium.extractall(target, members) 

#zippedChromium.close() 
+0

Lorsque je l'exécute, il retourne: destination existe déjà, en quittant – skylerl

+0

J'ai commenté la raise SystemExit (1) et il fait le répertoire, mais quand il extrait le .app, il ne comprend pas tout, juste le fichier lui-même. Avec .apps, il y a un dossier appelé Contents, qui n'a pas été téléchargé du tout. – skylerl

+0

Au lieu de commenter SystemExit, supprimez chromium-NNNNN avant d'exécuter le script. Lorsque vous exécutez le script, il créera le répertoire chrome-NNNNN et extraira chrome-mac/Chromium.app/* en chrome-NNNNN. –

0

Voici une autre coupe - c'est la même technique, mais il marche le résultat de démontrer que cela fonctionne.

import os 
import urllib2 
import zipfile 
from StringIO import StringIO 

response = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/LATEST') 
latestRev = response.read() 
print 'getting revision', latestRev 

# we have the revision, now we need to download the zip and extract it 
locRef='http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/%i/chrome-mac.zip' % (int(latestRev)) 
latestZip = StringIO(urllib2.urlopen(locRef).read()) 

# we have the .zip now we need to extract the Chromium.app file, it's in chrome-mac/Chromium.app/ 
zippedChromium = zipfile.ZipFile(latestZip) 
# find all zip members in chrome-mac/Chromium.app 
members = [m for m in zippedChromium.namelist() if m.startswith('chrome-mac/Chromium.app/')] 
#zippedChromium.extract(chromiumAppPath, workingDir) 
target = 'chromium-%s' % latestRev 
if os.path.isdir(target): 
    print 'destination already exists, exiting' 
    raise SystemExit(1) 
os.makedirs(target) 
zippedChromium.extractall(target, members) 

lengths = [ 
    (len(dirnames), len(filenames)) 
    for dirpath, dirnames, filenames in os.walk(target) 
    ] 
dirlengths, filelengths = zip(*lengths) 
ndirs = sum(dirlengths) 
nfiles = sum(filelengths) 
print 'extracted %(nfiles)d files in %(ndirs)d dirs' % vars() 
#zippedChromium.close() 

La sortie que je reçois quand je le lance est

> .\getapp.py 
getting revision 48479 
extracted 537 files in 184 dirs 
+0

Même chose = (IOError: [Errno 20] Pas un répertoire : 'chrome-48479/chrome-mac/Chromium.app/Contents' – skylerl

Questions connexes