3 # $ nix run nixpkgs#python3Packages.black -- update.py
5 # $ nix run nixpkgs#python3Packages.mypy -- update.py
7 # $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
13 from dataclasses
import dataclass
18 from multiprocessing
.dummy
import Pool
20 from pluginupdate
import update_plugins
, FetchConfig
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
30 PKG_LIST
= "maintainers/scripts/luarocks-packages.csv"
31 TMP_FILE
= "$(mktemp)"
32 GENERATED_NIXFILE
= "pkgs/development/lua-modules/generated-packages.nix"
34 HEADER
= """/* {GENERATED_NIXFILE} is an auto-generated file -- DO NOT EDIT!
35 Regenerate it with: nix run nixpkgs#luarocks-packages-updater
36 You can customize the generated packages in pkgs/development/lua-modules/overrides.nix
39 GENERATED_NIXFILE
=GENERATED_NIXFILE
44 /* GENERATED - do not edit this file */
51 """Name of the plugin, as seen on luarocks.org"""
53 """address to the git repository"""
55 """git reference (branch name/tag)"""
56 version
: Optional
[str]
57 """Set it to pin a package """
59 """luarocks.org registers packages under different manifests.
60 Its value can be 'http://luarocks.org/dev'
62 luaversion
: Optional
[str]
63 """Attribue of the lua interpreter if a package is available only for a specific lua version"""
64 maintainers
: Optional
[str]
65 """ Optional string listing maintainers separated by spaces"""
68 def normalized_name(self
) -> str:
69 return self
.name
.replace(".", "-")
72 # rename Editor to LangUpdate/ EcosystemUpdater
73 class LuaEditor(pluginupdate
.Editor
):
75 def create_parser(self
):
76 parser
= super().create_parser()
77 parser
.set_defaults(proc
=1)
80 def get_current_plugins(self
):
83 def load_plugin_spec(self
, input_file
) -> List
[LuaPlugin
]:
85 csvfilename
= input_file
86 log
.info("Loading package descriptions from %s", csvfilename
)
88 with
open(csvfilename
, newline
="") as csvfile
:
89 reader
= csv
.DictReader(
93 # name,server,version,luaversion,maintainers
94 plugin
= LuaPlugin(**row
)
95 luaPackages
.append(plugin
)
98 def update(self
, args
):
99 update_plugins(self
, args
)
101 def generate_nix(self
, results
: List
[Tuple
[LuaPlugin
, str]], outfilename
: str):
102 with tempfile
.NamedTemporaryFile("w+") as f
:
104 header2
= textwrap
.dedent(
106 { stdenv, lib, fetchurl, fetchgit, callPackage, ... } @ args:
112 for plugin
, nix_expr
in results
:
113 f
.write(f
"{plugin.normalized_name} = {nix_expr}")
117 # if everything went fine, move the generated file to its destination
118 # using copy since move doesn't work across disks
119 shutil
.copy(f
.name
, outfilename
)
121 print(f
"updated {outfilename}")
127 def get_update(self
, input_file
: str, outfile
: str, config
: FetchConfig
):
128 _prefetch
= generate_pkg_nix
130 def update() -> dict:
131 plugin_specs
= self
.load_plugin_spec(input_file
)
132 sorted_plugin_specs
= sorted(plugin_specs
, key
=lambda v
: v
.name
.lower())
135 pool
= Pool(processes
=config
.proc
)
136 results
= pool
.map(_prefetch
, sorted_plugin_specs
)
140 self
.generate_nix(results
, outfile
)
147 def rewrite_input(self
, input_file
: str, *args
, **kwargs
):
148 # vim plugin reads the file before update but that shouldn't be our case
149 # not implemented yet
150 # fieldnames = ['name', 'server', 'version', 'luaversion', 'maintainers']
151 # input_file = "toto.csv"
152 # with open(input_file, newline='') as csvfile:
153 # writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
154 # writer.writeheader()
156 # # name,server,version,luaversion,maintainers
157 # plugin = LuaPlugin(**row)
158 # luaPackages.append(plugin)
162 def generate_pkg_nix(plug
: LuaPlugin
):
164 Generate nix expression for a luarocks package
165 Our cache key associates "p.name-p.version" to its rockspec
167 log
.debug("Generating nix expression for %s", plug
.name
)
169 cmd
= ["luarocks", "nix"]
172 cmd
.append(f
"--maintainers={plug.maintainers}")
177 "src must be set when 'version' is set to \"src\" for package %s"
181 raise RuntimeError(msg
)
182 log
.debug("Updating from source %s", plug
.src
)
184 # update the plugin from luarocks
186 cmd
.append(plug
.name
)
187 if plug
.version
and plug
.version
!= "src":
188 cmd
.append(plug
.version
)
190 if plug
.server
!= "src" and plug
.server
:
191 cmd
.append(f
"--only-server={plug.server}")
194 cmd
.append(f
"--lua-version={plug.luaversion}")
195 luaver
= plug
.luaversion
.replace('.', '')
196 if luaver
:= os
.getenv(f
"LUA_{luaver}"):
197 cmd
.append(f
"--lua-dir={luaver}")
199 log
.debug("running %s", " ".join(cmd
))
201 output
= subprocess
.check_output(cmd
, text
=True)
202 output
= "callPackage(" + output
.strip() + ") {};\n\n"
203 return (plug
, output
)
212 default_out
=GENERATED_NIXFILE
,
218 if __name__
== "__main__":