biglybt: 3.5.0.0 -> 3.6.0.0
[NixPkgs.git] / pkgs / development / lua-modules / updater / updater.py
blob407b4104b774cce1c7f720e4646e5d8133c0a686
1 #!/usr/bin/env python
2 # format:
3 # $ nix run nixpkgs#python3Packages.black -- update.py
4 # type-check:
5 # $ nix run nixpkgs#python3Packages.mypy -- update.py
6 # linted:
7 # $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
9 import inspect
10 import os
11 import tempfile
12 import shutil
13 from dataclasses import dataclass
14 import subprocess
15 import csv
16 import logging
17 import textwrap
18 from multiprocessing.dummy import Pool
19 import pluginupdate
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
38 """.format(
39 GENERATED_NIXFILE=GENERATED_NIXFILE
42 FOOTER = """
44 /* GENERATED - do not edit this file */
45 """
48 @dataclass
49 class LuaPlugin:
50 name: str
51 """Name of the plugin, as seen on luarocks.org"""
52 src: str
53 """address to the git repository"""
54 ref: Optional[str]
55 """git reference (branch name/tag)"""
56 version: Optional[str]
57 """Set it to pin a package """
58 server: Optional[str]
59 """luarocks.org registers packages under different manifests.
60 Its value can be 'http://luarocks.org/dev'
61 """
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"""
67 @property
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)
78 return parser
80 def get_current_plugins(self):
81 return []
83 def load_plugin_spec(self, input_file) -> List[LuaPlugin]:
84 luaPackages = []
85 csvfilename = input_file
86 log.info("Loading package descriptions from %s", csvfilename)
88 with open(csvfilename, newline="") as csvfile:
89 reader = csv.DictReader(
90 csvfile,
92 for row in reader:
93 # name,server,version,luaversion,maintainers
94 plugin = LuaPlugin(**row)
95 luaPackages.append(plugin)
96 return luaPackages
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:
103 f.write(HEADER)
104 header2 = textwrap.dedent(
106 { stdenv, lib, fetchurl, fetchgit, callPackage, ... } @ args:
107 final: prev:
111 f.write(header2)
112 for plugin, nix_expr in results:
113 f.write(f"{plugin.normalized_name} = {nix_expr}")
114 f.write(FOOTER)
115 f.flush()
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}")
123 @property
124 def attr_path(self):
125 return "luaPackages"
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())
134 try:
135 pool = Pool(processes=config.proc)
136 results = pool.map(_prefetch, sorted_plugin_specs)
137 finally:
138 pass
140 self.generate_nix(results, outfile)
142 redirects = {}
143 return redirects
145 return update
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()
155 # for row in reader:
156 # # name,server,version,luaversion,maintainers
157 # plugin = LuaPlugin(**row)
158 # luaPackages.append(plugin)
159 pass
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"]
171 if plug.maintainers:
172 cmd.append(f"--maintainers={plug.maintainers}")
174 if plug.src != "":
175 if plug.src is None:
176 msg = (
177 "src must be set when 'version' is set to \"src\" for package %s"
178 % plug.name
180 log.error(msg)
181 raise RuntimeError(msg)
182 log.debug("Updating from source %s", plug.src)
183 cmd.append(plug.src)
184 # update the plugin from luarocks
185 else:
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}")
193 if plug.luaversion:
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)
206 def main():
207 editor = LuaEditor(
208 "lua",
209 ROOT,
211 default_in=PKG_LIST,
212 default_out=GENERATED_NIXFILE,
215 editor.run()
218 if __name__ == "__main__":
219 main()