#!/usr/bin/env python3 from __future__ import annotations import os import pathlib import platform import subprocess import httpx CURRENT_DIR = pathlib.Path(__file__).parent def main(): delta() def delta(): if platform.system() == 'Darwin': print('$ brew install git-delta') subprocess.run(['brew', 'install', 'git-delta'], check=True) return client = httpx.Client() r = client.get('https://api.github.com/repos/dandavison/delta/releases/latest', headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'}) r.raise_for_status() latest = r.json() if platform.system() == 'Linux': version = latest['name'] arch = get_output(['dpkg', '--print-architecture']) filename = f'git-delta_{version}_{arch}.deb' (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename) deb_path = CURRENT_DIR / 'git-delta.deb' download(client, asset['browser_download_url'], deb_path) try: subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True) finally: os.unlink(deb_path) def get_output(argv: list[str]) -> str: proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii') return proc.stdout.rstrip('\n') def download(client: httpx.Client, url: str, path: pathlib.Path) -> None: print('downloading', url, 'to', path) with client.stream('GET', url, follow_redirects=True) as r: r.raise_for_status() with path.open('wb') as f: for chunk in r.iter_bytes(): f.write(chunk) if __name__ == '__main__': main()