| 1 |
# SPDX-License-Identifier: BSD-2-Clause |
| 2 |
|
| 3 |
# Copyright (c) 2024 Phil Thompson <[email protected]> |
| 4 |
|
| 5 |
|
| 6 |
import base64 |
| 7 |
import hashlib |
| 8 |
import os |
| 9 |
import zipfile |
| 10 |
|
| 11 |
from sipbuild import UserException |
| 12 |
|
| 13 |
|
| 14 |
def create_wheel(wheel_path, names): |
| 15 |
""" Create the wheel from a list of file names. """ |
| 16 |
|
| 17 |
with zipfile.ZipFile(wheel_path, 'w', compression=zipfile.ZIP_DEFLATED) as zf: |
| 18 |
for name in names: |
| 19 |
zf.write(name) |
| 20 |
|
| 21 |
|
| 22 |
def unpack_wheel(wheel_path): |
| 23 |
""" Unpack a wheel in the current directory. """ |
| 24 |
|
| 25 |
try: |
| 26 |
zf = zipfile.ZipFile(wheel_path) |
| 27 |
except FileNotFoundError: |
| 28 |
raise UserException("Unable to find '{0}'".format(wheel_path)) |
| 29 |
|
| 30 |
for zi in zf.infolist(): |
| 31 |
zf.extract(zi) |
| 32 |
attr = zi.external_attr >> 16 |
| 33 |
if attr: |
| 34 |
os.chmod(zi.filename, attr) |
| 35 |
|
| 36 |
|
| 37 |
def write_record_file(distinfo_dir): |
| 38 |
""" Write the RECORD file for the contents of the current directory. |
| 39 |
Return a list of relative file names that were recorded. |
| 40 |
""" |
| 41 |
|
| 42 |
record_path = os.path.join(distinfo_dir, 'RECORD') |
| 43 |
try: |
| 44 |
os.remove(record_path) |
| 45 |
except FileNotFoundError: |
| 46 |
pass |
| 47 |
|
| 48 |
# Calculate the signatures of the files. |
| 49 |
record = [] |
| 50 |
|
| 51 |
for dirpath, dirnames, filenames in os.walk('.'): |
| 52 |
# Reproducable builds. |
| 53 |
dirnames.sort() |
| 54 |
filenames.sort() |
| 55 |
|
| 56 |
for filename in filenames: |
| 57 |
# This will result in a name with no leading '.'. |
| 58 |
name = os.path.relpath(os.path.join(dirpath, filename)) |
| 59 |
|
| 60 |
with open(name, 'rb') as f: |
| 61 |
data = f.read() |
| 62 |
|
| 63 |
digest = base64.urlsafe_b64encode( |
| 64 |
hashlib.sha256(data).digest()).rstrip(b'=').decode('ascii') |
| 65 |
record.append((name, digest, len(data))) |
| 66 |
|
| 67 |
# Write the file. |
| 68 |
names = [] |
| 69 |
|
| 70 |
with open(record_path, 'w') as f: |
| 71 |
for name, digest, nbytes in record: |
| 72 |
name = name.replace(os.path.sep, '/') |
| 73 |
f.write('{},sha256={},{}\n'.format(name, digest, nbytes)) |
| 74 |
names.append(name) |
| 75 |
|
| 76 |
f.write('{},,\n'.format(record_path)) |
| 77 |
names.append(record_path) |
| 78 |
|
| 79 |
return names |