| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import argparse |
| 3 |
import base64 |
| 4 |
import gzip |
| 5 |
import hashlib |
| 6 |
import lzma |
| 7 |
import os |
| 8 |
import shutil |
| 9 |
import subprocess |
| 10 |
import tarfile |
| 11 |
import zipfile |
| 12 |
from typing import AnyStr, Dict, Iterable, Optional |
| 13 |
|
| 14 |
from cockpit import __version__ |
| 15 |
|
| 16 |
VERSION = __version__ or '0' |
| 17 |
PACKAGE = f'cockpit-{VERSION}' |
| 18 |
TAG = 'py3-none-any' |
| 19 |
|
| 20 |
|
| 21 |
def find_sources(*, srcpkg: bool) -> Iterable[str]: |
| 22 |
try: |
| 23 |
subprocess.check_call(['vendor/checkout'], stdout=2) # Needed for git builds... |
| 24 |
except FileNotFoundError: # ...but not present in tarball... |
| 25 |
pass # ...and not needed either, because... |
| 26 |
assert os.path.exists('src/cockpit/_vendor/ferny/__init__.py') # ...the code should exist there already. |
| 27 |
|
| 28 |
if srcpkg: |
| 29 |
yield from ( |
| 30 |
'pyproject.toml', |
| 31 |
'src/build_backend.py', |
| 32 |
) |
| 33 |
|
| 34 |
for path, _dirs, files in os.walk('src', followlinks=True): |
| 35 |
if '__init__.py' in files: |
| 36 |
yield from [os.path.join(path, file) for file in files] |
| 37 |
|
| 38 |
|
| 39 |
def copy_sources(distdir: str) -> None: |
| 40 |
for source in find_sources(srcpkg=True): |
| 41 |
destination = os.path.join(distdir, source) |
| 42 |
os.makedirs(os.path.dirname(destination), exist_ok=True) |
| 43 |
shutil.copy(source, destination) |
| 44 |
|
| 45 |
|
| 46 |
def build_sdist(sdist_directory: str, |
| 47 |
config_settings: Optional[Dict[str, object]] = None) -> str: |
| 48 |
del config_settings |
| 49 |
sdist_filename = f'{PACKAGE}.tar.gz' |
| 50 |
# We do this manually to avoid adding timestamps. See https://bugs.python.org/issue31526 |
| 51 |
with gzip.GzipFile(f'{sdist_directory}/{sdist_filename}', mode='w', mtime=0) as gz: |
| 52 |
# https://github.com/python/typeshed/issues/5491 |
| 53 |
with tarfile.open(fileobj=gz, mode='w|', dereference=True) as sdist: # type: ignore[arg-type,call-overload] |
| 54 |
for filename in sorted(find_sources(srcpkg=True)): |
| 55 |
sdist.add(filename, arcname=f'{PACKAGE}/{filename}', ) |
| 56 |
return sdist_filename |
| 57 |
|
| 58 |
|
| 59 |
def build_wheel(wheel_directory: str, |
| 60 |
config_settings: Optional[Dict[str, object]] = None, |
| 61 |
metadata_directory: Optional[str] = None) -> str: |
| 62 |
del config_settings, metadata_directory |
| 63 |
wheel_filename = f'{PACKAGE}-{TAG}.whl' |
| 64 |
distinfo = { |
| 65 |
'WHEEL': [ |
| 66 |
'Wheel-Version: 1.0', |
| 67 |
'Generator: cockpit build_backend', |
| 68 |
'Root-Is-Purelib: true', |
| 69 |
f'Tag: {TAG}', |
| 70 |
], |
| 71 |
'METADATA': [ |
| 72 |
'Metadata-Version: 2.1', |
| 73 |
'Name: cockpit', |
| 74 |
f'Version: {VERSION}', |
| 75 |
], |
| 76 |
'entry_points.txt': [ |
| 77 |
'[console_scripts]', |
| 78 |
'cockpit-askpass = cockpit._vendor.ferny.interaction_client:main', |
| 79 |
'cockpit-bridge = cockpit.bridge:main', |
| 80 |
], |
| 81 |
} |
| 82 |
|
| 83 |
with zipfile.ZipFile(f'{wheel_directory}/{wheel_filename}', 'w') as wheel: |
| 84 |
def write(filename: str, data: AnyStr) -> None: |
| 85 |
# we do this manually to avoid adding timestamps |
| 86 |
wheel.writestr(zipfile.ZipInfo(filename), data) |
| 87 |
|
| 88 |
def beipack_self(main: str, args: str = '') -> bytes: |
| 89 |
from cockpit._vendor.bei import beipack |
| 90 |
contents = {name: wheel.read(name) for name in wheel.namelist()} |
| 91 |
pack = beipack.pack(contents, main, args=args).encode() |
| 92 |
return lzma.compress(pack, preset=lzma.PRESET_EXTREME) |
| 93 |
|
| 94 |
def write_distinfo(filename: str, lines: Iterable[str]) -> None: |
| 95 |
write(f'{PACKAGE}.dist-info/{filename}', ''.join(f'{line}\n' for line in lines)) |
| 96 |
|
| 97 |
def record_lines() -> Iterable[str]: |
| 98 |
for info in wheel.infolist(): |
| 99 |
digest = hashlib.sha256(wheel.read(info.filename)).digest() |
| 100 |
b64_digest = base64.urlsafe_b64encode(digest).rstrip(b'=').decode('ascii') |
| 101 |
yield f'{info.filename},sha256={b64_digest},{info.file_size}' |
| 102 |
yield f'{PACKAGE}.dist-info/RECORD,,' |
| 103 |
|
| 104 |
for filename in sorted(find_sources(srcpkg=False)): |
| 105 |
with open(filename, 'rb') as file: |
| 106 |
write(os.path.relpath(filename, start='src'), file.read()) |
| 107 |
|
| 108 |
write('cockpit/data/cockpit-bridge.beipack.xz', beipack_self('cockpit.bridge:main', 'beipack=True')) |
| 109 |
|
| 110 |
for filename, lines in distinfo.items(): |
| 111 |
write_distinfo(filename, lines) |
| 112 |
write_distinfo('RECORD', record_lines()) |
| 113 |
|
| 114 |
return wheel_filename |
| 115 |
|
| 116 |
|
| 117 |
def main() -> None: |
| 118 |
parser = argparse.ArgumentParser() |
| 119 |
group = parser.add_mutually_exclusive_group(required=True) |
| 120 |
group.add_argument('--copy', action='store_true') |
| 121 |
group.add_argument('--sdist', action='store_true') |
| 122 |
group.add_argument('--wheel', action='store_true') |
| 123 |
parser.add_argument('srcdir') |
| 124 |
parser.add_argument('destdir') |
| 125 |
args = parser.parse_args() |
| 126 |
|
| 127 |
# We have to chdir() for PEP 517, so make sure dest is absolute |
| 128 |
destdir = os.path.abspath(args.destdir) |
| 129 |
os.chdir(args.srcdir) |
| 130 |
|
| 131 |
os.makedirs(destdir, exist_ok=True) |
| 132 |
|
| 133 |
if args.copy: |
| 134 |
copy_sources(destdir) |
| 135 |
elif args.sdist: |
| 136 |
print(os.path.join(destdir, build_sdist(destdir))) |
| 137 |
else: |
| 138 |
print(os.path.join(destdir, build_wheel(destdir))) |
| 139 |
|
| 140 |
|
| 141 |
if __name__ == '__main__': |
| 142 |
main() |