diff --git a/src/bmaptool/BmapCopy.py b/src/bmaptool/BmapCopy.py index f8f0a2f..4beba3f 100644 --- a/src/bmaptool/BmapCopy.py +++ b/src/bmaptool/BmapCopy.py @@ -403,8 +403,14 @@ def _parse_bmap(self): self._f_bmap.seek(0) xml_extract = "" for num, line in enumerate(self._f_bmap): - if num >= err.position[0] - 4 and num <= err.position[0] + 4: - xml_extract += "Line %d: %s" % (num, line) + if num < err.position[0] - 4: + continue + if num > err.position[0] + 4: + break + xml_extract += "Line %d: %s" % ( + num, + line.decode(errors="backslashreplace"), + ) raise Error( "cannot parse the bmap file '%s' which should be a " @@ -715,7 +721,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1] - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/src/bmaptool/BmapHelpers.py b/src/bmaptool/BmapHelpers.py index 8a3ccb4..76c76ce 100644 --- a/src/bmaptool/BmapHelpers.py +++ b/src/bmaptool/BmapHelpers.py @@ -52,8 +52,8 @@ def human_size(size): def human_time(seconds): """Transform time in seconds to the HH:MM:SS format.""" - (minutes, seconds) = divmod(seconds, 60) - (hours, minutes) = divmod(minutes, 60) + minutes, seconds = divmod(seconds, 60) + hours, minutes = divmod(minutes, 60) result = "" if hours: diff --git a/src/bmaptool/CLI.py b/src/bmaptool/CLI.py index 40acf31..525192e 100644 --- a/src/bmaptool/CLI.py +++ b/src/bmaptool/CLI.py @@ -100,6 +100,9 @@ def __init__(self, file_obj, name): self._file_obj = file_obj self.name = name + def __iter__(self): + return iter(self._file_obj) + def __getattr__(self, name): return getattr(self._file_obj, name) @@ -215,7 +218,7 @@ def verify_bmap_signature_gpgbin(bmap_obj, detached_sig, gpgargv, keyring): stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) - (output, error) = sp.communicate() + output, error = sp.communicate() if sp.returncode > 0: if error.find(b"[GNUPG:] NO_PUBKEY "): error_out("No matching key found") @@ -362,6 +365,35 @@ def _add_ext(p, ext): have_method.add("gpgv") if not have_method: + # if no method is available: that is okay i --no-sig-verify was passed + if args.no_sig_verify: + # if signature was detached, there is no cleartext to return + if detached_sig: + return None + # Since no gpg is available and no signature verification is + # required, strip off the signature from the plaintext manually. + # Since mmap is used later, we cannot use io.BytesIO() but need a + # real file. + try: + tmp_obj = tempfile.TemporaryFile("w+b") + except IOError as err: + error_out("cannot create a temporary file for bmap:\n%s", err) + + header_done = False + for line in bmap_obj.readlines(): + if not header_done: + # header is done after the first empty line + if line == b"\n": + header_done = True + continue + if line == b"-----BEGIN PGP SIGNATURE-----\n": + break + if line.endswith(b"-----BEGIN PGP SIGNATURE-----\n"): + line.removeprefix(b"- ") + tmp_obj.write(line) + + tmp_obj.seek(0) + return tmp_obj error_out("Cannot verify GPG signature without GPG") for method in ["gpgme", "gpgv", "gpg"]: @@ -510,7 +542,7 @@ def open_files(args): # Open the bmap file. Try to discover the bmap file automatically if it # was not specified. - (bmap_obj, bmap_path) = find_and_open_bmap(args, image_obj.is_url) + bmap_obj, bmap_path = find_and_open_bmap(args, image_obj.is_url) if bmap_path == args.image: # Most probably the user specified the bmap file instead of the image diff --git a/src/bmaptool/TransRead.py b/src/bmaptool/TransRead.py index c35e033..b32e3cd 100644 --- a/src/bmaptool/TransRead.py +++ b/src/bmaptool/TransRead.py @@ -222,9 +222,13 @@ def __del__(self): if getattr(self, "_child_processes"): for child in self._child_processes: - if child.poll() is None: + returncode = child.poll() + if returncode is None: + # process is still running child.kill() - child.wait() + returncode = child.wait() + if returncode != 0: + raise Error(f"non-zero exit of {child.args}: {returncode}") self._child_processes = [] def _read_thread(self, f_from, f_to): diff --git a/tests/oldcodebase/BmapCopy1_0.py b/tests/oldcodebase/BmapCopy1_0.py index d381128..632f39a 100644 --- a/tests/oldcodebase/BmapCopy1_0.py +++ b/tests/oldcodebase/BmapCopy1_0.py @@ -501,7 +501,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1].with_traceback(exc_info[2]) - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/tests/oldcodebase/BmapCopy2_0.py b/tests/oldcodebase/BmapCopy2_0.py index bfd085e..0c3aac4 100644 --- a/tests/oldcodebase/BmapCopy2_0.py +++ b/tests/oldcodebase/BmapCopy2_0.py @@ -450,7 +450,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1].with_traceback(exc_info[2]) - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/tests/oldcodebase/BmapCopy2_1.py b/tests/oldcodebase/BmapCopy2_1.py index 77c6a2e..e274ffb 100644 --- a/tests/oldcodebase/BmapCopy2_1.py +++ b/tests/oldcodebase/BmapCopy2_1.py @@ -449,7 +449,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1].with_traceback(exc_info[2]) - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/tests/oldcodebase/BmapCopy2_2.py b/tests/oldcodebase/BmapCopy2_2.py index 89235f2..7449e94 100644 --- a/tests/oldcodebase/BmapCopy2_2.py +++ b/tests/oldcodebase/BmapCopy2_2.py @@ -450,7 +450,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1].with_traceback(exc_info[2]) - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/tests/oldcodebase/BmapCopy2_3.py b/tests/oldcodebase/BmapCopy2_3.py index 21097e2..faa31f2 100644 --- a/tests/oldcodebase/BmapCopy2_3.py +++ b/tests/oldcodebase/BmapCopy2_3.py @@ -486,7 +486,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1].with_traceback(exc_info[2]) - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/tests/oldcodebase/BmapCopy2_4.py b/tests/oldcodebase/BmapCopy2_4.py index 21097e2..faa31f2 100644 --- a/tests/oldcodebase/BmapCopy2_4.py +++ b/tests/oldcodebase/BmapCopy2_4.py @@ -486,7 +486,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1].with_traceback(exc_info[2]) - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/tests/oldcodebase/BmapCopy2_5.py b/tests/oldcodebase/BmapCopy2_5.py index 37b5f48..ab6f154 100644 --- a/tests/oldcodebase/BmapCopy2_5.py +++ b/tests/oldcodebase/BmapCopy2_5.py @@ -526,7 +526,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1].with_traceback(exc_info[2]) - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/tests/oldcodebase/BmapCopy2_6.py b/tests/oldcodebase/BmapCopy2_6.py index 9bbdc4d..cdd4e2c 100644 --- a/tests/oldcodebase/BmapCopy2_6.py +++ b/tests/oldcodebase/BmapCopy2_6.py @@ -526,7 +526,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1].with_traceback(exc_info[2]) - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/tests/oldcodebase/BmapCopy3_0.py b/tests/oldcodebase/BmapCopy3_0.py index 81eed59..898e0d3 100644 --- a/tests/oldcodebase/BmapCopy3_0.py +++ b/tests/oldcodebase/BmapCopy3_0.py @@ -575,7 +575,7 @@ def copy(self, sync=True, verify=True): exc_info = batch[1] raise exc_info[1].with_traceback(exc_info[2]) - (start, end, buf) = batch[1:4] + start, end, buf = batch[1:4] assert len(buf) <= (end - start + 1) * self.block_size assert len(buf) > (end - start) * self.block_size diff --git a/tests/test_bmap_helpers.py b/tests/test_bmap_helpers.py index 2cefad6..1ec90ea 100644 --- a/tests/test_bmap_helpers.py +++ b/tests/test_bmap_helpers.py @@ -32,7 +32,6 @@ from backports.tempfile import TemporaryDirectory from bmaptool import BmapHelpers - # This is a work-around for Centos 6 try: import unittest2 as unittest # pylint: disable=F0401