The save_build() has a rather specific name, but its purpose is really
general in nature: to archive a given directory (build dir) as a tarball
file somewhere (dest_dir).

This patch:
 * Renames save_build() to archive_as_tarball()
 * Moves it to virt_utils
 * Adds more flexiblity with regards to naming the tarball
 * Adds more flexiblity with regards to compression
 * Avoids using external 'tar' command by using 'tarfile' module
 * Adds more unittests

Signed-off-by: Cleber Rosa <[email protected]>
---
 client/virt/kvm_installer.py       |    8 ++--
 client/virt/virt_installer.py      |    9 -----
 client/virt/virt_utils.py          |   64 +++++++++++++++++++++++++++++++++++-
 client/virt/virt_utils_unittest.py |   21 ++++++++++++
 4 files changed, 88 insertions(+), 14 deletions(-)

diff --git a/client/virt/kvm_installer.py b/client/virt/kvm_installer.py
index 2dca976..e79c9fc 100644
--- a/client/virt/kvm_installer.py
+++ b/client/virt/kvm_installer.py
@@ -347,7 +347,7 @@ class YumInstaller(BaseInstaller):
                         unittest=self.unittest_prefix)
         self.reload_modules_if_needed()
         if self.save_results:
-            virt_installer.save_build(self.srcdir, self.results_dir)
+            virt_utils.archive_as_tarball(self.srcdir, self.results_dir)
 
 
 class KojiInstaller(YumInstaller):
@@ -403,7 +403,7 @@ class KojiInstaller(YumInstaller):
                         unittest=self.unittest_prefix)
         self.reload_modules_if_needed()
         if self.save_results:
-            virt_installer.save_build(self.srcdir, self.results_dir)
+            virt_utils.archive_as_tarball(self.srcdir, self.results_dir)
 
 
     def _get_rpm_names(self):
@@ -565,7 +565,7 @@ class SourceDirInstaller(BaseInstaller):
         self._install()
         self.reload_modules_if_needed()
         if self.save_results:
