mirror of
https://github.com/gunchev/home_bin.git
synced 2025-11-20 12:15:46 +00:00
Cleanup mksparse.py style, added custom exception.
This commit is contained in:
parent
3d4c3e860c
commit
babed4b418
1 changed files with 39 additions and 23 deletions
|
|
@ -25,44 +25,56 @@ Based on Brad Watson's work mkimage.py from
|
||||||
http://qemu.dad-answers.com/download/qemu/utilities/QEMU-HD-Create/
|
http://qemu.dad-answers.com/download/qemu/utilities/QEMU-HD-Create/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = "0.1"
|
import os.path
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
__version__ = "0.2"
|
||||||
__author__ = "Doncho Gunchev <gunchev@gmail.com>, Brad Watson"
|
__author__ = "Doncho Gunchev <gunchev@gmail.com>, Brad Watson"
|
||||||
__depends__ = ['Python-2.4']
|
__depends__ = ['Python-2.4']
|
||||||
#__copyright__ = """Have to ask Brad Watson, GPL?"""
|
#__copyright__ = """Have to ask Brad Watson, GPL?"""
|
||||||
|
|
||||||
|
|
||||||
|
class MkSparseError(Exception):
|
||||||
|
"""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:
|
||||||
f = open(sys.argv[1],"wb+")
|
sparse_file = open(sys.argv[1],"wb+")
|
||||||
except Exception, e:
|
except (IOError, OSError), exc:
|
||||||
raise Exception("Error: Can't create file '" + file_name + "':\n" + str(e))
|
raise MkSparseError("Error: Can't create file '" + file_name + "':\n"
|
||||||
|
+ str(exc))
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
# 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.
|
||||||
f.truncate(int(file_size))
|
sparse_file.truncate(int(file_size))
|
||||||
except Exception, e:
|
except (IOError, OSError), exc:
|
||||||
f.close() # clean the mess... TODO: more checks if this does not fail?
|
try:
|
||||||
os.unlink(sys.argv[1])
|
sparse_file.close() # clean the mess...
|
||||||
raise Exception("Error: Can't truncate '%s'\n%s" % (file_name, str(e)))
|
os.unlink(sys.argv[1])
|
||||||
try: # Hmm, do I need this at all? Maybe yes (judging from the strange errors
|
except (IOError, OSError):
|
||||||
# that occure in gzip at object destruction)
|
pass
|
||||||
f.close()
|
raise MkSparseError("Error: Can't truncate '%s'\n%s"
|
||||||
except Exception, e:
|
% (file_name, str(exc)))
|
||||||
raise Exception("Error: Can't close '%s'\n%s" % (file_name, str(e)))
|
try:
|
||||||
|
sparse_file.close()
|
||||||
|
except (IOError, OSError), exc:
|
||||||
|
raise MkSparseError("Error: Can't close '%s'\n%s" % (file_name,
|
||||||
|
str(exc)))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
if __name__ == "__main__":
|
"""The main function for a command line execution"""
|
||||||
import os.path
|
|
||||||
import re
|
|
||||||
import sys
|
|
||||||
|
|
||||||
if len(sys.argv) != 3:
|
if len(sys.argv) != 3:
|
||||||
# .pyo (docstrings stripped) workaround
|
# .pyo (docstrings stripped) workaround
|
||||||
print >> sys.stderr, __doc__ and __doc__ or "Usage: mksparse.py <image-name> <size>[kmg]"
|
print >> sys.stderr, (__doc__ and __doc__
|
||||||
|
or "Usage: mksparse.py <image-name> <size>[kmg]")
|
||||||
print >> sys.stderr, "Version:", __version__
|
print >> sys.stderr, "Version:", __version__
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
@ -72,7 +84,7 @@ if __name__ == "__main__":
|
||||||
# validate file size
|
# validate file size
|
||||||
try:
|
try:
|
||||||
(size, dim) = re.match('^(\d+)([KkMmGg])?$', file_size).groups()
|
(size, dim) = re.match('^(\d+)([KkMmGg])?$', file_size).groups()
|
||||||
except Exception, e:
|
except TypeError:
|
||||||
print >> sys.stderr, (sys.argv[0] + ': '
|
print >> sys.stderr, (sys.argv[0] + ': '
|
||||||
+ "Bad image size given: " + repr(file_size))
|
+ "Bad image size given: " + repr(file_size))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
@ -100,6 +112,10 @@ if __name__ == "__main__":
|
||||||
file_size = size
|
file_size = size
|
||||||
try:
|
try:
|
||||||
mk_sparse(file_name, file_size)
|
mk_sparse(file_name, file_size)
|
||||||
except Exception, e:
|
except MkSparseError, exc:
|
||||||
print >> sys.stderr, sys.argv[0] + ': ' + str(e)
|
print >> sys.stderr, sys.argv[0] + ': ' + str(exc)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue