2

我正在尝试从他们的快照中提取 Chromium.app 的新修订版,我可以很好地下载该文件,但是在提取它时,ZipFile 要么将 chrome-mac 文件夹提取为一个文件,而是说目录不' t 存在,等等。我对 python 很陌生,所以这些错误对我来说意义不大。这是我到目前为止所拥有的。

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()

有任何想法吗?

4

4 回答 4

4

看来您在 Python 中遇到了一个错误。This other question详细说明了问题和解决方法。您可以选择使用其中一种解决方法,或更新到 Python 2.6.5 或 2.7b2。

一种解决方法建议从固定的 Python复制修补的 zipfile.py 模块。

祝你好运!

于 2010-05-29T14:00:27.753 回答
0

这似乎对我有用:

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()
于 2010-05-28T11:23:04.097 回答
0

这是另一个剪辑 - 这是相同的技术,但它会遍历结果以证明它有效。

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()

我运行它时得到的输出是

> .\getapp.py
getting revision 48479
extracted 537 files in 184 dirs
于 2010-05-28T14:34:10.823 回答
0

在 Python 中从 zip 中提取 .app 存在另一个问题(通常的 zip 实用程序不会发生这种情况)。好像没有人提到过这个……

由于丢失了嵌套二进制文件上的执行权限位,.app 可以停止以这种方式进行后提取。不过,您可以通过再次授予该权限来解决此问题。

这是我正在使用的一个松散的代码片段。根据您的目的修改它(或编写更通用的函数以更通用的方式处理这种情况):

import os, zipfile
...
ZIP_PATH     = APP_PATH + ".zip" 
APP_BIN_DIR  = os.path.join( APP_PATH, "Contents/MacOS" )
zipfile.ZipFile( ZIP_PATH, 'r' ).extractall( WORK_DIR )   
BIN_PATH = os.path.join( APP_BIN_DIR, os.listdir( APP_BIN_DIR )[0] )
os.chmod( BIN_PATH, 0o777 )

我的程序已经知道在哪里APP_PATH可以找到 (即在 中WORK_DIR)。不过,我不得不把它拉上拉链,然后在事后把那个细节塞进去。我将我的 zip 命名为XXXXX.app.zip. 我很简单地解决了BIN_PATH这里的问题,不需要知道 .app 中的二进制文件的名称,因为我知道我的用例中只有一个文件。我授予它完全 (777) 权限,因为无论如何我只是删除了脚本末尾的 .app 。

于 2019-03-17T23:03:03.573 回答