def unpack_archive(buildscript, localfile, target_directory, checkoutdir=None): """ Unpack @localfile to @target_directory; if @checkoutdir is specified make sure the unpacked content gets into a directory by that name """ if checkoutdir: final_target_directory = target_directory target_directory = tempfile.mkdtemp(dir=final_target_directory) ext = os.path.splitext(localfile)[-1] if ext == '.lzma' and has_command('lzcat.exe') and has_command('tar.exe'): buildscript.execute('lzcat -d "%s" | tar xf -' % localfile, cwd=target_directory) elif ext == '.xz' and has_command('xzcat.exe') and has_command('tar.exe'): buildscript.execute('xzcat -d "%s" | tar xf -' % localfile, cwd=target_directory) elif ext == '.bz2' and has_command('bunzip2.exe') and has_command('tar.exe'): buildscript.execute('bunzip2 -dc "%s" | tar xf -' % localfile, cwd=target_directory) elif ext in ('.gz', '.tgz') and has_command('gzip.exe') and has_command('tar.exe'): buildscript.execute('gzip -dc "%s" | tar xf -' % localfile, cwd=target_directory) elif ext == '.zip' and has_command('unzip.exe'): buildscript.execute('unzip "%s"' % localfile, cwd=target_directory) else: try: if tarfile.is_tarfile(localfile): unpack_tar_file(localfile, target_directory) elif zipfile.is_zipfile(localfile): unpack_zip_file(localfile, target_directory) else: raise CommandError('Failed to unpack %s (unknown archive type)' % localfile) except: raise CommandError('Failed to unpack %s' % localfile) if checkoutdir: # tarball has been extracted in $destdir/$tmp/, check, then move the # content of that directory if len(os.listdir(target_directory)) == 0: raise CommandError('Failed to unpack %s (empty file?)' % localfile) if len(os.listdir(target_directory)) == 1: # a single directory, just move it tmpdirname = os.path.join(target_directory, os.listdir(target_directory)[0]) fileutils.rename(tmpdirname, os.path.join(final_target_directory, checkoutdir)) os.rmdir(target_directory) else: # more files, just rename the temporary directory to the final name fileutils.rename(target_directory, os.path.join(final_target_directory, checkoutdir))
def _quilt_checkout(self, buildscript): if not has_command("quilt"): raise FatalError("unable to find quilt") if os.path.exists(self.quilt.srcdir) and os.path.exists(os.path.join(self.srcdir, ".pc/applied-patches")): buildscript.execute("quilt pop -a", cwd=self.srcdir, extra_env={"QUILT_PATCHES": self.quilt.srcdir}) self.quilt.checkout(buildscript) if not os.path.exists(self.quilt.srcdir): raise FatalError("could not checkout quilt patch set") buildscript.execute("quilt push -a", cwd=self.srcdir, extra_env={"QUILT_PATCHES": self.quilt.srcdir})
def _download_tarball(self, buildscript, localfile): """Downloads the tarball off the internet, using wget or curl.""" extra_env = {"PATH": os.environ.get("UNMANGLED_PATH")} lines = [ ["wget.exe", "--continue", self.module, "-O", localfile], ["curl.exe", "--continue-at", "-", "-L", self.module, "-o", localfile], ] lines = [line for line in lines if has_command(line[0])] if not lines: raise FatalError("unable to find wget or curl") try: return buildscript.execute(lines[0], extra_env=extra_env) except CommandError: # Cleanup potential leftover file if os.path.exists(localfile): os.remove(localfile) raise