|
| 1 | +import glob |
| 2 | +import hashlib |
| 3 | +import io |
| 4 | +import os |
| 5 | +import pathlib |
| 6 | +import shutil |
| 7 | +import stat |
| 8 | +import sys |
| 9 | +import urllib.request |
| 10 | +import zipfile |
| 11 | +from base64 import urlsafe_b64encode |
| 12 | + |
| 13 | +packages = { |
| 14 | + "Windows amd64": { |
| 15 | + "asset": "windows-amd64.exe", |
| 16 | + "exec": "pglet.exe", |
| 17 | + "wheel_tags": ["py3-none-win_amd64"], |
| 18 | + "file_suffix": "py3-none-win_amd64", |
| 19 | + }, |
| 20 | + "Linux amd64": { |
| 21 | + "asset": "linux-amd64", |
| 22 | + "exec": "pglet", |
| 23 | + "wheel_tags": [ |
| 24 | + "py3-none-manylinux_2_17_x86_64", |
| 25 | + "py3-none-manylinux2014_x86_64", |
| 26 | + ], |
| 27 | + "file_suffix": "py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64", |
| 28 | + }, |
| 29 | + "Linux arm64": { |
| 30 | + "asset": "linux-arm64", |
| 31 | + "exec": "pglet", |
| 32 | + "wheel_tags": [ |
| 33 | + "py3-none-manylinux_2_17_aarch64", |
| 34 | + "py3-none-manylinux2014_aarch64", |
| 35 | + ], |
| 36 | + "file_suffix": "py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64", |
| 37 | + }, |
| 38 | + "Linux arm": { |
| 39 | + "asset": "linux-arm", |
| 40 | + "exec": "pglet", |
| 41 | + "wheel_tags": [ |
| 42 | + "py3-none-manylinux_2_17_armv7l", |
| 43 | + "py3-none-manylinux2014_armv7l", |
| 44 | + ], |
| 45 | + "file_suffix": "py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l", |
| 46 | + }, |
| 47 | + "macOS amd64": { |
| 48 | + "asset": "darwin-amd64", |
| 49 | + "exec": "pglet", |
| 50 | + "wheel_tags": ["py3-none-macosx_10_14_x86_64"], |
| 51 | + "file_suffix": "py3-none-macosx_10_14_x86_64", |
| 52 | + }, |
| 53 | + "macOS arm64": { |
| 54 | + "asset": "darwin-arm64", |
| 55 | + "exec": "pglet", |
| 56 | + "wheel_tags": ["py3-none-macosx_12_0_arm64"], |
| 57 | + "file_suffix": "py3-none-macosx_12_0_arm64", |
| 58 | + }, |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +def unpack_zip(zip_path, dest_dir): |
| 63 | + zf = zipfile.ZipFile(zip_path) |
| 64 | + zf.extractall(path=dest_dir) |
| 65 | + |
| 66 | + |
| 67 | +def download_pglet(version, suffix, dest_file): |
| 68 | + file_name = f"pglet-{version}-{suffix}" |
| 69 | + pglet_url = ( |
| 70 | + f"https://github.com/pglet/pglet/releases/download/v{version}/{file_name}" |
| 71 | + ) |
| 72 | + print(f"Downloading {pglet_url}...") |
| 73 | + urllib.request.urlretrieve(pglet_url, dest_file) |
| 74 | + st = os.stat(dest_file) |
| 75 | + os.chmod(dest_file, st.st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH) |
| 76 | + |
| 77 | + |
| 78 | +def get_pglet_version(current_dir): |
| 79 | + constants_py = current_dir.joinpath("pglet", "constants.py") |
| 80 | + |
| 81 | + version_prefix = "PGLET_SERVER_VERSION" |
| 82 | + with open(constants_py) as yml: |
| 83 | + for line in yml: |
| 84 | + if version_prefix in line: |
| 85 | + return line.split("=")[1].strip().strip('"') |
| 86 | + raise f"{version_prefix} not found in constants.py" |
| 87 | + |
| 88 | + |
| 89 | +def read_chunks(file, size=io.DEFAULT_BUFFER_SIZE): |
| 90 | + """Yield pieces of data from a file-like object until EOF.""" |
| 91 | + while True: |
| 92 | + chunk = file.read(size) |
| 93 | + if not chunk: |
| 94 | + break |
| 95 | + yield chunk |
| 96 | + |
| 97 | + |
| 98 | +def rehash(path, blocksize=1 << 20): |
| 99 | + """Return (hash, length) for path using hashlib.sha256()""" |
| 100 | + h = hashlib.sha256() |
| 101 | + length = 0 |
| 102 | + with open(path, "rb") as f: |
| 103 | + for block in read_chunks(f, size=blocksize): |
| 104 | + length += len(block) |
| 105 | + h.update(block) |
| 106 | + digest = "sha256=" + urlsafe_b64encode(h.digest()).decode("latin1").rstrip("=") |
| 107 | + # unicode/str python2 issues |
| 108 | + return (digest, str(length)) # type: ignore |
| 109 | + |
| 110 | + |
| 111 | +current_dir = pathlib.Path(os.getcwd()) |
| 112 | +print("current_dir", current_dir) |
| 113 | + |
| 114 | +whl_files = glob.glob(str(current_dir.joinpath("dist", "*.whl"))) |
| 115 | +if len(whl_files) == 0: |
| 116 | + print("No .whl files found. Run 'pdm build' first.") |
| 117 | + sys.exit(1) |
| 118 | + |
| 119 | +orig_whl = whl_files[0] |
| 120 | + |
| 121 | +package_version = os.path.basename(orig_whl).split("-")[1] |
| 122 | +pglet_version = get_pglet_version(current_dir) |
| 123 | + |
| 124 | +print("package_version", package_version) |
| 125 | +print("pglet_version", pglet_version) |
| 126 | + |
| 127 | +for name, package in packages.items(): |
| 128 | + print(f"Building {name}...") |
| 129 | + |
| 130 | + print("Unpacking original wheel file...") |
| 131 | + unpacked_whl = current_dir.joinpath("dist", "wheel") |
| 132 | + unpacked_whl.mkdir(exist_ok=True) |
| 133 | + unpack_zip(orig_whl, unpacked_whl) |
| 134 | + |
| 135 | + # read original WHEEL file omitting tags |
| 136 | + wheel_path = str( |
| 137 | + current_dir.joinpath( |
| 138 | + "dist", "wheel", f"pglet-{package_version}.dist-info", "WHEEL" |
| 139 | + ) |
| 140 | + ) |
| 141 | + wheel_lines = [] |
| 142 | + |
| 143 | + with open(wheel_path, "r") as f: |
| 144 | + for line in f.readlines(): |
| 145 | + if not "Tag: " in line: |
| 146 | + wheel_lines.append(line) |
| 147 | + |
| 148 | + # print(wheel_lines) |
| 149 | + |
| 150 | + # read original RECORD file |
| 151 | + record_path = str( |
| 152 | + current_dir.joinpath( |
| 153 | + "dist", "wheel", f"pglet-{package_version}.dist-info", "RECORD" |
| 154 | + ) |
| 155 | + ) |
| 156 | + record_lines = [] |
| 157 | + |
| 158 | + with open(record_path, "r") as f: |
| 159 | + for line in f.readlines(): |
| 160 | + if not "dist-info/WHEEL," in line: |
| 161 | + record_lines.append(line) |
| 162 | + |
| 163 | + # print(record_lines) |
| 164 | + |
| 165 | + # create "bin" directory |
| 166 | + bin_path = current_dir.joinpath("dist", "wheel", "pglet", "bin") |
| 167 | + bin_path.mkdir(exist_ok=True) |
| 168 | + asset = package["asset"] |
| 169 | + exec_filename = package["exec"] |
| 170 | + exec_path = str(bin_path.joinpath(exec_filename)) |
| 171 | + download_pglet(pglet_version, asset, exec_path) |
| 172 | + |
| 173 | + # update RECORD |
| 174 | + h, l = rehash(exec_path) |
| 175 | + record_lines.insert(len(record_lines) - 3, f"pglet/bin/{exec_filename},{h},{l}\n") |
| 176 | + # for line in record_lines: |
| 177 | + # print(line.strip()) |
| 178 | + |
| 179 | + # update WHEEL file |
| 180 | + for tag in package["wheel_tags"]: |
| 181 | + wheel_lines.append(f"Tag: {tag}\n") |
| 182 | + |
| 183 | + # save WHEEL |
| 184 | + with open(wheel_path, "w") as f: |
| 185 | + f.writelines(wheel_lines) |
| 186 | + |
| 187 | + # update RECORD |
| 188 | + h, l = rehash(wheel_path) |
| 189 | + record_lines.insert( |
| 190 | + len(record_lines) - 3, |
| 191 | + f"pglet-{package_version}.dist-info/WHEEL,{h},{l}\n", |
| 192 | + ) |
| 193 | + |
| 194 | + # save RECORD |
| 195 | + with open(record_path, "w") as f: |
| 196 | + f.writelines(record_lines) |
| 197 | + |
| 198 | + # zip |
| 199 | + suffix = package["file_suffix"] |
| 200 | + zip_filename = current_dir.joinpath("dist", f"pglet-{package_version}-{suffix}") |
| 201 | + shutil.make_archive(zip_filename, "zip", unpacked_whl) |
| 202 | + os.rename(f"{zip_filename}.zip", f"{zip_filename}.whl") |
| 203 | + |
| 204 | + # cleanup |
| 205 | + shutil.rmtree(str(unpacked_whl)) |
0 commit comments