Update mksparse.py to work with python 3.

Used to target python 2.4, RHEL 5... damn I'm getting old.
This commit is contained in:
Doncho N. Gunchev 2020-10-11 15:06:57 +03:00
parent 247953cdad
commit f4a8b2cc91

View file

@ -18,7 +18,9 @@ This script opens a file for writing, seeks at the desired position
(or file size) and truncates the file there. Can be handy while (or file size) and truncates the file there. Can be handy while
playing with KVM / qemu / bochs / loopback images. playing with KVM / qemu / bochs / loopback images.
Tested on linux-2.6 only. Tested on linux-2.6+ only.
NB: Check fallocate from util-linux, may work better for you.
Author: Doncho N. Gunchev <gunchev at gmail dot com> Author: Doncho N. Gunchev <gunchev at gmail dot com>
Based on Brad Watson's work mkimage.py from Based on Brad Watson's work mkimage.py from
@ -30,23 +32,21 @@ import os.path
import re import re
import sys import sys
__version__ = "0.2" __version__ = "0.3"
__author__ = "Doncho Gunchev <gunchev@gmail.com>, Brad Watson" __author__ = "Doncho Gunchev <gunchev@gmail.com>, Brad Watson"
__depends__ = ['Python-2.4'] __depends__ = ['Python-3']
#__copyright__ = """Have to ask Brad Watson, GPL?""" #__copyright__ = """Have to ask Brad Watson, GPL?"""
class MkSparseError(Exception): class MkSparseError(Exception):
"""MkSpace errors""" """MkSpace errors"""
pass
def mk_sparse(file_name, file_size): def mk_sparse(file_name, file_size):
""" Create a sparse file by truncating it at given position""" """Create a sparse file by truncating it at given position"""
try: try:
sparse_file = open(sys.argv[1],"wb+") sparse_file = open(sys.argv[1],"wb+")
except (IOError, OSError), exc: except (IOError, OSError) as exc:
raise MkSparseError("Error: Can't create file '" + file_name + "':\n" raise MkSparseError("Error: Can't create file '" + file_name + "':\n"
+ str(exc)) + str(exc))
else: else:
@ -54,7 +54,7 @@ def mk_sparse(file_name, file_size):
# Note that I don't wan (you too) to write() anything in the file # Note that I don't wan (you too) to write() anything in the file
# because this will consume at least one sector/block. # because this will consume at least one sector/block.
sparse_file.truncate(int(file_size)) sparse_file.truncate(int(file_size))
except (IOError, OSError), exc: except (IOError, OSError) as exc:
try: try:
sparse_file.close() # clean the mess... sparse_file.close() # clean the mess...
os.unlink(sys.argv[1]) os.unlink(sys.argv[1])
@ -64,7 +64,7 @@ def mk_sparse(file_name, file_size):
% (file_name, str(exc))) % (file_name, str(exc)))
try: try:
sparse_file.close() sparse_file.close()
except (IOError, OSError), exc: except (IOError, OSError) as exc:
raise MkSparseError("Error: Can't close '%s'\n%s" % (file_name, raise MkSparseError("Error: Can't close '%s'\n%s" % (file_name,
str(exc))) str(exc)))
@ -74,9 +74,9 @@ def main():
if len(sys.argv) != 3: if len(sys.argv) != 3:
# .pyo (docstrings stripped) workaround # .pyo (docstrings stripped) workaround
print >> sys.stderr, (__doc__ and __doc__ print((__doc__ and __doc__
or "Usage: mksparse.py <image-name> <size>[kmg]") or "Usage: mksparse.py <image-name> <size>[kmg]"), file=sys.stderr)
print >> sys.stderr, "Version:", __version__ print("Version:", __version__, file=sys.stderr)
sys.exit(1) sys.exit(1)
# 'Process' command line parameters # 'Process' command line parameters
@ -86,19 +86,19 @@ def main():
try: try:
(size, dim) = re.match('^(\d+)([KkMmGg])?$', file_size).groups() (size, dim) = re.match('^(\d+)([KkMmGg])?$', file_size).groups()
except TypeError: except TypeError:
print >> sys.stderr, (sys.argv[0] + ': ' print((sys.argv[0] + ': '
+ "Bad image size given: " + repr(file_size)) + "Bad image size given: " + repr(file_size)), file=sys.stderr)
sys.exit(2) sys.exit(2)
# Check if the file exists, -f (force) would be a good parameter # Check if the file exists, -f (force) would be a good parameter
if os.path.exists(file_name): if os.path.exists(file_name):
print >> sys.stderr, (sys.argv[0] + ': ' print((sys.argv[0] + ': '
+ ("Error: file (directory) '%s' already exists!" % (file_name))) + ("Error: file (directory) '%s' already exists!" % (file_name))), file=sys.stderr)
sys.exit(3) sys.exit(3)
dim = dim.lower() dim = dim.lower()
# Calculate size in bytes # Calculate size in bytes
size = long(size) size = int(size)
if dim == 'k': if dim == 'k':
size *= 1024 size *= 1024
elif dim == 'm': elif dim == 'm':
@ -106,15 +106,15 @@ def main():
elif dim == 'g': elif dim == 'g':
size *= 1024 * 1024 * 1024 size *= 1024 * 1024 * 1024
elif dim != None: elif dim != None:
print >> sys.stderr, (sys.argv[0] + ': ' print((sys.argv[0] + ': '
+ "Internal error: size modifier " + repr(dim) + " not handled.") + "Internal error: size modifier " + repr(dim) + " not handled."), file=sys.stderr)
sys.exit(4) sys.exit(4)
file_size = size file_size = size
try: try:
mk_sparse(file_name, file_size) mk_sparse(file_name, file_size)
except MkSparseError, exc: except MkSparseError as exc:
print >> sys.stderr, sys.argv[0] + ': ' + str(exc) print(sys.argv[0] + ': ' + str(exc), file=sys.stderr)
sys.exit(1) sys.exit(1)