95 lines
4.1 KiB
Python
95 lines
4.1 KiB
Python
"""Assemble installable archives from self-contained dotnet publish output."""
|
|
|
|
import hashlib
|
|
import io
|
|
import os
|
|
from pathlib import Path
|
|
import plistlib
|
|
import struct
|
|
import sys
|
|
import tarfile
|
|
|
|
DEPLOY = Path(__file__).resolve().parent
|
|
ROOT = DEPLOY.parent.parent
|
|
VERSION = (ROOT / 'VERSION').read_text(encoding='ascii').strip()
|
|
PAYLOAD = Path(os.environ.get('LANUPS_INSTALLER_PAYLOAD', ROOT / 'artifacts' / 'installer-payload-optimized')).resolve()
|
|
DIST = ROOT / 'dist'
|
|
ICON = DEPLOY.parent / 'src' / 'LanUps.Desktop' / 'Assets' / 'ups.png'
|
|
|
|
|
|
def binaries(rid, kind):
|
|
directory = PAYLOAD / rid / kind
|
|
executable = directory / f"LanUps.{kind}{'.exe' if rid.startswith('win-') else ''}"
|
|
if not executable.is_file():
|
|
raise FileNotFoundError(executable)
|
|
for path in sorted(directory.rglob('*')):
|
|
if path.is_file() and path.suffix.lower() not in {'.pdb', '.dbg'} and not any(part.endswith('.dSYM') for part in path.parts):
|
|
yield path, path.relative_to(directory).as_posix(), path == executable
|
|
|
|
|
|
def tar_bytes(archive, name, data, mode):
|
|
item = tarfile.TarInfo(name)
|
|
item.size = len(data)
|
|
item.mode = mode
|
|
archive.addfile(item, io.BytesIO(data))
|
|
|
|
|
|
def unix_package(rid):
|
|
mac = rid.startswith('osx-')
|
|
path = DIST / f'LanUps-{rid}.tar.gz'
|
|
with tarfile.open(path, 'w:gz', compresslevel=5) as archive:
|
|
tar_bytes(archive, 'install.sh', (DEPLOY / 'unix' / 'install.sh').read_bytes().replace(b'\r\n', b'\n'), 0o755)
|
|
tar_bytes(archive, 'architecture', rid.split('-', 1)[1].encode() + b'\n', 0o644)
|
|
folder = DEPLOY / ('macos' if mac else 'linux')
|
|
for sidecar in sorted(folder.iterdir()):
|
|
if sidecar.suffix == '.spec':
|
|
continue
|
|
tar_bytes(archive, sidecar.name, sidecar.read_bytes(), 0o644)
|
|
for kind in ('Agent', 'Desktop'):
|
|
prefix = 'LanUps.app/Contents/MacOS' if mac and kind == 'Desktop' else kind.lower()
|
|
for source, filename, executable in binaries(rid, kind):
|
|
item = tarfile.TarInfo(f'{prefix}/{filename}')
|
|
item.size = source.stat().st_size
|
|
item.mode = 0o755 if executable else 0o644
|
|
with source.open('rb') as stream:
|
|
archive.addfile(item, stream)
|
|
if mac:
|
|
info = dict(CFBundleExecutable='LanUps.Desktop', CFBundleIdentifier='local.lanups.desktop',
|
|
CFBundleName='LanUps', CFBundleDisplayName='局域网 UPS', CFBundlePackageType='APPL',
|
|
CFBundleIconFile='ups.icns', CFBundleVersion=VERSION, CFBundleShortVersionString=VERSION)
|
|
tar_bytes(archive, 'LanUps.app/Contents/Info.plist', plistlib.dumps(info), 0o644)
|
|
png = ICON.read_bytes()
|
|
image = b'icns' + struct.pack('>I', len(png) + 16) + b'ic08' + struct.pack('>I', len(png) + 8) + png
|
|
tar_bytes(archive, 'LanUps.app/Contents/Resources/ups.icns', image, 0o644)
|
|
else:
|
|
tar_bytes(archive, 'ups.png', ICON.read_bytes(), 0o644)
|
|
return path
|
|
|
|
|
|
def write_checksums():
|
|
extensions = ('.exe', '.deb', '.rpm', '.pkg', '.ipk', '.tar.gz')
|
|
with (DIST / 'SHA256SUMS').open('w', encoding='ascii', newline='\n') as sums:
|
|
for path in sorted(DIST.iterdir()):
|
|
if path.is_file() and path.name.endswith(extensions):
|
|
with path.open('rb') as stream:
|
|
sums.write(f"{hashlib.file_digest(stream, 'sha256').hexdigest()} {path.name}\n")
|
|
|
|
|
|
def main(targets):
|
|
if any(rid.startswith('win-') for rid in targets):
|
|
raise ValueError('Use package.ps1 to build the Windows Inno Setup installer.')
|
|
DIST.mkdir(parents=True, exist_ok=True)
|
|
router = DIST / 'LanUps-openwrt.tar.gz'
|
|
with tarfile.open(router, 'w:gz') as archive:
|
|
for source in sorted((DEPLOY / 'router').iterdir()):
|
|
tar_bytes(archive, source.name, source.read_bytes().replace(b'\r\n', b'\n'), 0o755)
|
|
print(router.name, flush=True)
|
|
for rid in targets:
|
|
path = unix_package(rid)
|
|
print(path.name, flush=True)
|
|
write_checksums()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main(sys.argv[1:])
|