validate_ec.py (4003B)
1 # This file is part of GNU Taler 2 # (C) 2026 Taler Systems S.A. 3 # 4 # GNU Taler is free software; you can redistribute it and/or modify it under the 5 # terms of the GNU General Public License as published by the Free Software 6 # Foundation; either version 3, or (at your option) any later version. 7 # 8 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 10 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 # 12 # You should have received a copy of the GNU General Public License along with 13 # GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 14 """Print the .ec files that are complete execution data, skip the rest. 15 16 A dump whose app was killed while writing leaves a truncated file, and jacoco's 17 `merge` aborts on the first one it cannot read -- so one bad file out of a 18 thousand loses a whole campaign's coverage. Rather than merge in batches and 19 guess, this walks each file's block structure once and keeps only the files 20 that end on a block boundary. 21 22 python3 validate_ec.py <dir> [--quiet] # one good path per line on stdout 23 24 The format is org.jacoco.core.data.ExecutionDataWriter's: a header block, then 25 session-info and execution-data blocks, with Java DataOutput encodings and 26 jacoco's own var-int for array lengths. 27 """ 28 29 from __future__ import annotations 30 31 import sys 32 from pathlib import Path 33 34 BLOCK_HEADER = 0x01 35 BLOCK_SESSIONINFO = 0x10 36 BLOCK_EXECUTIONDATA = 0x11 37 MAGIC = 0xC0C0 38 39 40 class Truncated(Exception): 41 pass 42 43 44 class Reader: 45 def __init__(self, data: bytes) -> None: 46 self.data = data 47 self.pos = 0 48 49 def take(self, n: int) -> bytes: 50 if self.pos + n > len(self.data): 51 raise Truncated(f"wanted {n} bytes at {self.pos} of {len(self.data)}") 52 out = self.data[self.pos:self.pos + n] 53 self.pos += n 54 return out 55 56 def u8(self) -> int: 57 return self.take(1)[0] 58 59 def u16(self) -> int: 60 return int.from_bytes(self.take(2), "big") 61 62 def u64(self) -> int: 63 return int.from_bytes(self.take(8), "big") 64 65 def utf(self) -> bytes: 66 return self.take(self.u16()) 67 68 def var_int(self) -> int: 69 value, shift = 0, 0 70 while True: 71 b = self.u8() 72 value |= (b & 0x7F) << shift 73 if not b & 0x80: 74 return value 75 shift += 7 76 77 def boolean_array(self) -> None: 78 bits = self.var_int() 79 self.take((bits + 7) // 8) 80 81 82 def complete(path: Path) -> bool: 83 """Whether this file is execution data all the way to its last byte.""" 84 r = Reader(path.read_bytes()) 85 if not r.data: 86 return False 87 try: 88 while r.pos < len(r.data): 89 block = r.u8() 90 if block == BLOCK_HEADER: 91 if r.u16() != MAGIC: 92 return False 93 r.u16() # format version 94 elif block == BLOCK_SESSIONINFO: 95 r.utf() 96 r.u64() # start 97 r.u64() # dump 98 elif block == BLOCK_EXECUTIONDATA: 99 r.u64() # class id 100 r.utf() # class name 101 r.boolean_array() 102 else: 103 return False 104 except Truncated: 105 return False 106 return True 107 108 109 def main() -> int: 110 if len(sys.argv) < 2: 111 print(__doc__, file=sys.stderr) 112 return 2 113 quiet = "--quiet" in sys.argv 114 files = sorted(Path(sys.argv[1]).rglob("*.ec")) 115 good = [] 116 for f in files: 117 if complete(f): 118 good.append(f) 119 elif not quiet: 120 print(f"skipping incomplete {f} ({f.stat().st_size} bytes)", 121 file=sys.stderr) 122 if not quiet: 123 print(f"{len(good)} of {len(files)} execution data files are complete", 124 file=sys.stderr) 125 for f in good: 126 print(f) 127 return 0 if good else 1 128 129 130 if __name__ == "__main__": 131 sys.exit(main())