reassemble-voc-database.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """Reassemble the two private transport ZIPs; never install or replace a database."""
  2. import argparse
  3. import hashlib
  4. import json
  5. import os
  6. from pathlib import Path
  7. import sys
  8. import tempfile
  9. import zipfile
  10. sys.dont_write_bytecode = True
  11. FILENAME = 'ksf-voc-database-20261106-r1.zip'
  12. SIZE = 25913256
  13. SHA256 = '1ba1ac2fa6a8b774f8945d88e014c07dd5e03332792af298a41f015ba6946f9f'
  14. PART_SIZES = [14000000, 11913256]
  15. def require(value, code):
  16. if not value:
  17. raise ValueError(code)
  18. def digest(p):
  19. with Path(p).open('rb') as f:
  20. return hashlib.file_digest(f, 'sha256').hexdigest()
  21. def regular_path(p):
  22. p = Path(p)
  23. require(p.is_absolute(), 'ABSOLUTE_PATH_REQUIRED')
  24. for parent in (p, *p.parents):
  25. require(not parent.is_symlink(), 'SYMLINK_PATH_REFUSED')
  26. return p
  27. def reassemble(parts, output):
  28. require(len(parts) == 2, 'EXACTLY_TWO_PARTS_REQUIRED')
  29. paths = [regular_path(p) for p in parts]
  30. require(len(set(paths)) == 2 and all(p.is_file() for p in paths), 'PART_MISSING_OR_DUPLICATED')
  31. output = regular_path(output)
  32. require(output.name == FILENAME and output.parent.is_dir(), 'INVALID_OUTPUT_PATH')
  33. ordered = {}
  34. common = None
  35. for p in paths:
  36. with zipfile.ZipFile(p) as z:
  37. require(z.getinfo('TRANSPORT.json').file_size < 16000, 'INVALID_TRANSPORT_MANIFEST')
  38. meta = json.loads(z.read('TRANSPORT.json'))
  39. require(meta.get('filename') == FILENAME and meta.get('bytes') == SIZE and
  40. meta.get('sha256') == SHA256 and len(meta.get('parts', [])) == 2,
  41. 'WRONG_SOURCE_PACKAGE')
  42. require(common is None or common == meta, 'TRANSPORT_MANIFEST_MISMATCH')
  43. common = meta
  44. found = [i for i in (1, 2) if FILENAME + f'.part{i:02}' in z.namelist()]
  45. require(len(found) == 1 and found[0] not in ordered, 'PART_MISSING_OR_DUPLICATED')
  46. index = found[0]
  47. entry = meta['parts'][index - 1]
  48. name = FILENAME + f'.part{index:02}'
  49. require(entry.get('chunk') == name and entry.get('bytes') == PART_SIZES[index - 1] and
  50. isinstance(entry.get('sha256'), str) and len(entry['sha256']) == 64 and
  51. z.getinfo(name).file_size == PART_SIZES[index - 1], 'INVALID_PART_SIZE_OR_NAME')
  52. require(len(z.namelist()) == len(set(z.namelist())) == 4 and
  53. set(z.namelist()) == {name, 'TRANSPORT.json', 'reassemble-voc-database.py', 'README.md'},
  54. 'UNEXPECTED_TRANSPORT_CONTENTS')
  55. ordered[index] = (p, entry)
  56. require(set(ordered) == {1, 2}, 'PART_MISSING_OR_DUPLICATED')
  57. if output.exists():
  58. require(output.is_file() and output.stat().st_size == SIZE and digest(output) == SHA256,
  59. 'DIFFERENT_OUTPUT_ALREADY_EXISTS')
  60. return {'ok': True, 'operation': 'unchanged', 'output': str(output), 'bytes': SIZE,
  61. 'sha256': SHA256, 'fileCreated': False, 'installed': False, 'databaseWrites': 0}
  62. fd, temporary = tempfile.mkstemp(prefix='.ksf-voc-reassemble-', dir=output.parent)
  63. try:
  64. total = 0
  65. whole = hashlib.sha256()
  66. with os.fdopen(fd, 'wb') as dest:
  67. for index in (1, 2):
  68. p, entry = ordered[index]
  69. chunk_hash = hashlib.sha256()
  70. length = 0
  71. with zipfile.ZipFile(p) as z, z.open(entry['chunk']) as source:
  72. while data := source.read(1024 * 1024):
  73. length += len(data)
  74. require(length <= entry['bytes'], 'PART_TOO_LARGE')
  75. chunk_hash.update(data)
  76. whole.update(data)
  77. dest.write(data)
  78. require(length == entry['bytes'] and chunk_hash.hexdigest() == entry['sha256'],
  79. 'PART_HASH_MISMATCH')
  80. total += length
  81. require(total == SIZE and whole.hexdigest() == SHA256, 'REASSEMBLED_HASH_MISMATCH')
  82. dest.flush()
  83. os.fsync(dest.fileno())
  84. # A same-filesystem hard link publishes the verified file without overwriting a race winner.
  85. os.link(temporary, output)
  86. return {'ok': True, 'operation': 'reassembled', 'output': str(output), 'bytes': total,
  87. 'sha256': whole.hexdigest(), 'fileCreated': True, 'installed': False, 'databaseWrites': 0}
  88. finally:
  89. os.unlink(temporary)
  90. def main():
  91. parser = argparse.ArgumentParser(description=__doc__)
  92. parser.add_argument('--part', action='append', required=True, help='Actual absolute path to each transport ZIP; repeat twice')
  93. parser.add_argument('--output', required=True, help='Absolute path of the original ZIP under an existing private directory')
  94. args = parser.parse_args()
  95. try:
  96. print(json.dumps(reassemble(args.part, args.output), ensure_ascii=False, indent=2))
  97. except Exception as exc:
  98. code = str(exc) if isinstance(exc, ValueError) else 'REASSEMBLY_STOPPED'
  99. print(json.dumps({'ok': False, 'error': {'code': code}, 'installed': False, 'databaseWrites': 0}))
  100. return 1
  101. return 0
  102. if __name__ == '__main__':
  103. raise SystemExit(main())