2023-11-16 04:20:00 +00:00
|
|
|
#!/usr/bin/env python
|
2021-08-10 14:31:46 +00:00
|
|
|
# format:
|
2023-10-09 19:29:22 +00:00
|
|
|
# $ nix run nixpkgs#python3Packages.black -- update.py
|
2021-08-10 14:31:46 +00:00
|
|
|
# type-check:
|
2023-10-09 19:29:22 +00:00
|
|
|
# $ nix run nixpkgs#python3Packages.mypy -- update.py
|
2021-08-10 14:31:46 +00:00
|
|
|
# linted:
|
2023-10-09 19:29:22 +00:00
|
|
|
# $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
|
2021-08-10 14:31:46 +00:00
|
|
|
|
|
|
|
import inspect
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
from dataclasses import dataclass
|
|
|
|
import subprocess
|
|
|
|
import csv
|
|
|
|
import logging
|
2021-08-12 14:41:47 +00:00
|
|
|
import textwrap
|
|
|
|
from multiprocessing.dummy import Pool
|
2024-01-02 11:29:13 +00:00
|
|
|
import pluginupdate
|
|
|
|
from pluginupdate import update_plugins, FetchConfig
|
2021-08-10 14:31:46 +00:00
|
|
|
|
2021-09-18 10:52:07 +00:00
|
|
|
from typing import List, Tuple, Optional
|
2021-08-10 14:31:46 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
log = logging.getLogger()
|
|
|
|
log.addHandler(logging.StreamHandler())
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))).parent.parent # type: ignore
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
PKG_LIST = "maintainers/scripts/luarocks-packages.csv"
|
|
|
|
TMP_FILE = "$(mktemp)"
|
|
|
|
GENERATED_NIXFILE = "pkgs/development/lua-modules/generated-packages.nix"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-10-17 09:34:42 +00:00
|
|
|
HEADER = """/* {GENERATED_NIXFILE} is an auto-generated file -- DO NOT EDIT!
|
2024-04-21 15:54:59 +00:00
|
|
|
Regenerate it with: nix run nixpkgs#luarocks-packages-updater
|
2021-08-10 14:31:46 +00:00
|
|
|
You can customize the generated packages in pkgs/development/lua-modules/overrides.nix
|
2020-04-24 23:36:52 +00:00
|
|
|
*/
|
2023-10-09 19:29:22 +00:00
|
|
|
""".format(
|
|
|
|
GENERATED_NIXFILE=GENERATED_NIXFILE
|
|
|
|
)
|
2021-08-10 14:31:46 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
FOOTER = """
|
2021-08-10 14:31:46 +00:00
|
|
|
}
|
|
|
|
/* GENERATED - do not edit this file */
|
|
|
|
"""
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
@dataclass
|
|
|
|
class LuaPlugin:
|
|
|
|
name: str
|
2023-10-09 19:29:22 +00:00
|
|
|
"""Name of the plugin, as seen on luarocks.org"""
|
2024-05-15 15:35:15 +00:00
|
|
|
rockspec: str
|
|
|
|
"""Full path towards the rockspec"""
|
2021-09-18 10:52:07 +00:00
|
|
|
ref: Optional[str]
|
2023-10-09 19:29:22 +00:00
|
|
|
"""git reference (branch name/tag)"""
|
2021-09-18 10:52:07 +00:00
|
|
|
version: Optional[str]
|
2023-10-09 19:29:22 +00:00
|
|
|
"""Set it to pin a package """
|
2021-09-18 10:52:07 +00:00
|
|
|
server: Optional[str]
|
2023-10-09 19:29:22 +00:00
|
|
|
"""luarocks.org registers packages under different manifests.
|
2021-09-18 10:52:07 +00:00
|
|
|
Its value can be 'http://luarocks.org/dev'
|
2023-10-09 19:29:22 +00:00
|
|
|
"""
|
2021-09-18 10:52:07 +00:00
|
|
|
luaversion: Optional[str]
|
2023-10-09 19:29:22 +00:00
|
|
|
"""Attribue of the lua interpreter if a package is available only for a specific lua version"""
|
2021-09-18 10:52:07 +00:00
|
|
|
maintainers: Optional[str]
|
2023-10-09 19:29:22 +00:00
|
|
|
""" Optional string listing maintainers separated by spaces"""
|
2021-08-10 14:31:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def normalized_name(self) -> str:
|
|
|
|
return self.name.replace(".", "-")
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
# rename Editor to LangUpdate/ EcosystemUpdater
|
2023-04-29 16:46:19 +00:00
|
|
|
class LuaEditor(pluginupdate.Editor):
|
2023-11-16 04:20:00 +00:00
|
|
|
|
|
|
|
def create_parser(self):
|
|
|
|
parser = super().create_parser()
|
|
|
|
parser.set_defaults(proc=1)
|
|
|
|
return parser
|
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
def get_current_plugins(self):
|
|
|
|
return []
|
|
|
|
|
2021-08-12 14:41:47 +00:00
|
|
|
def load_plugin_spec(self, input_file) -> List[LuaPlugin]:
|
2021-08-10 14:31:46 +00:00
|
|
|
luaPackages = []
|
2023-10-09 19:29:22 +00:00
|
|
|
csvfilename = input_file
|
2021-08-10 14:31:46 +00:00
|
|
|
log.info("Loading package descriptions from %s", csvfilename)
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
with open(csvfilename, newline="") as csvfile:
|
|
|
|
reader = csv.DictReader(
|
|
|
|
csvfile,
|
|
|
|
)
|
2021-08-10 14:31:46 +00:00
|
|
|
for row in reader:
|
|
|
|
# name,server,version,luaversion,maintainers
|
|
|
|
plugin = LuaPlugin(**row)
|
|
|
|
luaPackages.append(plugin)
|
|
|
|
return luaPackages
|
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
def update(self, args):
|
|
|
|
update_plugins(self, args)
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
def generate_nix(self, results: List[Tuple[LuaPlugin, str]], outfilename: str):
|
2021-08-12 14:41:47 +00:00
|
|
|
with tempfile.NamedTemporaryFile("w+") as f:
|
|
|
|
f.write(HEADER)
|
|
|
|
header2 = textwrap.dedent(
|
2023-10-09 19:29:22 +00:00
|
|
|
"""
|
2024-05-15 15:35:15 +00:00
|
|
|
{ stdenv, lib, fetchurl, fetchgit, callPackage, ... }:
|
2021-10-17 09:34:42 +00:00
|
|
|
final: prev:
|
2021-08-12 14:41:47 +00:00
|
|
|
{
|
2023-10-09 19:29:22 +00:00
|
|
|
"""
|
|
|
|
)
|
2021-08-12 14:41:47 +00:00
|
|
|
f.write(header2)
|
2023-10-09 19:29:22 +00:00
|
|
|
for plugin, nix_expr in results:
|
2021-08-12 14:41:47 +00:00
|
|
|
f.write(f"{plugin.normalized_name} = {nix_expr}")
|
|
|
|
f.write(FOOTER)
|
|
|
|
f.flush()
|
|
|
|
|
|
|
|
# if everything went fine, move the generated file to its destination
|
|
|
|
# using copy since move doesn't work across disks
|
|
|
|
shutil.copy(f.name, outfilename)
|
|
|
|
|
|
|
|
print(f"updated {outfilename}")
|
|
|
|
|
2021-08-10 14:31:46 +00:00
|
|
|
@property
|
|
|
|
def attr_path(self):
|
|
|
|
return "luaPackages"
|
|
|
|
|
2022-03-10 19:12:11 +00:00
|
|
|
def get_update(self, input_file: str, outfile: str, config: FetchConfig):
|
2021-08-12 14:41:47 +00:00
|
|
|
_prefetch = generate_pkg_nix
|
2021-08-10 14:31:46 +00:00
|
|
|
|
|
|
|
def update() -> dict:
|
|
|
|
plugin_specs = self.load_plugin_spec(input_file)
|
2021-08-12 14:41:47 +00:00
|
|
|
sorted_plugin_specs = sorted(plugin_specs, key=lambda v: v.name.lower())
|
|
|
|
|
|
|
|
try:
|
2022-03-10 19:12:11 +00:00
|
|
|
pool = Pool(processes=config.proc)
|
2021-08-12 14:41:47 +00:00
|
|
|
results = pool.map(_prefetch, sorted_plugin_specs)
|
|
|
|
finally:
|
|
|
|
pass
|
2021-08-10 14:31:46 +00:00
|
|
|
|
2021-08-12 14:41:47 +00:00
|
|
|
self.generate_nix(results, outfile)
|
2021-08-10 14:31:46 +00:00
|
|
|
|
2022-03-10 19:12:11 +00:00
|
|
|
redirects = {}
|
2021-08-10 14:31:46 +00:00
|
|
|
return redirects
|
|
|
|
|
|
|
|
return update
|
|
|
|
|
2021-08-12 14:41:47 +00:00
|
|
|
def rewrite_input(self, input_file: str, *args, **kwargs):
|
|
|
|
# vim plugin reads the file before update but that shouldn't be our case
|
2021-08-10 14:31:46 +00:00
|
|
|
# not implemented yet
|
2021-08-12 14:41:47 +00:00
|
|
|
# fieldnames = ['name', 'server', 'version', 'luaversion', 'maintainers']
|
|
|
|
# input_file = "toto.csv"
|
|
|
|
# with open(input_file, newline='') as csvfile:
|
|
|
|
# writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
|
|
|
# writer.writeheader()
|
|
|
|
# for row in reader:
|
|
|
|
# # name,server,version,luaversion,maintainers
|
|
|
|
# plugin = LuaPlugin(**row)
|
|
|
|
# luaPackages.append(plugin)
|
2021-08-10 14:31:46 +00:00
|
|
|
pass
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
|
2021-08-12 14:41:47 +00:00
|
|
|
def generate_pkg_nix(plug: LuaPlugin):
|
2023-10-09 19:29:22 +00:00
|
|
|
"""
|
2021-08-12 14:41:47 +00:00
|
|
|
Generate nix expression for a luarocks package
|
|
|
|
Our cache key associates "p.name-p.version" to its rockspec
|
2023-10-09 19:29:22 +00:00
|
|
|
"""
|
2021-08-12 14:41:47 +00:00
|
|
|
log.debug("Generating nix expression for %s", plug.name)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
cmd = ["luarocks", "nix"]
|
2021-08-10 14:31:46 +00:00
|
|
|
|
2021-08-12 14:41:47 +00:00
|
|
|
if plug.maintainers:
|
|
|
|
cmd.append(f"--maintainers={plug.maintainers}")
|
2021-08-10 14:31:46 +00:00
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
if plug.rockspec != "":
|
|
|
|
if plug.ref or plug.version:
|
2023-10-09 19:29:22 +00:00
|
|
|
msg = (
|
2024-05-15 15:35:15 +00:00
|
|
|
"'version' and 'ref' will be ignored as the rockspec is hardcoded for package %s"
|
2023-10-09 19:29:22 +00:00
|
|
|
% plug.name
|
|
|
|
)
|
2024-05-15 15:35:15 +00:00
|
|
|
log.warn(msg)
|
|
|
|
|
|
|
|
log.debug("Updating from rockspec %s", plug.rockspec)
|
|
|
|
cmd.append(plug.rockspec)
|
|
|
|
|
2021-09-18 10:52:07 +00:00
|
|
|
# update the plugin from luarocks
|
|
|
|
else:
|
|
|
|
cmd.append(plug.name)
|
|
|
|
if plug.version and plug.version != "src":
|
|
|
|
cmd.append(plug.version)
|
|
|
|
|
|
|
|
if plug.server != "src" and plug.server:
|
|
|
|
cmd.append(f"--only-server={plug.server}")
|
|
|
|
|
2021-08-12 14:41:47 +00:00
|
|
|
if plug.luaversion:
|
2022-09-14 18:05:37 +00:00
|
|
|
cmd.append(f"--lua-version={plug.luaversion}")
|
2024-01-02 11:29:13 +00:00
|
|
|
luaver = plug.luaversion.replace('.', '')
|
|
|
|
if luaver := os.getenv(f"LUA_{luaver}"):
|
|
|
|
cmd.append(f"--lua-dir={luaver}")
|
2021-08-10 14:31:46 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
log.debug("running %s", " ".join(cmd))
|
2022-06-16 17:23:12 +00:00
|
|
|
|
2023-11-16 04:20:00 +00:00
|
|
|
output = subprocess.check_output(cmd, text=True)
|
2021-10-17 09:34:42 +00:00
|
|
|
output = "callPackage(" + output.strip() + ") {};\n\n"
|
2021-08-12 14:41:47 +00:00
|
|
|
return (plug, output)
|
2021-08-10 14:31:46 +00:00
|
|
|
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
def main():
|
|
|
|
editor = LuaEditor(
|
|
|
|
"lua",
|
|
|
|
ROOT,
|
|
|
|
"",
|
2023-11-16 04:20:00 +00:00
|
|
|
default_in=PKG_LIST,
|
|
|
|
default_out=GENERATED_NIXFILE,
|
2023-10-09 19:29:22 +00:00
|
|
|
)
|
2021-08-10 14:31:46 +00:00
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
editor.run()
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
if __name__ == "__main__":
|
2021-08-10 14:31:46 +00:00
|
|
|
main()
|