PluginDownloader: Make plugin py3k-friendly.

This commit is contained in:
Valentin Lorentz 2012-08-04 20:39:30 +02:00
parent 0134696b8b
commit c99fe9519e

View File

@ -29,6 +29,8 @@
### ###
import os import os
import io
import sys
import json import json
import shutil import shutil
import urllib import urllib
@ -36,6 +38,7 @@ import urllib2
import tarfile import tarfile
from cStringIO import StringIO from cStringIO import StringIO
BytesIO = StringIO if sys.version_info[0] < 3 else io.BytesIO
import supybot.log as log import supybot.log as log
import supybot.conf as conf import supybot.conf as conf
@ -79,7 +82,7 @@ class GithubRepository(GitRepository):
args = dict([(x,y) for x,y in args.items() if y is not None]) args = dict([(x,y) for x,y in args.items() if y is not None])
url = '%s/%s/%s?%s' % (self._apiUrl, type_, uri_end, url = '%s/%s/%s?%s' % (self._apiUrl, type_, uri_end,
urllib.urlencode(args)) urllib.urlencode(args))
return json.load(utils.web.getUrlFd(url)) return json.loads(utils.web.getUrl(url).decode('utf8'))
def getPluginList(self): def getPluginList(self):
plugins = self._query( plugins = self._query(
@ -101,14 +104,17 @@ class GithubRepository(GitRepository):
def _download(self, plugin): def _download(self, plugin):
try: try:
fileObject = urllib2.urlopen(self._downloadUrl) response = utils.web.getUrlFd(self._downloadUrl)
fileObject2 = StringIO() if sys.version_info[0] < 3:
fileObject2.write(fileObject.read()) assert response.getcode() == 200, response.getcode()
fileObject.close() else:
fileObject2.seek(0) assert response.status == 200, response.status
return tarfile.open(fileobj=fileObject2, mode='r:gz') fileObject = BytesIO()
finally: fileObject.write(response.read())
del fileObject finally: # urllib does not handle 'with' statements :(
response.close()
fileObject.seek(0)
return tarfile.open(fileobj=fileObject, mode='r:gz')
def install(self, plugin): def install(self, plugin):
archive = self._download(plugin) archive = self._download(plugin)
prefix = archive.getnames()[0] prefix = archive.getnames()[0]