# HG changeset patch # User brett # Date 1167611116 18000 # Node ID 28dbd52a8bb84feb88acb4e406eb5eb03432aa6f # Parent 6f9e1bb597197395e8608fe84e271f25e839906e [svn] Add a -f/--flat option, which will extract the archive contents into the current directory, rather than a dedicated subdirectory. diff -r 6f9e1bb59719 -r 28dbd52a8bb8 scripts/x --- a/scripts/x Sun Dec 31 19:20:26 2006 -0500 +++ b/scripts/x Sun Dec 31 19:25:16 2006 -0500 @@ -22,6 +22,7 @@ import mimetypes import optparse import os +import stat import subprocess import sys import tempfile @@ -171,7 +172,6 @@ pass def check_contents(self): - self.archive.seek(0, 0) archive_type = None filenames = self.get_filenames() try: @@ -216,6 +216,7 @@ class TarExtractor(BaseExtractor): def get_filenames(self): + self.archive.seek(0, 0) return ProcessStreamer(['tar', '-t'], self.archive) def extract_archive(self): @@ -231,6 +232,7 @@ self.archive = StringIO() def get_filenames(self): + self.archive.seek(0, 0) return ProcessStreamer(['zipinfo', '-1', self.filename], None) def extract_archive(self): @@ -239,6 +241,7 @@ class CpioExtractor(BaseExtractor): def get_filenames(self): + self.archive.seek(0, 0) return ProcessStreamer(['cpio', '-t'], self.archive, stderr=subprocess.PIPE) @@ -301,11 +304,11 @@ pieces.pop() return '.'.join(pieces) - def suggest_target(self): - return FilenameChecker().check(self.basename()) + def get_filenames(self): + yield self.basename() def check_contents(self): - return self.basename() + return DECOMPRESSED def extract(self, path): output = open(path, 'w') @@ -322,20 +325,37 @@ def extract(self): checker = self.extractor.name_checker(self.extractor.basename()) - if self.options.overwrite or checker.is_free(): + if self.options.flat: + self.target = '.' + return self.do_extract('.') + elif self.options.overwrite or checker.is_free(): self.target = self.extractor.basename() - self.overwrite() + return self.overwrite() else: self.target = checker.check() - self.safe_extract() + return self.safe_extract() def do_extract(self, directory): try: self.extractor.extract(directory) except ExtractorError, error: - return error.strerror + return str(error) def cleanup(self): + if self.options.flat: + self.cleanup_files() + else: + self.cleanup_directory() + + def cleanup_files(self): + for filename in self.extractor.get_filenames(): + stat_info = os.stat(filename) + perms = stat.S_IRUSR | stat.S_IWUSR + if stat.S_ISDIR(stat_info.st_mode): + perms |= stat.S_IXUSR + os.chmod(filename, stat_info.st_mode | perms) + + def cleanup_directory(self): command = 'find' status = subprocess.call(['find', self.target, '-type', 'd', '-exec', 'chmod', 'u+rwx', '{}', ';']) @@ -412,9 +432,9 @@ parser.add_option('-o', '--overwrite', dest='overwrite', action='store_true', default=False, help='overwrite any existing target directory') -## parser.add_option('-f', '--flat', '--no-directory', dest='flat', -## action='store_true', default=False, -## help="don't put contents in their own directory") + parser.add_option('-f', '--flat', '--no-directory', dest='flat', + action='store_true', default=False, + help="don't put contents in their own directory") ## parser.add_option('-n', '--noninteractive', dest='batch', ## action='store_true', default=False, ## help="don't ask how to handle special cases") diff -r 6f9e1bb59719 -r 28dbd52a8bb8 tests/compare.py --- a/tests/compare.py Sun Dec 31 19:20:26 2006 -0500 +++ b/tests/compare.py Sun Dec 31 19:25:16 2006 -0500 @@ -106,7 +106,7 @@ if message is None: last_part = '' else: - last_part = ': ' + message + last_part = ': ' + str(message) print "%7s: %s%s" % (status, self.name, last_part) return status.lower() diff -r 6f9e1bb59719 -r 28dbd52a8bb8 tests/tests.yml --- a/tests/tests.yml Sun Dec 31 19:20:26 2006 -0500 +++ b/tests/tests.yml Sun Dec 31 19:25:16 2006 -0500 @@ -79,3 +79,22 @@ tar -jxf ../$1 prerun: | mkdir test-1.23 + +- name: flat option + directory: inside-dir + filename: ../test-1.23.tar.bz2 + options: -f + baseline: | + tar -jxf $1 + +- name: flat recursion and permissions + directory: inside-dir + filename: ../test-recursive-badperms.tar.bz2 + options: -fr + baseline: | + tar -jxf $1 + tar -xf test-badperms.tar + chmod 755 testdir + posttest: | + if [ "x`cat testdir/testfile`" != "xhey" ]; then exit 1; fi +