-            virt_installer.save_build(self.srcdir, self.results_dir)
+            virt_utils.archive_as_tarball(self.srcdir, self.results_dir)
 
 class GitRepo(object):
     def __init__(self, installer, prefix,
@@ -717,7 +717,7 @@ class GitInstaller(SourceDirInstaller):
         self._install()
         self.reload_modules_if_needed()
         if self.save_results:
-            virt_installer.save_build(self.srcdir, self.results_dir)
+            virt_utils.archive_as_tarball(self.srcdir, self.results_dir)
 
 
 class PreInstalledKvm(BaseInstaller):
diff --git a/client/virt/virt_installer.py b/client/virt/virt_installer.py
index 5ee128c..49d277e 100644
--- a/client/virt/virt_installer.py
+++ b/client/virt/virt_installer.py
@@ -21,12 +21,3 @@ def check_configure_options(script_path):
             option_list.append(option)
 
     return option_list
-
-
-def save_build(build_dir, dest_dir):
-    logging.debug('Saving the result of the build on %s', dest_dir)
-    base_name = os.path.basename(build_dir)
-    tarball_name = base_name + '.tar.bz2'
-    os.chdir(os.path.dirname(build_dir))
-    utils.system('tar -cjf %s %s' % (tarball_name, base_name))
-    shutil.move(tarball_name, os.path.join(dest_dir, tarball_name))
diff --git a/client/virt/virt_utils.py b/client/virt/virt_utils.py
index 6da9a49..89a1193 100644
--- a/client/virt/virt_utils.py
+++ b/client/virt/virt_utils.py
@@ -5,7 +5,7 @@ KVM test utility functions.
 """
 
 import time, string, random, socket, os, signal, re, logging, commands, cPickle
-import fcntl, shelve, ConfigParser, threading, sys, UserDict, inspect
+import fcntl, shelve, ConfigParser, threading, sys, UserDict, inspect, tarfile
 import struct
 from autotest_lib.client.bin import utils, os_dep
 from autotest_lib.client.common_lib import error, logging_config
@@ -1330,6 +1330,68 @@ def get_cpu_vendor(cpu_flags=[], verbose=True):
     return vendor
 
 
+def get_archive_tarball_name(source_dir, tarball_name, compression):
+    '''
+    Get the name for a tarball file, based on source, name and compression
+    '''
+    if tarball_name is None:
+        tarball_name = os.path.basename(source_dir)
+
+    if not tarball_name.endswith('.tar'):
+        tarball_name = '%s.tar' % tarball_name
+
+    if compression and not tarball_name.endswith('.%s' % compression):
+        tarball_name = '%s.%s' % (tarball_name, compression)
+
+    return tarball_name
+
+
+def archive_as_tarball(source_dir, dest_dir, tarball_name=None,
+                       compression='bz2', verbose=True):
+    '''
+    Saves the given source directory to the given destination as a tarball
+
+    If the name of the archive is omitted, it will be taken from the
+    source_dir. If it is an absolute path, dest_dir will be ignored. But,
+    if both the destination directory and tarball anem is given, and the
+    latter is not an absolute path, they will be combined.
+
+    For archiving directory '/tmp' in '/net/server/backup' as file
+    'tmp.tar.bz2', simply use:
+
+    >>> virt_utils.archive_as_tarball('/tmp', '/net/server/backup')
+
+    To save the file it with a different name, say 'host1-tmp.tar.bz2'
+    and save it under '/net/server/backup', use:
+
+    >>> virt_utils.archive_as_tarball('/tmp', '/net/server/backup',
+                                      'host1-tmp')
+
+    To save with gzip compression instead (resulting in the file
+    '/net/server/backup/host1-tmp.tar.gz'), use:
+
+    >>> virt_utils.archive_as_tarball('/tmp', '/net/server/backup',
+                                      'host1-tmp', 'gz')
+    '''
+    tarball_name = get_archive_tarball_name(source_dir,
+                                            tarball_name,
+                                            compression)
+    if not os.path.isabs(tarball_name):
+        tarball_path = os.path.join(dest_dir, tarball_name)
+    else:
+        tarball_path = tarball_name
+
+    if verbose:
+        logging.debug('Archiving %s as %s' % (source_dir,
+                                              tarball_path))
+
+    os.chdir(os.path.dirname(source_dir))
+    tarball = tarfile.TarFile(name=tarball_path, mode='w')
+    tarball = tarball.open(name=tarball_path, mode='w:%s' % compression)
+    tarball.add(os.path.basename(source_dir))
+    tarball.close()
+
+
 class Thread(threading.Thread):
     """
     Run a function in a background thread.
diff --git a/client/virt/virt_utils_unittest.py 
b/client/virt/virt_utils_unittest.py
index 8158ac3..9a2c417 100755
--- a/client/virt/virt_utils_unittest.py
+++ b/client/virt/virt_utils_unittest.py
@@ -43,5 +43,26 @@ class virt_utils_test(unittest.TestCase):
         self.assertEqual(vendor, 'unknown')
 
 
+    def test_get_archive_tarball_name(self):
+        tarball_name = virt_utils.get_archive_tarball_name('/tmp',
+                                                           'tmp-archive',
+                                                           'bz2')
+        self.assertEqual(tarball_name, 'tmp-archive.tar.bz2')
+
+
+    def test_get_archive_tarball_name_absolute(self):
+        tarball_name = virt_utils.get_archive_tarball_name('/tmp',
+                                                           '/var/tmp/tmp',
+                                                           'bz2')
+        self.assertEqual(tarball_name, '/var/tmp/tmp.tar.bz2')
+
+
+    def test_get_archive_tarball_name_from_dir(self):
+        tarball_name = virt_utils.get_archive_tarball_name('/tmp',
+                                                           None,
+                                                           'bz2')
+        self.assertEqual(tarball_name, 'tmp.tar.bz2')
+
+
 if __name__ == '__main__':
     unittest.main()
-- 
1.7.4.4

_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to