def extract_orig_tarball(tarball_filename, component, target, strip_components=None): """Extract an orig tarball. :param tarball: Path to the tarball :param component: Component name (or None for top-level) :param target: Target path """ from tarfile import TarFile tar_args = ["tar"] if tarball_filename.endswith(".tar.bz2"): tar_args.append('xjf') tf = TarFile.bz2open(tarball_filename) elif (tarball_filename.endswith(".tar.lzma") or tarball_filename.endswith(".tar.xz")): tar_args.append('xJf') tf = TarFile.xzopen(tarball_filename) elif tarball_filename.endswith(".tar"): tar_args.append('xf') tf = TarFile.open(tarball_filename) elif (tarball_filename.endswith(".tar.gz") or tarball_filename.endswith(".tgz")): tf = TarFile.gzopen(tarball_filename) tar_args.append('xzf') else: note('Unable to figure out type of %s, ' 'assuming .tar.gz', tarball_filename) tf = TarFile.gzopen(tarball_filename) tar_args.append('xzf') try: if strip_components is None: if needs_strip_components(tf): strip_components = 1 else: strip_components = 0 finally: tf.close() if component is not None: target_path = os.path.join(target, component) os.mkdir(target_path) else: target_path = target tar_args.extend([tarball_filename, "-C", target_path]) if strip_components is not None: tar_args.extend(["--strip-components", str(strip_components)]) proc = subprocess.Popen(tar_args, preexec_fn=subprocess_setup, stderr=subprocess.PIPE) (stdout, stderr) = proc.communicate() if proc.returncode != 0: raise TarFailed("extract", tarball_filename, error=stderr)