install_extras.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. client = httpx.Client()
  13. r = client.get('https://api.github.com/repos/dandavison/delta/releases/latest',
  14. headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'})
  15. r.raise_for_status()
  16. latest = r.json()
  17. if platform.system() == 'Linux':
  18. version = latest['name']
  19. arch = get_output(['dpkg', '--print-architecture'])
  20. filename = f'git-delta_{version}_{arch}.deb'
  21. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  22. deb_path = CURRENT_DIR / 'git-delta.deb'
  23. download(client, asset['browser_download_url'], deb_path)
  24. try:
  25. subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
  26. finally:
  27. os.unlink(deb_path)
  28. def get_output(argv: list[str]) -> str:
  29. proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii')
  30. return proc.stdout.rstrip('\n')
  31. def download(client: httpx.Client, url: str, path: pathlib.Path) -> None:
  32. print('downloading', url, 'to', path)
  33. with client.stream('GET', url, follow_redirects=True) as r:
  34. r.raise_for_status()
  35. with path.open('wb') as f:
  36. for chunk in r.iter_bytes():
  37. f.write(chunk)
  38. if __name__ == '__main__':
  39. main()