Returns a tuple consisting of the open file object and a list of unmapped
block ranges (holes) in the file. """
- file_obj = tempfile.NamedTemporaryFile("wb+")
- block_size = BmapHelpers.get_block_size(file_obj)
-
#
# Generate sparse files with one single hole spanning the entire file
#
# A block-sized hole
+ file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "4Khole_",
+ suffix = ".img")
+ block_size = BmapHelpers.get_block_size(file_obj)
file_obj.truncate(block_size)
yield (file_obj, [(0, 0)])
+ file_obj.close()
- # A block size +/- 1 byte hole
+ # A block size + 1 byte hole
+ file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "4Khole_plus_1_",
+ suffix = ".img")
file_obj.truncate(block_size + 1)
yield (file_obj, [(0, 0)])
+ file_obj.close()
+
+ # A block size - 1 byte hole
+ file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "4Khole_minus_1_",
+ suffix = ".img")
file_obj.truncate(block_size - 1)
yield (file_obj, [(0, 0)])
+ file_obj.close()
# A 1-byte hole
+ file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "1byte_hole_",
+ suffix = ".img")
file_obj.truncate(1)
yield (file_obj, [(0, 0)])
+ file_obj.close()
# And 10 holes of random size
- for size in [random.randint(1, max_size) for _ in xrange(10)]:
+ for i in xrange(10):
+ size = random.randint(1, max_size)
+ file_obj = tempfile.NamedTemporaryFile("wb+", suffix = ".img",
+ prefix = "rand_hole_%d_" % i)
file_obj.truncate(size)
blocks_cnt = (size + block_size - 1) / block_size
yield (file_obj, [(0, blocks_cnt - 1)])
+ file_obj.close()
#
# Generate a random sparse files
#
# The maximum size
+ file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "sparse_",
+ suffix = ".img")
holes = create_random_sparse_file(file_obj, max_size)
yield (file_obj, holes)
+ file_obj.close()
- # The maximum size +/- 1 byte
+ # The maximum size + 1 byte
+ file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "sparse_plus_1_",
+ suffix = ".img")
holes = create_random_sparse_file(file_obj, max_size + 1)
yield (file_obj, holes)
+ file_obj.close()
+
+ # The maximum size - 1 byte
+ file_obj = tempfile.NamedTemporaryFile("wb+", prefix = "sparse_minus_1_",
+ suffix = ".img")
holes = create_random_sparse_file(file_obj, max_size - 1)
yield (file_obj, holes)
+ file_obj.close()
# And 10 files of random size
- for size in [random.randint(1, max_size) for _ in xrange(10)]:
+ for i in xrange(10):
+ size = random.randint(1, max_size)
+ file_obj = tempfile.NamedTemporaryFile("wb+", suffix = ".img",
+ prefix = "sparse_%d_" % i)
holes = create_random_sparse_file(file_obj, size)
yield (file_obj, holes)
-
- file_obj.close()
+ file_obj.close()
# * Too many public methods - R0904
# pylint: disable=R0902,R0904
+import os
import tempfile
import filecmp
import hashlib
import gzip
import shutil
+ # Make sure the temporary files start with the same name as 'file_obj' in
+ # order to simplify debugging.
+ prefix = os.path.splitext(os.path.basename(file_obj.name))[0] + '.'
+
# Generate a .bz2 version of the file
- tmp_file_obj = tempfile.NamedTemporaryFile('wb+', suffix = '.bz2')
+ tmp_file_obj = tempfile.NamedTemporaryFile('wb+', prefix = prefix,
+ suffix = '.bz2')
bz2_file_obj = bz2.BZ2File(tmp_file_obj.name, 'wb')
file_obj.seek(0)
shutil.copyfileobj(file_obj, bz2_file_obj)
tmp_file_obj.close()
# Generate a .gz version of the file
- tmp_file_obj = tempfile.NamedTemporaryFile('wb+', suffix = '.gz')
+ tmp_file_obj = tempfile.NamedTemporaryFile('wb+', prefix = prefix,
+ suffix = '.gz')
gzip_file_obj = gzip.GzipFile(tmp_file_obj.name, 'wb')
file_obj.seek(0)
shutil.copyfileobj(file_obj, gzip_file_obj)
to a different file, and then checks that the original file and the copy
are identical. """
+ # Make sure the temporary files start with the same name as 'f_image' in
+ # order to simplify debugging.
+ prefix = os.path.splitext(os.path.basename(f_image.name))[0] + '.'
+
# Create and open a temporary file for a copy of the copy
- f_copy = tempfile.NamedTemporaryFile("wb+")
+ f_copy = tempfile.NamedTemporaryFile("wb+", prefix = prefix,
+ suffix = ".copy")
# Create and open 2 temporary files for the bmap
- f_bmap1 = tempfile.NamedTemporaryFile("w+")
- f_bmap2 = tempfile.NamedTemporaryFile("w+")
+ f_bmap1 = tempfile.NamedTemporaryFile("w+", prefix = prefix,
+ suffix = ".bmap1")
+ f_bmap2 = tempfile.NamedTemporaryFile("w+", prefix = prefix,
+ suffix = ".bmap2")
image_sha1 = _calculate_sha1(f_image)