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
# format:
# $ nix run nixpkgs#black maintainers/scripts/pluginupdate.py
# $ nix run nixpkgs#ruff maintainers/scripts/pluginupdate.py
# type-check:
# $ nix run nixpkgs#python3.pkgs.mypy maintainers/scripts/pluginupdate.py
# linted:
@@ -195,7 +195,7 @@ class RepoGitHub(Repo):
xml = req.read()
# 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)
root = ET.fromstring(xml)
@@ -328,12 +328,11 @@ def load_plugins_from_csv(
return plugins
def run_nix_expr(expr, nixpkgs: str, **args):
'''
"""
:param expr nix expression to fetch current plugins
:param nixpkgs Path towards a nixpkgs checkout
'''
"""
with CleanEnvironment(nixpkgs) as nix_path:
cmd = [
"nix",
@@ -624,7 +623,7 @@ def print_download_error(plugin: PluginDesc, ex: Exception):
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]:
""" """
failures: List[Tuple[PluginDesc, Exception]] = []
@@ -792,9 +791,11 @@ def update_plugins(editor: Editor, args):
log.info("Start updating plugins")
if args.proc > 1 and args.github_token == None:
log.warning("You have enabled parallel updates but haven't set a github token.\n"
"You may be hit with `HTTP Error 429: too many requests` as a consequence."
"Either set --proc=1 or --github-token=YOUR_TOKEN. ")
log.warning(
"You have enabled parallel updates but haven't set a github token.\n"
"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)
update = editor.get_update(args.input_file, args.outfile, fetch_config)
@@ -810,11 +811,9 @@ def update_plugins(editor: Editor, args):
if autocommit:
try:
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)
commit(repo,
f"{editor.attr_path}: update on {updated}", [args.outfile]
)
commit(repo, f"{editor.attr_path}: update on {updated}", [args.outfile])
except git.InvalidGitRepositoryError as e:
print(f"Not in a git repository: {e}", file=sys.stderr)
sys.exit(1)

View File

@@ -2,47 +2,63 @@
#!nix-shell update-shell.nix -i python3
# format:
# $ nix run nixpkgs.python3Packages.black -c black update.py
# $ nix run nixpkgs#python3Packages.ruff -- update.py
# type-check:
# $ nix run nixpkgs.python3Packages.mypy -c mypy update.py
# $ nix run nixpkgs#python3Packages.mypy -- update.py
# 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 os
import sys
from typing import List, Tuple
from pathlib import Path
from typing import List, Tuple
# 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(
0, os.path.join(ROOT.parent.parent.parent.parent.parent, "maintainers", "scripts")
)
import pluginupdate
GET_PLUGINS = f"""(with import <localpkgs> {{}};
GET_PLUGINS = f"""(
with import <localpkgs> {{ }};
let
inherit (kakouneUtils.override {{}}) buildKakounePluginFrom2Nix;
inherit (kakouneUtils.override {{ }}) buildKakounePluginFrom2Nix;
generated = callPackage {ROOT}/generated.nix {{
inherit buildKakounePluginFrom2Nix;
}};
hasChecksum = value: lib.isAttrs value && lib.hasAttrByPath ["src" "outputHash"] value;
getChecksum = name: value:
if hasChecksum value then {{
submodules = value.src.fetchSubmodules or false;
sha256 = value.src.outputHash;
rev = value.src.rev;
}} else null;
hasChecksum =
value:
lib.isAttrs value
&& lib.hasAttrByPath [
"src"
"outputHash"
] 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;
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!"
class KakouneEditor(pluginupdate.Editor):
def generate_nix(self, plugins: List[Tuple[pluginupdate.PluginDesc, pluginupdate.Plugin]], outfile: str):
def generate_nix(
self,
plugins: List[Tuple[pluginupdate.PluginDesc, pluginupdate.Plugin]],
outfile: str,
):
sorted_plugins = sorted(plugins, key=lambda v: v[1].name.lower())
with open(outfile, "w+") as f:

View File

@@ -3,7 +3,7 @@
# run with:
# $ nix run .\#vimPluginsUpdater
# format:
# $ nix run nixpkgs#python3Packages.black -- update.py
# $ nix run nixpkgs#python3Packages.ruff -- update.py
# type-check:
# $ nix run nixpkgs#python3Packages.mypy -- update.py
# linted:
@@ -19,14 +19,13 @@
#
import inspect
import os
import logging
import textwrap
import json
import logging
import os
import subprocess
from typing import List, Tuple
import textwrap
from pathlib import Path
from typing import List, Tuple
log = logging.getLogger("vim-updater")
@@ -37,12 +36,12 @@ log.addHandler(sh)
# Import plugin update library from maintainers/scripts/pluginupdate.py
ROOT = Path(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
import pluginupdate
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 = (
@@ -56,9 +55,7 @@ class VimEditor(pluginupdate.Editor):
nvim_treesitter_updated = False
def generate_nix(
self,
plugins: List[Tuple[PluginDesc, pluginupdate.Plugin]],
outfile: str
self, plugins: List[Tuple[PluginDesc, pluginupdate.Plugin]], outfile: str
):
log.info("Generating nix code")
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(
"(import <localpkgs> { }).vimPlugins.nvim-treesitter.src.rev",
self.nixpkgs,
timeout=10
timeout=10,
)
GET_PLUGINS_LUA = """
@@ -109,8 +106,9 @@ class VimEditor(pluginupdate.Editor):
f.write("\n}\n")
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
content = f" {plugin.normalized_name} = "
@@ -138,19 +136,25 @@ class VimEditor(pluginupdate.Editor):
if self.nvim_treesitter_updated:
print("updating nvim-treesitter grammars")
cmd = [
"nix", "build",
"vimPlugins.nvim-treesitter.src", "-f", self.nixpkgs
, "--print-out-paths"
"nix",
"build",
"vimPlugins.nvim-treesitter.src",
"-f",
self.nixpkgs,
"--print-out-paths",
]
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)
treesitter_generated_nix_path = os.path.join(
NIXPKGS_NVIMTREESITTER_FOLDER,
"generated.nix"
NIXPKGS_NVIMTREESITTER_FOLDER, "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:
index = self.nixpkgs_repo.index

View File

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