Skip to content

Instantly share code, notes, and snippets.

@smichr
Last active December 15, 2015 04:39
Show Gist options
  • Save smichr/5203202 to your computer and use it in GitHub Desktop.
Save smichr/5203202 to your computer and use it in GitHub Desktop.
def gut(string):
"""gzip the string and return the uu -encoded version."""
import StringIO, uu, gzip, hashlib
def md5(e):
return hashlib.md5(str(e)).hexdigest()
zipp = StringIO.StringIO()
uuu = StringIO.StringIO()
g = gzip.GzipFile(fileobj=zipp, mode='wb') # gzip(path, 'wb')
g.write(string)
g.close()
zipp.seek(0)
uu.encode(zipp, uuu)
# qp uu.encode(zipp, uuu, 1)
# uu.binhex(zipp, uuu)
zipp.seek(0);zipp.flush()
uuu.seek(0)
code = uuu.read()
s = code.strip()
if s.endswith('end'):
lines = s.splitlines()[:-1] # remove the end
if not lines[-1].strip():
lines[-1] = 'end\n'
code = '\n'.join(lines)
[f.close() for f in [uuu, zipp, g]]
return """
Copy the text between triple-# and run as a script to decode.
###
code = \\
r'''%s'''
def tug(string):
\"\"\"Return text of a uu encoded gzipped text.\"\"\"
import StringIO, uu, gzip
f1 = StringIO.StringIO()
f2 = StringIO.StringIO()
f2.write(string)
f2.seek(0)
uu.decode(f2, f1)
f2.seek(0)
f2.flush()
f1.seek(0)
text = gzip.GzipFile(fileobj=f1, mode='rb')
msg = text.read()
[f.close() for f in [text, f1, f2]]
return msg
code = tug(code)
import hashlib
assert hashlib.md5(str(code)).hexdigest() == \\
'%s'
###""" % (code, md5(string))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment