reproducible-epub.py (3316B)
1 #!/usr/bin/env python3 2 3 # This file is part of GNU TALER. 4 # Copyright (C) 2026 Taler Systems SA 5 # 6 # TALER is free software; you can redistribute it and/or modify it under the 7 # terms of the GNU Lesser General Public License as published by the Free 8 # Software Foundation; either version 2.1, or (at your option) any later 9 # version. 10 # 11 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY 12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 14 # more details. 15 # 16 # You should have received a copy of the GNU Lesser General Public License 17 # along with TALER; see the file COPYING. If not, see 18 # <http://www.gnu.org/licenses/> 19 20 """Set every entry timestamp of an EPUB file to $SOURCE_DATE_EPOCH. 21 22 Sphinx uses SOURCE_DATE_EPOCH for the dates it writes into the documents it 23 generates, but it assembles the EPUB container with zipfile.ZipFile.write(), 24 which reads the modification time of each member off the filesystem -- that is, 25 from the moment of the build. Two EPUBs built from identical sources therefore 26 differ in all of their entry headers. Rewriting the archive with a single 27 fixed timestamp is enough to make them identical; the entry order, the 28 compression of each entry and the stored (uncompressed) "mimetype" entry that 29 the format requires to come first are all preserved as they were. 30 31 Does nothing if SOURCE_DATE_EPOCH is unset, so that calling this unconditionally 32 is harmless. 33 """ 34 35 import os 36 import shutil 37 import sys 38 import tempfile 39 import time 40 import zipfile 41 42 # The DOS timestamps in a zip archive cannot express anything before this. 43 ZIP_EPOCH = (1980, 1, 1, 0, 0, 0) 44 45 46 def normalize(filename, date_time): 47 """Rewrite the archive at @a filename with all entry times at @a date_time.""" 48 with zipfile.ZipFile(filename) as original: 49 infolist = original.infolist() 50 contents = [original.read(info) for info in infolist] 51 52 # Write to a temporary file in the same directory and move it into place, so 53 # that an error cannot leave a half-written archive behind under the name of 54 # a complete one. 55 handle, temporary = tempfile.mkstemp( 56 dir=os.path.dirname(os.path.abspath(filename)), suffix=".epub" 57 ) 58 os.close(handle) 59 try: 60 with zipfile.ZipFile(temporary, "w") as rewritten: 61 for info, data in zip(infolist, contents): 62 entry = zipfile.ZipInfo(filename=info.filename, date_time=date_time) 63 entry.compress_type = info.compress_type 64 entry.external_attr = info.external_attr 65 entry.internal_attr = info.internal_attr 66 entry.create_system = info.create_system 67 entry.comment = info.comment 68 rewritten.writestr(entry, data) 69 shutil.move(temporary, filename) 70 except BaseException: 71 if os.path.exists(temporary): 72 os.unlink(temporary) 73 raise 74 75 76 def main(argv): 77 source_date_epoch = os.environ.get("SOURCE_DATE_EPOCH") 78 if not source_date_epoch: 79 return 0 80 date_time = max(time.gmtime(int(source_date_epoch))[:6], ZIP_EPOCH) 81 for filename in argv[1:]: 82 normalize(filename, date_time) 83 return 0 84 85 86 if __name__ == "__main__": 87 sys.exit(main(sys.argv))