From: Gui Chen Date: Fri, 25 Apr 2014 06:04:46 +0000 (-0400) Subject: add _do_tar and _do_untar to utilize 'tar' command X-Git-Tag: 0.25~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=66c6d023850bae6049fa16f7b497e4b90b8316be;p=tools%2Fmic.git add _do_tar and _do_untar to utilize 'tar' command Change-Id: Iefc0ca78636e07ffe125560c3336031c5be4f9fb Signed-off-by: Gui Chen --- diff --git a/mic/archive.py b/mic/archive.py index c92aaa6..4f0e915 100644 --- a/mic/archive.py +++ b/mic/archive.py @@ -195,6 +195,60 @@ def decompress(file_path, decompress_format=None): return func(file_path, False) +def _do_tar(archive_name, target_name): + """ Archive the directory or the file with 'tar' utility + + @archive_name: the name of the tarball file + @target_name: the name of the target to tar + @retval: the path of the archived file + """ + if os.path.isdir(target_name): + target_dir = target_name + target_name = "." + else: + target_dir = os.path.dirname(target_name) + target_name = os.path.basename(target_name) + + cmdln = ["tar", "-S", "-C", target_dir, "-cf", archive_name, target_name] + + _call_external(cmdln) + + return archive_name + +def _do_untar(archive_name, target_dir=None): + """ Unarchive the archived file with 'tar' utility + + @archive_name: the name of the archived file + @target_dir: the directory which the target locates + @retval: the target directory + """ + if not target_dir: + target_dir = os.getcwd() + + cmdln = ["tar", "-S", "-C", target_dir, "-xf", archive_name] + + _call_external(cmdln) + + return target_dir + +def _imp_tarfile(archive_name, target_name): + """ Archive the directory or the file with tarfile module + + @archive_name: the name of the tarball file + @target_name: the name of the target to tar + @retval: the path of the archived file + """ + import tarfile + tar = tarfile.open(archive_name, 'w') + if os.path.isdir(target_name): + for child in os.listdir(target_name): + tar.add(os.path.join(target_name, child), child) + else: + tar.add(target_name, os.path.basename(target_name)) + + tar.close() + return archive_name + def _make_tarball(archive_name, target_name, compressor=None): """ Create a tarball from all the files under 'target_name' or itself. @@ -208,18 +262,11 @@ def _make_tarball(archive_name, target_name, compressor=None): if not os.path.exists(archive_dir): os.makedirs(archive_dir) - import tarfile - tarball_name = tempfile.mktemp(suffix=".tar", dir=archive_dir) - tar = tarfile.open(tarball_name, 'w') - - if os.path.isdir(target_name): - for child in os.listdir(target_name): - tar.add(os.path.join(target_name, child), child) + if which("tar") is not None: + _do_tar(tarball_name, target_name) else: - tar.add(target_name, os.path.basename(target_name)) - - tar.close() + _imp_tarfile(tarball_name, target_name) if compressor: tarball_name = compressor(tarball_name, True)