pytrainer: unpin python 3.10
[NixPkgs.git] / pkgs / applications / editors / vim / plugins / update.py
bloba8525b9018b57b34bc898e1afcdce92f22154ec2
1 #!/usr/bin/env python
3 # run with:
4 # $ nix run .\#vimPluginsUpdater
5 # format:
6 # $ nix run nixpkgs#python3Packages.black -- update.py
7 # type-check:
8 # $ nix run nixpkgs#python3Packages.mypy -- update.py
9 # linted:
10 # $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
12 # If you see `HTTP Error 429: too many requests` errors while running this
13 # script, refer to:
15 # https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md#updating-plugins-in-nixpkgs-updating-plugins-in-nixpkgs
17 # (or the equivalent file /doc/languages-frameworks/vim.section.md
18 # from Nixpkgs master tree).
21 import inspect
22 import os
23 import logging
24 import textwrap
25 import json
26 import subprocess
27 from typing import List, Tuple
28 from pathlib import Path
31 log = logging.getLogger("vim-updater")
33 sh = logging.StreamHandler()
34 formatter = logging.Formatter("%(name)s:%(levelname)s: %(message)s")
35 sh.setFormatter(formatter)
36 log.addHandler(sh)
38 # Import plugin update library from maintainers/scripts/pluginupdate.py
39 ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
40 import pluginupdate
41 import importlib
42 from pluginupdate import run_nix_expr, PluginDesc
44 treesitter = importlib.import_module('nvim-treesitter.update')
48 HEADER = (
49 "# GENERATED by ./pkgs/applications/editors/vim/plugins/update.py. Do not edit!"
52 NIXPKGS_NVIMTREESITTER_FOLDER = "pkgs/applications/editors/vim/plugins/nvim-treesitter"
55 class VimEditor(pluginupdate.Editor):
56 nvim_treesitter_updated = False
58 def generate_nix(
59 self,
60 plugins: List[Tuple[PluginDesc, pluginupdate.Plugin]],
61 outfile: str
63 log.info("Generating nix code")
64 sorted_plugins = sorted(plugins, key=lambda v: v[0].name.lower())
65 log.debug("Loading nvim-treesitter revision from nix...")
66 nvim_treesitter_rev = pluginupdate.run_nix_expr(
67 "(import <localpkgs> { }).vimPlugins.nvim-treesitter.src.rev",
68 self.nixpkgs,
69 timeout=10
72 GET_PLUGINS_LUA = """
73 with import <localpkgs> {};
74 lib.attrNames lua51Packages"""
75 log.debug("Loading list of lua plugins...")
76 luaPlugins = run_nix_expr(GET_PLUGINS_LUA, self.nixpkgs, timeout=30)
78 def _isNeovimPlugin(plug: pluginupdate.Plugin) -> bool:
79 """
80 Whether it's a neovim-only plugin
81 We can check if it's available in lua packages
82 """
83 if plug.normalized_name in luaPlugins:
84 log.debug("%s is a neovim plugin", plug)
85 return True
86 return False
88 with open(outfile, "w+") as f:
89 log.debug("Writing to %s", outfile)
90 f.write(HEADER)
91 f.write(
92 textwrap.dedent(
93 """
94 { lib, buildVimPlugin, buildNeovimPlugin, fetchFromGitHub, fetchgit }:
96 final: prev:
98 """
101 for pdesc, plugin in sorted_plugins:
102 content = self.plugin2nix(pdesc, plugin, _isNeovimPlugin(plugin))
103 f.write(content)
104 if (
105 plugin.name == "nvim-treesitter"
106 and plugin.commit != nvim_treesitter_rev
108 self.nvim_treesitter_updated = True
109 f.write("\n}\n")
110 print(f"updated {outfile}")
112 def plugin2nix(self, pdesc: PluginDesc, plugin: pluginupdate.Plugin, isNeovim: bool) -> str:
114 repo = pdesc.repo
116 content = f" {plugin.normalized_name} = "
117 src_nix = repo.as_nix(plugin)
118 content += """{buildFn} {{
119 pname = "{plugin.name}";
120 version = "{plugin.version}";
121 src = {src_nix};
122 meta.homepage = "{repo.uri}";
125 """.format(
126 buildFn="buildNeovimPlugin" if isNeovim else "buildVimPlugin",
127 plugin=plugin,
128 src_nix=src_nix,
129 repo=repo,
131 log.debug(content)
132 return content
134 def update(self, args):
135 pluginupdate.update_plugins(self, args)
137 # TODO this should probably be skipped when running outside a nixpkgs checkout
138 if self.nvim_treesitter_updated:
139 print("updating nvim-treesitter grammars")
140 cmd = [
141 "nix", "build",
142 "vimPlugins.nvim-treesitter.src", "-f", self.nixpkgs
143 , "--print-out-paths"
145 log.debug("Running command: %s", " ".join(cmd))
146 nvim_treesitter_dir = subprocess.check_output(cmd, text=True, timeout=90).strip()
148 generated = treesitter.update_grammars(nvim_treesitter_dir)
149 treesitter_generated_nix_path = os.path.join(
150 NIXPKGS_NVIMTREESITTER_FOLDER,
151 "generated.nix"
153 open(os.path.join(args.nixpkgs, treesitter_generated_nix_path), "w").write(generated)
155 if self.nixpkgs_repo:
156 index = self.nixpkgs_repo.index
157 for diff in index.diff(None):
158 if diff.a_path == treesitter_generated_nix_path:
159 msg = "vimPlugins.nvim-treesitter: update grammars"
160 print(f"committing to nixpkgs: {msg}")
161 index.add([treesitter_generated_nix_path])
162 index.commit(msg)
163 return
164 print("no updates to nvim-treesitter grammars")
167 def main():
168 global luaPlugins
170 log.debug(f"Loading from {ROOT}/get-plugins.nix")
171 with open(f"{ROOT}/get-plugins.nix") as f:
172 GET_PLUGINS = f.read()
173 editor = VimEditor(
174 "vim", Path("pkgs/applications/editors/vim/plugins"), GET_PLUGINS
176 editor.run()
179 if __name__ == "__main__":
180 main()