pluginupdate.py: reformat with ruff

Ruff - an extremely fast Python linter and code formatter, written in Rust.
This commit is contained in:
PerchunPak
2024-11-09 00:14:32 +01:00
parent 9c075c8fa5
commit 8b503ec432
4 changed files with 86 additions and 73 deletions

View File

@@ -4,7 +4,7 @@
# - pkgs/development/lua-modules/updater/updater.py # - pkgs/development/lua-modules/updater/updater.py
# format: # format:
# $ nix run nixpkgs#black maintainers/scripts/pluginupdate.py # $ nix run nixpkgs#ruff maintainers/scripts/pluginupdate.py
# type-check: # type-check:
# $ nix run nixpkgs#python3.pkgs.mypy maintainers/scripts/pluginupdate.py # $ nix run nixpkgs#python3.pkgs.mypy maintainers/scripts/pluginupdate.py
# linted: # linted:
@@ -195,7 +195,7 @@ class RepoGitHub(Repo):
xml = req.read() xml = req.read()
# Filter out illegal XML characters # Filter out illegal XML characters
illegal_xml_regex = re.compile(b"[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]") illegal_xml_regex = re.compile(b"[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]")
xml = illegal_xml_regex.sub(b"", xml) xml = illegal_xml_regex.sub(b"", xml)
root = ET.fromstring(xml) root = ET.fromstring(xml)
@@ -328,12 +328,11 @@ def load_plugins_from_csv(
return plugins return plugins
def run_nix_expr(expr, nixpkgs: str, **args): def run_nix_expr(expr, nixpkgs: str, **args):
''' """
:param expr nix expression to fetch current plugins :param expr nix expression to fetch current plugins
:param nixpkgs Path towards a nixpkgs checkout :param nixpkgs Path towards a nixpkgs checkout
''' """
with CleanEnvironment(nixpkgs) as nix_path: with CleanEnvironment(nixpkgs) as nix_path:
cmd = [ cmd = [
"nix", "nix",
@@ -624,7 +623,7 @@ def print_download_error(plugin: PluginDesc, ex: Exception):
def check_results( def check_results(
results: List[Tuple[PluginDesc, Union[Exception, Plugin], Optional[Repo]]] results: List[Tuple[PluginDesc, Union[Exception, Plugin], Optional[Repo]]],
) -> Tuple[List[Tuple[PluginDesc, Plugin]], Redirects]: ) -> Tuple[List[Tuple[PluginDesc, Plugin]], Redirects]:
""" """ """ """
failures: List[Tuple[PluginDesc, Exception]] = [] failures: List[Tuple[PluginDesc, Exception]] = []
@@ -792,9 +791,11 @@ def update_plugins(editor: Editor, args):
log.info("Start updating plugins") log.info("Start updating plugins")
if args.proc > 1 and args.github_token == None: if args.proc > 1 and args.github_token == None:
log.warning("You have enabled parallel updates but haven't set a github token.\n" log.warning(
"You may be hit with `HTTP Error 429: too many requests` as a consequence." "You have enabled parallel updates but haven't set a github token.\n"
"Either set --proc=1 or --github-token=YOUR_TOKEN. ") "You may be hit with `HTTP Error 429: too many requests` as a consequence."
"Either set --proc=1 or --github-token=YOUR_TOKEN. "
)
fetch_config = FetchConfig(args.proc, args.github_token) fetch_config = FetchConfig(args.proc, args.github_token)
update = editor.get_update(args.input_file, args.outfile, fetch_config) update = editor.get_update(args.input_file, args.outfile, fetch_config)
@@ -810,11 +811,9 @@ def update_plugins(editor: Editor, args):
if autocommit: if autocommit:
try: try:
repo = git.Repo(os.getcwd()) repo = git.Repo(os.getcwd())
updated = datetime.now(tz=UTC).strftime('%Y-%m-%d') updated = datetime.now(tz=UTC).strftime("%Y-%m-%d")
print(args.outfile) print(args.outfile)
commit(repo, commit(repo, f"{editor.attr_path}: update on {updated}", [args.outfile])
f"{editor.attr_path}: update on {updated}", [args.outfile]
)
except git.InvalidGitRepositoryError as e: except git.InvalidGitRepositoryError as e:
print(f"Not in a git repository: {e}", file=sys.stderr) print(f"Not in a git repository: {e}", file=sys.stderr)
sys.exit(1) sys.exit(1)

View File

@@ -2,47 +2,63 @@
#!nix-shell update-shell.nix -i python3 #!nix-shell update-shell.nix -i python3
# format: # format:
# $ nix run nixpkgs.python3Packages.black -c black update.py # $ nix run nixpkgs#python3Packages.ruff -- update.py
# type-check: # type-check:
# $ nix run nixpkgs.python3Packages.mypy -c mypy update.py # $ nix run nixpkgs#python3Packages.mypy -- update.py
# linted: # linted:
# $ nix run nixpkgs.python3Packages.flake8 -c flake8 --ignore E501,E265,E402 update.py # $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
import inspect import inspect
import os import os
import sys import sys
from typing import List, Tuple
from pathlib import Path from pathlib import Path
from typing import List, Tuple
# Import plugin update library from maintainers/scripts/pluginupdate.py # Import plugin update library from maintainers/scripts/pluginupdate.py
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))) # type: ignore ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))) # type: ignore
sys.path.insert( sys.path.insert(
0, os.path.join(ROOT.parent.parent.parent.parent.parent, "maintainers", "scripts") 0, os.path.join(ROOT.parent.parent.parent.parent.parent, "maintainers", "scripts")
) )
import pluginupdate import pluginupdate
GET_PLUGINS = f"""(with import <localpkgs> {{}}; GET_PLUGINS = f"""(
with import <localpkgs> {{ }};
let let
inherit (kakouneUtils.override {{}}) buildKakounePluginFrom2Nix; inherit (kakouneUtils.override {{ }}) buildKakounePluginFrom2Nix;
generated = callPackage {ROOT}/generated.nix {{ generated = callPackage {ROOT}/generated.nix {{
inherit buildKakounePluginFrom2Nix; inherit buildKakounePluginFrom2Nix;
}}; }};
hasChecksum = value: lib.isAttrs value && lib.hasAttrByPath ["src" "outputHash"] value; hasChecksum =
getChecksum = name: value: value:
if hasChecksum value then {{ lib.isAttrs value
submodules = value.src.fetchSubmodules or false; && lib.hasAttrByPath [
sha256 = value.src.outputHash; "src"
rev = value.src.rev; "outputHash"
}} else null; ] value;
getChecksum =
name: value:
if hasChecksum value then
{{
submodules = value.src.fetchSubmodules or false;
sha256 = value.src.outputHash;
rev = value.src.rev;
}}
else
null;
checksums = lib.mapAttrs getChecksum generated; checksums = lib.mapAttrs getChecksum generated;
in lib.filterAttrs (n: v: v != null) checksums)""" in
lib.filterAttrs (n: v: v != null) checksums
)"""
HEADER = "# This file has been generated by ./pkgs/applications/editors/kakoune/plugins/update.py. Do not edit!" HEADER = "# This file has been generated by ./pkgs/applications/editors/kakoune/plugins/update.py. Do not edit!"
class KakouneEditor(pluginupdate.Editor): class KakouneEditor(pluginupdate.Editor):
def generate_nix(
self,
def generate_nix(self, plugins: List[Tuple[pluginupdate.PluginDesc, pluginupdate.Plugin]], outfile: str): plugins: List[Tuple[pluginupdate.PluginDesc, pluginupdate.Plugin]],
outfile: str,
):
sorted_plugins = sorted(plugins, key=lambda v: v[1].name.lower()) sorted_plugins = sorted(plugins, key=lambda v: v[1].name.lower())
with open(outfile, "w+") as f: with open(outfile, "w+") as f:

View File

@@ -3,7 +3,7 @@
# run with: # run with:
# $ nix run .\#vimPluginsUpdater # $ nix run .\#vimPluginsUpdater
# format: # format:
# $ nix run nixpkgs#python3Packages.black -- update.py # $ nix run nixpkgs#python3Packages.ruff -- update.py
# type-check: # type-check:
# $ nix run nixpkgs#python3Packages.mypy -- update.py # $ nix run nixpkgs#python3Packages.mypy -- update.py
# linted: # linted:
@@ -19,14 +19,13 @@
# #
import inspect import inspect
import os
import logging
import textwrap
import json import json
import logging
import os
import subprocess import subprocess
from typing import List, Tuple import textwrap
from pathlib import Path from pathlib import Path
from typing import List, Tuple
log = logging.getLogger("vim-updater") log = logging.getLogger("vim-updater")
@@ -37,12 +36,12 @@ log.addHandler(sh)
# Import plugin update library from maintainers/scripts/pluginupdate.py # Import plugin update library from maintainers/scripts/pluginupdate.py
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))) ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
import pluginupdate
import importlib import importlib
from pluginupdate import run_nix_expr, PluginDesc
treesitter = importlib.import_module('nvim-treesitter.update') import pluginupdate
from pluginupdate import PluginDesc, run_nix_expr
treesitter = importlib.import_module("nvim-treesitter.update")
HEADER = ( HEADER = (
@@ -56,9 +55,7 @@ class VimEditor(pluginupdate.Editor):
nvim_treesitter_updated = False nvim_treesitter_updated = False
def generate_nix( def generate_nix(
self, self, plugins: List[Tuple[PluginDesc, pluginupdate.Plugin]], outfile: str
plugins: List[Tuple[PluginDesc, pluginupdate.Plugin]],
outfile: str
): ):
log.info("Generating nix code") log.info("Generating nix code")
sorted_plugins = sorted(plugins, key=lambda v: v[0].name.lower()) sorted_plugins = sorted(plugins, key=lambda v: v[0].name.lower())
@@ -66,7 +63,7 @@ class VimEditor(pluginupdate.Editor):
nvim_treesitter_rev = pluginupdate.run_nix_expr( nvim_treesitter_rev = pluginupdate.run_nix_expr(
"(import <localpkgs> { }).vimPlugins.nvim-treesitter.src.rev", "(import <localpkgs> { }).vimPlugins.nvim-treesitter.src.rev",
self.nixpkgs, self.nixpkgs,
timeout=10 timeout=10,
) )
GET_PLUGINS_LUA = """ GET_PLUGINS_LUA = """
@@ -109,8 +106,9 @@ class VimEditor(pluginupdate.Editor):
f.write("\n}\n") f.write("\n}\n")
print(f"updated {outfile}") print(f"updated {outfile}")
def plugin2nix(self, pdesc: PluginDesc, plugin: pluginupdate.Plugin, isNeovim: bool) -> str: def plugin2nix(
self, pdesc: PluginDesc, plugin: pluginupdate.Plugin, isNeovim: bool
) -> str:
repo = pdesc.repo repo = pdesc.repo
content = f" {plugin.normalized_name} = " content = f" {plugin.normalized_name} = "
@@ -138,19 +136,25 @@ class VimEditor(pluginupdate.Editor):
if self.nvim_treesitter_updated: if self.nvim_treesitter_updated:
print("updating nvim-treesitter grammars") print("updating nvim-treesitter grammars")
cmd = [ cmd = [
"nix", "build", "nix",
"vimPlugins.nvim-treesitter.src", "-f", self.nixpkgs "build",
, "--print-out-paths" "vimPlugins.nvim-treesitter.src",
"-f",
self.nixpkgs,
"--print-out-paths",
] ]
log.debug("Running command: %s", " ".join(cmd)) log.debug("Running command: %s", " ".join(cmd))
nvim_treesitter_dir = subprocess.check_output(cmd, text=True, timeout=90).strip() nvim_treesitter_dir = subprocess.check_output(
cmd, text=True, timeout=90
).strip()
generated = treesitter.update_grammars(nvim_treesitter_dir) generated = treesitter.update_grammars(nvim_treesitter_dir)
treesitter_generated_nix_path = os.path.join( treesitter_generated_nix_path = os.path.join(
NIXPKGS_NVIMTREESITTER_FOLDER, NIXPKGS_NVIMTREESITTER_FOLDER, "generated.nix"
"generated.nix" )
open(os.path.join(args.nixpkgs, treesitter_generated_nix_path), "w").write(
generated
) )
open(os.path.join(args.nixpkgs, treesitter_generated_nix_path), "w").write(generated)
if self.nixpkgs_repo: if self.nixpkgs_repo:
index = self.nixpkgs_repo.index index = self.nixpkgs_repo.index

View File

@@ -1,26 +1,26 @@
#!/usr/bin/env python #!/usr/bin/env python
# format: # format:
# $ nix run nixpkgs#python3Packages.black -- update.py # $ nix run nixpkgs#python3Packages.ruff -- update.py
# type-check: # type-check:
# $ nix run nixpkgs#python3Packages.mypy -- update.py # $ nix run nixpkgs#python3Packages.mypy -- update.py
# linted: # linted:
# $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py # $ nix run nixpkgs#python3Packages.flake8 -- --ignore E501,E265,E402 update.py
import inspect
import os
import tempfile
import shutil
from dataclasses import dataclass
import subprocess
import csv import csv
import inspect
import logging import logging
import os
import shutil
import subprocess
import tempfile
import textwrap import textwrap
from dataclasses import dataclass
from multiprocessing.dummy import Pool from multiprocessing.dummy import Pool
import pluginupdate
from pluginupdate import update_plugins, FetchConfig
from typing import List, Tuple, Optional
from pathlib import Path from pathlib import Path
from typing import List, Optional, Tuple
import pluginupdate
from pluginupdate import FetchConfig, update_plugins
log = logging.getLogger() log = logging.getLogger()
log.addHandler(logging.StreamHandler()) log.addHandler(logging.StreamHandler())
@@ -35,9 +35,7 @@ HEADER = """/* {GENERATED_NIXFILE} is an auto-generated file -- DO NOT EDIT!
Regenerate it with: nix run nixpkgs#luarocks-packages-updater Regenerate it with: nix run nixpkgs#luarocks-packages-updater
You can customize the generated packages in pkgs/development/lua-modules/overrides.nix You can customize the generated packages in pkgs/development/lua-modules/overrides.nix
*/ */
""".format( """.format(GENERATED_NIXFILE=GENERATED_NIXFILE)
GENERATED_NIXFILE=GENERATED_NIXFILE
)
FOOTER = """ FOOTER = """
} }
@@ -71,7 +69,6 @@ class LuaPlugin:
# rename Editor to LangUpdate/ EcosystemUpdater # rename Editor to LangUpdate/ EcosystemUpdater
class LuaEditor(pluginupdate.Editor): class LuaEditor(pluginupdate.Editor):
def create_parser(self): def create_parser(self):
parser = super().create_parser() parser = super().create_parser()
parser.set_defaults(proc=1) parser.set_defaults(proc=1)
@@ -173,10 +170,7 @@ def generate_pkg_nix(plug: LuaPlugin):
if plug.rockspec != "": if plug.rockspec != "":
if plug.ref or plug.version: if plug.ref or plug.version:
msg = ( msg = "'version' and 'ref' will be ignored as the rockspec is hardcoded for package %s" % plug.name
"'version' and 'ref' will be ignored as the rockspec is hardcoded for package %s"
% plug.name
)
log.warning(msg) log.warning(msg)
log.debug("Updating from rockspec %s", plug.rockspec) log.debug("Updating from rockspec %s", plug.rockspec)
@@ -193,7 +187,7 @@ def generate_pkg_nix(plug: LuaPlugin):
if plug.luaversion: if plug.luaversion:
cmd.append(f"--lua-version={plug.luaversion}") cmd.append(f"--lua-version={plug.luaversion}")
luaver = plug.luaversion.replace('.', '') luaver = plug.luaversion.replace(".", "")
if luaver := os.getenv(f"LUA_{luaver}"): if luaver := os.getenv(f"LUA_{luaver}"):
cmd.append(f"--lua-dir={luaver}") cmd.append(f"--lua-dir={luaver}")