| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- #!/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()
|