2019-02-04 11:30:58 +00:00
|
|
|
#!/usr/bin/env nix-shell
|
2021-08-16 13:53:07 +00:00
|
|
|
#!nix-shell update-luarocks-shell.nix -i python3
|
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
# format:
|
|
|
|
# $ nix run nixpkgs.python3Packages.black -c black update.py
|
|
|
|
# type-check:
|
|
|
|
# $ nix run nixpkgs.python3Packages.mypy -c mypy update.py
|
|
|
|
# linted:
|
|
|
|
# $ nix run nixpkgs.python3Packages.flake8 -c flake8 --ignore E501,E265,E402 update.py
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
from dataclasses import dataclass
|
|
|
|
import subprocess
|
|
|
|
import csv
|
|
|
|
import logging
|
2021-07-30 00:25:15 +00:00
|
|
|
import textwrap
|
|
|
|
from multiprocessing.dummy import Pool
|
2021-07-30 00:25:15 +00:00
|
|
|
|
2021-08-16 15:44:40 +00:00
|
|
|
from typing import List, Tuple, Optional
|
2021-07-30 00:25:15 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
log = logging.getLogger()
|
|
|
|
log.addHandler(logging.StreamHandler())
|
2019-02-04 11:30:58 +00:00
|
|
|
|
2022-03-07 11:08:51 +00:00
|
|
|
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))).parent.parent # type: ignore
|
|
|
|
from pluginupdate import Editor, update_plugins, FetchConfig, CleanEnvironment
|
2019-02-04 11:30:58 +00:00
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
PKG_LIST="maintainers/scripts/luarocks-packages.csv"
|
2019-02-04 11:30:58 +00:00
|
|
|
TMP_FILE="$(mktemp)"
|
2021-07-30 00:25:15 +00:00
|
|
|
GENERATED_NIXFILE="pkgs/development/lua-modules/generated-packages.nix"
|
2022-06-01 22:00:14 +00:00
|
|
|
LUAROCKS_CONFIG="maintainers/scripts/luarocks-config.lua"
|
2019-02-04 11:30:58 +00:00
|
|
|
|
2021-10-03 22:10:46 +00:00
|
|
|
HEADER = """/* {GENERATED_NIXFILE} is an auto-generated file -- DO NOT EDIT!
|
2019-02-04 11:30:58 +00:00
|
|
|
Regenerate it with:
|
2021-07-30 00:25:15 +00:00
|
|
|
nixpkgs$ ./maintainers/scripts/update-luarocks-packages
|
2019-02-04 11:30:58 +00:00
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
You can customize the generated packages in pkgs/development/lua-modules/overrides.nix
|
2019-02-04 11:30:58 +00:00
|
|
|
*/
|
2021-07-30 00:25:15 +00:00
|
|
|
""".format(GENERATED_NIXFILE=GENERATED_NIXFILE)
|
|
|
|
|
|
|
|
FOOTER="""
|
|
|
|
}
|
|
|
|
/* GENERATED - do not edit this file */
|
|
|
|
"""
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class LuaPlugin:
|
|
|
|
name: str
|
2021-08-16 15:44:40 +00:00
|
|
|
'''Name of the plugin, as seen on luarocks.org'''
|
|
|
|
src: str
|
|
|
|
'''address to the git repository'''
|
2021-08-31 20:15:04 +00:00
|
|
|
ref: Optional[str]
|
2021-08-16 15:44:40 +00:00
|
|
|
'''git reference (branch name/tag)'''
|
|
|
|
version: Optional[str]
|
|
|
|
'''Set it to pin a package '''
|
2021-08-31 20:15:04 +00:00
|
|
|
server: Optional[str]
|
2021-08-16 15:44:40 +00:00
|
|
|
'''luarocks.org registers packages under different manifests.
|
|
|
|
Its value can be 'http://luarocks.org/dev'
|
|
|
|
'''
|
|
|
|
luaversion: Optional[str]
|
|
|
|
'''Attribue of the lua interpreter if a package is available only for a specific lua version'''
|
|
|
|
maintainers: Optional[str]
|
|
|
|
''' Optional string listing maintainers separated by spaces'''
|
2021-07-30 00:25:15 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def normalized_name(self) -> str:
|
|
|
|
return self.name.replace(".", "-")
|
|
|
|
|
|
|
|
# rename Editor to LangUpdate/ EcosystemUpdater
|
|
|
|
class LuaEditor(Editor):
|
|
|
|
def get_current_plugins(self):
|
|
|
|
return []
|
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
def load_plugin_spec(self, input_file) -> List[LuaPlugin]:
|
2021-07-30 00:25:15 +00:00
|
|
|
luaPackages = []
|
|
|
|
csvfilename=input_file
|
|
|
|
log.info("Loading package descriptions from %s", csvfilename)
|
|
|
|
|
|
|
|
with open(csvfilename, newline='') as csvfile:
|
|
|
|
reader = csv.DictReader(csvfile,)
|
|
|
|
for row in reader:
|
|
|
|
# name,server,version,luaversion,maintainers
|
|
|
|
plugin = LuaPlugin(**row)
|
|
|
|
luaPackages.append(plugin)
|
|
|
|
return luaPackages
|
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
def generate_nix(
|
|
|
|
self,
|
|
|
|
results: List[Tuple[LuaPlugin, str]],
|
|
|
|
outfilename: str
|
|
|
|
):
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile("w+") as f:
|
|
|
|
f.write(HEADER)
|
|
|
|
header2 = textwrap.dedent(
|
|
|
|
# header2 = inspect.cleandoc(
|
|
|
|
"""
|
2021-10-03 22:10:46 +00:00
|
|
|
{ self, stdenv, lib, fetchurl, fetchgit, callPackage, ... } @ args:
|
|
|
|
final: prev:
|
2021-07-30 00:25:15 +00:00
|
|
|
{
|
|
|
|
""")
|
|
|
|
f.write(header2)
|
|
|
|
for (plugin, nix_expr) in results:
|
|
|
|
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-07-30 00:25:15 +00:00
|
|
|
@property
|
|
|
|
def attr_path(self):
|
|
|
|
return "luaPackages"
|
|
|
|
|
2022-03-07 11:08:51 +00:00
|
|
|
def get_update(self, input_file: str, outfile: str, config: FetchConfig):
|
2021-07-30 00:25:15 +00:00
|
|
|
_prefetch = generate_pkg_nix
|
2021-07-30 00:25:15 +00:00
|
|
|
|
|
|
|
def update() -> dict:
|
|
|
|
plugin_specs = self.load_plugin_spec(input_file)
|
2021-07-30 00:25:15 +00:00
|
|
|
sorted_plugin_specs = sorted(plugin_specs, key=lambda v: v.name.lower())
|
|
|
|
|
|
|
|
try:
|
2022-03-07 11:08:51 +00:00
|
|
|
pool = Pool(processes=config.proc)
|
2021-07-30 00:25:15 +00:00
|
|
|
results = pool.map(_prefetch, sorted_plugin_specs)
|
|
|
|
finally:
|
|
|
|
pass
|
2021-07-30 00:25:15 +00:00
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
self.generate_nix(results, outfile)
|
2021-07-30 00:25:15 +00:00
|
|
|
|
2022-03-07 11:08:51 +00:00
|
|
|
redirects = {}
|
2021-07-30 00:25:15 +00:00
|
|
|
return redirects
|
|
|
|
|
|
|
|
return update
|
|
|
|
|
2021-07-30 00:25:15 +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-07-30 00:25:15 +00:00
|
|
|
# not implemented yet
|
2021-07-30 00:25:15 +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-07-30 00:25:15 +00:00
|
|
|
pass
|
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
def generate_pkg_nix(plug: LuaPlugin):
|
|
|
|
'''
|
|
|
|
Generate nix expression for a luarocks package
|
|
|
|
Our cache key associates "p.name-p.version" to its rockspec
|
|
|
|
'''
|
|
|
|
log.debug("Generating nix expression for %s", plug.name)
|
2022-05-14 13:53:02 +00:00
|
|
|
custom_env = os.environ.copy()
|
|
|
|
custom_env['LUAROCKS_CONFIG'] = LUAROCKS_CONFIG
|
2019-02-04 11:30:58 +00:00
|
|
|
|
2022-09-06 21:43:12 +00:00
|
|
|
# we add --dev else luarocks wont find all the "scm" (=dev) versions of the
|
|
|
|
# packages
|
|
|
|
# , "--dev"
|
|
|
|
cmd = [ "luarocks", "nix" ]
|
2021-07-30 00:25:15 +00:00
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
if plug.maintainers:
|
|
|
|
cmd.append(f"--maintainers={plug.maintainers}")
|
2021-07-30 00:25:15 +00:00
|
|
|
|
2021-08-16 15:44:40 +00:00
|
|
|
# if plug.server == "src":
|
|
|
|
if plug.src != "":
|
|
|
|
if plug.src is None:
|
|
|
|
msg = "src must be set when 'version' is set to \"src\" for package %s" % plug.name
|
|
|
|
log.error(msg)
|
|
|
|
raise RuntimeError(msg)
|
|
|
|
log.debug("Updating from source %s", plug.src)
|
|
|
|
cmd.append(plug.src)
|
|
|
|
# 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-07-30 00:25:15 +00:00
|
|
|
if plug.luaversion:
|
2022-09-06 21:43:12 +00:00
|
|
|
cmd.append(f"--lua-version={plug.luaversion}")
|
2021-07-30 00:25:15 +00:00
|
|
|
|
2021-08-16 15:44:40 +00:00
|
|
|
log.debug("running %s", ' '.join(cmd))
|
2022-05-14 13:53:02 +00:00
|
|
|
|
|
|
|
output = subprocess.check_output(cmd, env=custom_env, text=True)
|
2021-10-03 22:10:46 +00:00
|
|
|
output = "callPackage(" + output.strip() + ") {};\n\n"
|
2021-07-30 00:25:15 +00:00
|
|
|
return (plug, output)
|
2021-07-30 00:25:15 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
editor = LuaEditor("lua", ROOT, '',
|
2021-07-30 00:25:15 +00:00
|
|
|
default_in = ROOT.joinpath(PKG_LIST),
|
|
|
|
default_out = ROOT.joinpath(GENERATED_NIXFILE)
|
|
|
|
)
|
|
|
|
|
2023-04-14 20:02:17 +00:00
|
|
|
editor.run()
|
2019-02-04 11:30:58 +00:00
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
if __name__ == "__main__":
|
2019-02-04 11:30:58 +00:00
|
|
|
|
2021-07-30 00:25:15 +00:00
|
|
|
main()
|
2019-06-13 10:00:00 +00:00
|
|
|
|
2021-08-16 15:44:40 +00:00
|
|
|
# vim: set ft=python noet fdm=manual fenc=utf-8 ff=unix sts=0 sw=4 ts=4 :
|