浏览代码

add script for installing things

raylu 2 年之前
父节点
当前提交
21a517169c
共有 1 个文件被更改,包括 50 次插入0 次删除
  1. 50 0
      install_extras.py

+ 50 - 0
install_extras.py

@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+from __future__ import annotations
+
+import os
+import pathlib
+import platform
+import subprocess
+
+import httpx
+
+CURRENT_DIR = pathlib.Path(__file__).parent
+
+def main():
+	delta()
+
+def delta():
+	client = httpx.Client()
+	r = client.get('https://api.github.com/repos/dandavison/delta/releases/latest',
+			headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'})
+	r.raise_for_status()
+	latest = r.json()
+
+	if platform.system() == 'Linux':
+		version = latest['name']
+		arch = get_output(['dpkg', '--print-architecture'])
+		filename = f'git-delta_{version}_{arch}.deb'
+
+	(asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
+	deb_path = CURRENT_DIR / 'git-delta.deb'
+	download(client, asset['browser_download_url'], deb_path)
+
+	try:
+		subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
+	finally:
+		os.unlink(deb_path)
+
+def get_output(argv: list[str]) -> str:
+	proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii')
+	return proc.stdout.rstrip('\n')
+
+def download(client: httpx.Client, url: str, path: pathlib.Path) -> None:
+	print('downloading', url, 'to', path)
+	with client.stream('GET', url, follow_redirects=True) as r:
+		r.raise_for_status()
+		with path.open('wb') as f:
+			for chunk in r.iter_bytes():
+				f.write(chunk)
+
+if __name__ == '__main__':
+	main()