Skip to content

Commit

Permalink
Use StringIO where BytesIO is not available (Python 2.5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Maier authored and nmaier committed May 17, 2011
1 parent abd995c commit 780df68
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions xpisign.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

from __future__ import with_statement

import io
import os
import re
import zipfile
Expand All @@ -43,6 +42,20 @@
from base64 import b64encode as base64
from hashlib import md5, sha1

try:
from io import BytesIO
except ImportError:
try:
from cStringIO import cStringIO as _BytesIO
except:
from StringIO import StringIO as _BytesIO
class BytesIO(_BytesIO):
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()


try:
import M2Crypto.SMIME as M2S
import M2Crypto.X509 as M2X509
Expand Down Expand Up @@ -190,13 +203,13 @@ def maybe_optimize_inner_archive(name, content):
if not RE_ARCHIVES.search(name):
return name, content

with io.BytesIO(content) as cp:
with BytesIO(content) as cp:
with ZipFile(cp, "r") as zp:
files = [maybe_optimize_inner_archive(n, zp.read(n))
for n in sorted(zp.namelist())
if not RE_DIRECTORY.search(n)
]
rv = io.BytesIO()
rv = BytesIO()
with StreamPositionRestore(rv):
with ZipFile(rv, "w", zipfile.ZIP_STORED) as zp:
for i,c in files:
Expand Down Expand Up @@ -247,7 +260,7 @@ def xpisign(xpifile,
return outfile

if not outfile:
outfile = io.BytesIO()
outfile = BytesIO()

# read file list and contents, skipping any existing meta files
with StreamPositionRestore(xpifile):
Expand Down Expand Up @@ -363,7 +376,7 @@ def main(args):
try:
# buffer stuff, in case xpifile == outfile
with open(xpifile, "rb") as tp:
xp = io.BytesIO(tp.read())
xp = BytesIO(tp.read())
with xp:
try:
with open(outfile, "wb") as op:
Expand Down

0 comments on commit 780df68

Please sign in to comment.