Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Format all Python code with black
Browse files Browse the repository at this point in the history
  • Loading branch information
mntns authored and dedekind committed Jun 26, 2022
1 parent b0d6e90 commit 2f258f2
Show file tree
Hide file tree
Showing 24 changed files with 2,066 additions and 1,438 deletions.
4 changes: 2 additions & 2 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

from bmaptools.CLI import main

if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe|\.pyz)?$', '', sys.argv[0])
if __name__ == "__main__":
sys.argv[0] = re.sub(r"(-script\.pyw|\.exe|\.pyz)?$", "", sys.argv[0])
sys.exit(main())
221 changes: 134 additions & 87 deletions bmaptools/BmapCopy.py

Large diffs are not rendered by default.

71 changes: 40 additions & 31 deletions bmaptools/BmapCreate.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
# 3.0, and was only fixed in bmap-tools v3.1.
SUPPORTED_BMAP_VERSION = "2.0"

_BMAP_START_TEMPLATE = \
"""<?xml version="1.0" ?>
_BMAP_START_TEMPLATE = """<?xml version="1.0" ?>
<!-- This file contains the block map for an image file, which is basically
a list of useful (mapped) block numbers in the image file. In other words,
it lists only those blocks which contain data (boot sector, partition
Expand Down Expand Up @@ -108,6 +107,7 @@ class Error(Exception):
one type of exceptions, and we basically throw human-readable problem
description in case of errors.
"""

pass


Expand Down Expand Up @@ -155,8 +155,9 @@ def __init__(self, image, bmap, chksum_type="sha256"):
try:
self._cs_len = len(hashlib.new(self._cs_type).hexdigest())
except ValueError as err:
raise Error("cannot initialize hash function \"%s\": %s" %
(self._cs_type, err))
raise Error(
'cannot initialize hash function "%s": %s' % (self._cs_type, err)
)

if hasattr(image, "read"):
self._f_image = image
Expand All @@ -175,14 +176,16 @@ def __init__(self, image, bmap, chksum_type="sha256"):
try:
self.filemap = Filemap.filemap(self._f_image)
except (Filemap.Error, Filemap.ErrorNotSupp) as err:
raise Error("cannot generate bmap for file '%s': %s"
% (self._image_path, err))
raise Error(
"cannot generate bmap for file '%s': %s" % (self._image_path, err)
)

self.image_size = self.filemap.image_size
self.image_size_human = human_size(self.image_size)
if self.image_size == 0:
raise Error("cannot generate bmap for zero-sized image file '%s'"
% self._image_path)
raise Error(
"cannot generate bmap for zero-sized image file '%s'" % self._image_path
)

self.block_size = self.filemap.block_size
self.blocks_cnt = self.filemap.blocks_cnt
Expand All @@ -197,20 +200,18 @@ def __del__(self):
def _open_image_file(self):
"""Open the image file."""
try:
self._f_image = open(self._image_path, 'rb')
self._f_image = open(self._image_path, "rb")
except IOError as err:
raise Error("cannot open image file '%s': %s"
% (self._image_path, err))
raise Error("cannot open image file '%s': %s" % (self._image_path, err))

self._f_image_needs_close = True

def _open_bmap_file(self):
"""Open the bmap file."""
try:
self._f_bmap = open(self._bmap_path, 'w+')
self._f_bmap = open(self._bmap_path, "w+")
except IOError as err:
raise Error("cannot open bmap file '%s': %s"
% (self._bmap_path, err))
raise Error("cannot open bmap file '%s': %s" % (self._bmap_path, err))

self._f_bmap_needs_close = True

Expand All @@ -224,36 +225,44 @@ def _bmap_file_start(self):
# whitespaces instead of real numbers. Assume the longest possible
# numbers.

xml = _BMAP_START_TEMPLATE \
% (SUPPORTED_BMAP_VERSION, self.image_size_human,
self.image_size, self.block_size, self.blocks_cnt)
xml = _BMAP_START_TEMPLATE % (
SUPPORTED_BMAP_VERSION,
self.image_size_human,
self.image_size,
self.block_size,
self.blocks_cnt,
)
xml += " <!-- Count of mapped blocks: "

self._f_bmap.write(xml)
self._mapped_count_pos1 = self._f_bmap.tell()

xml = "%s or %s -->\n" % (' ' * len(self.image_size_human),
' ' * len("100.0%"))
xml = "%s or %s -->\n" % (
" " * len(self.image_size_human),
" " * len("100.0%"),
)
xml += " <MappedBlocksCount> "

self._f_bmap.write(xml)
self._mapped_count_pos2 = self._f_bmap.tell()

xml = "%s </MappedBlocksCount>\n\n" % (' ' * len(str(self.blocks_cnt)))
xml = "%s </MappedBlocksCount>\n\n" % (" " * len(str(self.blocks_cnt)))

# pylint: disable=C0301
xml += " <!-- Type of checksum used in this file -->\n"
xml += " <ChecksumType> %s </ChecksumType>\n\n" % self._cs_type

xml += " <!-- The checksum of this bmap file. When it is calculated, the value of\n"
xml += " the checksum has be zero (all ASCII \"0\" symbols). -->\n"
xml += ' the checksum has be zero (all ASCII "0" symbols). -->\n'
xml += " <BmapFileChecksum> "

self._f_bmap.write(xml)
self._chksum_pos = self._f_bmap.tell()

xml = "0" * self._cs_len + " </BmapFileChecksum>\n\n"
xml += " <!-- The block map which consists of elements which may either be a\n"
xml += (
" <!-- The block map which consists of elements which may either be a\n"
)
xml += " range of blocks or a single block. The 'chksum' attribute\n"
xml += " (if present) is the checksum of this blocks range. -->\n"
xml += " <BlockMap>\n"
Expand All @@ -274,8 +283,9 @@ def _bmap_file_end(self):
self._f_bmap.write(xml)

self._f_bmap.seek(self._mapped_count_pos1)
self._f_bmap.write("%s or %.1f%%"
% (self.mapped_size_human, self.mapped_percent))
self._f_bmap.write(
"%s or %.1f%%" % (self.mapped_size_human, self.mapped_percent)
)

self._f_bmap.seek(self._mapped_count_pos2)
self._f_bmap.write("%u" % self.mapped_cnt)
Expand Down Expand Up @@ -330,16 +340,16 @@ def generate(self, include_checksums=True):
self.mapped_cnt += last - first + 1
if include_checksums:
chksum = self._calculate_chksum(first, last)
chksum = " chksum=\"%s\"" % chksum
chksum = ' chksum="%s"' % chksum
else:
chksum = ""

if first != last:
self._f_bmap.write(" <Range%s> %s-%s </Range>\n"
% (chksum, first, last))
self._f_bmap.write(
" <Range%s> %s-%s </Range>\n" % (chksum, first, last)
)
else:
self._f_bmap.write(" <Range%s> %s </Range>\n"
% (chksum, first))
self._f_bmap.write(" <Range%s> %s </Range>\n" % (chksum, first))

self.mapped_size = self.mapped_cnt * self.block_size
self.mapped_size_human = human_size(self.mapped_size)
Expand All @@ -350,7 +360,6 @@ def generate(self, include_checksums=True):
try:
self._f_bmap.flush()
except IOError as err:
raise Error("cannot flush the bmap file '%s': %s"
% (self._bmap_path, err))
raise Error("cannot flush the bmap file '%s': %s" % (self._bmap_path, err))

self._f_image.seek(image_pos)
33 changes: 21 additions & 12 deletions bmaptools/BmapHelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
from subprocess import PIPE

# Path to check for zfs compatibility.
ZFS_COMPAT_PARAM_PATH = '/sys/module/zfs/parameters/zfs_dmu_offset_next_sync'
ZFS_COMPAT_PARAM_PATH = "/sys/module/zfs/parameters/zfs_dmu_offset_next_sync"


class Error(Exception):
"""A class for all the other exceptions raised by this module."""

pass


def human_size(size):
"""Transform size in bytes into a human-readable form."""
if size == 1:
Expand All @@ -44,7 +47,8 @@ def human_size(size):
if size < 1024:
return "%.1f %s" % (size, modifier)

return "%.1f %s" % (size, 'EiB')
return "%.1f %s" % (size, "EiB")


def human_time(seconds):
"""Transform time in seconds to the HH:MM:SS format."""
Expand All @@ -59,6 +63,7 @@ def human_time(seconds):

return result + "%.1fs" % seconds


def get_block_size(file_obj):
"""
Return block size for file object 'file_obj'. Errors are indicated by the
Expand All @@ -68,18 +73,19 @@ def get_block_size(file_obj):
# Get the block size of the host file-system for the image file by calling
# the FIGETBSZ ioctl (number 2).
try:
binary_data = ioctl(file_obj, 2, struct.pack('I', 0))
bsize = struct.unpack('I', binary_data)[0]
binary_data = ioctl(file_obj, 2, struct.pack("I", 0))
bsize = struct.unpack("I", binary_data)[0]
if not bsize:
raise IOError("get 0 bsize by FIGETBSZ ioctl")
except IOError as err:
stat = os.fstat(file_obj.fileno())
if hasattr(stat, 'st_blksize'):
if hasattr(stat, "st_blksize"):
bsize = stat.st_blksize
else:
raise IOError("Unable to determine block size")
return bsize


def program_is_available(name):
"""
This is a helper function which check if the external program 'name' is
Expand All @@ -93,6 +99,7 @@ def program_is_available(name):

return False


def get_file_system_type(path):
"""Return the file system type for 'path'."""

Expand All @@ -112,11 +119,14 @@ def get_file_system_type(path):
ftype = fields[1].lower()

if not ftype:
raise Error("failed to find file system type for path at '%s'\n"
"Here is the 'df -T' output\nstdout:\n%s\nstderr:\n%s"
% (path, stdout, stderr))
raise Error(
"failed to find file system type for path at '%s'\n"
"Here is the 'df -T' output\nstdout:\n%s\nstderr:\n%s"
% (path, stdout, stderr)
)
return ftype


def is_zfs_configuration_compatible():
"""Return if hosts zfs configuration is compatible."""

Expand All @@ -128,11 +138,10 @@ def is_zfs_configuration_compatible():
with open(path, "r") as fobj:
return int(fobj.readline()) == 1
except IOError as err:
raise Error("cannot open zfs param path '%s': %s"
% (path, err))
raise Error("cannot open zfs param path '%s': %s" % (path, err))
except ValueError as err:
raise Error("invalid value read from param path '%s': %s"
% (path, err))
raise Error("invalid value read from param path '%s': %s" % (path, err))


def is_compatible_file_system(path):
"""Return if paths file system is compatible."""
Expand Down
Loading

0 comments on commit 2f258f2

Please sign in to comment.