Created
September 20, 2021 17:26
-
-
Save willir/c96dceedbbc4a3b463ea37841fcf1d83 to your computer and use it in GitHub Desktop.
Builds local luarocks server of specified rocks and their dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import re | |
import sys | |
from contextlib import ExitStack | |
from pathlib import Path | |
from subprocess import run, PIPE | |
from typing import Set, Tuple | |
def add_dependencies(name: str, ver: str, packages: Set[Tuple[str, str]]): | |
ptn = re.compile(r"([\w\d_-]+)\s*(?:[>=<]+\s*[\d\w.-]+\s*,?\s*)*\(using ([\d\w.-]+)\)") | |
out = run(["luarocks", "show", name, ver], check=True, stdout=PIPE, encoding="utf8").stdout | |
depends_started = False | |
for line in out.splitlines(): | |
if line.startswith("Depends on:"): | |
depends_started = True | |
continue | |
if not depends_started: | |
continue | |
if not line.strip(): | |
break | |
m = ptn.search(line) | |
print(line) | |
assert m, f"Cannot parse line: {line}" | |
dep_name, dep_ver = m.group(1), m.group(2) | |
if dep_name != "lua": | |
packages.add((dep_name, dep_ver)) | |
def main(): | |
parser = argparse.ArgumentParser( | |
description="Builds a luarocks local server from the specified rocks and their dependencies" | |
) | |
parser.add_argument( | |
"rocks", type=str, | |
nargs="?", | |
default="-", | |
help="path to file with rocks in ({name} {ver}\\n)* format", | |
) | |
args = parser.parse_args() | |
server_path = Path.cwd() / "rocks-server" | |
server_path.mkdir() | |
downloaded = set() | |
with ExitStack() as st: | |
f = sys.stdin if args.rocks == "-" else st.enter_context(open(args.rocks)) | |
packages = {(n, v) for n, v in map(str.split, f)} | |
while packages: | |
name, ver = packages.pop() | |
if (name, ver) in downloaded: | |
continue | |
print(f"Downloading {name} {ver}...", file=sys.stderr) | |
run(["luarocks", "download", name, ver], check=True, cwd=server_path) | |
for rockspec in server_path.glob(f"{name}*.rockspec"): | |
run(["luarocks", "pack", rockspec], check=True, cwd=server_path) | |
run(["luarocks", "install", "--local", name, ver], check=True) | |
downloaded.add((name, ver)) | |
add_dependencies(name, ver, packages) | |
print(packages) | |
run(["luarocks-admin", "make-manifest", "rocks-server"], check=True) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment