|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import time |
| 6 | +import zipfile |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from os import PathLike |
| 11 | + from pathlib import Path |
| 12 | + from typing import Optional, Tuple, Union |
| 13 | + |
| 14 | +DEFAULT_DATE_TIME = (1980, 1, 1, 0, 0, 0) |
| 15 | +DEFAULT_DIR_MODE = 0o755 |
| 16 | +DEFAULT_FILE_MODE = 0o644 |
| 17 | + |
| 18 | +class SourceDateEpochError(Exception): |
| 19 | + """Raise when there are issues with $SOURCE_DATE_EPOCH""" |
| 20 | + |
| 21 | +def date_time() -> Tuple[int, int, int, int, int, int]: |
| 22 | + """Returns date_time value used to force overwrite on all ZipInfo objects. Defaults to |
| 23 | + 1980-01-01 00:00:00. You can set this with the environment variable SOURCE_DATE_EPOCH as an |
| 24 | + integer value representing seconds since Epoch. |
| 25 | + """ |
| 26 | + source_date_epoch = os.environ.get("SOURCE_DATE_EPOCH", None) |
| 27 | + if source_date_epoch is not None: |
| 28 | + dt = time.gmtime(int(source_date_epoch))[:6] |
| 29 | + if dt[0] < 1980: |
| 30 | + raise SourceDateEpochError( |
| 31 | + "$SOURCE_DATE_EPOCH must be >= 315532800, since ZIP files need MS-DOS date/time format, which can be 1/1/1980, at minimum." |
| 32 | + ) |
| 33 | + return dt |
| 34 | + return DEFAULT_DATE_TIME |
| 35 | + |
| 36 | +class ZipFile(zipfile.ZipFile): |
| 37 | + def write_reproducibly( |
| 38 | + self, |
| 39 | + filename: PathLike, |
| 40 | + arcname: Optional[Union[Path, str]] = None, |
| 41 | + compress_type: Optional[int] = None, |
| 42 | + compresslevel: Optional[int] = None, |
| 43 | + ): |
| 44 | + if not self.fp: |
| 45 | + raise ValueError("Attempt to write to ZIP archive that was already closed") |
| 46 | + if self._writing: |
| 47 | + raise ValueError("Can't write to ZIP archive while an open writing handle exists") |
| 48 | + |
| 49 | + zinfo = zipfile.ZipInfo.from_file(filename, arcname, strict_timestamps=self._strict_timestamps) |
| 50 | + zinfo.date_time = date_time() |
| 51 | + if zinfo.is_dir(): |
| 52 | + zinfo.external_attr = (0o40000 | DEFAULT_DIR_MODE) << 16 |
| 53 | + zinfo.external_attr |= 0x10 # MS-DOS directory flag |
| 54 | + else: |
| 55 | + zinfo.external_attr = DEFAULT_FILE_MODE << 16 |
| 56 | + |
| 57 | + if zinfo.is_dir(): |
| 58 | + zinfo.compress_size = 0 |
| 59 | + zinfo.CRC = 0 |
| 60 | + self.mkdir(zinfo) |
| 61 | + else: |
| 62 | + if compress_type is not None: |
| 63 | + zinfo.compress_type = compress_type |
| 64 | + else: |
| 65 | + zinfo.compress_type = self.compression |
| 66 | + |
| 67 | + if compresslevel is not None: |
| 68 | + zinfo._compresslevel = compresslevel |
| 69 | + else: |
| 70 | + zinfo._compresslevel = self.compresslevel |
| 71 | + |
| 72 | + with open(filename, "rb") as src, self.open(zinfo, "w") as dest: |
| 73 | + shutil.copyfileobj(src, dest, 1024 * 8) |
| 74 | + |
| 75 | + def writestr_reproducibly( |
| 76 | + self, |
| 77 | + zinfo_or_arcname: Union[str, zipfile.ZipInfo], |
| 78 | + data: Union[str, bytes], |
| 79 | + compress_type: Optional[int] = None, |
| 80 | + compresslevel: Optional[int] = None, |
| 81 | + ): |
| 82 | + if isinstance(data, str): |
| 83 | + data = data.encode("utf-8") |
| 84 | + |
| 85 | + if not isinstance(zinfo_or_arcname, zipfile.ZipInfo): |
| 86 | + zinfo = zipfile.ZipInfo(filename=zinfo_or_arcname, date_time=date_time()) |
| 87 | + zinfo.compress_type = self.compression |
| 88 | + zinfo._compresslevel = self.compresslevel |
| 89 | + if zinfo.is_dir(): |
| 90 | + zinfo.external_attr = (0o40000 | DEFAULT_DIR_MODE) << 16 |
| 91 | + zinfo.external_attr |= 0x10 # MS-DOS directory flag |
| 92 | + else: |
| 93 | + zinfo.external_attr = DEFAULT_FILE_MODE << 16 |
| 94 | + else: |
| 95 | + zinfo = zinfo_or_arcname |
| 96 | + |
| 97 | + zinfo.file_size = len(data) |
| 98 | + if compress_type is not None: |
| 99 | + zinfo.compress_type = compress_type |
| 100 | + |
| 101 | + if compresslevel is not None: |
| 102 | + zinfo._compresslevel = compresslevel |
| 103 | + |
| 104 | + if not self.fp: |
| 105 | + raise ValueError("Attempt to write to ZIP archive that was already closed") |
| 106 | + if self._writing: |
| 107 | + raise ValueError("Can't write to ZIP archive while an open writing handle exists.") |
| 108 | + |
| 109 | + with self._lock: |
| 110 | + with self.open(zinfo, mode="w") as dest: |
| 111 | + dest.write(data) |
0 commit comments