archive.py: refine extract_archive to extract tarball file
authorGui Chen <gui.chen@intel.com>
Sat, 26 Apr 2014 04:23:30 +0000 (00:23 -0400)
committeradmin <yuhuan.yang@samsung.com>
Thu, 4 Feb 2016 09:44:11 +0000 (17:44 +0800)
Change-Id: I5af9f17de9d1ac5d12bf1706ce7609ea4c4e201f
Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/archive.py

index a384b86..d506b92 100644 (file)
@@ -294,7 +294,7 @@ def _imp_tarfile(archive_name, target_name):
 def _make_tarball(archive_name, target_name, compressor=None):
     """ Create a tarball from all the files under 'target_name' or itself.
 
-    @archive_name: the name of the archived file
+    @archive_name: the name of the archived file to create
     @target_name: the directory or the file name to archive
     @compressor: callback function to compress the tarball
     @retval: indicate the compressing result
@@ -317,6 +317,18 @@ def _make_tarball(archive_name, target_name, compressor=None):
 
     return os.path.exists(archive_name)
 
+def _extract_tarball(archive_name, target_dir, compressor=None):
+    """ Extract a tarball to a target directory
+
+    @archive_name: the name of the archived file to extract
+    @target_dir: the directory of the extracted target
+    @retval: indicte the untar result
+    """
+
+    _do_untar(archive_name, target_dir)
+
+    return not os.path.exists(archive_name)
+
 def _make_zipfile(archive_name, target_name):
     """ Create a zip file from all the files under 'target_name' or itself.
 
@@ -388,6 +400,7 @@ def make_archive(archive_name, target_name):
 
     @archive_name: the name of the archived file
     @target_name: the directory or the file to archive
+    @retval: the archiving result
     """
     for aformat, suffixes in _ARCHIVE_SUFFIXES.iteritems():
         if filter(archive_name.endswith, suffixes):
@@ -403,10 +416,17 @@ def make_archive(archive_name, target_name):
 
     return func(archive_name, target_name, **kwargs)
 
-def extract_archive():
+def extract_archive(archive_name, target_name):
     """ Extract the given file
+
+    @archive_name: the name of the archived file to extract
+    @target_name: the directory name where the target locates
+    @retval: the extracting result
     """
-    pass
+    if not os.path.exists(target_dir):
+        os.makedirs(target_dir)
+
+    return _extract_tarball(archive_name, target_name)
 
 packing = make_archive
 compressing = compress