17.12.2012 Views

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

try:<br />

compressed = gzopen(filename+'.gz', mode='wb')<br />

try:<br />

for line in original.readlines():<br />

compressed.write(line)<br />

finally:<br />

compressed.close()<br />

os.remove(filename)<br />

finally:<br />

original.close()<br />

def _decompress(filename):<br />

""" décompresse une archive gzip<br />

attention, écrase un éventuel fichier "resultfile"<br />

"""<br />

archive = gzopen(filename)<br />

try:<br />

if filename.endswith('.gz'):<br />

resultfile = filename[:-3]<br />

else:<br />

resultfile = '%s.uncompressed' % filename<br />

uncompressed = open(resultfile, mode='w')<br />

try:<br />

for line in archive.readlines():<br />

uncompressed.write(line)<br />

finally:<br />

uncompressed.close()<br />

os.remove(filename)<br />

finally:<br />

archive.close()<br />

def main(options, arguments):<br />

if len(arguments) != 1:<br />

print 'usage: %s' % parser.usage<br />

sys.exit(2)<br />

compress = options.compress isnot None<br />

decompress = options.decompress isnot None<br />

Principaux modules<br />

CHAPITRE 8<br />

if (compress and decompress) or (not compress and not decompress):<br />

print 'usage: %s' % parser.usage<br />

sys.exit(2)<br />

filename = arguments[0]<br />

if compress:<br />

_compress(filename)<br />

else:<br />

_decompress(filename)<br />

253

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!