| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #!/usr/bin/env python3
- from __future__ import annotations
- import os
- import pathlib
- import platform
- import subprocess
- import tarfile
- import httpx
- CURRENT_DIR = pathlib.Path(__file__).parent
- def main():
- delta()
- eza()
- git_whence()
- 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('git-delta package already installed')
- return
- client = httpx.Client()
- latest = gh_latest_version(client, 'dandavision', '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 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('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'
- download(client, url, CURRENT_DIR / 'eza.tar.gz')
- with tarfile.open(CURRENT_DIR / 'eza.tar.gz', 'r:gz') as tar:
- tar.extract('./eza', CURRENT_DIR)
- os.unlink(CURRENT_DIR / 'eza.tar.gz')
- 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('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 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)
- if __name__ == '__main__':
- main()
|