archive: Refactor Archive{Bzip2,Gzip}.unpack_dir methods

Prepend output path with target dir instead of chdir'ing. Also remove
rstrip's used for dropping extensions.
This commit is contained in:
Fatih Aşıcı
2010-06-15 08:44:57 +00:00
parent 9783f60d29
commit c4df178d6a
+22 -19
View File
@@ -163,6 +163,7 @@ class ArchiveBinary(ArchiveBase):
class ArchiveBzip2(ArchiveBase): class ArchiveBzip2(ArchiveBase):
"""ArchiveBzip2 handles Bzip2 archive files""" """ArchiveBzip2 handles Bzip2 archive files"""
def __init__(self, file_path, arch_type="bz2"): def __init__(self, file_path, arch_type="bz2"):
super(ArchiveBzip2, self).__init__(file_path, arch_type) super(ArchiveBzip2, self).__init__(file_path, arch_type)
@@ -172,22 +173,23 @@ class ArchiveBzip2(ArchiveBase):
def unpack_dir(self, target_dir): def unpack_dir(self, target_dir):
"""Unpack Bzip2 archive to a given target directory(target_dir).""" """Unpack Bzip2 archive to a given target directory(target_dir)."""
oldwd = os.getcwd()
os.chdir(target_dir) output_path = util.join_path(target_dir,
os.path.basename(self.file_path))
if output_path.endswith(".bz2"):
output_path = output_path[:-4]
import bz2 import bz2
self.bzip2 = bz2.BZ2File(self.file_path, "r") bz2_file = bz2.BZ2File(self.file_path, "r")
self.output = \ output = open(output_path, "w")
open(os.path.basename(self.file_path.rstrip(".bz2")), "w") output.write(bz2_file.read())
self.output.write(self.bzip2.read()) output.close()
self.output.close() bz2_file.close()
self.bzip2.close()
os.chdir(oldwd)
class ArchiveGzip(ArchiveBase): class ArchiveGzip(ArchiveBase):
"""ArchiveGzip handles Gzip archive files""" """ArchiveGzip handles Gzip archive files"""
def __init__(self, file_path, arch_type="gz"): def __init__(self, file_path, arch_type="gz"):
super(ArchiveGzip, self).__init__(file_path, arch_type) super(ArchiveGzip, self).__init__(file_path, arch_type)
@@ -197,17 +199,18 @@ class ArchiveGzip(ArchiveBase):
def unpack_dir(self, target_dir): def unpack_dir(self, target_dir):
"""Unpack Gzip archive to a given target directory(target_dir).""" """Unpack Gzip archive to a given target directory(target_dir)."""
oldwd = os.getcwd()
os.chdir(target_dir) output_path = util.join_path(target_dir,
os.path.basename(self.file_path))
if output_path.endswith(".gz"):
output_path = output_path[:-3]
import gzip import gzip
self.gzip = gzip.GzipFile(self.file_path, "r") gzip_file = gzip.GzipFile(self.file_path, "r")
self.output = open(os.path.basename(self.file_path.rstrip(".gz")), "w") output = open(output_path, "w")
self.output.write(self.gzip.read()) output.write(gzip_file.read())
self.output.close() output.close()
self.gzip.close() gzip_file.close()
os.chdir(oldwd)
class ArchiveTar(ArchiveBase): class ArchiveTar(ArchiveBase):