Changeset 6613


Ignore:
Timestamp:
Mar 25, 2009, 2:35:26 PM (15 years ago)
Author:
rwilson
Message:

Added functions to tar/untar files.

Location:
anuga_core/source/anuga/utilities
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/system_tools.py

    r6612 r6613  
    99import urllib2
    1010import getpass
     11import tarfile
    1112
    1213
     
    359360
    360361
     362##
     363# @brief Tar a file (or directory) into a tarfile.
     364# @param files A list of files (or directories) to tar.
     365# @param tarfile The created tarfile name.
     366def tar_file(files, tarname):
     367    '''Compress a file or directory into a tar file.'''
     368
     369    o = tarfile.open(tarname, 'w:gz')
     370    for file in files:
     371        o.add(file)
     372    o.close()
     373
     374
     375##
     376# @brief Untar a file into an optional target directory.
     377# @param tarname Name of the file to untar.
     378# @param target_dir Directory to untar into.
     379def untar_file(tarname, target_dir='.'):
     380    '''Uncompress a tar file.'''
     381
     382    o = tarfile.open(tarname, 'r:gz')
     383    members = o.getmembers()
     384    for member in members:
     385        o.extract(member, target_dir)
     386    o.close()
  • anuga_core/source/anuga/utilities/test_system_tools.py

    r6556 r6613  
    33
    44import unittest
     5import tempfile
    56import Numeric as num
    67import zlib
     
    162163        test_it(source, expected)
    163164
     165    def test_tar_untar_files(self):
     166        '''Test that tarring & untarring files is OK.'''
     167
     168        # these test files must exist in the current directory
     169        files = ('test_system_tools.py', 'system_tools.py')
     170
     171        # name of tar file and test (temp) directory
     172        tar_filename = 'test.tgz'
     173        tmp_dir = tempfile.mkdtemp()
     174
     175        # tar and untar the test files into a temporary directory
     176        tar_file(files, tar_filename)
     177        untar_file(tar_filename, tmp_dir)
     178
     179        # see if original files and untarred ones are the same
     180        for file in files:
     181            fd = open(file, 'r')
     182            orig = fd.readlines()
     183            fd.close()
     184
     185            fd = open(os.path.join(tmp_dir, file), 'r')
     186            copy = fd.readlines()
     187            fd.close()
     188
     189            msg = "Original file %s isn't the same as untarred copy?" % file
     190            self.failUnless(orig == copy, msg)
     191
     192
    164193#-------------------------------------------------------------
    165194if __name__ == "__main__":
     
    168197    runner.run(suite)
    169198
    170 
    171 
    172 
Note: See TracChangeset for help on using the changeset viewer.