File size: 940 Bytes
9776024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os, subprocess
from typing import Optional

ADAPTER_DIR = ".config/adapters"

def list_adapters():
    if not os.path.exists(ADAPTER_DIR):
        return []
    return [d for d in os.listdir(ADAPTER_DIR) if os.path.isdir(os.path.join(ADAPTER_DIR, d))]

def adapter_path(name: str):
    p = os.path.join(ADAPTER_DIR, name)
    return p if os.path.exists(p) else None

def install_adapter_from_remote(url: str, name: str):
    os.makedirs(ADAPTER_DIR, exist_ok=True)
    tarball = os.path.join(ADAPTER_DIR, f"{name}.zip")
    try:
        subprocess.check_call(["wget", "-O", tarball, url])
    except Exception:
        try:
            subprocess.check_call(["curl", "-L", url, "-o", tarball])
        except Exception as e:
            raise RuntimeError("download failed: " + str(e))
    subprocess.check_call(["unzip", "-o", tarball, "-d", os.path.join(ADAPTER_DIR, name)])
    return adapter_path(name)