2023-04-14 21:02:17 +01:00
|
|
|
# python library used to update plugins:
|
|
|
|
# - pkgs/applications/editors/vim/plugins/update.py
|
|
|
|
# - pkgs/applications/editors/kakoune/plugins/update.py
|
2024-04-04 15:42:28 +01:00
|
|
|
# - pkgs/development/lua-modules/updater/updater.py
|
2018-09-09 23:19:41 +01:00
|
|
|
|
|
|
|
# format:
|
2023-08-12 16:41:04 +01:00
|
|
|
# $ nix run nixpkgs#black maintainers/scripts/pluginupdate.py
|
2018-09-09 23:19:41 +01:00
|
|
|
# type-check:
|
2023-08-12 16:41:04 +01:00
|
|
|
# $ nix run nixpkgs#python3.pkgs.mypy maintainers/scripts/pluginupdate.py
|
2018-09-09 23:19:41 +01:00
|
|
|
# linted:
|
2023-08-12 16:41:04 +01:00
|
|
|
# $ nix run nixpkgs#python3.pkgs.flake8 -- --ignore E501,E265 maintainers/scripts/pluginupdate.py
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2019-11-18 16:54:29 +00:00
|
|
|
import argparse
|
2022-03-30 00:54:26 +01:00
|
|
|
import csv
|
2018-09-09 23:19:41 +01:00
|
|
|
import functools
|
2020-03-21 04:27:09 +00:00
|
|
|
import http
|
2018-09-09 23:19:41 +01:00
|
|
|
import json
|
2023-08-12 16:41:04 +01:00
|
|
|
import logging
|
2018-09-09 23:19:41 +01:00
|
|
|
import os
|
2024-01-03 10:18:49 +00:00
|
|
|
import re
|
2018-09-09 23:19:41 +01:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-04-12 22:05:00 +01:00
|
|
|
import time
|
2018-09-09 23:19:41 +01:00
|
|
|
import traceback
|
|
|
|
import urllib.error
|
2020-03-21 04:27:09 +00:00
|
|
|
import urllib.parse
|
2018-09-09 23:19:41 +01:00
|
|
|
import urllib.request
|
|
|
|
import xml.etree.ElementTree as ET
|
2023-08-12 16:41:04 +01:00
|
|
|
from dataclasses import asdict, dataclass
|
2023-11-16 03:05:52 +00:00
|
|
|
from datetime import UTC, datetime
|
2020-04-12 22:05:00 +01:00
|
|
|
from functools import wraps
|
2018-09-09 23:19:41 +01:00
|
|
|
from multiprocessing.dummy import Pool
|
|
|
|
from pathlib import Path
|
2018-09-12 07:51:48 +01:00
|
|
|
from tempfile import NamedTemporaryFile
|
2023-08-12 16:41:04 +01:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
|
|
|
|
from urllib.parse import urljoin, urlparse
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2020-03-27 21:10:48 +00:00
|
|
|
import git
|
|
|
|
|
2019-11-16 15:15:08 +00:00
|
|
|
ATOM_ENTRY = "{http://www.w3.org/2005/Atom}entry" # " vim gets confused here
|
|
|
|
ATOM_LINK = "{http://www.w3.org/2005/Atom}link" # "
|
|
|
|
ATOM_UPDATED = "{http://www.w3.org/2005/Atom}updated" # "
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2021-05-15 13:16:00 +01:00
|
|
|
LOG_LEVELS = {
|
2023-08-12 16:41:04 +01:00
|
|
|
logging.getLevelName(level): level
|
|
|
|
for level in [logging.DEBUG, logging.INFO, logging.WARN, logging.ERROR]
|
2021-05-15 13:16:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log = logging.getLogger()
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
|
2019-11-16 15:15:42 +00:00
|
|
|
def retry(ExceptionToCheck: Any, tries: int = 4, delay: float = 3, backoff: float = 2):
|
|
|
|
"""Retry calling the decorated function using an exponential backoff.
|
|
|
|
http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
|
|
|
|
original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
|
|
|
|
(BSD licensed)
|
|
|
|
:param ExceptionToCheck: the exception on which to retry
|
|
|
|
:param tries: number of times to try (not retry) before giving up
|
|
|
|
:param delay: initial delay between retries in seconds
|
|
|
|
:param backoff: backoff multiplier e.g. value of 2 will double the delay
|
|
|
|
each retry
|
|
|
|
"""
|
|
|
|
|
|
|
|
def deco_retry(f: Callable) -> Callable:
|
|
|
|
@wraps(f)
|
|
|
|
def f_retry(*args: Any, **kwargs: Any) -> Any:
|
|
|
|
mtries, mdelay = tries, delay
|
|
|
|
while mtries > 1:
|
|
|
|
try:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
except ExceptionToCheck as e:
|
|
|
|
print(f"{str(e)}, Retrying in {mdelay} seconds...")
|
|
|
|
time.sleep(mdelay)
|
|
|
|
mtries -= 1
|
|
|
|
mdelay *= backoff
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
return f_retry # true decorator
|
|
|
|
|
|
|
|
return deco_retry
|
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
|
2022-02-17 11:19:45 +00:00
|
|
|
@dataclass
|
|
|
|
class FetchConfig:
|
|
|
|
proc: int
|
|
|
|
github_token: str
|
|
|
|
|
2021-02-20 09:09:50 +00:00
|
|
|
|
2022-02-17 11:19:45 +00:00
|
|
|
def make_request(url: str, token=None) -> urllib.request.Request:
|
2020-07-05 09:48:12 +01:00
|
|
|
headers = {}
|
|
|
|
if token is not None:
|
2021-02-20 09:09:50 +00:00
|
|
|
headers["Authorization"] = f"token {token}"
|
2020-07-05 09:48:12 +01:00
|
|
|
return urllib.request.Request(url, headers=headers)
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2022-03-30 00:54:26 +01:00
|
|
|
|
2022-05-01 00:10:49 +01:00
|
|
|
# a dictionary of plugins and their new repositories
|
2023-08-12 16:41:04 +01:00
|
|
|
Redirects = Dict["PluginDesc", "Repo"]
|
|
|
|
|
2022-03-30 00:54:26 +01:00
|
|
|
|
2018-09-09 23:19:41 +01:00
|
|
|
class Repo:
|
2023-08-12 16:41:04 +01:00
|
|
|
def __init__(self, uri: str, branch: str) -> None:
|
2022-01-10 23:20:52 +00:00
|
|
|
self.uri = uri
|
2023-08-12 16:41:04 +01:00
|
|
|
"""Url to the repo"""
|
2022-03-30 00:54:26 +01:00
|
|
|
self._branch = branch
|
2022-05-01 00:10:49 +01:00
|
|
|
# Redirect is the new Repo to use
|
2023-08-12 16:41:04 +01:00
|
|
|
self.redirect: Optional["Repo"] = None
|
2022-02-17 11:19:45 +00:00
|
|
|
self.token = "dummy_token"
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2022-01-10 23:20:52 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2024-05-27 10:43:36 +01:00
|
|
|
return self.uri.strip("/").split("/")[-1]
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2022-03-30 00:54:26 +01:00
|
|
|
@property
|
|
|
|
def branch(self):
|
|
|
|
return self._branch or "HEAD"
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"{self.uri}"
|
2023-08-12 16:41:04 +01:00
|
|
|
|
2018-09-09 23:19:41 +01:00
|
|
|
def __repr__(self) -> str:
|
2022-01-10 23:20:52 +00:00
|
|
|
return f"Repo({self.name}, {self.uri})"
|
|
|
|
|
|
|
|
@retry(urllib.error.URLError, tries=4, delay=3, backoff=2)
|
|
|
|
def has_submodules(self) -> bool:
|
|
|
|
return True
|
|
|
|
|
|
|
|
@retry(urllib.error.URLError, tries=4, delay=3, backoff=2)
|
|
|
|
def latest_commit(self) -> Tuple[str, datetime]:
|
2022-03-30 00:54:26 +01:00
|
|
|
log.debug("Latest commit")
|
2022-01-10 23:20:52 +00:00
|
|
|
loaded = self._prefetch(None)
|
2023-08-12 16:41:04 +01:00
|
|
|
updated = datetime.strptime(loaded["date"], "%Y-%m-%dT%H:%M:%S%z")
|
2022-01-10 23:20:52 +00:00
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
return loaded["rev"], updated
|
2022-01-10 23:20:52 +00:00
|
|
|
|
|
|
|
def _prefetch(self, ref: Optional[str]):
|
|
|
|
cmd = ["nix-prefetch-git", "--quiet", "--fetch-submodules", self.uri]
|
|
|
|
if ref is not None:
|
|
|
|
cmd.append(ref)
|
|
|
|
log.debug(cmd)
|
|
|
|
data = subprocess.check_output(cmd)
|
|
|
|
loaded = json.loads(data)
|
|
|
|
return loaded
|
|
|
|
|
|
|
|
def prefetch(self, ref: Optional[str]) -> str:
|
2022-03-30 00:54:26 +01:00
|
|
|
print("Prefetching")
|
2022-01-10 23:20:52 +00:00
|
|
|
loaded = self._prefetch(ref)
|
|
|
|
return loaded["sha256"]
|
|
|
|
|
|
|
|
def as_nix(self, plugin: "Plugin") -> str:
|
2023-08-12 16:41:04 +01:00
|
|
|
return f"""fetchgit {{
|
2022-11-18 01:51:34 +00:00
|
|
|
url = "{self.uri}";
|
|
|
|
rev = "{plugin.commit}";
|
|
|
|
sha256 = "{plugin.sha256}";
|
2023-08-12 16:41:04 +01:00
|
|
|
}}"""
|
2022-01-10 23:20:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RepoGitHub(Repo):
|
2023-08-12 16:41:04 +01:00
|
|
|
def __init__(self, owner: str, repo: str, branch: str) -> None:
|
2022-01-10 23:20:52 +00:00
|
|
|
self.owner = owner
|
|
|
|
self.repo = repo
|
2022-02-17 11:19:45 +00:00
|
|
|
self.token = None
|
2023-08-12 16:41:04 +01:00
|
|
|
"""Url to the repo"""
|
2022-03-30 00:54:26 +01:00
|
|
|
super().__init__(self.url(""), branch)
|
2023-08-12 16:41:04 +01:00
|
|
|
log.debug(
|
|
|
|
"Instantiating github repo owner=%s and repo=%s", self.owner, self.repo
|
|
|
|
)
|
2022-01-10 23:20:52 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.repo
|
|
|
|
|
|
|
|
def url(self, path: str) -> str:
|
2022-03-30 00:54:26 +01:00
|
|
|
res = urljoin(f"https://github.com/{self.owner}/{self.repo}/", path)
|
|
|
|
return res
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2019-11-16 15:15:42 +00:00
|
|
|
@retry(urllib.error.URLError, tries=4, delay=3, backoff=2)
|
2018-09-09 23:19:41 +01:00
|
|
|
def has_submodules(self) -> bool:
|
|
|
|
try:
|
2022-02-17 11:19:45 +00:00
|
|
|
req = make_request(self.url(f"blob/{self.branch}/.gitmodules"), self.token)
|
2020-07-05 09:48:12 +01:00
|
|
|
urllib.request.urlopen(req, timeout=10).close()
|
2018-09-09 23:19:41 +01:00
|
|
|
except urllib.error.HTTPError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
return True
|
|
|
|
|
2019-11-16 15:15:42 +00:00
|
|
|
@retry(urllib.error.URLError, tries=4, delay=3, backoff=2)
|
2018-09-09 23:19:41 +01:00
|
|
|
def latest_commit(self) -> Tuple[str, datetime]:
|
2020-04-12 22:05:00 +01:00
|
|
|
commit_url = self.url(f"commits/{self.branch}.atom")
|
2022-03-30 00:54:26 +01:00
|
|
|
log.debug("Sending request to %s", commit_url)
|
2022-02-17 11:19:45 +00:00
|
|
|
commit_req = make_request(commit_url, self.token)
|
2020-07-05 09:48:12 +01:00
|
|
|
with urllib.request.urlopen(commit_req, timeout=10) as req:
|
2022-01-10 23:20:52 +00:00
|
|
|
self._check_for_redirect(commit_url, req)
|
2018-09-09 23:19:41 +01:00
|
|
|
xml = req.read()
|
2024-01-03 10:18:49 +00:00
|
|
|
|
|
|
|
# Filter out illegal XML characters
|
|
|
|
illegal_xml_regex = re.compile(b"[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]")
|
|
|
|
xml = illegal_xml_regex.sub(b"", xml)
|
|
|
|
|
2018-09-09 23:19:41 +01:00
|
|
|
root = ET.fromstring(xml)
|
|
|
|
latest_entry = root.find(ATOM_ENTRY)
|
|
|
|
assert latest_entry is not None, f"No commits found in repository {self}"
|
|
|
|
commit_link = latest_entry.find(ATOM_LINK)
|
|
|
|
assert commit_link is not None, f"No link tag found feed entry {xml}"
|
|
|
|
url = urlparse(commit_link.get("href"))
|
|
|
|
updated_tag = latest_entry.find(ATOM_UPDATED)
|
|
|
|
assert (
|
|
|
|
updated_tag is not None and updated_tag.text is not None
|
|
|
|
), f"No updated tag found feed entry {xml}"
|
|
|
|
updated = datetime.strptime(updated_tag.text, "%Y-%m-%dT%H:%M:%SZ")
|
2019-11-16 15:15:08 +00:00
|
|
|
return Path(str(url.path)).name, updated
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2022-01-10 23:20:52 +00:00
|
|
|
def _check_for_redirect(self, url: str, req: http.client.HTTPResponse):
|
2020-03-21 04:27:09 +00:00
|
|
|
response_url = req.geturl()
|
|
|
|
if url != response_url:
|
|
|
|
new_owner, new_name = (
|
|
|
|
urllib.parse.urlsplit(response_url).path.strip("/").split("/")[:2]
|
|
|
|
)
|
|
|
|
|
2022-03-30 00:54:26 +01:00
|
|
|
new_repo = RepoGitHub(owner=new_owner, repo=new_name, branch=self.branch)
|
2022-05-01 00:10:49 +01:00
|
|
|
self.redirect = new_repo
|
2020-03-21 04:27:09 +00:00
|
|
|
|
2022-01-10 23:20:52 +00:00
|
|
|
def prefetch(self, commit: str) -> str:
|
|
|
|
if self.has_submodules():
|
|
|
|
sha256 = super().prefetch(commit)
|
|
|
|
else:
|
|
|
|
sha256 = self.prefetch_github(commit)
|
|
|
|
return sha256
|
2018-09-09 23:19:41 +01:00
|
|
|
|
|
|
|
def prefetch_github(self, ref: str) -> str:
|
2022-03-30 00:54:26 +01:00
|
|
|
cmd = ["nix-prefetch-url", "--unpack", self.url(f"archive/{ref}.tar.gz")]
|
|
|
|
log.debug("Running %s", cmd)
|
|
|
|
data = subprocess.check_output(cmd)
|
2018-09-09 23:19:41 +01:00
|
|
|
return data.strip().decode("utf-8")
|
|
|
|
|
2022-01-10 23:20:52 +00:00
|
|
|
def as_nix(self, plugin: "Plugin") -> str:
|
|
|
|
if plugin.has_submodules:
|
|
|
|
submodule_attr = "\n fetchSubmodules = true;"
|
|
|
|
else:
|
|
|
|
submodule_attr = ""
|
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
return f"""fetchFromGitHub {{
|
2022-01-10 23:20:52 +00:00
|
|
|
owner = "{self.owner}";
|
|
|
|
repo = "{self.repo}";
|
|
|
|
rev = "{plugin.commit}";
|
|
|
|
sha256 = "{plugin.sha256}";{submodule_attr}
|
2023-08-12 16:41:04 +01:00
|
|
|
}}"""
|
2022-01-10 23:20:52 +00:00
|
|
|
|
|
|
|
|
2022-05-01 00:10:49 +01:00
|
|
|
@dataclass(frozen=True)
|
2022-01-10 23:20:52 +00:00
|
|
|
class PluginDesc:
|
|
|
|
repo: Repo
|
|
|
|
branch: str
|
|
|
|
alias: Optional[str]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
if self.alias is None:
|
|
|
|
return self.repo.name
|
|
|
|
else:
|
|
|
|
return self.alias
|
|
|
|
|
2022-03-30 00:54:26 +01:00
|
|
|
def __lt__(self, other):
|
|
|
|
return self.repo.name < other.repo.name
|
|
|
|
|
|
|
|
@staticmethod
|
2023-08-12 16:41:04 +01:00
|
|
|
def load_from_csv(config: FetchConfig, row: Dict[str, str]) -> "PluginDesc":
|
2022-03-30 00:54:26 +01:00
|
|
|
branch = row["branch"]
|
2023-08-12 16:41:04 +01:00
|
|
|
repo = make_repo(row["repo"], branch.strip())
|
2022-03-30 00:54:26 +01:00
|
|
|
repo.token = config.github_token
|
|
|
|
return PluginDesc(repo, branch.strip(), row["alias"])
|
|
|
|
|
|
|
|
@staticmethod
|
2023-08-12 16:41:04 +01:00
|
|
|
def load_from_string(config: FetchConfig, line: str) -> "PluginDesc":
|
2022-03-30 00:54:26 +01:00
|
|
|
branch = "HEAD"
|
|
|
|
alias = None
|
|
|
|
uri = line
|
|
|
|
if " as " in uri:
|
|
|
|
uri, alias = uri.split(" as ")
|
|
|
|
alias = alias.strip()
|
|
|
|
if "@" in uri:
|
|
|
|
uri, branch = uri.split("@")
|
|
|
|
repo = make_repo(uri.strip(), branch.strip())
|
|
|
|
repo.token = config.github_token
|
|
|
|
return PluginDesc(repo, branch.strip(), alias)
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
|
2022-03-30 00:54:26 +01:00
|
|
|
@dataclass
|
2018-09-09 23:19:41 +01:00
|
|
|
class Plugin:
|
2022-03-30 00:54:26 +01:00
|
|
|
name: str
|
|
|
|
commit: str
|
|
|
|
has_submodules: bool
|
|
|
|
sha256: str
|
|
|
|
date: Optional[datetime] = None
|
2018-09-09 23:19:41 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def normalized_name(self) -> str:
|
|
|
|
return self.name.replace(".", "-")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def version(self) -> str:
|
|
|
|
assert self.date is not None
|
|
|
|
return self.date.strftime("%Y-%m-%d")
|
|
|
|
|
|
|
|
def as_json(self) -> Dict[str, str]:
|
|
|
|
copy = self.__dict__.copy()
|
|
|
|
del copy["date"]
|
|
|
|
return copy
|
|
|
|
|
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
def load_plugins_from_csv(
|
|
|
|
config: FetchConfig,
|
|
|
|
input_file: Path,
|
|
|
|
) -> List[PluginDesc]:
|
2022-03-30 00:54:26 +01:00
|
|
|
log.debug("Load plugins from csv %s", input_file)
|
|
|
|
plugins = []
|
2023-08-12 16:41:04 +01:00
|
|
|
with open(input_file, newline="") as csvfile:
|
2022-03-30 00:54:26 +01:00
|
|
|
log.debug("Writing into %s", input_file)
|
2023-08-12 16:41:04 +01:00
|
|
|
reader = csv.DictReader(
|
|
|
|
csvfile,
|
|
|
|
)
|
2022-03-30 00:54:26 +01:00
|
|
|
for line in reader:
|
|
|
|
plugin = PluginDesc.load_from_csv(config, line)
|
|
|
|
plugins.append(plugin)
|
|
|
|
|
|
|
|
return plugins
|
2022-01-10 23:20:52 +00:00
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
|
2023-08-20 15:49:44 +01:00
|
|
|
|
|
|
|
def run_nix_expr(expr, nixpkgs: str):
|
|
|
|
'''
|
|
|
|
:param expr nix expression to fetch current plugins
|
|
|
|
:param nixpkgs Path towards a nixpkgs checkout
|
|
|
|
'''
|
|
|
|
with CleanEnvironment(nixpkgs) as nix_path:
|
2023-08-12 16:41:04 +01:00
|
|
|
cmd = [
|
|
|
|
"nix",
|
|
|
|
"eval",
|
|
|
|
"--extra-experimental-features",
|
|
|
|
"nix-command",
|
|
|
|
"--impure",
|
|
|
|
"--json",
|
|
|
|
"--expr",
|
|
|
|
expr,
|
|
|
|
"--nix-path",
|
|
|
|
nix_path,
|
|
|
|
]
|
2023-10-13 19:44:38 +01:00
|
|
|
log.debug("Running command: %s", " ".join(cmd))
|
|
|
|
out = subprocess.check_output(cmd, timeout=90)
|
2023-04-14 21:02:17 +01:00
|
|
|
data = json.loads(out)
|
|
|
|
return data
|
2022-05-01 23:58:41 +01:00
|
|
|
|
|
|
|
|
2021-02-20 09:09:50 +00:00
|
|
|
class Editor:
|
|
|
|
"""The configuration of the update script."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
root: Path,
|
|
|
|
get_plugins: str,
|
|
|
|
default_in: Optional[Path] = None,
|
|
|
|
default_out: Optional[Path] = None,
|
|
|
|
deprecated: Optional[Path] = None,
|
|
|
|
cache_file: Optional[str] = None,
|
|
|
|
):
|
2021-07-30 01:25:15 +01:00
|
|
|
log.debug("get_plugins:", get_plugins)
|
2021-02-20 09:09:50 +00:00
|
|
|
self.name = name
|
|
|
|
self.root = root
|
|
|
|
self.get_plugins = get_plugins
|
|
|
|
self.default_in = default_in or root.joinpath(f"{name}-plugin-names")
|
|
|
|
self.default_out = default_out or root.joinpath("generated.nix")
|
|
|
|
self.deprecated = deprecated or root.joinpath("deprecated.json")
|
|
|
|
self.cache_file = cache_file or f"{name}-plugin-cache.json"
|
2022-11-04 17:56:57 +00:00
|
|
|
self.nixpkgs_repo = None
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2023-04-14 21:02:17 +01:00
|
|
|
def add(self, args):
|
2023-08-12 16:41:04 +01:00
|
|
|
"""CSV spec"""
|
2023-04-14 21:02:17 +01:00
|
|
|
log.debug("called the 'add' command")
|
|
|
|
fetch_config = FetchConfig(args.proc, args.github_token)
|
|
|
|
editor = self
|
|
|
|
for plugin_line in args.add_plugins:
|
|
|
|
log.debug("using plugin_line", plugin_line)
|
|
|
|
pdesc = PluginDesc.load_from_string(fetch_config, plugin_line)
|
|
|
|
log.debug("loaded as pdesc", pdesc)
|
2023-08-12 16:41:04 +01:00
|
|
|
append = [pdesc]
|
|
|
|
editor.rewrite_input(
|
|
|
|
fetch_config, args.input_file, editor.deprecated, append=append
|
|
|
|
)
|
|
|
|
plugin, _ = prefetch_plugin(
|
|
|
|
pdesc,
|
|
|
|
)
|
2023-04-14 21:02:17 +01:00
|
|
|
autocommit = not args.no_commit
|
|
|
|
if autocommit:
|
|
|
|
commit(
|
|
|
|
editor.nixpkgs_repo,
|
|
|
|
"{drv_name}: init at {version}".format(
|
|
|
|
drv_name=editor.get_drv_name(plugin.normalized_name),
|
2023-08-12 16:41:04 +01:00
|
|
|
version=plugin.version,
|
2023-04-14 21:02:17 +01:00
|
|
|
),
|
|
|
|
[args.outfile, args.input_file],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Expects arguments generated by 'update' subparser
|
2023-08-12 16:41:04 +01:00
|
|
|
def update(self, args):
|
|
|
|
"""CSV spec"""
|
2023-04-14 21:02:17 +01:00
|
|
|
print("the update member function should be overriden in subclasses")
|
|
|
|
|
2023-08-20 15:49:44 +01:00
|
|
|
def get_current_plugins(self, nixpkgs) -> List[Plugin]:
|
2021-07-30 01:25:15 +01:00
|
|
|
"""To fill the cache"""
|
2023-08-20 15:49:44 +01:00
|
|
|
data = run_nix_expr(self.get_plugins, nixpkgs)
|
2022-05-01 00:10:49 +01:00
|
|
|
plugins = []
|
|
|
|
for name, attr in data.items():
|
|
|
|
p = Plugin(name, attr["rev"], attr["submodules"], attr["sha256"])
|
|
|
|
plugins.append(p)
|
|
|
|
return plugins
|
2021-07-30 01:25:15 +01:00
|
|
|
|
2022-02-17 11:19:45 +00:00
|
|
|
def load_plugin_spec(self, config: FetchConfig, plugin_file) -> List[PluginDesc]:
|
2023-08-12 16:41:04 +01:00
|
|
|
"""CSV spec"""
|
2022-03-30 00:54:26 +01:00
|
|
|
return load_plugins_from_csv(config, plugin_file)
|
2021-07-30 01:25:15 +01:00
|
|
|
|
2023-04-14 21:02:17 +01:00
|
|
|
def generate_nix(self, _plugins, _outfile: str):
|
2023-08-12 16:41:04 +01:00
|
|
|
"""Returns nothing for now, writes directly to outfile"""
|
2021-07-30 01:25:15 +01:00
|
|
|
raise NotImplementedError()
|
2021-07-30 01:25:15 +01:00
|
|
|
|
2022-02-17 11:19:45 +00:00
|
|
|
def get_update(self, input_file: str, outfile: str, config: FetchConfig):
|
2023-08-20 15:49:44 +01:00
|
|
|
cache: Cache = Cache(self.get_current_plugins(self.nixpkgs), self.cache_file)
|
2022-02-17 11:19:45 +00:00
|
|
|
_prefetch = functools.partial(prefetch, cache=cache)
|
|
|
|
|
|
|
|
def update() -> dict:
|
2022-03-30 00:54:26 +01:00
|
|
|
plugins = self.load_plugin_spec(config, input_file)
|
2022-02-17 11:19:45 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
pool = Pool(processes=config.proc)
|
2022-03-30 00:54:26 +01:00
|
|
|
results = pool.map(_prefetch, plugins)
|
2022-02-17 11:19:45 +00:00
|
|
|
finally:
|
|
|
|
cache.store()
|
|
|
|
|
|
|
|
plugins, redirects = check_results(results)
|
|
|
|
|
|
|
|
self.generate_nix(plugins, outfile)
|
|
|
|
|
|
|
|
return redirects
|
|
|
|
|
|
|
|
return update
|
|
|
|
|
2021-07-30 01:25:15 +01:00
|
|
|
@property
|
|
|
|
def attr_path(self):
|
|
|
|
return self.name + "Plugins"
|
|
|
|
|
2021-07-30 01:25:15 +01:00
|
|
|
def get_drv_name(self, name: str):
|
|
|
|
return self.attr_path + "." + name
|
|
|
|
|
2021-07-30 01:25:15 +01:00
|
|
|
def rewrite_input(self, *args, **kwargs):
|
|
|
|
return rewrite_input(*args, **kwargs)
|
|
|
|
|
2021-07-30 01:25:15 +01:00
|
|
|
def create_parser(self):
|
2023-04-14 21:02:17 +01:00
|
|
|
common = argparse.ArgumentParser(
|
|
|
|
add_help=False,
|
2023-08-12 16:41:04 +01:00
|
|
|
description=(
|
|
|
|
f"""
|
2022-01-10 23:20:52 +00:00
|
|
|
Updates nix derivations for {self.name} plugins.\n
|
|
|
|
By default from {self.default_in} to {self.default_out}"""
|
2023-08-12 16:41:04 +01:00
|
|
|
),
|
2021-07-30 01:25:15 +01:00
|
|
|
)
|
2023-08-20 15:49:44 +01:00
|
|
|
common.add_argument(
|
|
|
|
"--nixpkgs",
|
|
|
|
type=str,
|
|
|
|
default=os.getcwd(),
|
|
|
|
help="Adjust log level",
|
|
|
|
)
|
2023-04-14 21:02:17 +01:00
|
|
|
common.add_argument(
|
2021-07-30 01:25:15 +01:00
|
|
|
"--input-names",
|
|
|
|
"-i",
|
|
|
|
dest="input_file",
|
2023-10-29 20:02:55 +00:00
|
|
|
type=Path,
|
2021-07-30 01:25:15 +01:00
|
|
|
default=self.default_in,
|
|
|
|
help="A list of plugins in the form owner/repo",
|
|
|
|
)
|
2023-04-14 21:02:17 +01:00
|
|
|
common.add_argument(
|
2021-07-30 01:25:15 +01:00
|
|
|
"--out",
|
|
|
|
"-o",
|
|
|
|
dest="outfile",
|
|
|
|
default=self.default_out,
|
2023-10-29 20:02:55 +00:00
|
|
|
type=Path,
|
2021-07-30 01:25:15 +01:00
|
|
|
help="Filename to save generated nix code",
|
|
|
|
)
|
2023-04-14 21:02:17 +01:00
|
|
|
common.add_argument(
|
2021-07-30 01:25:15 +01:00
|
|
|
"--proc",
|
|
|
|
"-p",
|
|
|
|
dest="proc",
|
|
|
|
type=int,
|
|
|
|
default=30,
|
2022-02-17 11:19:45 +00:00
|
|
|
help="Number of concurrent processes to spawn. Setting --github-token allows higher values.",
|
|
|
|
)
|
2023-04-14 21:02:17 +01:00
|
|
|
common.add_argument(
|
2022-02-17 11:19:45 +00:00
|
|
|
"--github-token",
|
|
|
|
"-t",
|
|
|
|
type=str,
|
|
|
|
default=os.getenv("GITHUB_API_TOKEN"),
|
|
|
|
help="""Allows to set --proc to higher values.
|
|
|
|
Uses GITHUB_API_TOKEN environment variables as the default value.""",
|
2021-07-30 01:25:15 +01:00
|
|
|
)
|
2023-04-14 21:02:17 +01:00
|
|
|
common.add_argument(
|
2023-08-12 16:41:04 +01:00
|
|
|
"--no-commit",
|
|
|
|
"-n",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Whether to autocommit changes",
|
2021-07-30 01:25:15 +01:00
|
|
|
)
|
2023-04-14 21:02:17 +01:00
|
|
|
common.add_argument(
|
2023-08-12 16:41:04 +01:00
|
|
|
"--debug",
|
|
|
|
"-d",
|
|
|
|
choices=LOG_LEVELS.keys(),
|
2021-07-30 01:25:15 +01:00
|
|
|
default=logging.getLevelName(logging.WARN),
|
2023-08-12 16:41:04 +01:00
|
|
|
help="Adjust log level",
|
2021-07-30 01:25:15 +01:00
|
|
|
)
|
2023-04-14 21:02:17 +01:00
|
|
|
|
|
|
|
main = argparse.ArgumentParser(
|
|
|
|
parents=[common],
|
2023-08-12 16:41:04 +01:00
|
|
|
description=(
|
|
|
|
f"""
|
2023-04-14 21:02:17 +01:00
|
|
|
Updates nix derivations for {self.name} plugins.\n
|
|
|
|
By default from {self.default_in} to {self.default_out}"""
|
2023-08-12 16:41:04 +01:00
|
|
|
),
|
2023-04-14 21:02:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
subparsers = main.add_subparsers(dest="command", required=False)
|
|
|
|
padd = subparsers.add_parser(
|
2023-08-12 16:41:04 +01:00
|
|
|
"add",
|
|
|
|
parents=[],
|
2023-04-14 21:02:17 +01:00
|
|
|
description="Add new plugin",
|
|
|
|
add_help=False,
|
|
|
|
)
|
|
|
|
padd.set_defaults(func=self.add)
|
|
|
|
padd.add_argument(
|
|
|
|
"add_plugins",
|
|
|
|
default=None,
|
|
|
|
nargs="+",
|
|
|
|
help=f"Plugin to add to {self.attr_path} from Github in the form owner/repo",
|
|
|
|
)
|
|
|
|
|
|
|
|
pupdate = subparsers.add_parser(
|
|
|
|
"update",
|
|
|
|
description="Update all or a subset of existing plugins",
|
|
|
|
add_help=False,
|
|
|
|
)
|
|
|
|
pupdate.set_defaults(func=self.update)
|
|
|
|
return main
|
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
def run(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
"""
|
2023-04-14 21:02:17 +01:00
|
|
|
Convenience function
|
2023-08-12 16:41:04 +01:00
|
|
|
"""
|
2023-04-14 21:02:17 +01:00
|
|
|
parser = self.create_parser()
|
|
|
|
args = parser.parse_args()
|
|
|
|
command = args.command or "update"
|
|
|
|
log.setLevel(LOG_LEVELS[args.debug])
|
|
|
|
log.info("Chose to run command: %s", command)
|
2023-08-20 15:49:44 +01:00
|
|
|
self.nixpkgs = args.nixpkgs
|
2023-04-14 21:02:17 +01:00
|
|
|
|
2023-08-20 15:49:44 +01:00
|
|
|
self.nixpkgs_repo = git.Repo(args.nixpkgs, search_parent_directories=True)
|
2023-04-14 21:02:17 +01:00
|
|
|
|
|
|
|
getattr(self, command)(args)
|
|
|
|
|
2021-07-30 01:25:15 +01:00
|
|
|
|
2018-09-12 07:51:48 +01:00
|
|
|
class CleanEnvironment(object):
|
2023-08-20 15:49:44 +01:00
|
|
|
def __init__(self, nixpkgs):
|
|
|
|
self.local_pkgs = nixpkgs
|
|
|
|
|
2023-08-12 16:32:02 +01:00
|
|
|
def __enter__(self) -> str:
|
2023-08-20 15:49:44 +01:00
|
|
|
"""
|
2021-02-20 09:09:50 +00:00
|
|
|
local_pkgs = str(Path(__file__).parent.parent.parent)
|
2023-08-20 15:49:44 +01:00
|
|
|
"""
|
|
|
|
self.old_environ = os.environ.copy()
|
2018-09-12 07:51:48 +01:00
|
|
|
self.empty_config = NamedTemporaryFile()
|
|
|
|
self.empty_config.write(b"{}")
|
|
|
|
self.empty_config.flush()
|
2023-08-20 15:49:44 +01:00
|
|
|
return f"localpkgs={self.local_pkgs}"
|
2018-09-12 07:51:48 +01:00
|
|
|
|
|
|
|
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
|
|
|
|
os.environ.update(self.old_environ)
|
|
|
|
self.empty_config.close()
|
|
|
|
|
|
|
|
|
2020-03-21 04:27:09 +00:00
|
|
|
def prefetch_plugin(
|
2021-08-13 22:48:03 +01:00
|
|
|
p: PluginDesc,
|
2020-04-12 22:05:00 +01:00
|
|
|
cache: "Optional[Cache]" = None,
|
2022-05-01 00:10:49 +01:00
|
|
|
) -> Tuple[Plugin, Optional[Repo]]:
|
2022-01-10 23:20:52 +00:00
|
|
|
repo, branch, alias = p.repo, p.branch, p.alias
|
|
|
|
name = alias or p.repo.name
|
|
|
|
commit = None
|
|
|
|
log.info(f"Fetching last commit for plugin {name} from {repo.uri}@{branch}")
|
2018-09-09 23:19:41 +01:00
|
|
|
commit, date = repo.latest_commit()
|
2020-03-31 02:01:33 +01:00
|
|
|
cached_plugin = cache[commit] if cache else None
|
|
|
|
if cached_plugin is not None:
|
2021-07-30 01:25:15 +01:00
|
|
|
log.debug("Cache hit !")
|
2022-01-10 23:20:52 +00:00
|
|
|
cached_plugin.name = name
|
2020-03-31 02:01:33 +01:00
|
|
|
cached_plugin.date = date
|
|
|
|
return cached_plugin, repo.redirect
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2022-01-10 23:20:52 +00:00
|
|
|
has_submodules = repo.has_submodules()
|
2022-05-01 00:10:49 +01:00
|
|
|
log.debug(f"prefetch {name}")
|
2022-01-10 23:20:52 +00:00
|
|
|
sha256 = repo.prefetch(commit)
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2020-03-21 04:27:09 +00:00
|
|
|
return (
|
2022-01-10 23:20:52 +00:00
|
|
|
Plugin(name, commit, has_submodules, sha256, date=date),
|
2020-03-21 04:27:09 +00:00
|
|
|
repo.redirect,
|
|
|
|
)
|
2018-09-09 23:19:41 +01:00
|
|
|
|
|
|
|
|
2022-05-01 00:10:49 +01:00
|
|
|
def print_download_error(plugin: PluginDesc, ex: Exception):
|
2018-09-09 23:19:41 +01:00
|
|
|
print(f"{plugin}: {ex}", file=sys.stderr)
|
|
|
|
ex_traceback = ex.__traceback__
|
|
|
|
tb_lines = [
|
|
|
|
line.rstrip("\n")
|
|
|
|
for line in traceback.format_exception(ex.__class__, ex, ex_traceback)
|
|
|
|
]
|
|
|
|
print("\n".join(tb_lines))
|
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
|
2018-09-09 23:19:41 +01:00
|
|
|
def check_results(
|
2022-05-01 00:10:49 +01:00
|
|
|
results: List[Tuple[PluginDesc, Union[Exception, Plugin], Optional[Repo]]]
|
2022-03-30 00:54:26 +01:00
|
|
|
) -> Tuple[List[Tuple[PluginDesc, Plugin]], Redirects]:
|
2023-08-12 16:41:04 +01:00
|
|
|
""" """
|
2022-05-01 00:10:49 +01:00
|
|
|
failures: List[Tuple[PluginDesc, Exception]] = []
|
2018-09-09 23:19:41 +01:00
|
|
|
plugins = []
|
2022-05-01 00:10:49 +01:00
|
|
|
redirects: Redirects = {}
|
2023-08-12 16:41:04 +01:00
|
|
|
for pdesc, result, redirect in results:
|
2018-09-09 23:19:41 +01:00
|
|
|
if isinstance(result, Exception):
|
2022-05-01 00:10:49 +01:00
|
|
|
failures.append((pdesc, result))
|
2018-09-09 23:19:41 +01:00
|
|
|
else:
|
2022-05-01 00:10:49 +01:00
|
|
|
new_pdesc = pdesc
|
|
|
|
if redirect is not None:
|
|
|
|
redirects.update({pdesc: redirect})
|
|
|
|
new_pdesc = PluginDesc(redirect, pdesc.branch, pdesc.alias)
|
|
|
|
plugins.append((new_pdesc, result))
|
2018-09-09 23:19:41 +01:00
|
|
|
|
|
|
|
print(f"{len(results) - len(failures)} plugins were checked", end="")
|
|
|
|
if len(failures) == 0:
|
|
|
|
print()
|
2020-03-21 04:27:09 +00:00
|
|
|
return plugins, redirects
|
2018-09-09 23:19:41 +01:00
|
|
|
else:
|
|
|
|
print(f", {len(failures)} plugin(s) could not be downloaded:\n")
|
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
for plugin, exception in failures:
|
2018-09-09 23:19:41 +01:00
|
|
|
print_download_error(plugin, exception)
|
2018-09-16 16:58:48 +01:00
|
|
|
|
|
|
|
sys.exit(1)
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2023-08-12 16:41:04 +01:00
|
|
|
|
2022-03-30 00:54:26 +01:00
|
|
|
def make_repo(uri: str, branch) -> Repo:
|
2023-08-12 16:41:04 +01:00
|
|
|
"""Instantiate a Repo with the correct specialization depending on server (gitub spec)"""
|
2022-01-10 23:20:52 +00:00
|
|
|
# dumb check to see if it's of the form owner/repo (=> github) or https://...
|
2022-03-30 00:54:26 +01:00
|
|
|
res = urlparse(uri)
|
2023-08-12 16:41:04 +01:00
|
|
|
if res.netloc in ["github.com", ""]:
|
|
|
|
res = res.path.strip("/").split("/")
|
2022-03-30 00:54:26 +01:00
|
|
|
repo = RepoGitHub(res[0], res[1], branch)
|
2022-01-10 23:20:52 +00:00
|
|
|
else:
|
2022-03-30 00:54:26 +01:00
|
|
|
repo = Repo(uri.strip(), branch)
|
2022-01-10 23:20:52 +00:00
|
|
|
return repo
|
|
|
|
|
2019-11-06 08:35:21 +00:00
|
|
|
|
2021-02-20 09:09:50 +00:00
|
|
|
def get_cache_path(cache_file_name: str) -> Optional[Path]:
|
2018-09-09 23:19:41 +01:00
|
|
|
xdg_cache = os.environ.get("XDG_CACHE_HOME", None)
|
|
|
|
if xdg_cache is None:
|
|
|
|
home = os.environ.get("HOME", None)
|
|
|
|
if home is None:
|
|
|
|
return None
|
|
|
|
xdg_cache = str(Path(home, ".cache"))
|
|
|
|
|
2021-02-20 09:09:50 +00:00
|
|
|
return Path(xdg_cache, cache_file_name)
|
2018-09-09 23:19:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Cache:
|
2021-02-20 09:09:50 +00:00
|
|
|
def __init__(self, initial_plugins: List[Plugin], cache_file_name: str) -> None:
|
|
|
|
self.cache_file = get_cache_path(cache_file_name)
|
2018-09-09 23:19:41 +01:00
|
|
|
|
|
|
|
downloads = {}
|
|
|
|
for plugin in initial_plugins:
|
|
|
|
downloads[plugin.commit] = plugin
|
|
|
|
downloads.update(self.load())
|
|
|
|
self.downloads = downloads
|
|
|
|
|
|
|
|
def load(self) -> Dict[str, Plugin]:
|
|
|
|
if self.cache_file is None or not self.cache_file.exists():
|
|
|
|
return {}
|
|
|
|
|
|
|
|
downloads: Dict[str, Plugin] = {}
|
|
|
|
with open(self.cache_file) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
for attr in data.values():
|
|
|
|
p = Plugin(
|
|
|
|
attr["name"], attr["commit"], attr["has_submodules"], attr["sha256"]
|
|
|
|
)
|
|
|
|
downloads[attr["commit"]] = p
|
|
|
|
return downloads
|
|
|
|
|
|
|
|
def store(self) -> None:
|
|
|
|
if self.cache_file is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
os.makedirs(self.cache_file.parent, exist_ok=True)
|
|
|
|
with open(self.cache_file, "w+") as f:
|
|
|
|
data = {}
|
|
|
|
for name, attr in self.downloads.items():
|
|
|
|
data[name] = attr.as_json()
|
|
|
|
json.dump(data, f, indent=4, sort_keys=True)
|
|
|
|
|
|
|
|
def __getitem__(self, key: str) -> Optional[Plugin]:
|
|
|
|
return self.downloads.get(key, None)
|
|
|
|
|
|
|
|
def __setitem__(self, key: str, value: Plugin) -> None:
|
|
|
|
self.downloads[key] = value
|
|
|
|
|
|
|
|
|
|
|
|
def prefetch(
|
2021-08-13 22:48:03 +01:00
|
|
|
pluginDesc: PluginDesc, cache: Cache
|
2022-05-01 00:10:49 +01:00
|
|
|
) -> Tuple[PluginDesc, Union[Exception, Plugin], Optional[Repo]]:
|
2018-09-09 23:19:41 +01:00
|
|
|
try:
|
2021-08-13 22:48:03 +01:00
|
|
|
plugin, redirect = prefetch_plugin(pluginDesc, cache)
|
2018-09-09 23:19:41 +01:00
|
|
|
cache[plugin.commit] = plugin
|
2022-01-10 23:20:52 +00:00
|
|
|
return (pluginDesc, plugin, redirect)
|
2018-09-09 23:19:41 +01:00
|
|
|
except Exception as e:
|
2022-05-01 00:10:49 +01:00
|
|
|
return (pluginDesc, e, None)
|
2018-09-09 23:19:41 +01:00
|
|
|
|
|
|
|
|
2020-03-29 17:09:59 +01:00
|
|
|
def rewrite_input(
|
2022-02-23 18:12:39 +00:00
|
|
|
config: FetchConfig,
|
2021-02-20 09:09:50 +00:00
|
|
|
input_file: Path,
|
|
|
|
deprecated: Path,
|
2022-03-30 00:54:26 +01:00
|
|
|
# old pluginDesc and the new
|
2022-05-01 00:10:49 +01:00
|
|
|
redirects: Redirects = {},
|
2022-03-30 00:54:26 +01:00
|
|
|
append: List[PluginDesc] = [],
|
2020-03-29 17:09:59 +01:00
|
|
|
):
|
2023-08-12 16:41:04 +01:00
|
|
|
plugins = load_plugins_from_csv(
|
|
|
|
config,
|
|
|
|
input_file,
|
|
|
|
)
|
2020-03-24 19:00:31 +00:00
|
|
|
|
2022-03-30 00:54:26 +01:00
|
|
|
plugins.extend(append)
|
2020-03-28 17:42:31 +00:00
|
|
|
|
2020-03-24 19:00:31 +00:00
|
|
|
if redirects:
|
2020-03-27 15:38:16 +00:00
|
|
|
cur_date_iso = datetime.now().strftime("%Y-%m-%d")
|
2021-02-20 09:09:50 +00:00
|
|
|
with open(deprecated, "r") as f:
|
2020-03-26 06:11:20 +00:00
|
|
|
deprecations = json.load(f)
|
2022-05-01 00:10:49 +01:00
|
|
|
for pdesc, new_repo in redirects.items():
|
|
|
|
new_pdesc = PluginDesc(new_repo, pdesc.branch, pdesc.alias)
|
|
|
|
old_plugin, _ = prefetch_plugin(pdesc)
|
|
|
|
new_plugin, _ = prefetch_plugin(new_pdesc)
|
2020-03-29 17:09:59 +01:00
|
|
|
if old_plugin.normalized_name != new_plugin.normalized_name:
|
|
|
|
deprecations[old_plugin.normalized_name] = {
|
|
|
|
"new": new_plugin.normalized_name,
|
2020-03-27 15:38:16 +00:00
|
|
|
"date": cur_date_iso,
|
|
|
|
}
|
2021-02-20 09:09:50 +00:00
|
|
|
with open(deprecated, "w") as f:
|
2020-03-26 06:11:20 +00:00
|
|
|
json.dump(deprecations, f, indent=4, sort_keys=True)
|
2021-03-22 21:56:44 +00:00
|
|
|
f.write("\n")
|
2020-03-26 06:11:20 +00:00
|
|
|
|
2020-03-24 19:00:31 +00:00
|
|
|
with open(input_file, "w") as f:
|
2022-03-30 00:54:26 +01:00
|
|
|
log.debug("Writing into %s", input_file)
|
|
|
|
# fields = dataclasses.fields(PluginDesc)
|
2023-08-12 16:41:04 +01:00
|
|
|
fieldnames = ["repo", "branch", "alias"]
|
|
|
|
writer = csv.DictWriter(f, fieldnames, dialect="unix", quoting=csv.QUOTE_NONE)
|
2022-03-30 00:54:26 +01:00
|
|
|
writer.writeheader()
|
|
|
|
for plugin in sorted(plugins):
|
|
|
|
writer.writerow(asdict(plugin))
|
2020-03-21 04:27:09 +00:00
|
|
|
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2020-03-31 02:01:33 +01:00
|
|
|
def commit(repo: git.Repo, message: str, files: List[Path]) -> None:
|
2021-02-14 15:47:33 +00:00
|
|
|
repo.index.add([str(f.resolve()) for f in files])
|
2020-03-28 06:29:53 +00:00
|
|
|
|
2021-02-14 15:47:33 +00:00
|
|
|
if repo.index.diff("HEAD"):
|
2020-03-31 02:01:33 +01:00
|
|
|
print(f'committing to nixpkgs "{message}"')
|
|
|
|
repo.index.commit(message)
|
|
|
|
else:
|
|
|
|
print("no changes in working tree to commit")
|
2018-09-09 23:19:41 +01:00
|
|
|
|
2020-03-28 06:29:53 +00:00
|
|
|
|
2021-07-30 01:25:15 +01:00
|
|
|
def update_plugins(editor: Editor, args):
|
2023-08-20 15:49:44 +01:00
|
|
|
"""The main entry function of this module.
|
|
|
|
All input arguments are grouped in the `Editor`."""
|
2021-02-20 09:09:50 +00:00
|
|
|
|
2021-05-15 13:16:00 +01:00
|
|
|
log.info("Start updating plugins")
|
2022-02-17 11:19:45 +00:00
|
|
|
fetch_config = FetchConfig(args.proc, args.github_token)
|
|
|
|
update = editor.get_update(args.input_file, args.outfile, fetch_config)
|
2020-03-28 06:29:53 +00:00
|
|
|
|
2020-03-31 02:01:33 +01:00
|
|
|
redirects = update()
|
2022-02-23 18:12:39 +00:00
|
|
|
editor.rewrite_input(fetch_config, args.input_file, editor.deprecated, redirects)
|
2021-04-22 02:21:36 +01:00
|
|
|
|
|
|
|
autocommit = not args.no_commit
|
|
|
|
|
|
|
|
if autocommit:
|
2023-10-29 20:02:55 +00:00
|
|
|
try:
|
|
|
|
repo = git.Repo(os.getcwd())
|
2023-11-16 03:05:52 +00:00
|
|
|
updated = datetime.now(tz=UTC).strftime('%Y-%m-%d')
|
2023-10-29 20:02:55 +00:00
|
|
|
print(args.outfile)
|
|
|
|
commit(repo,
|
2023-11-16 03:05:52 +00:00
|
|
|
f"{editor.attr_path}: update on {updated}", [args.outfile]
|
2023-10-29 20:02:55 +00:00
|
|
|
)
|
|
|
|
except git.InvalidGitRepositoryError as e:
|
|
|
|
print(f"Not in a git repository: {e}", file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2020-03-28 06:29:53 +00:00
|
|
|
|
2020-03-31 02:01:33 +01:00
|
|
|
if redirects:
|
|
|
|
update()
|
2021-04-22 02:21:36 +01:00
|
|
|
if autocommit:
|
|
|
|
commit(
|
2022-11-04 17:56:57 +00:00
|
|
|
editor.nixpkgs_repo,
|
2021-07-30 01:25:15 +01:00
|
|
|
f"{editor.attr_path}: resolve github repository redirects",
|
2021-04-22 02:21:36 +01:00
|
|
|
[args.outfile, args.input_file, editor.deprecated],
|
|
|
|
)
|