install_extras.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env python3
  2. from __future__ import annotations
  3. import os
  4. import pathlib
  5. import platform
  6. import shutil
  7. import subprocess
  8. import tarfile
  9. import httpx
  10. CURRENT_DIR = pathlib.Path(__file__).parent
  11. def main():
  12. delta()
  13. dua()
  14. eza()
  15. git_whence()
  16. starship()
  17. def delta():
  18. if platform.system() == 'Darwin':
  19. print('$ brew install git-delta')
  20. subprocess.run(['brew', 'install', 'git-delta'], check=True)
  21. return
  22. else:
  23. assert platform.system() == 'Linux'
  24. if subprocess.run(['dpkg', '-l', 'git-delta'], stdout=subprocess.DEVNULL).returncode == 0:
  25. print('git-delta package already installed')
  26. return
  27. client = httpx.Client()
  28. latest = gh_latest_version(client, 'dandavison', 'delta')
  29. version = latest['name']
  30. arch = get_output(['dpkg', '--print-architecture'])
  31. filename = f'git-delta_{version}_{arch}.deb'
  32. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  33. deb_path = CURRENT_DIR / 'git-delta.deb'
  34. download(client, asset['browser_download_url'], deb_path)
  35. try:
  36. subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
  37. finally:
  38. os.unlink(deb_path)
  39. def dua():
  40. if platform.system() == 'Darwin':
  41. print('$ brew install dua-cli')
  42. subprocess.run(['brew', 'install', 'dua-cli'], check=True)
  43. return
  44. else:
  45. assert platform.system() == 'Linux'
  46. dua_path = CURRENT_DIR / 'dua'
  47. if dua_path.exists():
  48. print('dua already downloaded')
  49. return
  50. client = httpx.Client()
  51. latest = gh_latest_version(client, 'Byron', 'dua-cli')
  52. version = latest['name']
  53. dirname = f'dua-{version}-{platform.machine()}-unknown-linux-musl'
  54. filename = dirname + '.tar.gz'
  55. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  56. tarball_path = CURRENT_DIR / 'dua.tar.gz'
  57. download(client, asset['browser_download_url'], tarball_path)
  58. with tarfile.open(tarball_path) as tar:
  59. with tar.extractfile(dirname + '/dua') as binary, dua_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager]
  60. shutil.copyfileobj(binary, f)
  61. os.unlink(tarball_path)
  62. dua_path.chmod(0o755)
  63. def eza():
  64. if platform.system() == 'Darwin':
  65. print('$ brew install eza')
  66. subprocess.run(['brew', 'install', 'eza'], check=True)
  67. return
  68. else:
  69. assert platform.system() == 'Linux'
  70. if (CURRENT_DIR / 'eza').exists():
  71. print('eza already downloaded')
  72. return
  73. client = httpx.Client()
  74. url = f'https://github.com/eza-community/eza/releases/latest/download/eza_{platform.machine()}-unknown-linux-gnu.tar.gz'
  75. tarball_path = CURRENT_DIR / 'eza.tar.gz'
  76. download(client, url, tarball_path)
  77. with tarfile.open(tarball_path) as tar:
  78. tar.extract('./eza', CURRENT_DIR)
  79. tarball_path.unlink()
  80. def git_whence():
  81. if platform.system() == 'Darwin':
  82. print('$ brew install raylu/formulae/git-whence')
  83. subprocess.run(['brew', 'install', 'raylu/formulae/git-whence'], check=True)
  84. return
  85. else:
  86. assert platform.system() == 'Linux'
  87. if (CURRENT_DIR / 'git-whence').exists():
  88. print('git-whence already downloaded')
  89. return
  90. client = httpx.Client()
  91. url = f'https://github.com/raylu/git-whence/releases/latest/download/git-whence-{platform.machine()}-unknown-linux-gnu'
  92. download(client, url, CURRENT_DIR / 'git-whence')
  93. os.chmod(CURRENT_DIR / 'git-whence', 0o755)
  94. def starship():
  95. if platform.system() == 'Darwin':
  96. print('$ brew install starship')
  97. subprocess.run(['brew', 'install', 'starship'], check=True)
  98. return
  99. else:
  100. assert platform.system() == 'Linux'
  101. if (CURRENT_DIR / 'starship').exists():
  102. print('starship already downloaded')
  103. return
  104. client = httpx.Client()
  105. url = f'https://github.com/starship/starship/releases/latest/download/starship-{platform.machine()}-unknown-linux-gnu.tar.gz'
  106. tarball_path = CURRENT_DIR / 'starship.tar.gz'
  107. download(client, url, tarball_path)
  108. with tarfile.open(tarball_path, 'r:gz') as tar:
  109. tar.extract('starship', CURRENT_DIR)
  110. tarball_path.unlink()
  111. def get_output(argv: list[str]) -> str:
  112. proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii')
  113. return proc.stdout.rstrip('\n')
  114. def gh_latest_version(client: httpx.Client, org: str, repo: str) -> dict:
  115. r = client.get(f'https://api.github.com/repos/{org}/{repo}/releases/latest',
  116. headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'})
  117. r.raise_for_status()
  118. return r.json()
  119. def download(client: httpx.Client, url: str, path: pathlib.Path) -> None:
  120. print('downloading', url, 'to', path)
  121. with client.stream('GET', url, follow_redirects=True) as r:
  122. r.raise_for_status()
  123. with path.open('wb') as f:
  124. for chunk in r.iter_bytes():
  125. f.write(chunk)
  126. if __name__ == '__main__':
  127. main()