install_extras.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. from __future__ import annotations
  3. import os
  4. import pathlib
  5. import platform
  6. import subprocess
  7. import httpx
  8. CURRENT_DIR = pathlib.Path(__file__).parent
  9. def main():
  10. delta()
  11. def delta():
  12. if platform.system() == 'Darwin':
  13. print('$ brew install git-delta')
  14. subprocess.run(['brew', 'install', 'git-delta'], check=True)
  15. return
  16. client = httpx.Client()
  17. r = client.get('https://api.github.com/repos/dandavison/delta/releases/latest',
  18. headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'})
  19. r.raise_for_status()
  20. latest = r.json()
  21. if platform.system() == 'Linux':
  22. version = latest['name']
  23. arch = get_output(['dpkg', '--print-architecture'])
  24. filename = f'git-delta_{version}_{arch}.deb'
  25. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  26. deb_path = CURRENT_DIR / 'git-delta.deb'
  27. download(client, asset['browser_download_url'], deb_path)
  28. try:
  29. subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
  30. finally:
  31. os.unlink(deb_path)
  32. def get_output(argv: list[str]) -> str:
  33. proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii')
  34. return proc.stdout.rstrip('\n')
  35. def download(client: httpx.Client, url: str, path: pathlib.Path) -> None:
  36. print('downloading', url, 'to', path)
  37. with client.stream('GET', url, follow_redirects=True) as r:
  38. r.raise_for_status()
  39. with path.open('wb') as f:
  40. for chunk in r.iter_bytes():
  41. f.write(chunk)
  42. if __name__ == '__main__':
  43. main()