Forráskód Böngészése

install_extras: eza

raylu 2 éve
szülő
commit
22d3abdf43
1 módosított fájl, 39 hozzáadás és 8 törlés
  1. 39 8
      install_extras.py

+ 39 - 8
install_extras.py

@@ -5,6 +5,7 @@ import os
 import pathlib
 import platform
 import subprocess
+import tarfile
 
 import httpx
 
@@ -12,23 +13,26 @@ CURRENT_DIR = pathlib.Path(__file__).parent
 
 def main():
 	delta()
+	eza()
 
 def delta():
 	if platform.system() == 'Darwin':
 		print('$ brew install git-delta')
 		subprocess.run(['brew', 'install', 'git-delta'], check=True)
 		return
+	else:
+		assert platform.system() == 'Linux'
+
+	if subprocess.run(['dpkg', '-l', 'git-delta'], stdout=subprocess.DEVNULL).returncode == 0:
+		print('git-delta package already installed')
+		return
 
 	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()
+	latest = gh_latest_version(client, 'dandavision', 'delta')
 
-	if platform.system() == 'Linux':
-		version = latest['name']
-		arch = get_output(['dpkg', '--print-architecture'])
-		filename = f'git-delta_{version}_{arch}.deb'
+	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'
@@ -39,10 +43,37 @@ def delta():
 	finally:
 		os.unlink(deb_path)
 
+def eza():
+	if platform.system() == 'Darwin':
+		print('$ brew install eza')
+		subprocess.run(['brew', 'install', 'eza'], check=True)
+		return
+	else:
+		assert platform.system() == 'Linux'
+
+	if (CURRENT_DIR / 'eza').exists():
+		print('eza already downloaded')
+		return
+
+	client = httpx.Client()
+	if platform.system() == 'Linux':
+		url = f'https://github.com/eza-community/eza/releases/latest/download/eza_{platform.machine()}-unknown-linux-gnu.tar.gz'
+		download(client, url, CURRENT_DIR / 'eza.tar.gz')
+		with tarfile.open(CURRENT_DIR / 'eza.tar.gz', 'r:gz') as tar:
+			tar.extract('./eza', CURRENT_DIR)
+		os.unlink(CURRENT_DIR / 'eza.tar.gz')
+
+
 def get_output(argv: list[str]) -> str:
 	proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii')
 	return proc.stdout.rstrip('\n')
 
+def gh_latest_version(client: httpx.Client, org: str, repo: str) -> dict:
+	r = client.get(f'https://api.github.com/repos/{org}/{repo}/releases/latest',
+			headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'})
+	r.raise_for_status()
+	return r.json()
+
 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: