qcow2.py: python style fixes
Fix flake8 complaints. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Message-Id: <20200606081806.23897-2-vsementsov@virtuozzo.com> Tested-by: Eric Blake <eblake@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> [eblake: commit message improved] Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
parent
1d74594065
commit
02756054e1
|
@ -4,6 +4,7 @@ import sys
|
||||||
import struct
|
import struct
|
||||||
import string
|
import string
|
||||||
|
|
||||||
|
|
||||||
class QcowHeaderExtension:
|
class QcowHeaderExtension:
|
||||||
|
|
||||||
def __init__(self, magic, length, data):
|
def __init__(self, magic, length, data):
|
||||||
|
@ -11,14 +12,15 @@ class QcowHeaderExtension:
|
||||||
padding = 8 - (length % 8)
|
padding = 8 - (length % 8)
|
||||||
data += b"\0" * padding
|
data += b"\0" * padding
|
||||||
|
|
||||||
self.magic = magic
|
self.magic = magic
|
||||||
self.length = length
|
self.length = length
|
||||||
self.data = data
|
self.data = data
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, magic, data):
|
def create(cls, magic, data):
|
||||||
return QcowHeaderExtension(magic, len(data), data)
|
return QcowHeaderExtension(magic, len(data), data)
|
||||||
|
|
||||||
|
|
||||||
class QcowHeader:
|
class QcowHeader:
|
||||||
|
|
||||||
uint32_t = 'I'
|
uint32_t = 'I'
|
||||||
|
@ -26,27 +28,27 @@ class QcowHeader:
|
||||||
|
|
||||||
fields = [
|
fields = [
|
||||||
# Version 2 header fields
|
# Version 2 header fields
|
||||||
[ uint32_t, '%#x', 'magic' ],
|
[uint32_t, '%#x', 'magic'],
|
||||||
[ uint32_t, '%d', 'version' ],
|
[uint32_t, '%d', 'version'],
|
||||||
[ uint64_t, '%#x', 'backing_file_offset' ],
|
[uint64_t, '%#x', 'backing_file_offset'],
|
||||||
[ uint32_t, '%#x', 'backing_file_size' ],
|
[uint32_t, '%#x', 'backing_file_size'],
|
||||||
[ uint32_t, '%d', 'cluster_bits' ],
|
[uint32_t, '%d', 'cluster_bits'],
|
||||||
[ uint64_t, '%d', 'size' ],
|
[uint64_t, '%d', 'size'],
|
||||||
[ uint32_t, '%d', 'crypt_method' ],
|
[uint32_t, '%d', 'crypt_method'],
|
||||||
[ uint32_t, '%d', 'l1_size' ],
|
[uint32_t, '%d', 'l1_size'],
|
||||||
[ uint64_t, '%#x', 'l1_table_offset' ],
|
[uint64_t, '%#x', 'l1_table_offset'],
|
||||||
[ uint64_t, '%#x', 'refcount_table_offset' ],
|
[uint64_t, '%#x', 'refcount_table_offset'],
|
||||||
[ uint32_t, '%d', 'refcount_table_clusters' ],
|
[uint32_t, '%d', 'refcount_table_clusters'],
|
||||||
[ uint32_t, '%d', 'nb_snapshots' ],
|
[uint32_t, '%d', 'nb_snapshots'],
|
||||||
[ uint64_t, '%#x', 'snapshot_offset' ],
|
[uint64_t, '%#x', 'snapshot_offset'],
|
||||||
|
|
||||||
# Version 3 header fields
|
# Version 3 header fields
|
||||||
[ uint64_t, 'mask', 'incompatible_features' ],
|
[uint64_t, 'mask', 'incompatible_features'],
|
||||||
[ uint64_t, 'mask', 'compatible_features' ],
|
[uint64_t, 'mask', 'compatible_features'],
|
||||||
[ uint64_t, 'mask', 'autoclear_features' ],
|
[uint64_t, 'mask', 'autoclear_features'],
|
||||||
[ uint32_t, '%d', 'refcount_order' ],
|
[uint32_t, '%d', 'refcount_order'],
|
||||||
[ uint32_t, '%d', 'header_length' ],
|
[uint32_t, '%d', 'header_length'],
|
||||||
];
|
]
|
||||||
|
|
||||||
fmt = '>' + ''.join(field[0] for field in fields)
|
fmt = '>' + ''.join(field[0] for field in fields)
|
||||||
|
|
||||||
|
@ -59,7 +61,7 @@ class QcowHeader:
|
||||||
|
|
||||||
header = struct.unpack(QcowHeader.fmt, buf)
|
header = struct.unpack(QcowHeader.fmt, buf)
|
||||||
self.__dict__ = dict((field[2], header[i])
|
self.__dict__ = dict((field[2], header[i])
|
||||||
for i, field in enumerate(QcowHeader.fields))
|
for i, field in enumerate(QcowHeader.fields))
|
||||||
|
|
||||||
self.set_defaults()
|
self.set_defaults()
|
||||||
self.cluster_size = 1 << self.cluster_bits
|
self.cluster_size = 1 << self.cluster_bits
|
||||||
|
@ -96,7 +98,8 @@ class QcowHeader:
|
||||||
else:
|
else:
|
||||||
padded = (length + 7) & ~7
|
padded = (length + 7) & ~7
|
||||||
data = fd.read(padded)
|
data = fd.read(padded)
|
||||||
self.extensions.append(QcowHeaderExtension(magic, length, data))
|
self.extensions.append(QcowHeaderExtension(magic, length,
|
||||||
|
data))
|
||||||
|
|
||||||
def update_extensions(self, fd):
|
def update_extensions(self, fd):
|
||||||
|
|
||||||
|
@ -108,14 +111,13 @@ class QcowHeader:
|
||||||
fd.write(buf)
|
fd.write(buf)
|
||||||
fd.write(ex.data)
|
fd.write(ex.data)
|
||||||
|
|
||||||
if self.backing_file != None:
|
if self.backing_file is not None:
|
||||||
self.backing_file_offset = fd.tell()
|
self.backing_file_offset = fd.tell()
|
||||||
fd.write(self.backing_file)
|
fd.write(self.backing_file)
|
||||||
|
|
||||||
if fd.tell() > self.cluster_size:
|
if fd.tell() > self.cluster_size:
|
||||||
raise Exception("I think I just broke the image...")
|
raise Exception("I think I just broke the image...")
|
||||||
|
|
||||||
|
|
||||||
def update(self, fd):
|
def update(self, fd):
|
||||||
header_bytes = self.header_length
|
header_bytes = self.header_length
|
||||||
|
|
||||||
|
@ -163,19 +165,21 @@ def cmd_dump_header(fd):
|
||||||
h.dump()
|
h.dump()
|
||||||
h.dump_extensions()
|
h.dump_extensions()
|
||||||
|
|
||||||
|
|
||||||
def cmd_dump_header_exts(fd):
|
def cmd_dump_header_exts(fd):
|
||||||
h = QcowHeader(fd)
|
h = QcowHeader(fd)
|
||||||
h.dump_extensions()
|
h.dump_extensions()
|
||||||
|
|
||||||
|
|
||||||
def cmd_set_header(fd, name, value):
|
def cmd_set_header(fd, name, value):
|
||||||
try:
|
try:
|
||||||
value = int(value, 0)
|
value = int(value, 0)
|
||||||
except:
|
except ValueError:
|
||||||
print("'%s' is not a valid number" % value)
|
print("'%s' is not a valid number" % value)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
fields = (field[2] for field in QcowHeader.fields)
|
fields = (field[2] for field in QcowHeader.fields)
|
||||||
if not name in fields:
|
if name not in fields:
|
||||||
print("'%s' is not a known header field" % name)
|
print("'%s' is not a known header field" % name)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
@ -183,25 +187,29 @@ def cmd_set_header(fd, name, value):
|
||||||
h.__dict__[name] = value
|
h.__dict__[name] = value
|
||||||
h.update(fd)
|
h.update(fd)
|
||||||
|
|
||||||
|
|
||||||
def cmd_add_header_ext(fd, magic, data):
|
def cmd_add_header_ext(fd, magic, data):
|
||||||
try:
|
try:
|
||||||
magic = int(magic, 0)
|
magic = int(magic, 0)
|
||||||
except:
|
except ValueError:
|
||||||
print("'%s' is not a valid magic number" % magic)
|
print("'%s' is not a valid magic number" % magic)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
h = QcowHeader(fd)
|
h = QcowHeader(fd)
|
||||||
h.extensions.append(QcowHeaderExtension.create(magic, data.encode('ascii')))
|
h.extensions.append(QcowHeaderExtension.create(magic,
|
||||||
|
data.encode('ascii')))
|
||||||
h.update(fd)
|
h.update(fd)
|
||||||
|
|
||||||
|
|
||||||
def cmd_add_header_ext_stdio(fd, magic):
|
def cmd_add_header_ext_stdio(fd, magic):
|
||||||
data = sys.stdin.read()
|
data = sys.stdin.read()
|
||||||
cmd_add_header_ext(fd, magic, data)
|
cmd_add_header_ext(fd, magic, data)
|
||||||
|
|
||||||
|
|
||||||
def cmd_del_header_ext(fd, magic):
|
def cmd_del_header_ext(fd, magic):
|
||||||
try:
|
try:
|
||||||
magic = int(magic, 0)
|
magic = int(magic, 0)
|
||||||
except:
|
except ValueError:
|
||||||
print("'%s' is not a valid magic number" % magic)
|
print("'%s' is not a valid magic number" % magic)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
@ -219,12 +227,13 @@ def cmd_del_header_ext(fd, magic):
|
||||||
|
|
||||||
h.update(fd)
|
h.update(fd)
|
||||||
|
|
||||||
|
|
||||||
def cmd_set_feature_bit(fd, group, bit):
|
def cmd_set_feature_bit(fd, group, bit):
|
||||||
try:
|
try:
|
||||||
bit = int(bit, 0)
|
bit = int(bit, 0)
|
||||||
if bit < 0 or bit >= 64:
|
if bit < 0 or bit >= 64:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
except:
|
except ValueError:
|
||||||
print("'%s' is not a valid bit number in range [0, 64)" % bit)
|
print("'%s' is not a valid bit number in range [0, 64)" % bit)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
@ -236,21 +245,27 @@ def cmd_set_feature_bit(fd, group, bit):
|
||||||
elif group == 'autoclear':
|
elif group == 'autoclear':
|
||||||
h.autoclear_features |= 1 << bit
|
h.autoclear_features |= 1 << bit
|
||||||
else:
|
else:
|
||||||
print("'%s' is not a valid group, try 'incompatible', 'compatible', or 'autoclear'" % group)
|
print("'%s' is not a valid group, try "
|
||||||
|
"'incompatible', 'compatible', or 'autoclear'" % group)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
h.update(fd)
|
h.update(fd)
|
||||||
|
|
||||||
|
|
||||||
cmds = [
|
cmds = [
|
||||||
[ 'dump-header', cmd_dump_header, 0, 'Dump image header and header extensions' ],
|
['dump-header', cmd_dump_header, 0,
|
||||||
[ 'dump-header-exts', cmd_dump_header_exts, 0, 'Dump image header extensions' ],
|
'Dump image header and header extensions'],
|
||||||
[ 'set-header', cmd_set_header, 2, 'Set a field in the header'],
|
['dump-header-exts', cmd_dump_header_exts, 0,
|
||||||
[ 'add-header-ext', cmd_add_header_ext, 2, 'Add a header extension' ],
|
'Dump image header extensions'],
|
||||||
[ 'add-header-ext-stdio', cmd_add_header_ext_stdio, 1, 'Add a header extension, data from stdin' ],
|
['set-header', cmd_set_header, 2, 'Set a field in the header'],
|
||||||
[ 'del-header-ext', cmd_del_header_ext, 1, 'Delete a header extension' ],
|
['add-header-ext', cmd_add_header_ext, 2, 'Add a header extension'],
|
||||||
[ 'set-feature-bit', cmd_set_feature_bit, 2, 'Set a feature bit'],
|
['add-header-ext-stdio', cmd_add_header_ext_stdio, 1,
|
||||||
|
'Add a header extension, data from stdin'],
|
||||||
|
['del-header-ext', cmd_del_header_ext, 1, 'Delete a header extension'],
|
||||||
|
['set-feature-bit', cmd_set_feature_bit, 2, 'Set a feature bit'],
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def main(filename, cmd, args):
|
def main(filename, cmd, args):
|
||||||
fd = open(filename, "r+b")
|
fd = open(filename, "r+b")
|
||||||
try:
|
try:
|
||||||
|
@ -267,6 +282,7 @@ def main(filename, cmd, args):
|
||||||
finally:
|
finally:
|
||||||
fd.close()
|
fd.close()
|
||||||
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print("Usage: %s <file> <cmd> [<arg>, ...]" % sys.argv[0])
|
print("Usage: %s <file> <cmd> [<arg>, ...]" % sys.argv[0])
|
||||||
print("")
|
print("")
|
||||||
|
@ -274,6 +290,7 @@ def usage():
|
||||||
for name, handler, num_args, desc in cmds:
|
for name, handler, num_args, desc in cmds:
|
||||||
print(" %-20s - %s" % (name, desc))
|
print(" %-20s - %s" % (name, desc))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(sys.argv) < 3:
|
if len(sys.argv) < 3:
|
||||||
usage()
|
usage()
|
||||||
|
|
Loading…
Reference in a new issue