1 #!/usr/bin/env nix-shell
2 #!nix-shell update-luarocks-shell.nix -i python3
5 # $ nix run nixpkgs.python3Packages.black -c black update.py
7 # $ nix run nixpkgs.python3Packages.mypy -c mypy update.py
9 # $ nix run nixpkgs.python3Packages.flake8 -c flake8 --ignore E501,E265,E402 update.py
15 from dataclasses import dataclass
20 from multiprocessing.dummy import Pool
22 from typing import List, Tuple, Optional
23 from pathlib import Path
25 log = logging.getLogger()
26 log.addHandler(logging.StreamHandler())
28 ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))).parent.parent # type: ignore
29 from pluginupdate import Editor, update_plugins, FetchConfig, CleanEnvironment
31 PKG_LIST="maintainers/scripts/luarocks-packages.csv"
33 GENERATED_NIXFILE="pkgs/development/lua-modules/generated-packages.nix"
34 LUAROCKS_CONFIG="maintainers/scripts/luarocks-config.lua"
36 HEADER = """/* {GENERATED_NIXFILE} is an auto-generated file -- DO NOT EDIT!
38 nixpkgs$ ./maintainers/scripts/update-luarocks-packages
40 You can customize the generated packages in pkgs/development/lua-modules/overrides.nix
42 """.format(GENERATED_NIXFILE=GENERATED_NIXFILE)
46 /* GENERATED - do not edit this file */
52 '''Name of the plugin, as seen on luarocks.org'''
54 '''address to the git repository'''
56 '''git reference (branch name/tag)'''
57 version: Optional[str]
58 '''Set it to pin a package '''
60 '''luarocks.org registers packages under different manifests.
61 Its value can be 'http://luarocks.org/dev'
63 luaversion: Optional[str]
64 '''Attribue of the lua interpreter if a package is available only for a specific lua version'''
65 maintainers: Optional[str]
66 ''' Optional string listing maintainers separated by spaces'''
69 def normalized_name(self) -> str:
70 return self.name.replace(".", "-")
72 # rename Editor to LangUpdate/ EcosystemUpdater
73 class LuaEditor(Editor):
74 def get_current_plugins(self):
77 def load_plugin_spec(self, input_file) -> List[LuaPlugin]:
79 csvfilename=input_file
80 log.info("Loading package descriptions from %s", csvfilename)
82 with open(csvfilename, newline='') as csvfile:
83 reader = csv.DictReader(csvfile,)
85 # name,server,version,luaversion,maintainers
86 plugin = LuaPlugin(**row)
87 luaPackages.append(plugin)
92 results: List[Tuple[LuaPlugin, str]],
96 with tempfile.NamedTemporaryFile("w+") as f:
98 header2 = textwrap.dedent(
99 # header2 = inspect.cleandoc(
101 { self, stdenv, lib, fetchurl, fetchgit, callPackage, ... } @ args:
106 for (plugin, nix_expr) in results:
107 f.write(f"{plugin.normalized_name} = {nix_expr}")
111 # if everything went fine, move the generated file to its destination
112 # using copy since move doesn't work across disks
113 shutil.copy(f.name, outfilename)
115 print(f"updated {outfilename}")
121 def get_update(self, input_file: str, outfile: str, config: FetchConfig):
122 _prefetch = generate_pkg_nix
124 def update() -> dict:
125 plugin_specs = self.load_plugin_spec(input_file)
126 sorted_plugin_specs = sorted(plugin_specs, key=lambda v: v.name.lower())
129 pool = Pool(processes=config.proc)
130 results = pool.map(_prefetch, sorted_plugin_specs)
134 self.generate_nix(results, outfile)
141 def rewrite_input(self, input_file: str, *args, **kwargs):
142 # vim plugin reads the file before update but that shouldn't be our case
143 # not implemented yet
144 # fieldnames = ['name', 'server', 'version', 'luaversion', 'maintainers']
145 # input_file = "toto.csv"
146 # with open(input_file, newline='') as csvfile:
147 # writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
148 # writer.writeheader()
150 # # name,server,version,luaversion,maintainers
151 # plugin = LuaPlugin(**row)
152 # luaPackages.append(plugin)
155 def generate_pkg_nix(plug: LuaPlugin):
157 Generate nix expression for a luarocks package
158 Our cache key associates "p.name-p.version" to its rockspec
160 log.debug("Generating nix expression for %s", plug.name)
161 custom_env = os.environ.copy()
162 custom_env['LUAROCKS_CONFIG'] = LUAROCKS_CONFIG
164 # we add --dev else luarocks wont find all the "scm" (=dev) versions of the
167 cmd = [ "luarocks", "nix" ]
170 cmd.append(f"--maintainers={plug.maintainers}")
172 # if plug.server == "src":
175 msg = "src must be set when 'version' is set to \"src\" for package %s" % plug.name
177 raise RuntimeError(msg)
178 log.debug("Updating from source %s", plug.src)
180 # update the plugin from luarocks
182 cmd.append(plug.name)
183 if plug.version and plug.version != "src":
185 cmd.append(plug.version)
187 if plug.server != "src" and plug.server:
188 cmd.append(f"--only-server={plug.server}")
191 cmd.append(f"--lua-version={plug.luaversion}")
193 log.debug("running %s", ' '.join(cmd))
195 output = subprocess.check_output(cmd, env=custom_env, text=True)
196 output = "callPackage(" + output.strip() + ") {};\n\n"
197 return (plug, output)
201 editor = LuaEditor("lua", ROOT, '',
202 default_in = ROOT.joinpath(PKG_LIST),
203 default_out = ROOT.joinpath(GENERATED_NIXFILE)
206 parser = editor.create_parser()
207 args = parser.parse_args()
209 update_plugins(editor, args)
212 if __name__ == "__main__":
216 # vim: set ft=python noet fdm=manual fenc=utf-8 ff=unix sts=0 sw=4 ts=4 :