install_extras.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. from __future__ import annotations
  3. import os
  4. import pathlib
  5. import platform
  6. import subprocess
  7. import tarfile
  8. import httpx
  9. CURRENT_DIR = pathlib.Path(__file__).parent
  10. def main():
  11. delta()
  12. eza()
  13. git_whence()
  14. def delta():
  15. if platform.system() == 'Darwin':
  16. print('$ brew install git-delta')
  17. subprocess.run(['brew', 'install', 'git-delta'], check=True)
  18. return
  19. else:
  20. assert platform.system() == 'Linux'
  21. if subprocess.run(['dpkg', '-l', 'git-delta'], stdout=subprocess.DEVNULL).returncode == 0:
  22. print('git-delta package already installed')
  23. return
  24. client = httpx.Client()
  25. latest = gh_latest_version(client, 'dandavision', 'delta')
  26. version = latest['name']
  27. arch = get_output(['dpkg', '--print-architecture'])
  28. filename = f'git-delta_{version}_{arch}.deb'
  29. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  30. deb_path = CURRENT_DIR / 'git-delta.deb'
  31. download(client, asset['browser_download_url'], deb_path)
  32. try:
  33. subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
  34. finally:
  35. os.unlink(deb_path)
  36. def eza():
  37. if platform.system() == 'Darwin':
  38. print('$ brew install eza')
  39. subprocess.run(['brew', 'install', 'eza'], check=True)
  40. return
  41. else:
  42. assert platform.system() == 'Linux'
  43. if (CURRENT_DIR / 'eza').exists():
  44. print('eza already downloaded')
  45. return
  46. client = httpx.Client()
  47. url = f'https://github.com/eza-community/eza/releases/latest/download/eza_{platform.machine()}-unknown-linux-gnu.tar.gz'
  48. download(client, url, CURRENT_DIR / 'eza.tar.gz')
  49. with tarfile.open(CURRENT_DIR / 'eza.tar.gz', 'r:gz') as tar:
  50. tar.extract('./eza', CURRENT_DIR)
  51. os.unlink(CURRENT_DIR / 'eza.tar.gz')
  52. def git_whence():
  53. if platform.system() == 'Darwin':
  54. print('$ brew install raylu/formulae/git-whence')
  55. subprocess.run(['brew', 'install', 'raylu/formulae/git-whence'], check=True)
  56. return
  57. else:
  58. assert platform.system() == 'Linux'
  59. if (CURRENT_DIR / 'git-whence').exists():
  60. print('git-whence already downloaded')
  61. return
  62. client = httpx.Client()
  63. url = f'https://github.com/raylu/git-whence/releases/latest/download/git-whence-{platform.machine()}-unknown-linux-gnu'
  64. download(client, url, CURRENT_DIR / 'git-whence')
  65. os.chmod(CURRENT_DIR / 'git-whence', 0o755)
  66. def get_output(argv: list[str]) -> str:
  67. proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii')
  68. return proc.stdout.rstrip('\n')
  69. def gh_latest_version(client: httpx.Client, org: str, repo: str) -> dict:
  70. r = client.get(f'https://api.github.com/repos/{org}/{repo}/releases/latest',
  71. headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'})
  72. r.raise_for_status()
  73. return r.json()
  74. def download(client: httpx.Client, url: str, path: pathlib.Path) -> None:
  75. print('downloading', url, 'to', path)
  76. with client.stream('GET', url, follow_redirects=True) as r:
  77. r.raise_for_status()
  78. with path.open('wb') as f:
  79. for chunk in r.iter_bytes():
  80. f.write(chunk)
  81. if __name__ == '__main__':
  82. main()