Readers go by the directory at the end, by the header before each file or by both, so all of it has to agree
(path: Path)
| 123 | |
| 124 | |
| 125 | def read_wheel(path: Path) -> list[Member]: |
| 126 | """Readers go by the directory at the end, by the header before each file or by both, so all of it has to agree""" |
| 127 | wheel = path.read_bytes() |
| 128 | members: list[Member] = [] |
| 129 | position = 0 |
| 130 | directory_size = 0 |
| 131 | with zipfile.ZipFile(path) as archive: |
| 132 | entries = archive.infolist() |
| 133 | for entry in entries: |
| 134 | header = entry.FileHeader() |
| 135 | start, end = position + len(header), position + len(header) + entry.compress_size |
| 136 | data = decompress(wheel[start:end], wbits=-15) |
| 137 | # hatchling sets the regular-file bit on the package's files, not on those it generates |
| 138 | mode = entry.external_attr >> 16 & ~0o100000 |
| 139 | if ( |
| 140 | not wheel.startswith(header, position) |
| 141 | or entry.header_offset != position |
| 142 | or entry.compress_type != zipfile.ZIP_DEFLATED |
| 143 | or data is None |
| 144 | or data != archive.read(entry) |
| 145 | or mode not in (0o644, 0o755) |
| 146 | or entry.extra |
| 147 | # all but the flag of a non-ASCII name |
| 148 | or entry.flag_bits & ~0x800 |
| 149 | ): |
| 150 | problems.append(f"{path.name}: {entry.filename} is not stored the way hatchling stores a file") |
| 151 | return members |
| 152 | members.append(Member(entry.filename, data, executable=mode == 0o755)) |
| 153 | position = end |
| 154 | directory_size += len(header) + 16 |
| 155 | |
| 156 | count = len(entries) |
| 157 | sizes = (0, 0, count, count, directory_size, position, 0) |
| 158 | if wheel[position + directory_size :] != struct.pack("<4s4H2LH", b"PK\5\6", *sizes): |
| 159 | problems.append(f"{path.name} has something besides its files") |
| 160 | return members |
| 161 | |
| 162 | |
| 163 | def read_sdist(path: Path) -> list[Member]: |
no test coverage detected