install_extras.py 5.1 KB

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