at 22.05-pre 3.1 kB view raw
1#!/usr/bin/env nix-shell 2#!nix-shell update-shell.nix -i python3 3 4 5# format: 6# $ nix run nixpkgs.python3Packages.black -c black update.py 7# type-check: 8# $ nix run nixpkgs.python3Packages.mypy -c mypy update.py 9# linted: 10# $ nix run nixpkgs.python3Packages.flake8 -c flake8 --ignore E501,E265,E402 update.py 11 12# If you see `HTTP Error 429: too many requests` errors while running this script, 13# refer to: 14# 15# https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/vim.section.md#updating-plugins-in-nixpkgs-updating-plugins-in-nixpkgs 16 17import inspect 18import os 19import sys 20import logging 21import textwrap 22from typing import List, Tuple 23from pathlib import Path 24 25log = logging.getLogger() 26log.addHandler(logging.StreamHandler()) 27 28# Import plugin update library from maintainers/scripts/pluginupdate.py 29ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))) 30sys.path.insert(0, os.path.join(ROOT.parent.parent.parent, "maintainers", "scripts")) 31import pluginupdate 32 33GET_PLUGINS = f"""(with import <localpkgs> {{}}; 34let 35 inherit (vimUtils.override {{inherit vim;}}) buildVimPluginFrom2Nix; 36 generated = callPackage {ROOT}/generated.nix {{ 37 inherit buildVimPluginFrom2Nix; 38 }}; 39 hasChecksum = value: lib.isAttrs value && lib.hasAttrByPath ["src" "outputHash"] value; 40 getChecksum = name: value: 41 if hasChecksum value then {{ 42 submodules = value.src.fetchSubmodules or false; 43 sha256 = value.src.outputHash; 44 rev = value.src.rev; 45 }} else null; 46 checksums = lib.mapAttrs getChecksum generated; 47in lib.filterAttrs (n: v: v != null) checksums)""" 48 49HEADER = ( 50 "# This file has been generated by ./pkgs/misc/vim-plugins/update.py. Do not edit!" 51) 52 53 54class VimEditor(pluginupdate.Editor): 55 def generate_nix(self, plugins: List[Tuple[str, str, pluginupdate.Plugin]], outfile: str): 56 sorted_plugins = sorted(plugins, key=lambda v: v[2].name.lower()) 57 58 with open(outfile, "w+") as f: 59 f.write(HEADER) 60 f.write(textwrap.dedent(""" 61 { lib, buildVimPluginFrom2Nix, fetchFromGitHub }: 62 63 final: prev: 64 {""" 65 )) 66 for owner, repo, plugin in sorted_plugins: 67 if plugin.has_submodules: 68 submodule_attr = "\n fetchSubmodules = true;" 69 else: 70 submodule_attr = "" 71 72 f.write(textwrap.indent(textwrap.dedent( 73 f""" 74 {plugin.normalized_name} = buildVimPluginFrom2Nix {{ 75 pname = "{plugin.name}"; 76 version = "{plugin.version}"; 77 src = fetchFromGitHub {{ 78 owner = "{owner}"; 79 repo = "{repo}"; 80 rev = "{plugin.commit}"; 81 sha256 = "{plugin.sha256}";{submodule_attr} 82 }}; 83 meta.homepage = "https://github.com/{owner}/{repo}/"; 84 }}; 85""" 86 ), ' ')) 87 f.write("\n}\n") 88 print(f"updated {outfile}") 89 90 91 92def main(): 93 editor = VimEditor("vim", ROOT, GET_PLUGINS) 94 parser = editor.create_parser() 95 args = parser.parse_args() 96 pluginupdate.update_plugins(editor, args) 97 98 99if __name__ == "__main__": 100 main()