install_extras.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 zipfile
  10. import httpx
  11. CURRENT_DIR = pathlib.Path(__file__).parent
  12. def main():
  13. bat()
  14. curlie()
  15. delta()
  16. dua()
  17. dust()
  18. eza()
  19. fx()
  20. git_whence()
  21. jujutsu()
  22. lazygit()
  23. oh_my_posh()
  24. #starship()
  25. uv()
  26. yazi()
  27. def bat():
  28. if platform.system() == 'Darwin':
  29. print('$ brew install git-delta')
  30. subprocess.run(['brew', 'install', 'bat'], check=True)
  31. return
  32. else:
  33. assert platform.system() == 'Linux'
  34. if subprocess.run(['dpkg', '-l', 'bat'], stdout=subprocess.DEVNULL).returncode == 0:
  35. print_gray('bat package already installed')
  36. return
  37. subprocess.run(['sudo', 'apt', 'install', 'bat', '--yes'], check=True)
  38. def curlie():
  39. if platform.system() == 'Darwin':
  40. print('$ brew install curlie')
  41. subprocess.run(['brew', 'install', 'curlie'], check=True)
  42. return
  43. if subprocess.run(['dpkg', '-l', 'curlie'], stdout=subprocess.DEVNULL).returncode == 0:
  44. print_gray('curlie package already installed')
  45. return
  46. client = httpx.Client()
  47. latest = gh_latest_version(client, 'rs', 'curlie')
  48. version = latest['tag_name'].removeprefix('v')
  49. system = platform.system().lower()
  50. arch = get_output(['dpkg', '--print-architecture'])
  51. filename = f'curlie_{version}_{system}_{arch}.deb'
  52. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  53. deb_path = CURRENT_DIR / 'curlie.deb'
  54. download(client, asset['browser_download_url'], deb_path)
  55. try:
  56. subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
  57. finally:
  58. os.unlink(deb_path)
  59. def delta():
  60. if platform.system() == 'Darwin':
  61. print('$ brew install git-delta')
  62. subprocess.run(['brew', 'install', 'git-delta'], check=True)
  63. return
  64. else:
  65. assert platform.system() == 'Linux'
  66. if subprocess.run(['dpkg', '-l', 'git-delta'], stdout=subprocess.DEVNULL).returncode == 0:
  67. print_gray('git-delta package already installed')
  68. return
  69. client = httpx.Client()
  70. latest = gh_latest_version(client, 'dandavison', 'delta')
  71. version = latest['name']
  72. arch = get_output(['dpkg', '--print-architecture'])
  73. filename = f'git-delta_{version}_{arch}.deb'
  74. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  75. deb_path = CURRENT_DIR / 'git-delta.deb'
  76. download(client, asset['browser_download_url'], deb_path)
  77. try:
  78. subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
  79. finally:
  80. os.unlink(deb_path)
  81. def dua():
  82. if platform.system() == 'Darwin':
  83. print('$ brew install dua-cli')
  84. subprocess.run(['brew', 'install', 'dua-cli'], check=True)
  85. return
  86. else:
  87. assert platform.system() == 'Linux'
  88. dua_path = CURRENT_DIR / 'dua'
  89. if dua_path.exists():
  90. print_gray('dua already downloaded')
  91. return
  92. client = httpx.Client()
  93. latest = gh_latest_version(client, 'Byron', 'dua-cli')
  94. version = latest['name']
  95. dirname = f'dua-{version}-{platform.machine()}-unknown-linux-musl'
  96. filename = dirname + '.tar.gz'
  97. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  98. tarball_path = CURRENT_DIR / 'dua.tar.gz'
  99. download(client, asset['browser_download_url'], tarball_path)
  100. with tarfile.open(tarball_path) as tar:
  101. with tar.extractfile(dirname + '/dua') as binary, dua_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager]
  102. shutil.copyfileobj(binary, f)
  103. os.unlink(tarball_path)
  104. dua_path.chmod(0o755)
  105. def dust():
  106. if platform.system() == 'Darwin':
  107. print('$ brew install dust')
  108. subprocess.run(['brew', 'install', 'dust'], check=True)
  109. return
  110. else:
  111. assert platform.system() == 'Linux'
  112. dust_path = CURRENT_DIR / 'dust'
  113. if dust_path.exists():
  114. print_gray('dust already downloaded')
  115. return
  116. client = httpx.Client()
  117. latest = gh_latest_version(client, 'bootandy', 'dust')
  118. version = latest['name']
  119. dirname = f'dust-{version}-{platform.machine()}-unknown-linux-musl'
  120. filename = dirname + '.tar.gz'
  121. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  122. tarball_path = CURRENT_DIR / 'dust.tar.gz'
  123. download(client, asset['browser_download_url'], tarball_path)
  124. with tarfile.open(tarball_path) as tar:
  125. with tar.extractfile(dirname + '/dust') as binary, dust_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager]
  126. shutil.copyfileobj(binary, f)
  127. os.unlink(tarball_path)
  128. dust_path.chmod(0o755)
  129. def eza():
  130. if platform.system() == 'Darwin':
  131. print('$ brew install eza')
  132. subprocess.run(['brew', 'install', 'eza'], check=True)
  133. return
  134. else:
  135. assert platform.system() == 'Linux'
  136. if (CURRENT_DIR / 'eza').exists():
  137. print_gray('eza already downloaded')
  138. return
  139. client = httpx.Client()
  140. url = f'https://github.com/eza-community/eza/releases/latest/download/eza_{platform.machine()}-unknown-linux-gnu.tar.gz'
  141. tarball_path = CURRENT_DIR / 'eza.tar.gz'
  142. download(client, url, tarball_path)
  143. with tarfile.open(tarball_path) as tar:
  144. tar.extract('./eza', CURRENT_DIR)
  145. tarball_path.unlink()
  146. def fx():
  147. if platform.system() == 'Darwin':
  148. print('$ brew install fx')
  149. subprocess.run(['brew', 'install', 'fx'], check=True)
  150. return
  151. else:
  152. assert platform.system() == 'Linux'
  153. if (CURRENT_DIR / 'fx').exists():
  154. print_gray('fx already downloaded')
  155. return
  156. client = httpx.Client()
  157. arch = platform.machine()
  158. if arch == 'aarch64':
  159. arch = 'arm64'
  160. elif arch == 'x86_64':
  161. arch = 'amd64'
  162. url = f'https://github.com/antonmedv/fx/releases/latest/download/fx_linux_{arch}'
  163. download(client, url, CURRENT_DIR / 'fx')
  164. os.chmod(CURRENT_DIR / 'fx', 0o755)
  165. def git_whence():
  166. if platform.system() == 'Darwin':
  167. print('$ brew install raylu/formulae/git-whence')
  168. subprocess.run(['brew', 'install', 'raylu/formulae/git-whence'], check=True)
  169. return
  170. else:
  171. assert platform.system() == 'Linux'
  172. if (CURRENT_DIR / 'git-whence').exists():
  173. print_gray('git-whence already downloaded')
  174. return
  175. client = httpx.Client()
  176. url = f'https://github.com/raylu/git-whence/releases/latest/download/git-whence-{platform.machine()}-unknown-linux-gnu'
  177. download(client, url, CURRENT_DIR / 'git-whence')
  178. os.chmod(CURRENT_DIR / 'git-whence', 0o755)
  179. def jujutsu():
  180. if platform.system() == 'Darwin':
  181. print('$ brew install jj')
  182. subprocess.run(['brew', 'install', 'jj'], check=True)
  183. return
  184. else:
  185. assert platform.system() == 'Linux'
  186. if (CURRENT_DIR / 'jj').exists():
  187. print_gray('jj already downloaded')
  188. return
  189. client = httpx.Client()
  190. latest = gh_latest_version(client, 'jj-vcs', 'jj')
  191. version = latest['name']
  192. arch = platform.machine()
  193. url = f'https://github.com/jj-vcs/jj/releases/download/{version}/jj-{version}-{arch}-unknown-linux-musl.tar.gz'
  194. tarball_path = CURRENT_DIR / 'jj.tar.gz'
  195. download(client, url, tarball_path)
  196. with tarfile.open(tarball_path, 'r:gz') as tar:
  197. tar.extract('./jj', CURRENT_DIR, filter='data')
  198. tarball_path.unlink()
  199. def lazygit():
  200. if (CURRENT_DIR / 'lazygit').exists():
  201. print_gray('lazygit package already installed')
  202. return
  203. client = httpx.Client()
  204. latest = gh_latest_version(client, 'jesseduffield', 'lazygit')
  205. version = latest['name']
  206. arch = platform.machine()
  207. if arch == 'aarch64':
  208. arch = 'arm64'
  209. url = f'https://github.com/jesseduffield/lazygit/releases/download/{version}/lazygit_{version.lstrip("v")}_{platform.system()}_{arch}.tar.gz'
  210. download(client, url, CURRENT_DIR / 'lazygit.tgz')
  211. with tarfile.open(CURRENT_DIR / 'lazygit.tgz', 'r:gz') as tar:
  212. tar.extract('lazygit', CURRENT_DIR)
  213. os.unlink(CURRENT_DIR / 'lazygit.tgz')
  214. def oh_my_posh():
  215. if platform.system() == 'Darwin':
  216. print('$ brew install oh-my-posh')
  217. subprocess.run(['brew', 'install', 'oh-my-posh'], check=True)
  218. return
  219. else:
  220. assert platform.system() == 'Linux'
  221. if (CURRENT_DIR / 'oh-my-posh').exists():
  222. print_gray('oh-my-posh already downloaded')
  223. return
  224. client = httpx.Client()
  225. arch = platform.machine()
  226. if arch == 'aarch64':
  227. arch = 'arm64'
  228. elif arch == 'x86_64':
  229. arch = 'amd64'
  230. url = f'https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-{arch}'
  231. download(client, url, CURRENT_DIR / 'oh-my-posh')
  232. os.chmod(CURRENT_DIR / 'oh-my-posh', 0o755)
  233. def starship():
  234. if platform.system() == 'Darwin':
  235. print('$ brew install starship')
  236. subprocess.run(['brew', 'install', 'starship'], check=True)
  237. return
  238. else:
  239. assert platform.system() == 'Linux'
  240. if (CURRENT_DIR / 'starship').exists():
  241. print_gray('starship already downloaded')
  242. return
  243. client = httpx.Client()
  244. arch = platform.machine()
  245. if arch == 'x86_64':
  246. libc = 'gnu'
  247. else:
  248. libc = 'musl'
  249. url = f'https://github.com/starship/starship/releases/latest/download/starship-{arch}-unknown-linux-{libc}.tar.gz'
  250. tarball_path = CURRENT_DIR / 'starship.tar.gz'
  251. download(client, url, tarball_path)
  252. with tarfile.open(tarball_path, 'r:gz') as tar:
  253. tar.extract('starship', CURRENT_DIR)
  254. tarball_path.unlink()
  255. def uv():
  256. if (CURRENT_DIR / 'uv').exists():
  257. print_gray('uv already downloaded')
  258. return
  259. if which := shutil.which('uv'):
  260. print_gray('uv already at ' + which)
  261. return
  262. client = httpx.Client()
  263. os = platform.system().lower()
  264. arch = platform.machine()
  265. if arch == 'darwin':
  266. prefix = f'uv-{arch}-apple-darwin'
  267. else:
  268. prefix = f'uv-{arch}-unknown-linux-gnu'
  269. url = f'https://github.com/astral-sh/uv/releases/latest/download/{prefix}.tar.gz'
  270. tarball_path = CURRENT_DIR / 'uv.tar.gz'
  271. download(client, url, tarball_path)
  272. with tarfile.open(tarball_path, 'r:gz') as tar:
  273. for filename in ['uv', 'uvx']:
  274. target_path = CURRENT_DIR / filename
  275. with tar.extractfile(f'{prefix}/{filename}') as binary, target_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager]
  276. shutil.copyfileobj(binary, f)
  277. target_path.chmod(0o755)
  278. tarball_path.unlink()
  279. def yazi():
  280. if platform.system() == 'Darwin':
  281. print('$ brew install yazi')
  282. subprocess.run(['brew', 'install', 'yazi'], check=True)
  283. return
  284. else:
  285. assert platform.system() == 'Linux'
  286. if (CURRENT_DIR / 'yazi').exists():
  287. print_gray('yazi already downloaded')
  288. return
  289. client = httpx.Client()
  290. name = f'yazi-{platform.machine()}-unknown-linux-musl'
  291. url = f'https://github.com/sxyazi/yazi/releases/latest/download/{name}.zip'
  292. zip_path = CURRENT_DIR / 'yazi.zip'
  293. download(client, url, zip_path)
  294. with zipfile.ZipFile(zip_path, 'r') as zipf:
  295. for filename in ['ya', 'yazi']:
  296. extracted = (CURRENT_DIR / filename)
  297. with zipf.open(f'{name}/{filename}', 'r') as binary, extracted.open('wb') as f:
  298. shutil.copyfileobj(binary, f)
  299. extracted.chmod(0o755)
  300. zip_path.unlink()
  301. def get_output(argv: list[str]) -> str:
  302. proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii')
  303. return proc.stdout.rstrip('\n')
  304. def gh_latest_version(client: httpx.Client, org: str, repo: str) -> dict:
  305. r = client.get(f'https://api.github.com/repos/{org}/{repo}/releases/latest',
  306. headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'})
  307. r.raise_for_status()
  308. return r.json()
  309. def download(client: httpx.Client, url: str, path: pathlib.Path) -> None:
  310. print('downloading', url, 'to', path)
  311. with client.stream('GET', url, follow_redirects=True) as r:
  312. r.raise_for_status()
  313. with path.open('wb') as f:
  314. for chunk in r.iter_bytes():
  315. f.write(chunk)
  316. def print_gray(*args: object):
  317. print('\x1B[90;m', end='')
  318. print(*args, end='')
  319. print('\x1B[0m')
  320. if __name__ == '__main__':
  321. main()