#!/usr/bin/env python3 """Decompress all example lzma files from http://bugs.python.org/issue21872""" import lzma import os import posixpath import sys from glob import glob from shutil import unpack_archive from subprocess import call, check_call, DEVNULL from urllib.parse import unquote, urlparse from urllib.request import urlretrieve failed = False # download, unpack zip archive with "bad" lzma files for url in ['http://bugs.python.org/file35779/Archive.zip', 'http://bugs.python.org/file35822/more_bad_lzma_files.zip']: path = posixpath.basename(unquote(urlparse(url).path)) if not os.path.exists(path): urlretrieve(url, path) unpack_archive(path) # report which files lzma module can't decompress lzma._BUFFER_SIZE = 10000 #XXX non-8192 values handle more input files with no error xzfiles = glob('*.bi5') for lzma_path in xzfiles: try: with lzma.open(lzma_path, 'rb') as file: file.read() except Exception as e: failed = True print('error: %s: %s' % (lzma_path, e)) # check that xz decompresses the files without errors cmd = ["xz", "--format=lzma", "--decompress", "--stdout"] # . show that the command fails for an invalid archive file (python text) assert call(cmd + [sys.argv[0]], stderr=DEVNULL, stdout=DEVNULL) == 1 # . show that xzfiles are valid xz archives check_call(cmd + xzfiles, stdout=DEVNULL) sys.exit(failed)