From 0161a0cb4d35068473340d65c564e4d3a4f78182 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Thu, 18 Jul 2002 17:49:13 +0000 Subject: [PATCH] tgzs, etc extract archives by default, not streams. git-svn-id: https://rox.svn.sourceforge.net/svnroot/rox/trunk/Archive@1710 66de3db3-b00d-0410-b41b-f4738ad19bea --- formats.py | 73 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/formats.py b/formats.py index 71a649b..55311b6 100644 --- a/formats.py +++ b/formats.py @@ -135,38 +135,6 @@ for x in operations: except AttributeError: pass -def guess_format(data): - "Return a good default Operation, judging by the first 300 bytes or so." - l = len(data) - def string(offset, match): - return data[offset:offset + len(match)] == match - def short(offset, match): - if l > offset + 1: - a = data[offset] - b = data[offset + 1] - return ((a == match & 0xff) and (b == (match >> 8))) or \ - (b == match & 0xff) and (a == (match >> 8)) - return 0 - - # Archives - if string(257, 'ustar\0') or string(257, 'ustar\040\040\0'): - return tar - if short(0, 070707) or short(0, 0143561) or string(0, '070707') or \ - string(0, '070701') or string(0, '070702'): - return cpio - if string(0, '!') or string(0, '\\') or string(0, ''): - if string(7, '\ndebian'): - return deb - if string(0, 'Rar!'): return rar - if string(0, 'PK\003\004'): return zip - - # Compressed streams - if string(0, '\037\213'): return gz - if string(0, 'BZh'): return bz2 - if string(0, 'BZ'): return bz2 # bzip, but maybe bzip2 can cope? - - return make_gz - class FileData: "A file on the local filesystem." def __init__(self, path): @@ -195,7 +163,7 @@ class FileData: tmp.write(start) shutil.copyfileobj(source, tmp) self.source = tmp - self.default = guess_format(start) + self.default = self.guess_format(start) if path == '-': name = 'Data' @@ -208,6 +176,45 @@ class FileData: name = new self.default_name = name + def guess_format(self, data): + "Return a good default Operation, judging by the first 300 bytes or so." + l = len(data) + def string(offset, match): + return data[offset:offset + len(match)] == match + def short(offset, match): + if l > offset + 1: + a = data[offset] + b = data[offset + 1] + return ((a == match & 0xff) and (b == (match >> 8))) or \ + (b == match & 0xff) and (a == (match >> 8)) + return 0 + + # Archives + if string(257, 'ustar\0') or string(257, 'ustar\040\040\0'): + return tar + if short(0, 070707) or short(0, 0143561) or string(0, '070707') or \ + string(0, '070701') or string(0, '070702'): + return cpio + if string(0, '!') or string(0, '\\') or string(0, ''): + if string(7, '\ndebian'): + return deb + if string(0, 'Rar!'): return rar + if string(0, 'PK\003\004'): return zip + + # Compressed streams + if string(0, '\037\213'): + if self.path.endswith('.tar.gz') or self.path.endswith('.tgz'): + return tgz + return gz + if string(0, 'BZh') or string(0, 'BZ'): + if self.path.endswith('.tar.bz') or self.path.endswith('.tar.bz2') or \ + self.path.endswith('.tbz') or self.path.endswith('.tbz2'): + return tbz + return bz2 + + return make_gz + + class DirData: def __init__(self, path): self.path = path -- 2.11.4.GIT