vim/update.py: fix handling of redirects

This commit is contained in:
Matthieu Coudron 2022-05-01 01:10:49 +02:00 committed by Matthieu Coudron
parent 6fa768884c
commit 1b8e123255

View file

@ -87,7 +87,8 @@ def make_request(url: str, token=None) -> urllib.request.Request:
return urllib.request.Request(url, headers=headers)
Redirects = Dict['Repo', 'Repo']
# a dictionary of plugins and their new repositories
Redirects = Dict['PluginDesc', 'Repo']
class Repo:
def __init__(
@ -96,8 +97,8 @@ class Repo:
self.uri = uri
'''Url to the repo'''
self._branch = branch
# {old_uri: new_uri}
self.redirect: Redirects = {}
# Redirect is the new Repo to use
self.redirect: Optional['Repo'] = None
self.token = "dummy_token"
@property
@ -207,7 +208,7 @@ class RepoGitHub(Repo):
)
new_repo = RepoGitHub(owner=new_owner, repo=new_name, branch=self.branch)
self.redirect[self] = new_repo
self.redirect = new_repo
def prefetch(self, commit: str) -> str:
@ -237,7 +238,7 @@ class RepoGitHub(Repo):
}}'''
@dataclass
@dataclass(frozen=True)
class PluginDesc:
repo: Repo
branch: str
@ -332,9 +333,19 @@ class Editor:
self.deprecated = deprecated or root.joinpath("deprecated.json")
self.cache_file = cache_file or f"{name}-plugin-cache.json"
def get_current_plugins(self):
def get_current_plugins(editor) -> List[Plugin]:
"""To fill the cache"""
return get_current_plugins(self)
with CleanEnvironment():
cmd = ["nix", "eval", "--extra-experimental-features", "nix-command", "--impure", "--json", "--expr", editor.get_plugins]
log.debug("Running command %s", cmd)
out = subprocess.check_output(cmd)
data = json.loads(out)
plugins = []
for name, attr in data.items():
print("get_current_plugins: name %s" % name)
p = Plugin(name, attr["rev"], attr["submodules"], attr["sha256"])
plugins.append(p)
return plugins
def load_plugin_spec(self, config: FetchConfig, plugin_file) -> List[PluginDesc]:
'''CSV spec'''
@ -448,24 +459,10 @@ class CleanEnvironment(object):
self.empty_config.close()
def get_current_plugins(editor: Editor) -> List[Plugin]:
with CleanEnvironment():
cmd = ["nix", "eval", "--extra-experimental-features", "nix-command", "--impure", "--json", "--expr", editor.get_plugins]
log.debug("Running command %s", cmd)
out = subprocess.check_output(cmd)
data = json.loads(out)
plugins = []
for name, attr in data.items():
print("get_current_plugins: name %s" % name)
p = Plugin(name, attr["rev"], attr["submodules"], attr["sha256"])
plugins.append(p)
return plugins
def prefetch_plugin(
p: PluginDesc,
cache: "Optional[Cache]" = None,
) -> Tuple[Plugin, Redirects]:
) -> Tuple[Plugin, Optional[Repo]]:
repo, branch, alias = p.repo, p.branch, p.alias
name = alias or p.repo.name
commit = None
@ -479,7 +476,7 @@ def prefetch_plugin(
return cached_plugin, repo.redirect
has_submodules = repo.has_submodules()
print(f"prefetch {name}")
log.debug(f"prefetch {name}")
sha256 = repo.prefetch(commit)
return (
@ -488,7 +485,7 @@ def prefetch_plugin(
)
def print_download_error(plugin: str, ex: Exception):
def print_download_error(plugin: PluginDesc, ex: Exception):
print(f"{plugin}: {ex}", file=sys.stderr)
ex_traceback = ex.__traceback__
tb_lines = [
@ -498,19 +495,21 @@ def print_download_error(plugin: str, ex: Exception):
print("\n".join(tb_lines))
def check_results(
results: List[Tuple[PluginDesc, Union[Exception, Plugin], Redirects]]
results: List[Tuple[PluginDesc, Union[Exception, Plugin], Optional[Repo]]]
) -> Tuple[List[Tuple[PluginDesc, Plugin]], Redirects]:
''' '''
failures: List[Tuple[str, Exception]] = []
failures: List[Tuple[PluginDesc, Exception]] = []
plugins = []
# {old: new} plugindesc
redirects: Dict[Repo, Repo] = {}
redirects: Redirects = {}
for (pdesc, result, redirect) in results:
if isinstance(result, Exception):
failures.append((pdesc.name, result))
failures.append((pdesc, result))
else:
plugins.append((pdesc, result))
redirects.update(redirect)
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))
print(f"{len(results) - len(failures)} plugins were checked", end="")
if len(failures) == 0:
@ -591,13 +590,13 @@ class Cache:
def prefetch(
pluginDesc: PluginDesc, cache: Cache
) -> Tuple[PluginDesc, Union[Exception, Plugin], dict]:
) -> Tuple[PluginDesc, Union[Exception, Plugin], Optional[Repo]]:
try:
plugin, redirect = prefetch_plugin(pluginDesc, cache)
cache[plugin.commit] = plugin
return (pluginDesc, plugin, redirect)
except Exception as e:
return (pluginDesc, e, {})
return (pluginDesc, e, None)
@ -606,7 +605,7 @@ def rewrite_input(
input_file: Path,
deprecated: Path,
# old pluginDesc and the new
redirects: Dict[PluginDesc, PluginDesc] = {},
redirects: Redirects = {},
append: List[PluginDesc] = [],
):
plugins = load_plugins_from_csv(config, input_file,)
@ -618,9 +617,10 @@ def rewrite_input(
cur_date_iso = datetime.now().strftime("%Y-%m-%d")
with open(deprecated, "r") as f:
deprecations = json.load(f)
for old, new in redirects.items():
old_plugin, _ = prefetch_plugin(old)
new_plugin, _ = prefetch_plugin(new)
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)
if old_plugin.normalized_name != new_plugin.normalized_name:
deprecations[old_plugin.normalized_name] = {
"new": new_plugin.normalized_name,