install_extras.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. bat()
  13. delta()
  14. dua()
  15. dust()
  16. eza()
  17. git_whence()
  18. lazygit()
  19. starship()
  20. def bat():
  21. if platform.system() == 'Darwin':
  22. print('$ brew install git-delta')
  23. subprocess.run(['brew', 'install', 'bat'], check=True)
  24. return
  25. else:
  26. assert platform.system() == 'Linux'
  27. if subprocess.run(['dpkg', '-l', 'bat'], stdout=subprocess.DEVNULL).returncode == 0:
  28. print('bat package already installed')
  29. return
  30. subprocess.run(['sudo', 'apt', 'install', 'bat', '--yes'], check=True)
  31. def delta():
  32. if platform.system() == 'Darwin':
  33. print('$ brew install git-delta')
  34. subprocess.run(['brew', 'install', 'git-delta'], check=True)
  35. return
  36. else:
  37. assert platform.system() == 'Linux'
  38. if subprocess.run(['dpkg', '-l', 'git-delta'], stdout=subprocess.DEVNULL).returncode == 0:
  39. print('git-delta package already installed')
  40. return
  41. client = httpx.Client()
  42. latest = gh_latest_version(client, 'dandavison', 'delta')
  43. version = latest['name']
  44. arch = get_output(['dpkg', '--print-architecture'])
  45. filename = f'git-delta_{version}_{arch}.deb'
  46. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  47. deb_path = CURRENT_DIR / 'git-delta.deb'
  48. download(client, asset['browser_download_url'], deb_path)
  49. try:
  50. subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
  51. finally:
  52. os.unlink(deb_path)
  53. def dua():
  54. if platform.system() == 'Darwin':
  55. print('$ brew install dua-cli')
  56. subprocess.run(['brew', 'install', 'dua-cli'], check=True)
  57. return
  58. else:
  59. assert platform.system() == 'Linux'
  60. dua_path = CURRENT_DIR / 'dua'
  61. if dua_path.exists():
  62. print('dua already downloaded')
  63. return
  64. client = httpx.Client()
  65. latest = gh_latest_version(client, 'Byron', 'dua-cli')
  66. version = latest['name']
  67. dirname = f'dua-{version}-{platform.machine()}-unknown-linux-musl'
  68. filename = dirname + '.tar.gz'
  69. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  70. tarball_path = CURRENT_DIR / 'dua.tar.gz'
  71. download(client, asset['browser_download_url'], tarball_path)
  72. with tarfile.open(tarball_path) as tar:
  73. with tar.extractfile(dirname + '/dua') as binary, dua_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager]
  74. shutil.copyfileobj(binary, f)
  75. os.unlink(tarball_path)
  76. dua_path.chmod(0o755)
  77. def dust():
  78. if platform.system() == 'Darwin':
  79. print('$ brew install dust')
  80. subprocess.run(['brew', 'install', 'dust'], check=True)
  81. return
  82. else:
  83. assert platform.system() == 'Linux'
  84. dust_path = CURRENT_DIR / 'dust'
  85. if dust_path.exists():
  86. print('dust already downloaded')
  87. return
  88. client = httpx.Client()
  89. latest = gh_latest_version(client, 'bootandy', 'dust')
  90. version = latest['name']
  91. dirname = f'dust-{version}-{platform.machine()}-unknown-linux-musl'
  92. filename = dirname + '.tar.gz'
  93. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  94. tarball_path = CURRENT_DIR / 'dust.tar.gz'
  95. download(client, asset['browser_download_url'], tarball_path)
  96. with tarfile.open(tarball_path) as tar:
  97. with tar.extractfile(dirname + '/dust') as binary, dust_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager]
  98. shutil.copyfileobj(binary, f)
  99. os.unlink(tarball_path)
  100. dust_path.chmod(0o755)
  101. def eza():
  102. if platform.system() == 'Darwin':
  103. print('$ brew install eza')
  104. subprocess.run(['brew', 'install', 'eza'], check=True)
  105. return
  106. else:
  107. assert platform.system() == 'Linux'
  108. if (CURRENT_DIR / 'eza').exists():
  109. print('eza already downloaded')
  110. return
  111. client = httpx.Client()
  112. url = f'https://github.com/eza-community/eza/releases/latest/download/eza_{platform.machine()}-unknown-linux-gnu.tar.gz'
  113. tarball_path = CURRENT_DIR / 'eza.tar.gz'
  114. download(client, url, tarball_path)
  115. with tarfile.open(tarball_path) as tar:
  116. tar.extract('./eza', CURRENT_DIR)
  117. tarball_path.unlink()
  118. def git_whence():
  119. if platform.system() == 'Darwin':
  120. print('$ brew install raylu/formulae/git-whence')
  121. subprocess.run(['brew', 'install', 'raylu/formulae/git-whence'], check=True)
  122. return
  123. else:
  124. assert platform.system() == 'Linux'
  125. if (CURRENT_DIR / 'git-whence').exists():
  126. print('git-whence already downloaded')
  127. return
  128. client = httpx.Client()
  129. url = f'https://github.com/raylu/git-whence/releases/latest/download/git-whence-{platform.machine()}-unknown-linux-gnu'
  130. download(client, url, CURRENT_DIR / 'git-whence')
  131. os.chmod(CURRENT_DIR / 'git-whence', 0o755)
  132. def lazygit():
  133. if (CURRENT_DIR / 'lazygit').exists():
  134. print('lazygit package already installed')
  135. return
  136. client = httpx.Client()
  137. latest = gh_latest_version(client, 'jesseduffield', 'lazygit')
  138. version = latest['name']
  139. arch = platform.machine()
  140. if arch == 'aarch64':
  141. arch = 'arm64'
  142. url = f'https://github.com/jesseduffield/lazygit/releases/download/{version}/lazygit_{version.lstrip("v")}_{platform.system()}_{arch}.tar.gz'
  143. download(client, url, CURRENT_DIR / 'lazygit.tgz')
  144. with tarfile.open(CURRENT_DIR / 'lazygit.tgz', 'r:gz') as tar:
  145. tar.extract('lazygit', CURRENT_DIR)
  146. os.unlink(CURRENT_DIR / 'lazygit.tgz')
  147. def starship():
  148. if platform.system() == 'Darwin':
  149. print('$ brew install starship')
  150. subprocess.run(['brew', 'install', 'starship'], check=True)
  151. return
  152. else:
  153. assert platform.system() == 'Linux'
  154. if (CURRENT_DIR / 'starship').exists():
  155. print('starship already downloaded')
  156. return
  157. client = httpx.Client()
  158. arch = platform.machine()
  159. if arch == 'x86_64':
  160. libc = 'gnu'
  161. else:
  162. libc = 'musl'
  163. url = f'https://github.com/starship/starship/releases/latest/download/starship-{arch}-unknown-linux-{libc}.tar.gz'
  164. tarball_path = CURRENT_DIR / 'starship.tar.gz'
  165. download(client, url, tarball_path)
  166. with tarfile.open(tarball_path, 'r:gz') as tar:
  167. tar.extract('starship', CURRENT_DIR)
  168. tarball_path.unlink()
  169. def get_output(argv: list[str]) -> str:
  170. proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii')
  171. return proc.stdout.rstrip('\n')
  172. def gh_latest_version(client: httpx.Client, org: str, repo: str) -> dict:
  173. r = client.get(f'https://api.github.com/repos/{org}/{repo}/releases/latest',
  174. headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'})
  175. r.raise_for_status()
  176. return r.json()
  177. def download(client: httpx.Client, url: str, path: pathlib.Path) -> None:
  178. print('downloading', url, 'to', path)
  179. with client.stream('GET', url, follow_redirects=True) as r:
  180. r.raise_for_status()
  181. with path.open('wb') as f:
  182. for chunk in r.iter_bytes():
  183. f.write(chunk)
  184. if __name__ == '__main__':
  185. main()