#!/usr/bin/env python3 # /// script # dependencies = [ # "httpx", # ] # /// from __future__ import annotations import os import pathlib import platform import shutil import subprocess import tarfile import zipfile import httpx CURRENT_DIR = pathlib.Path(__file__).parent def main(): bat() choose() curlie() delta() dua() dust() eza() fx() git_whence() jjui() jujutsu() lazygit() oh_my_posh() procs() #starship() uv() yazi() def bat(): if platform.system() == 'Darwin': print('$ brew install git-delta') subprocess.run(['brew', 'install', 'bat'], check=True) return else: assert platform.system() == 'Linux' if subprocess.run(['dpkg', '-l', 'bat'], stdout=subprocess.DEVNULL).returncode == 0: print_gray('bat package already installed') return subprocess.run(['sudo', 'apt', 'install', 'bat', '--yes'], check=True) def choose(): if platform.system() == 'Darwin': print('$ brew install choose-rust') subprocess.run(['brew', 'install', 'choose-rust'], check=True) return else: assert platform.system() == 'Linux' if (CURRENT_DIR / 'choose').exists(): print_gray('choose already downloaded') return client = httpx.Client() arch = platform.machine() if arch == 'arm64': arch = 'aarch64' url = f'https://github.com/theryangeary/choose/releases/latest/download/choose-{arch}-unknown-linux-gnu' download(client, url, CURRENT_DIR / 'choose') os.chmod(CURRENT_DIR / 'choose', 0o755) def curlie(): if platform.system() == 'Darwin': print('$ brew install curlie') subprocess.run(['brew', 'install', 'curlie'], check=True) return if subprocess.run(['dpkg', '-l', 'curlie'], stdout=subprocess.DEVNULL).returncode == 0: print_gray('curlie package already installed') return client = httpx.Client() latest = gh_latest_version(client, 'rs', 'curlie') version = latest['tag_name'].removeprefix('v') system = platform.system().lower() arch = get_output(['dpkg', '--print-architecture']) filename = f'curlie_{version}_{system}_{arch}.deb' (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename) deb_path = CURRENT_DIR / 'curlie.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 delta(): if platform.system() == 'Darwin': print('$ brew install git-delta') subprocess.run(['brew', 'install', 'git-delta'], check=True) return else: assert platform.system() == 'Linux' if subprocess.run(['dpkg', '-l', 'git-delta'], stdout=subprocess.DEVNULL).returncode == 0: print_gray('git-delta package already installed') return client = httpx.Client() latest = gh_latest_version(client, 'dandavison', 'delta') 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 dua(): if platform.system() == 'Darwin': print('$ brew install dua-cli') subprocess.run(['brew', 'install', 'dua-cli'], check=True) return else: assert platform.system() == 'Linux' dua_path = CURRENT_DIR / 'dua' if dua_path.exists(): print_gray('dua already downloaded') return client = httpx.Client() latest = gh_latest_version(client, 'Byron', 'dua-cli') version = latest['name'] dirname = f'dua-{version}-{platform.machine()}-unknown-linux-musl' filename = dirname + '.tar.gz' (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename) tarball_path = CURRENT_DIR / 'dua.tar.gz' download(client, asset['browser_download_url'], tarball_path) with tarfile.open(tarball_path) as tar: with tar.extractfile(dirname + '/dua') as binary, dua_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager] shutil.copyfileobj(binary, f) os.unlink(tarball_path) dua_path.chmod(0o755) def dust(): if platform.system() == 'Darwin': print('$ brew install dust') subprocess.run(['brew', 'install', 'dust'], check=True) return else: assert platform.system() == 'Linux' dust_path = CURRENT_DIR / 'dust' if dust_path.exists(): print_gray('dust already downloaded') return client = httpx.Client() latest = gh_latest_version(client, 'bootandy', 'dust') version = latest['name'] dirname = f'dust-{version}-{platform.machine()}-unknown-linux-musl' filename = dirname + '.tar.gz' (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename) tarball_path = CURRENT_DIR / 'dust.tar.gz' download(client, asset['browser_download_url'], tarball_path) with tarfile.open(tarball_path) as tar: with tar.extractfile(dirname + '/dust') as binary, dust_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager] shutil.copyfileobj(binary, f) os.unlink(tarball_path) dust_path.chmod(0o755) def eza(): if platform.system() == 'Darwin': print('$ brew install eza') subprocess.run(['brew', 'install', 'eza'], check=True) return else: assert platform.system() == 'Linux' if (CURRENT_DIR / 'eza').exists(): print_gray('eza already downloaded') return client = httpx.Client() url = f'https://github.com/eza-community/eza/releases/latest/download/eza_{platform.machine()}-unknown-linux-gnu.tar.gz' tarball_path = CURRENT_DIR / 'eza.tar.gz' download(client, url, tarball_path) with tarfile.open(tarball_path) as tar: tar.extract('./eza', CURRENT_DIR) tarball_path.unlink() def fx(): if platform.system() == 'Darwin': print('$ brew install fx') subprocess.run(['brew', 'install', 'fx'], check=True) return else: assert platform.system() == 'Linux' if (CURRENT_DIR / 'fx').exists(): print_gray('fx already downloaded') return client = httpx.Client() arch = platform.machine() if arch == 'aarch64': arch = 'arm64' elif arch == 'x86_64': arch = 'amd64' url = f'https://github.com/antonmedv/fx/releases/latest/download/fx_linux_{arch}' download(client, url, CURRENT_DIR / 'fx') os.chmod(CURRENT_DIR / 'fx', 0o755) def git_whence(): if platform.system() == 'Darwin': print('$ brew install raylu/formulae/git-whence') subprocess.run(['brew', 'install', 'raylu/formulae/git-whence'], check=True) return else: assert platform.system() == 'Linux' if (CURRENT_DIR / 'git-whence').exists(): print_gray('git-whence already downloaded') return client = httpx.Client() url = f'https://github.com/raylu/git-whence/releases/latest/download/git-whence-{platform.machine()}-unknown-linux-gnu' download(client, url, CURRENT_DIR / 'git-whence') os.chmod(CURRENT_DIR / 'git-whence', 0o755) def jjui(): if (CURRENT_DIR / 'jjui').exists(): print_gray('jjui already downloaded') return if platform.system() == 'Darwin': print('$ brew install jjui') subprocess.run(['brew', 'install', 'jjui'], check=True) return else: assert platform.system() == 'Linux' client = httpx.Client() latest = gh_latest_version(client, 'idursun', 'jjui') version = latest['name'] system = platform.system().lower() arch = platform.machine() name = f'jjui-{version}-{arch}-{system}' url = f'https://github.com/idursun/jjui/releases/download/{version}/{name}.zip' zip_path = CURRENT_DIR / 'jjui.zip' download(client, url, zip_path) with zipfile.ZipFile(zip_path, 'r') as zipf: extracted = (CURRENT_DIR / 'jjui') with zipf.open(name, 'r') as binary, extracted.open('wb') as f: shutil.copyfileobj(binary, f) extracted.chmod(0o755) zip_path.unlink() def jujutsu(): if (CURRENT_DIR / 'jj').exists(): print_gray('jj already downloaded') return if platform.system() == 'Darwin': print('$ brew install jj') subprocess.run(['brew', 'install', 'jj'], check=True) return else: assert platform.system() == 'Linux' client = httpx.Client() latest = gh_latest_version(client, 'jj-vcs', 'jj') version = latest['name'] arch = platform.machine() url = f'https://github.com/jj-vcs/jj/releases/download/{version}/jj-{version}-{arch}-unknown-linux-musl.tar.gz' tarball_path = CURRENT_DIR / 'jj.tar.gz' download(client, url, tarball_path) with tarfile.open(tarball_path, 'r:gz') as tar: tar.extract('./jj', CURRENT_DIR, filter='data') tarball_path.unlink() def lazygit(): if (CURRENT_DIR / 'lazygit').exists(): print_gray('lazygit package already installed') return client = httpx.Client() latest = gh_latest_version(client, 'jesseduffield', 'lazygit') version = latest['name'] arch = platform.machine() if arch == 'aarch64': arch = 'arm64' url = f'https://github.com/jesseduffield/lazygit/releases/download/{version}/lazygit_{version.lstrip("v")}_{platform.system()}_{arch}.tar.gz' download(client, url, CURRENT_DIR / 'lazygit.tgz') with tarfile.open(CURRENT_DIR / 'lazygit.tgz', 'r:gz') as tar: tar.extract('lazygit', CURRENT_DIR) os.unlink(CURRENT_DIR / 'lazygit.tgz') def oh_my_posh(): if platform.system() == 'Darwin': print('$ brew install oh-my-posh') subprocess.run(['brew', 'install', 'oh-my-posh'], check=True) return else: assert platform.system() == 'Linux' if (CURRENT_DIR / 'oh-my-posh').exists(): print_gray('oh-my-posh already downloaded') return client = httpx.Client() arch = platform.machine() if arch == 'aarch64': arch = 'arm64' elif arch == 'x86_64': arch = 'amd64' url = f'https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-{arch}' download(client, url, CURRENT_DIR / 'oh-my-posh') os.chmod(CURRENT_DIR / 'oh-my-posh', 0o755) def procs(): if (CURRENT_DIR / 'procs').exists(): print_gray('procs already downloaded') return client = httpx.Client() latest = gh_latest_version(client, 'dalance', 'procs') version = latest['name'] system = platform.system().lower() arch = platform.machine() if arch == 'arm64': arch = 'aarch64' if system == 'darwin': system = 'mac' name = f'procs-{version}-{arch}-{system}.zip' url = f'https://github.com/dalance/procs/releases/download/{version}/{name}' zip_path = CURRENT_DIR / 'procs.zip' download(client, url, zip_path) with zipfile.ZipFile(zip_path, 'r') as zipf: extracted = (CURRENT_DIR / 'procs') with zipf.open('procs', 'r') as binary, extracted.open('wb') as f: shutil.copyfileobj(binary, f) extracted.chmod(0o755) zip_path.unlink() def starship(): if platform.system() == 'Darwin': print('$ brew install starship') subprocess.run(['brew', 'install', 'starship'], check=True) return else: assert platform.system() == 'Linux' if (CURRENT_DIR / 'starship').exists(): print_gray('starship already downloaded') return client = httpx.Client() arch = platform.machine() if arch == 'x86_64': libc = 'gnu' else: libc = 'musl' url = f'https://github.com/starship/starship/releases/latest/download/starship-{arch}-unknown-linux-{libc}.tar.gz' tarball_path = CURRENT_DIR / 'starship.tar.gz' download(client, url, tarball_path) with tarfile.open(tarball_path, 'r:gz') as tar: tar.extract('starship', CURRENT_DIR) tarball_path.unlink() def uv(): if (CURRENT_DIR / 'uv').exists(): print_gray('uv already downloaded') return if which := shutil.which('uv'): print_gray('uv already at ' + which) return client = httpx.Client() arch = platform.machine() if arch == 'arm64': arch = 'aarch64' os = platform.system().lower() if os == 'darwin': prefix = f'uv-{arch}-apple-darwin' else: prefix = f'uv-{arch}-unknown-linux-gnu' url = f'https://github.com/astral-sh/uv/releases/latest/download/{prefix}.tar.gz' tarball_path = CURRENT_DIR / 'uv.tar.gz' download(client, url, tarball_path) with tarfile.open(tarball_path, 'r:gz') as tar: for filename in ['uv', 'uvx']: target_path = CURRENT_DIR / filename with tar.extractfile(f'{prefix}/{filename}') as binary, target_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager] shutil.copyfileobj(binary, f) target_path.chmod(0o755) tarball_path.unlink() def yazi(): if platform.system() == 'Darwin': print('$ brew install yazi') subprocess.run(['brew', 'install', 'yazi'], check=True) return else: assert platform.system() == 'Linux' if (CURRENT_DIR / 'yazi').exists(): print_gray('yazi already downloaded') return client = httpx.Client() name = f'yazi-{platform.machine()}-unknown-linux-musl' url = f'https://github.com/sxyazi/yazi/releases/latest/download/{name}.zip' zip_path = CURRENT_DIR / 'yazi.zip' download(client, url, zip_path) with zipfile.ZipFile(zip_path, 'r') as zipf: for filename in ['ya', 'yazi']: extracted = (CURRENT_DIR / filename) with zipf.open(f'{name}/{filename}', 'r') as binary, extracted.open('wb') as f: shutil.copyfileobj(binary, f) extracted.chmod(0o755) zip_path.unlink() def get_output(argv: list[str]) -> str: proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii') return proc.stdout.rstrip('\n') def gh_latest_version(client: httpx.Client, org: str, repo: str) -> dict: r = client.get(f'https://api.github.com/repos/{org}/{repo}/releases/latest', headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'}) r.raise_for_status() return r.json() 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) def print_gray(*args: object): print('\x1B[90;m', end='') print(*args, end='') print('\x1B[0m') if __name__ == '__main__': main()